expanded the break test, improved compiler error reporting.

This commit is contained in:
prod2 2022-01-29 21:22:42 +01:00
parent 2651cf3cbd
commit 45e22d9fed
2 changed files with 60 additions and 3 deletions

View File

@ -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'.")

View File

@ -35,4 +35,58 @@ print "expect: nothing";
print "middle";
};
print "outer";
};
};
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();