fix(checker): handle all unary ops in evaluator
This commit is contained in:
@@ -99,6 +99,8 @@ class Evaluator(m.Expr.Visitor[Any]):
|
||||
left: Any = self.evaluate(expr.left)
|
||||
right: Any = self.evaluate(expr.right)
|
||||
match expr.operator.type:
|
||||
case TokenType.PLUS:
|
||||
return left + right
|
||||
case TokenType.MINUS:
|
||||
return left - right
|
||||
case TokenType.STAR:
|
||||
@@ -123,8 +125,12 @@ class Evaluator(m.Expr.Visitor[Any]):
|
||||
def visit_unary_expr(self, expr: m.UnaryExpr) -> Any:
|
||||
right: Any = self.evaluate(expr.right)
|
||||
match expr.operator.type:
|
||||
case TokenType.PLUS:
|
||||
return +right
|
||||
case TokenType.MINUS:
|
||||
return -right
|
||||
case TokenType.BANG:
|
||||
return not right
|
||||
case _:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@@ -18,14 +18,14 @@ LOGICAL_OPERATORS: dict[TokenType, type[ast.boolop]] = {
|
||||
}
|
||||
|
||||
BINARY_OPERATORS: dict[TokenType, type[ast.operator]] = {
|
||||
# TokenType.PLUS: ast.Add,
|
||||
TokenType.PLUS: ast.Add,
|
||||
TokenType.MINUS: ast.Sub,
|
||||
TokenType.STAR: ast.Mult,
|
||||
TokenType.SLASH: ast.Div,
|
||||
}
|
||||
|
||||
UNARY_OPERATORS: dict[TokenType, type[ast.unaryop]] = {
|
||||
# TokenType.PLUS: ast.UAdd,
|
||||
TokenType.PLUS: ast.UAdd,
|
||||
TokenType.MINUS: ast.USub,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user