Definively finished arity setup

This commit is contained in:
Lafrite 2014-10-23 13:13:28 +02:00
parent cdf5a552b2
commit 4630d0de74

View File

@ -248,9 +248,9 @@ class Expression(object):
>>> Expression.in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')']) >>> Expression.in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
[2, 5, '+', 1, '-', 3, 4, '*', '/'] [2, 5, '+', 1, '-', 3, 4, '*', '/']
>>> Expression.in2post_fix(['-', '(', '-', 2, ')']) >>> Expression.in2post_fix(['-', '(', '-', 2, ')'])
[2, 5, '+', 1, '-', 3, 4, '*', '/'] [2, '-', '-']
>>> Expression.in2post_fix(['-', '(', '-', 2, '+', 3, "*", 4, ')']) >>> Expression.in2post_fix(['-', '(', '-', 2, '+', 3, "*", 4, ')'])
[2, 5, '+', 1, '-', 3, 4, '*', '/'] [2, '-', 3, 4, '*', '+', '-']
""" """
# Stack where operator will be stocked # Stack where operator will be stocked
opStack = Stack() opStack = Stack()
@ -262,8 +262,8 @@ class Expression(object):
for (pos_token,token) in enumerate(infix_tokens): for (pos_token,token) in enumerate(infix_tokens):
# Pour voir ce qu'il se passe dans cette procédure # # Pour voir ce qu'il se passe dans cette procédure
print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack)) # print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
if token == "(": if token == "(":
opStack.push(token) opStack.push(token)
# Set next arity counter # Set next arity counter
@ -271,10 +271,7 @@ class Expression(object):
elif token == ")": elif token == ")":
op = opStack.pop() op = opStack.pop()
while op != "(": while op != "(":
#print(str(op), " -> ", op.arity)
postfix_tokens.append(op) postfix_tokens.append(op)
#arity = arity_Stack.pop()
#arity_Stack.push(arity - (op.arity + 1))
op = opStack.pop() op = opStack.pop()
# Go back to old arity # Go back to old arity
@ -289,15 +286,9 @@ class Expression(object):
op = opStack.pop() op = opStack.pop()
postfix_tokens.append(op) postfix_tokens.append(op)
## decrease arity
#arity = arity_Stack.pop()
#arity_Stack.push(arity - (op.arity - 1))
#print(str(op), " -> ", op.arity)
arity = arity_Stack.pop() arity = arity_Stack.pop()
opStack.push(Operator(token, arity + 1)) opStack.push(Operator(token, arity + 1))
print("--", token, " -> ", str(arity + 1)) # print("--", token, " -> ", str(arity + 1))
# Reset arity to 0 in case there is other operators (the real operation would be "-op.arity + 1") # Reset arity to 0 in case there is other operators (the real operation would be "-op.arity + 1")
arity_Stack.push(0) arity_Stack.push(0)
else: else:
@ -309,16 +300,11 @@ class Expression(object):
op = opStack.pop() op = opStack.pop()
postfix_tokens.append(op) postfix_tokens.append(op)
# decrease arity # # Pour voir ce qu'il se passe dans cette procédure
arity = arity_Stack.pop() # print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
arity_Stack.push(arity - (op.arity - 1))
#print(str(op), " -> ", op.arity) if arity_Stack.peek() != 1:
raise ValueError("Unvalid expression. The arity Stack is ", str(arity_Stack))
print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
#if arity_Stack != 1:
# raise ValueError("No float number please")
return postfix_tokens return postfix_tokens