def createFunctionDefinition(self, def_token, var_token, params, val_node): lamToken = LexToken() lamToken.value = 'lambda' lamToken.type = 'LAMBDA' return LetNode(def_token, [ VariableNode(var_token), LambdaNode(lamToken, [Node(None, None, nodes(params)), val_node]), ])
from Node import EquationNode, VariableNode if __name__ == '__main__': A = VariableNode("A", 3) B = VariableNode("B", None) Fx = EquationNode("Fx") Fx.construct_tree([A, B, "*", 2, "-"]) # 3B*2- #print(equation1.print_in_order(equation1.root)) # #print(equation1.calculate_in_order(equation1.root)) try: Fx.calc_value() print(C.data) except: pass Gx = EquationNode("Gx") Gx.construct_tree([A, 2, "/"]) C = VariableNode("C", None, [Gx]) C.calc_value() print(C.data)
def createString(self, var_token): return VariableNode(var_token)
def createBoolean(self, var_token): return VariableNode(var_token)
def createInteger(self, var_token): return VariableNode(var_token)
def createVariable(self, var_token): if var_token.value in self.functionMapping: var_token.value = self.functionMapping[var_token.value] return VariableNode(var_token)
def nodes(tokens): return list(map(lambda x: VariableNode(x), tokens))
def createLambda(self, lambda_token, params, val): if len(params) > 0: return LambdaNode(lambda_token, [Node(None, None, nodes(params)), val]) else: return VariableNode(lambda_token, [val])
def createVariableDefinition(self, def_token, var_token, val_node): return LetNode(def_token, [VariableNode(var_token), val_node])