Refactored some code to try and make it more inline with best pratices. Renamed parser as it conflicted with a standard Python library.
This commit is contained in:
+9
-9
@@ -1,13 +1,13 @@
|
||||
import ptoken
|
||||
import parser
|
||||
from base_unit import Token, TokenType
|
||||
from ast_gen import Variable, Num
|
||||
|
||||
debug = False
|
||||
|
||||
def calculate_results(node):
|
||||
|
||||
if type(node) is parser.Num:
|
||||
if type(node) is Num:
|
||||
return float(node.value)
|
||||
elif type(node) is parser.Variable:
|
||||
elif type(node) is Variable:
|
||||
return node.value
|
||||
|
||||
left = calculate_results(node.left)
|
||||
@@ -21,19 +21,19 @@ def operate(n1, n2, tokenType):
|
||||
n1 = float(n1)
|
||||
n2 = float(n2)
|
||||
|
||||
if tokenType == ptoken.TokenType.add:
|
||||
if tokenType == TokenType.add:
|
||||
if debug: print("Adding %f and %f to get %f." %(n1, n2, n1 + n2))
|
||||
return n1 + n2
|
||||
elif tokenType == ptoken.TokenType.subtract:
|
||||
elif tokenType == TokenType.subtract:
|
||||
if debug: print("Subtracting %f and %f to get %f." %(n1, n2, n1 - n2))
|
||||
return n1 - n2
|
||||
elif tokenType == ptoken.TokenType.multiply:
|
||||
elif tokenType == TokenType.multiply:
|
||||
if debug: print("Multiplying %f and %f to get %f" %(n1, n2, n1 * n2))
|
||||
return n1 * n2
|
||||
elif tokenType == ptoken.TokenType.divide:
|
||||
elif tokenType == TokenType.divide:
|
||||
if debug: print("Dividing %f and %f to get %f" %(n1, n2, n1 / n2))
|
||||
return n1 / n2
|
||||
elif tokenType == ptoken.TokenType.power:
|
||||
elif tokenType == TokenType.power:
|
||||
if debug: print("Raising %f to the power of %f to get %f" %(n1, n2, n1**n2))
|
||||
return n1 ** n2
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user