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
|
||||||
@ -332,10 +340,10 @@ if __name__ == '__main__':
|
|||||||
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
|
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
|
||||||
test(exp)
|
test(exp)
|
||||||
|
|
||||||
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2"
|
exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2"
|
||||||
test(exp)
|
test(exp)
|
||||||
|
|
||||||
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5"
|
exp="(2+5)/(3*4)+1/12+5*5"
|
||||||
test(exp)
|
test(exp)
|
||||||
|
|
||||||
import doctest
|
import doctest
|
||||||
|
Loading…
Reference in New Issue
Block a user