fix(checker): handle all unary ops in evaluator

This commit is contained in:
2026-07-07 00:09:39 +02:00
parent aab4641584
commit af6056a83f
2 changed files with 8 additions and 2 deletions

View File

@@ -99,6 +99,8 @@ class Evaluator(m.Expr.Visitor[Any]):
left: Any = self.evaluate(expr.left) left: Any = self.evaluate(expr.left)
right: Any = self.evaluate(expr.right) right: Any = self.evaluate(expr.right)
match expr.operator.type: match expr.operator.type:
case TokenType.PLUS:
return left + right
case TokenType.MINUS: case TokenType.MINUS:
return left - right return left - right
case TokenType.STAR: case TokenType.STAR:
@@ -123,8 +125,12 @@ class Evaluator(m.Expr.Visitor[Any]):
def visit_unary_expr(self, expr: m.UnaryExpr) -> Any: def visit_unary_expr(self, expr: m.UnaryExpr) -> Any:
right: Any = self.evaluate(expr.right) right: Any = self.evaluate(expr.right)
match expr.operator.type: match expr.operator.type:
case TokenType.PLUS:
return +right
case TokenType.MINUS: case TokenType.MINUS:
return -right return -right
case TokenType.BANG:
return not right
case _: case _:
raise NotImplementedError raise NotImplementedError

View File

@@ -18,14 +18,14 @@ LOGICAL_OPERATORS: dict[TokenType, type[ast.boolop]] = {
} }
BINARY_OPERATORS: dict[TokenType, type[ast.operator]] = { BINARY_OPERATORS: dict[TokenType, type[ast.operator]] = {
# TokenType.PLUS: ast.Add, TokenType.PLUS: ast.Add,
TokenType.MINUS: ast.Sub, TokenType.MINUS: ast.Sub,
TokenType.STAR: ast.Mult, TokenType.STAR: ast.Mult,
TokenType.SLASH: ast.Div, TokenType.SLASH: ast.Div,
} }
UNARY_OPERATORS: dict[TokenType, type[ast.unaryop]] = { UNARY_OPERATORS: dict[TokenType, type[ast.unaryop]] = {
# TokenType.PLUS: ast.UAdd, TokenType.PLUS: ast.UAdd,
TokenType.MINUS: ast.USub, TokenType.MINUS: ast.USub,
} }