Skip to content

variable assignment added #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/interpreter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from calc import *

class Interpreter:

__variables = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are using this for variable assignment why not store the corresponding values into the dictionary itself and then we can access each of values as keys.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do actually. And we can access values via keys like so:
variables = {'var1' : val1, 'var2' : val2}
print variables[var1]
output: val1


def __init__(self, calc):

self.calc = calc

def run(self):
Expand Down Expand Up @@ -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

Expand All @@ -68,8 +75,8 @@ def __parse_String__(self, code):
del code[0]
i += 1

return string;

return string;

def __parse_Print__(self, code):

Expand All @@ -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()
12 changes: 3 additions & 9 deletions src/source.bas
Original file line number Diff line number Diff line change
@@ -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