diff --git a/examples/02_operations.peb b/examples/02_operations.peb index fcc1707..4956cdc 100644 --- a/examples/02_operations.peb +++ b/examples/02_operations.peb @@ -3,3 +3,7 @@ let b = 5 let c = a + b c *= 7 let d = (3.2 - 0.2) + 4 * 6 / 2 +print(a) +print(b) +print(c) +print(d) \ No newline at end of file diff --git a/src/interpreter/interpreter.py b/src/interpreter/interpreter.py index bcea14a..fce0c3e 100644 --- a/src/interpreter/interpreter.py +++ b/src/interpreter/interpreter.py @@ -45,16 +45,16 @@ class Interpreter(Expr.Visitor[Any], Stmt.Visitor[None]): right: Any = self.evaluate(expr.right) match expr.operator.type: - case TokenType.MINUS: + case TokenType.MINUS | TokenType.MINUS_EQUAL: self.check_number_operands(expr.operator, left, right) return float(left) - float(right) - case TokenType.SLASH: + case TokenType.SLASH | TokenType.SLASH_EQUAL: self.check_number_operands(expr.operator, left, right) return float(left) / float(right) - case TokenType.STAR: + case TokenType.STAR | TokenType.STAR_EQUAL: self.check_number_operands(expr.operator, left, right) return float(left) * float(right) - case TokenType.PLUS: + case TokenType.PLUS | TokenType.PLUS_EQUAL: if isinstance(left, float) and isinstance(right, float): return float(left) + float(right) if isinstance(left, str) and isinstance(right, str): diff --git a/src/parser/parser.py b/src/parser/parser.py index ffc1cb2..dce12fa 100644 --- a/src/parser/parser.py +++ b/src/parser/parser.py @@ -140,13 +140,23 @@ class Parser: def assignment(self) -> Expr: expr: Expr = self.equality() - if self.match(TokenType.EQUAL): - equals: Token = self.previous() + if self.match(TokenType.EQUAL, TokenType.PLUS_EQUAL, TokenType.MINUS_EQUAL, TokenType.STAR_EQUAL, TokenType.SLASH_EQUAL): + operator: Token = self.previous() value: Expr = self.assignment() if isinstance(expr, VariableExpr): name: Token = expr.name - return AssignExpr(name, value) - self.error(equals, "Invalid assignment target.") + if operator.type == TokenType.EQUAL: + return AssignExpr(name, value) + else: + return AssignExpr( + name, + BinaryExpr( + VariableExpr(name), + operator, + value + ) + ) + self.error(operator, "Invalid assignment target.") return expr def equality(self) -> Expr: