Fixed bug with imports and added missing time module

This commit is contained in:
Mattia Giambirtone 2023-05-22 14:38:03 +02:00
parent 879fec20fe
commit b281961a5c
Signed by: nocturn9x
GPG Key ID: 8270F9F467971E59
3 changed files with 16 additions and 5 deletions

View File

@ -212,7 +212,7 @@ type
# The module importing us, if any # The module importing us, if any
parentModule*: Name parentModule*: Name
# Currently imported modules # Currently imported modules
modules*: HashSet[string] modules*: TableRef[string, Name]
TypedNode* = ref object TypedNode* = ref object
## A wapper for AST nodes ## A wapper for AST nodes
@ -354,7 +354,7 @@ proc resolve*(self: Compiler, name: string): Name =
# module, so we definitely can't # module, so we definitely can't
# use it # use it
continue continue
elif self.currentModule.path in obj.exportedTo: if self.currentModule.path in obj.exportedTo:
# The name is public in its owner # The name is public in its owner
# module and said module has explicitly # module and said module has explicitly
# exported it to us: we can use it # 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, line: node.moduleName.token.line,
kind: NameKind.Module, kind: NameKind.Module,
isPrivate: false, isPrivate: false,
isReal: true isReal: true,
)) ))
n = self.names[^1] n = self.names[^1]
declaredName = self.names[^1].ident.token.lexeme declaredName = self.names[^1].ident.token.lexeme

View File

@ -1689,6 +1689,10 @@ proc importStmt(self: BytecodeCompiler, node: ImportStmt, compile: bool = true)
# its public names to us # its public names to us
for name in self.findInModule("", module): for name in self.findInModule("", module):
name.exportedTo.incl(self.currentModule.path) 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: except IOError:
self.error(&"could not import '{module.ident.token.lexeme}': {getCurrentExceptionMsg()}") self.error(&"could not import '{module.ident.token.lexeme}': {getCurrentExceptionMsg()}")
except OSError: except OSError:
@ -2077,6 +2081,7 @@ proc compile*(self: BytecodeCompiler, ast: seq[Declaration], file: string, lines
let start = self.chunk.code.len() let start = self.chunk.code.len()
if not incremental: if not incremental:
self.jumps = @[] self.jumps = @[]
self.modules = newTable[string, Name]()
let pos = self.beginProgram() let pos = self.beginProgram()
let idx = self.stackIndex let idx = self.stackIndex
self.stackIndex = idx self.stackIndex = idx
@ -2100,7 +2105,7 @@ proc compileModule(self: BytecodeCompiler, module: Name) =
break break
elif i == searchPath.high(): elif i == searchPath.high():
self.error(&"""could not import '{path}': module not found""") self.error(&"""could not import '{path}': module not found""")
if self.modules.contains(module.path): if self.modules.hasKey(module.path):
return return
let source = readFile(path) let source = readFile(path)
let current = self.current let current = self.current
@ -2141,4 +2146,4 @@ proc compileModule(self: BytecodeCompiler, module: Name) =
self.replMode = replMode self.replMode = replMode
self.lines = lines self.lines = lines
self.source = src self.source = src
self.modules.incl(module.path) self.modules[module.path] = module

6
src/peon/stdlib/time.pn Normal file
View File

@ -0,0 +1,6 @@
import std;
fn clock*: float {
#pragma[magic: "SysClock64"]
}