From 058b021493577a45ff6418d8073008be401b5910 Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Thu, 18 Aug 2022 23:49:20 +0200 Subject: [PATCH] Fixed some bugs with void functions in the compiler --- src/frontend/compiler.nim | 4 +++- src/frontend/parser.nim | 2 +- tests/chainedCalls.pn | 21 +++++++++++---------- tests/std.pn | 5 +++++ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index e737726..de71377 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -712,6 +712,8 @@ proc inferType(self: Compiler, node: Declaration, strictMutable: bool = true): T proc typeToStr(self: Compiler, typ: Type): string = ## Returns the string representation of a ## type object + if typ.isNil(): + return "nil" case typ.kind: of Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Float32, Float64, @@ -1802,7 +1804,7 @@ proc funDecl(self: Compiler, node: FunDecl, fn: Name = nil, args: seq[Expression # the jump offset self.patchJump(jmp) # Pops a call frame - discard self.frames.pop() + # discard self.frames.pop() # Restores the enclosing function (if any). # Makes nested calls work (including recursion) self.currentFunction = function diff --git a/src/frontend/parser.nim b/src/frontend/parser.nim index b311c06..5011b46 100644 --- a/src/frontend/parser.nim +++ b/src/frontend/parser.nim @@ -116,7 +116,7 @@ proc addOperator(self: OperatorTable, lexeme: string) = ## criteria are similar to Nim's) if lexeme in self.tokens: return # We've already added it! - var prec = Precedence.high() + var prec = Power if lexeme.len() >= 2 and lexeme[^2..^1] in ["->", "~>", "=>"]: prec = Arrow elif lexeme.endsWith("=") and lexeme[0] notin {'<', '>', '!', '?', '~', '='} or lexeme == "=": diff --git a/tests/chainedCalls.pn b/tests/chainedCalls.pn index 305ca54..dfbb50c 100644 --- a/tests/chainedCalls.pn +++ b/tests/chainedCalls.pn @@ -1,4 +1,7 @@ -fn first(a, b: int): int { +import std; + + +fn first(a, b, c: int): int { return a; } @@ -8,19 +11,17 @@ fn second(a, b: int): int { fn last(a, b, c: int): int { - return c; + return c; } fn middle(a, b, c: int): int { - return last(a, c, b); + return last(a, c, b); } -fn first(a, b, c: int): int { - return middle(b, a, c); -} - - -first(1, 2, 3); -var x = first(second(1, 2), 3); +print(first(1, 2, 3) == 1); +var x = first(second(1, 2), 3, 4); +print(x == 2); +print(middle(3, 1, 2) == 1); +print(first(last(second(2, 1), 3, 0), 4, 5) == 0); \ No newline at end of file diff --git a/tests/std.pn b/tests/std.pn index c7aebd4..846214d 100644 --- a/tests/std.pn +++ b/tests/std.pn @@ -567,3 +567,8 @@ fn print*(x: float) { fn print*(x: string) { #pragma[magic: "PrintString"] } + + +fn print*(x: bool) { + #pragma[magic: "PrintBool"] +} \ No newline at end of file