From b281961a5c07ac7e138b19f9bfcf3151272f28dc Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Mon, 22 May 2023 14:38:03 +0200 Subject: [PATCH] Fixed bug with imports and added missing time module --- src/frontend/compiler/compiler.nim | 6 +++--- src/frontend/compiler/targets/bytecode/target.nim | 9 +++++++-- src/peon/stdlib/time.pn | 6 ++++++ 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 src/peon/stdlib/time.pn diff --git a/src/frontend/compiler/compiler.nim b/src/frontend/compiler/compiler.nim index 4f7181f..cca7a67 100644 --- a/src/frontend/compiler/compiler.nim +++ b/src/frontend/compiler/compiler.nim @@ -212,7 +212,7 @@ type # The module importing us, if any parentModule*: Name # Currently imported modules - modules*: HashSet[string] + modules*: TableRef[string, Name] TypedNode* = ref object ## A wapper for AST nodes @@ -354,7 +354,7 @@ proc resolve*(self: Compiler, name: string): Name = # module, so we definitely can't # use it continue - elif self.currentModule.path in obj.exportedTo: + if self.currentModule.path in obj.exportedTo: # The name is public in its owner # module and said module has explicitly # exported it to us: we can use it @@ -988,7 +988,7 @@ proc declare*(self: Compiler, node: ASTNode): Name {.discardable.} = line: node.moduleName.token.line, kind: NameKind.Module, isPrivate: false, - isReal: true + isReal: true, )) n = self.names[^1] declaredName = self.names[^1].ident.token.lexeme diff --git a/src/frontend/compiler/targets/bytecode/target.nim b/src/frontend/compiler/targets/bytecode/target.nim index fe21f23..1ea10c5 100644 --- a/src/frontend/compiler/targets/bytecode/target.nim +++ b/src/frontend/compiler/targets/bytecode/target.nim @@ -1689,6 +1689,10 @@ proc importStmt(self: BytecodeCompiler, node: ImportStmt, compile: bool = true) # its public names to us for name in self.findInModule("", module): name.exportedTo.incl(self.currentModule.path) + for module in self.modules.values(): + if self.currentModule.path in module.exportedTo: + for name in self.findInModule("", module): + name.exportedTo.incl(self.currentModule.path) except IOError: self.error(&"could not import '{module.ident.token.lexeme}': {getCurrentExceptionMsg()}") except OSError: @@ -2077,6 +2081,7 @@ proc compile*(self: BytecodeCompiler, ast: seq[Declaration], file: string, lines let start = self.chunk.code.len() if not incremental: self.jumps = @[] + self.modules = newTable[string, Name]() let pos = self.beginProgram() let idx = self.stackIndex self.stackIndex = idx @@ -2100,7 +2105,7 @@ proc compileModule(self: BytecodeCompiler, module: Name) = break elif i == searchPath.high(): self.error(&"""could not import '{path}': module not found""") - if self.modules.contains(module.path): + if self.modules.hasKey(module.path): return let source = readFile(path) let current = self.current @@ -2141,4 +2146,4 @@ proc compileModule(self: BytecodeCompiler, module: Name) = self.replMode = replMode self.lines = lines self.source = src - self.modules.incl(module.path) + self.modules[module.path] = module diff --git a/src/peon/stdlib/time.pn b/src/peon/stdlib/time.pn new file mode 100644 index 0000000..2252cfd --- /dev/null +++ b/src/peon/stdlib/time.pn @@ -0,0 +1,6 @@ +import std; + + +fn clock*: float { + #pragma[magic: "SysClock64"] +}