Fixed all bugs: test suite is passing

This commit is contained in:
Mattia Giambirtone 2022-11-05 14:27:57 +01:00
parent d048870165
commit b93ac201f2
5 changed files with 16 additions and 15 deletions

View File

@ -231,7 +231,7 @@ proc statement(self: Compiler, node: Statement)
proc declaration(self: Compiler, node: Declaration)
proc peek(self: Compiler, distance: int = 0): ASTNode
proc identifier(self: Compiler, node: IdentExpr)
proc varDecl(self: Compiler, node: VarDecl, name: Name)
proc varDecl(self: Compiler, node: VarDecl)
proc specialize(self: Compiler, name: Name, args: seq[Expression]): Name
proc matchImpl(self: Compiler, name: string, kind: Type, node: ASTNode = nil, allowFwd: bool = true): Name
proc infer(self: Compiler, node: LiteralExpr, allowGeneric: bool = false): Type
@ -501,8 +501,6 @@ proc resolve(self: Compiler, name: string): Name =
## resolved, it is also compiled on-the-fly
for obj in reversed(self.names):
if obj.ident.token.lexeme == name:
if obj.kind == NameKind.Argument and obj.belongsTo != self.currentFunction:
continue
if obj.owner != self.currentModule:
# We don't own this name, but we
# may still have access to it
@ -1991,7 +1989,7 @@ proc deferStmt(self: Compiler, node: DeferStmt) =
proc returnStmt(self: Compiler, node: ReturnStmt) =
## Compiles return statements
self.check(node.value, self.currentFunction.valueType.returnType)
if not node.value.isNil():
self.expression(node.value)
self.emitByte(OpCode.SetResult, node.token.line)
@ -2177,7 +2175,7 @@ proc statement(self: Compiler, node: Statement) =
self.expression(Expression(node))
proc varDecl(self: Compiler, node: VarDecl, name: Name) =
proc varDecl(self: Compiler, node: VarDecl) =
## Compiles variable declarations
# Our parser guarantees that the variable declaration
@ -2199,9 +2197,11 @@ proc varDecl(self: Compiler, node: VarDecl, name: Name) =
self.check(node.value, expected)
# If this doesn't fail, then we're good
typ = expected
name.valueType = typ
self.expression(node.value)
self.emitByte(StoreVar, node.token.line)
self.declareName(node)
var name = self.names[^1]
name.valueType = typ
self.emitBytes(self.getStackPos(name).toTriple(), node.token.line)
@ -2309,13 +2309,13 @@ proc declaration(self: Compiler, node: Declaration) =
## right away, but rather only when they're referenced
## the first time
case node.kind:
of NodeKind.varDecl, NodeKind.funDecl, NodeKind.typeDecl:
of NodeKind.funDecl, NodeKind.typeDecl:
self.declareName(node)
if node.kind == NodeKind.varDecl:
# We compile this immediately because we
# need to keep the stack in the right state
# at runtime
self.varDecl(VarDecl(node), self.names[^1])
of NodeKind.varDecl:
# We compile this immediately because we
# need to keep the stack in the right state
# at runtime
self.varDecl(VarDecl(node))
else:
self.statement(Statement(node))

View File

@ -297,6 +297,7 @@ proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints
styledEcho fgBlue, "Source line: " , fgDefault, line
styledEcho fgCyan, " ".repeat(len("Source line: ") + line.find(lexeme)) & "^".repeat(abs(relPos.stop - relPos.start - line.find(lexeme)))
except CompileError:
raise
let exc = CompileError(getCurrentException())
let lexeme = exc.node.token.lexeme
var lineNo = exc.node.token.line

View File

@ -9,4 +9,4 @@ fn makeAdder(x: int): fn (n: int): int {
}
print(makeAdder(5)(2)); # 7
print(makeAdder(5)(2)); # 7

View File

@ -8,5 +8,5 @@ fn foo: int;
print(foo());
fn foo: int {return 42;}
fn foo: int {return 42;} # Commenting this line will cause an error

View File

@ -1,7 +1,7 @@
import std;
var x = 1;
var x = 100000;
var y = "just a test";
print(y);
print("Starting GC torture test");