diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index 4c95162..c5984da 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -379,13 +379,13 @@ proc getStackPos(self: Compiler, name: IdentExpr, depth: int = self.scopeDepth): var found = false result = 2 for variable in self.names: - if variable.valueType.kind == Function: + if variable.isFunDecl: continue inc(result) if name.name.lexeme == variable.name.name.lexeme: if variable.isPrivate and variable.owner != self.currentModule: continue - elif variable.depth == depth: + else: found = true dec(result) break @@ -1223,7 +1223,7 @@ proc endScope(self: Compiler) = dec(self.scopeDepth) var names: seq[Name] = @[] for name in self.names: - if name.depth > self.scopeDepth and name.valueType.kind notin {Generic, CustomType, Function}: + if name.depth > self.scopeDepth and name.valueType.kind notin {Generic, CustomType} and not name.isFunDecl: names.add(name) if len(names) > 1: # If we're popping less than 65535 variables, then @@ -1435,7 +1435,7 @@ proc endFunctionBeforeReturn(self: Compiler) = ## its return instruction var popped = 0 for name in self.names: - if name.depth == self.scopeDepth and name.valueType.kind notin {Function, Generic, CustomType}: + if name.depth == self.scopeDepth and name.valueType.kind notin {Generic, CustomType} and not name.isFunDecl: inc(popped) if popped > 1: self.emitByte(PopN) @@ -1849,6 +1849,7 @@ proc compile*(self: Compiler, ast: seq[Declaration], file: string): Chunk = args: @[]), codePos: 13, # Jump address is hardcoded name: newIdentExpr(Token(lexeme: "", kind: Identifier)), + isFunDecl: true, line: -1) self.names.add(main) self.emitByte(LoadFunction) @@ -1860,7 +1861,7 @@ proc compile*(self: Compiler, ast: seq[Declaration], file: string): Chunk = self.emitBytes(0.toTriple()) while not self.done(): self.declaration(Declaration(self.step())) - self.endFunctionBeforeReturn() + self.endScope() self.patchReturnAddress(pos) self.emitByte(OpCode.Return) self.emitByte(0) # Entry point has no return value