|
@@ -1,3 +1,4 @@
|
|
|
|
+
|
|
from .tokens import TokenType, Token
|
|
from .tokens import TokenType, Token
|
|
|
|
|
|
|
|
|
|
@@ -13,13 +14,14 @@ class Interpreter():
|
|
self._text: str = ""
|
|
self._text: str = ""
|
|
|
|
|
|
def _next_token(self) -> Token:
|
|
def _next_token(self) -> Token:
|
|
|
|
+ main_part = ''
|
|
while self._current_char is not None:
|
|
while self._current_char is not None:
|
|
# self._current_char: str = self._text[self._pos]
|
|
# self._current_char: str = self._text[self._pos]
|
|
if self._current_char == " ":
|
|
if self._current_char == " ":
|
|
self._skip()
|
|
self._skip()
|
|
continue
|
|
continue
|
|
if self._current_char.isdigit():
|
|
if self._current_char.isdigit():
|
|
- return Token(TokenType.INTEGER, self._integer())
|
|
|
|
|
|
+ return Token(TokenType.FLOAT, self._float())
|
|
if self._current_char == "+":
|
|
if self._current_char == "+":
|
|
char = self._current_char
|
|
char = self._current_char
|
|
self._forward()
|
|
self._forward()
|
|
@@ -42,9 +44,9 @@ class Interpreter():
|
|
while self._current_char and self._current_char == " ":
|
|
while self._current_char and self._current_char == " ":
|
|
self._forward()
|
|
self._forward()
|
|
|
|
|
|
- def _integer(self):
|
|
|
|
|
|
+ def _float(self):
|
|
result: list = []
|
|
result: list = []
|
|
- while self._current_char and self._current_char.isdigit():
|
|
|
|
|
|
+ while self._current_char and (self._current_char.isdigit() or self._current_char == '.'):
|
|
result += self._current_char
|
|
result += self._current_char
|
|
self._forward()
|
|
self._forward()
|
|
return "".join(result)
|
|
return "".join(result)
|
|
@@ -55,21 +57,21 @@ class Interpreter():
|
|
else:
|
|
else:
|
|
raise InterpreterException("invalid token order")
|
|
raise InterpreterException("invalid token order")
|
|
|
|
|
|
- def _expr(self) -> int:
|
|
|
|
|
|
+ def _expr(self) -> float:
|
|
self._current_token = self._next_token() # берём первый токен
|
|
self._current_token = self._next_token() # берём первый токен
|
|
left = self._current_token
|
|
left = self._current_token
|
|
- self._check_token_type(TokenType.INTEGER)
|
|
|
|
|
|
+ self._check_token_type(TokenType.FLOAT)
|
|
op = self._current_token
|
|
op = self._current_token
|
|
if op.type_ == TokenType.PLUS:
|
|
if op.type_ == TokenType.PLUS:
|
|
self._check_token_type(TokenType.PLUS)
|
|
self._check_token_type(TokenType.PLUS)
|
|
else:
|
|
else:
|
|
self._check_token_type(TokenType.MINUS)
|
|
self._check_token_type(TokenType.MINUS)
|
|
right = self._current_token
|
|
right = self._current_token
|
|
- self._check_token_type(TokenType.INTEGER)
|
|
|
|
|
|
+ self._check_token_type(TokenType.FLOAT)
|
|
if op.type_ == TokenType.PLUS:
|
|
if op.type_ == TokenType.PLUS:
|
|
- return int(left.value) + int(right.value)
|
|
|
|
|
|
+ return float(left.value) + float(right.value)
|
|
elif op.type_ == TokenType.MINUS:
|
|
elif op.type_ == TokenType.MINUS:
|
|
- return int(left.value) - int(right.value)
|
|
|
|
|
|
+ return float(left.value) - float(right.value)
|
|
raise InterpreterException("bad operator")
|
|
raise InterpreterException("bad operator")
|
|
|
|
|
|
def __call__(self, text: str):
|
|
def __call__(self, text: str):
|