Added code to parse assignment expressions

master
Aadhavan Srinivasan 1 year ago
parent 88902d9066
commit 1caf8044ff

@ -1,6 +1,6 @@
def parse(expr): def parse(expr):
opers = ['+', '-', '*', '/'] opers = ['+', '-', '*', '/', '=']
num_par = 0 num_par = 0
tokenized = [] tokenized = []
@ -9,19 +9,19 @@ def parse(expr):
while index < len(expr): while index < len(expr):
if expr[index] in opers: if expr[index] in opers:
tokenized.append(expr[index]) tokenized.append(expr[index])
index += 1
elif expr[index].isdigit() or expr[index] == '.': elif expr[index].isdigit() or expr[index] == '.':
while (index < len(expr)) and (expr[index].isdigit() or expr[index] == '.'): while (index < len(expr)) and (expr[index].isdigit() or expr[index] == '.'):
temp_string += expr[index] temp_string += expr[index]
index += 1 index += 1
tokenized.append(temp_string) tokenized.append(temp_string)
temp_string = ""
elif expr[index] == '(': elif expr[index] == '(':
num_par = 1 num_par = 1
temp_string += expr[index] temp_string += expr[index]
index += 1 index += 1
while num_par != 0 and index < len(expr): while index < len(expr) and num_par != 0:
temp_string += expr[index] temp_string += expr[index]
if expr[index] == '(': if expr[index] == '(':
num_par += 1 num_par += 1
@ -30,9 +30,18 @@ def parse(expr):
index += 1 index += 1
tokenized.append(temp_string) tokenized.append(temp_string)
temp_string = ""
elif expr[index].isalpha(): # If you encounter a variable
temp_string += expr[index]
index += 1
while index < len(expr) and expr[index].isalpha():
temp_string += expr[index]
index += 1
tokenized.append(temp_string)
else:
index += 1
index += 1 temp_string = ""
return tokenized return tokenized

Loading…
Cancel
Save