diff --git a/src/interpreter.py b/src/interpreter.py index 6aac0fa..b827153 100644 --- a/src/interpreter.py +++ b/src/interpreter.py @@ -1,8 +1,11 @@ from calc import * class Interpreter: + + __variables = {} def __init__(self, calc): + self.calc = calc def run(self): @@ -43,7 +46,11 @@ def __tokenize_Statement__(self, code): if (token == "PRINT"): del code[0] - self.__parse_Print__(code) + self.__parse_Print__(code) + + elif (token == "LET"): + del code[0] + self.__parse_Let__(code) return 1 @@ -68,8 +75,8 @@ def __parse_String__(self, code): del code[0] i += 1 - return string; - + return string; + def __parse_Print__(self, code): @@ -80,9 +87,37 @@ def __parse_Print__(self, code): elif (code[0].isdigit()): num = self.calc.parse_Formula(code) - print num + print num + + + def __parse_Let__(self, code): + + varName = "" + varVal = "" + + while (code[0] != "="): + varName += code[0] + del code[0] + + if (code[0] == "="): + del code[0] + + try: + while (code[0] != "\n"): + + if (code[0].isdigit()): + varVal += code[0] + del code[0] + + except (IndexError): + self.__variables[varName] = int(varVal) + print self.__variables + return + + self.__variables[varName] = int(varVal) + print self.__variables calc = Calc() interpreter = Interpreter(calc) -interpreter.run() +interpreter.run() \ No newline at end of file diff --git a/src/source.bas b/src/source.bas index d8f68d9..35c0cc4 100644 --- a/src/source.bas +++ b/src/source.bas @@ -1,9 +1,3 @@ -PRINT "Hello, world" -PRINT "Hi" -PRINT 2 -PRINT 123 -PRINT 111 -PRINT "3*2+2*(5-6)-234 =" -PRINT 3*2+2*(5-6)-234 -PRINT "23-698*(4*(63-25*69)-56) =" -PRINT 23-698*(4*(63-25*69)-56) +LET a=10 +LET b=2 +LET c=4 \ No newline at end of file