parsing expression is done! :D
This commit is contained in:
parent
6e541144bb
commit
b52ec78d21
@ -113,19 +113,27 @@ class Expression(object):
|
|||||||
def str2tokens(self, exp):
|
def str2tokens(self, exp):
|
||||||
""" Parse the expression, ie tranform a string into a list of tokens
|
""" Parse the expression, ie tranform a string into a list of tokens
|
||||||
|
|
||||||
|
/!\ float are not availiable yet!
|
||||||
|
|
||||||
:param exp: The expression (a string)
|
:param exp: The expression (a string)
|
||||||
:returns: list of token
|
:returns: list of token
|
||||||
|
|
||||||
"""
|
"""
|
||||||
tokens = exp.split(" ")
|
tokens = ['']
|
||||||
|
|
||||||
for (i,t) in enumerate(tokens):
|
for character in exp:
|
||||||
try:
|
if character.isdigit():
|
||||||
tokens[i] = int(t)
|
if type(tokens[-1]) == int:
|
||||||
except ValueError:
|
tokens[-1] = tokens[-1]*10 + int(character)
|
||||||
pass
|
else:
|
||||||
|
tokens.append(int(character))
|
||||||
|
elif character in "+-*/()":
|
||||||
|
tokens.append(character)
|
||||||
|
elif character != " ":
|
||||||
|
raise ValueError("{} is an unvalid character".format(character))
|
||||||
|
|
||||||
return tokens
|
print(tokens[1:])
|
||||||
|
return tokens[1:]
|
||||||
|
|
||||||
# ---------------------
|
# ---------------------
|
||||||
# "fix" recognition
|
# "fix" recognition
|
||||||
|
Loading…
Reference in New Issue
Block a user