diff --git a/compiler.nim b/compiler.nim index 6c79504..15a5674 100644 --- a/compiler.nim +++ b/compiler.nim @@ -185,6 +185,9 @@ template genRule(ttype: TokenType, tprefix: (Compiler) -> void, tinfix: (Compile raise newException(Exception, "Invalid rule: pcNone only allowed for unary operators and primary values, not for infix ones!") rules[ttype] = ParseRule(name: $ttype, prefix: tprefix, infix: tinfix, prec: tprec) +for i in TokenType: + genRule(i, nop, nop, pcNone) + proc getRule(opType: TokenType): ParseRule = rules[opType] @@ -332,7 +335,7 @@ proc parsePrecedence(comp: Compiler, prec: Precedence) = let infixRule = comp.previous.tokenType.getRule().infix infixRule(comp) else: - comp.error("Expect expression.") + comp.error(&"Expect expression, got {($comp.previous.tokenType)[2..^1]}.") proc expression(comp: Compiler) = ## The lowest precedence among the pratt-parsed expressions @@ -604,7 +607,7 @@ tkWhile.genRule(parseWhile, nop, pcNone) proc parseFunct(comp: Compiler) = # jump over - let jumpOverBody = comp.emitJump(0, opFunctionDef) + let jumpOverBody = comp.emitJump(1, opFunctionDef) comp.consume(tkLeftParen, "Expected '(' after keyword 'funct'.") diff --git a/tests/break.nds b/tests/break.nds index 2591284..20e23cb 100644 --- a/tests/break.nds +++ b/tests/break.nds @@ -35,4 +35,58 @@ print "expect: nothing"; print "middle"; }; print "outer"; -}; \ No newline at end of file +}; + +print "expect 5"; + +var f = funct() { + var y = 1; + var z = 3; + var p = 1; + ^result = y + (z + p); + { + break @function; + }; + ^result = 10; +}; + +print f(); + +print "expect 15"; + +f = funct(m, n) + ^result = m + n +; + +print f(f(5, 5), 5); + + +print "expect: 10"; + +var g = funct() + ^result = {@a + ^a = { @b + ^b = { @c + ^c = 10; + }; + }; + } +; + +print g(); + +print "expect: 9"; + +var h = funct() + ^result = {@a + ^a = { @b + ^b = { @c + ^a = 3; + ^b = 6; + ^c = ^a + ^b; + }; + }; + } +; + +print h(); \ No newline at end of file