diff --git a/constants.py b/constants.py index c112449..d2f6168 100644 --- a/constants.py +++ b/constants.py @@ -13,6 +13,8 @@ PLUS = 'PLUS' MINUS = 'MINUS' MUL = 'MUL' DIV = 'DIV' +IDIV = 'IDIV' +MOD = 'MOD' POW = 'POW' SQRT = 'SQRT' @@ -20,6 +22,8 @@ HEX = 'HEX' DEC = 'DEC' BIN = 'BIN' +PI = 'PI' + SWAP = 'SWAP' POP = 'POP' diff --git a/interpreter.py b/interpreter.py index b5ff42d..f2b4c05 100644 --- a/interpreter.py +++ b/interpreter.py @@ -8,7 +8,7 @@ from constants import ( NUMBER, EOF, - PLUS, MINUS, MUL, DIV, POW, SQRT, + PLUS, MINUS, MUL, DIV, IDIV, MOD, POW, SQRT, HEX, DEC, BIN, SWAP, POP, ) @@ -53,7 +53,7 @@ class Interpreter: while self.current_token.type is not EOF: if self.current_token.type == NUMBER: self.stack.append(self.current_token.value) - elif self.current_token.type in (PLUS, MINUS, MUL, DIV, POW): + elif self.current_token.type in (PLUS, MINUS, MUL, DIV, IDIV, MOD, POW): self.evaluate_binary_operator(self.current_token) elif self.current_token.type in (SQRT): self.evaluate_unary_operator(self.current_token) diff --git a/lexer.py b/lexer.py index dca101f..5771210 100644 --- a/lexer.py +++ b/lexer.py @@ -12,8 +12,9 @@ import operator from token import Token from constants import ( NUMBER, EOF, - PLUS, MINUS, MUL, DIV, POW, SQRT, + PLUS, MINUS, MUL, DIV, IDIV, MOD, POW, SQRT, HEX, DEC, BIN, + PI, SWAP, POP, ) @@ -73,8 +74,15 @@ class Lexer(): elif self.current_char == '/': self.advance() + if self.current_char == '/': + self.advance() + return Token(IDIV, operator.floordiv) return Token(DIV, operator.truediv) + elif self.current_char == '%': + self.advance() + return Token(MOD, operator.mod) + elif self.current_char == '^': self.advance() return Token(POW, operator.pow) @@ -94,6 +102,8 @@ class Lexer(): return Token(BIN, bin) elif check.lower() == SQRT.lower(): return Token(SQRT, math.sqrt) + elif check.lower() == PI.lower(): + return Token(NUMBER, math.pi) elif check.lower() == SWAP.lower(): return Token(SWAP, text) elif check.lower() == POP.lower():