This commit is contained in:
Nocturn9x 2021-11-11 12:31:25 +01:00
parent e225d77e0d
commit e92e40b6ea
2 changed files with 9 additions and 2 deletions

View File

@ -169,6 +169,7 @@ proc visit_call(self: NodeVisitor, node: AstNode): AstNode =
of "tan":
callFunction(tan, self.eval(node.arguments[0]).value)
of "sqrt":
let arg = self.eval(node.arguments[0])
ensurePositive(arg)
callFunction(sqrt, self.eval(node.arguments[0]).value)
of "log":
@ -257,6 +258,12 @@ proc visit_unary(self: NodeVisitor, node: AstNode): AstNode =
else:
discard # Unreachable
of TokenType.Plus:
result = node # Unary + does nothing
case expr.kind:
of NodeKind.Float:
result = AstNode(kind: NodeKind.Float, value: expr.value)
of NodeKind.Integer:
result = AstNode(kind: NodeKind.Integer, value: expr.value)
else:
discard # Unreachable
else:
discard # Unreachable

View File

@ -174,7 +174,7 @@ proc call(self: Parser): AstNode =
proc unary(self: Parser): AstNode =
## Parses unary expressions such as -1
case self.step().kind:
of TokenType.Minus:
of TokenType.Minus, TokenType.Plus:
result = AstNode(kind: NodeKind.Unary, unOp: self.previous(), operand: self.unary())
else:
result = self.call()