Spaces:
Sleeping
Sleeping
File size: 1,546 Bytes
c9abdca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
'''
ABSTRACT SYNTAX TREE
This file contains the Python class that defines the abstract syntax tree (AST) representation.
'''
class OperatorNode:
'''
Class to represent operator nodes (i.e., an operator and its operands) as an AST.
Args:
operator (object): operator object (e.g., Add, Subtract, etc.)
children (list): list of children nodes (operands)
Example of usage:
add_node = OperatorNode(Add(), [IntegerConstant(7), IntegerConstant(5)])
subtract_node = OperatorNode(Subtract(), [IntegerConstant(3), IntegerConstant(1)])
multiply_node = OperatorNode(Multiply(), [add_node, subtract_node])
'''
def __init__(self, operator, children):
self.operator = operator # Operator object (e.g., Add, Subtract, etc.)
self.children = children # list of children nodes (operands)
def evaluate(self):
# check arity
if len(self.children) != self.operator.arity:
raise ValueError("Invalid number of operands for operator")
# recursively evaluate the operator and its operands
operands = [child.evaluate() for child in self.children]
return self.operator.evaluate(*operands)
def str(self):
# check arity
if len(self.children) != self.operator.arity:
raise ValueError("Invalid number of operands for operator")
# recursively generate a string representation of the AST
operand_strings = [child.str() for child in self.children]
return self.operator.str(*operand_strings) |