Olesya Ivanova před 3 roky
rodič
revize
3de088c288
4 změnil soubory, kde provedl 19 přidání a 10 odebrání
  1. 6 0
      README.md
  2. 10 8
      interpreter/interpreter.py
  3. 1 0
      interpreter/tokens.py
  4. 2 2
      main.py

+ 6 - 0
README.md

@@ -0,0 +1,6 @@
+Интерпретатор математических выражений на Python  
+================================================  
+Умеет складывать и вычитать два числа (с любым количествов цифр)  
+Числа могут быть как float, так и int  
+Возвращает всегда float  
+Тестировать в файле main.py, он лежит в корне папки.  

+ 10 - 8
interpreter/interpreter.py

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

+ 1 - 0
interpreter/tokens.py

@@ -3,6 +3,7 @@ from enum import Enum, auto
 
 class TokenType(Enum):
     INTEGER = auto()
+    FLOAT = auto()
     PLUS = auto()
     MINUS = auto()
     EOS = auto()  # конец строки

+ 2 - 2
main.py

@@ -2,5 +2,5 @@ from interpreter import Interpreter
 
 if __name__ == "__main__":
     interpreter = Interpreter()
-    print(interpreter("22+22"))
-    print(interpreter.interpret("   3 + 3 "))
+    print(interpreter("2.2+2"))
+    print(interpreter.interpret("23 - 4. "))