|
@@ -12,31 +12,64 @@ class Interpreter():
|
|
self._lexer = Lexer()
|
|
self._lexer = Lexer()
|
|
|
|
|
|
def _check_token_type(self, type_: TokenType):
|
|
def _check_token_type(self, type_: TokenType):
|
|
|
|
+ print(type_)
|
|
if self._current_token.type_ == type_:
|
|
if self._current_token.type_ == type_:
|
|
self._current_token = self._lexer.next()
|
|
self._current_token = self._lexer.next()
|
|
else:
|
|
else:
|
|
raise InterpreterException("invalid token order")
|
|
raise InterpreterException("invalid token order")
|
|
|
|
|
|
- def _expr(self) -> float:
|
|
|
|
- self._current_token = self._lexer.next() # берём первый токен
|
|
|
|
- left = self._current_token
|
|
|
|
- self._check_token_type(TokenType.FLOAT)
|
|
|
|
- op = self._current_token
|
|
|
|
- if op.type_ == TokenType.PLUS:
|
|
|
|
- self._check_token_type(TokenType.PLUS)
|
|
|
|
- else:
|
|
|
|
|
|
+ def _factor(self) -> float:
|
|
|
|
+ token = self._current_token
|
|
|
|
+ if token.type_ == TokenType.MINUS:
|
|
self._check_token_type(TokenType.MINUS)
|
|
self._check_token_type(TokenType.MINUS)
|
|
- right = self._current_token
|
|
|
|
- self._check_token_type(TokenType.FLOAT)
|
|
|
|
- if op.type_ == TokenType.PLUS:
|
|
|
|
- return float(left.value) + float(right.value)
|
|
|
|
- elif op.type_ == TokenType.MINUS:
|
|
|
|
- return float(left.value) - float(right.value)
|
|
|
|
- raise InterpreterException("bad operator")
|
|
|
|
-
|
|
|
|
- def __call__(self, text: str):
|
|
|
|
|
|
+ val = self._factor()
|
|
|
|
+ return float(-val)
|
|
|
|
+ if token.type_ == TokenType.PLUS:
|
|
|
|
+ self._check_token_type(TokenType.PLUS)
|
|
|
|
+ val = self._factor()
|
|
|
|
+ return float(val)
|
|
|
|
+ if token.type_ == TokenType.INTEGER:
|
|
|
|
+ self._check_token_type(TokenType.INTEGER)
|
|
|
|
+ return float(token.value)
|
|
|
|
+ if token.type_ == TokenType.LPAREN:
|
|
|
|
+ self._check_token_type(TokenType.LPAREN)
|
|
|
|
+ result = self._expr()
|
|
|
|
+ self._check_token_type(TokenType.RPAREN)
|
|
|
|
+ return result
|
|
|
|
+ raise InterpreterException("invalid factor")
|
|
|
|
+
|
|
|
|
+ def _term(self) -> float:
|
|
|
|
+ result = self._factor()
|
|
|
|
+ ops = [TokenType.MUL, TokenType.DIV]
|
|
|
|
+ while self._current_token.type_ in ops:
|
|
|
|
+ token = self._current_token
|
|
|
|
+ if token.type_ == TokenType.MUL:
|
|
|
|
+ self._check_token_type(TokenType.MUL)
|
|
|
|
+ result *= self._factor()
|
|
|
|
+ elif token.type_ == TokenType.DIV:
|
|
|
|
+ self._check_token_type(TokenType.DIV)
|
|
|
|
+ result /= self._factor()
|
|
|
|
+ return result
|
|
|
|
+
|
|
|
|
+ def _expr(self) -> int:
|
|
|
|
+ ops = [TokenType.PLUS, TokenType.MINUS]
|
|
|
|
+ result = self._term()
|
|
|
|
+ while self._current_token.type_ in ops:
|
|
|
|
+ token = self._current_token
|
|
|
|
+ if token.type_ == TokenType.PLUS:
|
|
|
|
+ self._check_token_type(TokenType.PLUS)
|
|
|
|
+ result += self._term()
|
|
|
|
+ elif token.type_ == TokenType.MINUS:
|
|
|
|
+ self._check_token_type(TokenType.MINUS)
|
|
|
|
+ result -= self._term()
|
|
|
|
+ else:
|
|
|
|
+ raise InterpreterException("bad operator")
|
|
|
|
+ return result
|
|
|
|
+
|
|
|
|
+ def __call__(self, text: str) -> int:
|
|
return self.interpret(text)
|
|
return self.interpret(text)
|
|
|
|
|
|
- def interpret(self, text: str) -> float:
|
|
|
|
|
|
+ def interpret(self, text: str) -> int:
|
|
self._lexer.init(text)
|
|
self._lexer.init(text)
|
|
|
|
+ self._current_token = self._lexer.next()
|
|
return self._expr()
|
|
return self._expr()
|