Fixed a couple parsing bugs

This commit is contained in:
nocturn9x 2021-03-12 10:00:17 +01:00
parent 7e667a6ec7
commit 724d873b92
2 changed files with 6 additions and 2 deletions

View File

@ -23,7 +23,6 @@ Features:
## Current limitations
- No equation-solving (coming soon)
- The parsing is a bit weird because something like `2 2` will parse the first 2 and just stop instead of erroring out (FIXME)
## How to use it

View File

@ -135,6 +135,8 @@ proc primary(self: Parser): AstNode =
result = AstNode(kind: NodeKind.Float, value: 0.0)
discard parseFloat(value.lexeme, result.value)
of TokenType.LeftParen:
if self.done():
self.error("unexpected EOL")
let expression = self.binary()
self.expect(TokenType.RightParen, "unexpected EOL")
result = AstNode(kind: NodeKind.Grouping, expr: expression)
@ -157,7 +159,7 @@ proc call(self: Parser): AstNode =
arguments.add(self.binary())
result = AstNode(kind: NodeKind.Call, arguments: arguments, function: expression)
if len(arguments) != arities[expression.name]:
self.error(&"Wrong number of arguments supplied to function '{expression.name}': expected {arities[expression.name]}, got {len(arguments)}")
self.error(&"Wrong number of arguments for '{expression.name}': expected {arities[expression.name]}, got {len(arguments)}")
self.expect(TokenType.RightParen, "unclosed function call")
else:
result = expression
@ -212,5 +214,8 @@ proc parse*(self: Parser, tokens: seq[Token]): AstNode =
self.tokens = tokens
self.current = 0
result = self.binary()
if len(self.tokens[self.current..<len(self.tokens)]) > 1:
# Extra tokens (except EOF) that have not been parsed!
self.error("invalid syntax")