示例#1
0
 def __init__(self, actuals_list):
     ASTnode.__init__(self)
     self.actuals = []  # list of expressions
     while (len(actuals_list) > 0):
         push(top(actuals_list), self.actuals)
         pop(actuals_list)
     self.information = self.actuals
示例#2
0
 def __init__(self, parameters):
     ASTnode.__init__(self)
     self.formals = []
     #inserting a set of tuples: (identifierNode, typeNode)
     while len(parameters) > 0:
         push(top(parameters), self.formals)
         pop(parameters)  # perhaps change this!!
     self.information = self.formals
示例#3
0
 def check_formals(self):
     current_function = function_record[-1]
     formal_list = []
     for formal in self.formals.get_formals():
         if formal[0].get_value() in formal_list:
             msg = "Duplicate parameter {} in function {}."
             msg = msg.format(formal[0].get_value(), current_function)
             return msg
         push(formal[0].get_value(), formal_list)
示例#4
0
    def __init__(self, name, parameters, returnType, body):
        ASTnode.__init__(self)
        self.bodyNode = body
        self.typeNode = returnType
        self.formals = parameters
        self.identifierNode = FunctionIdentifierNode(name)
        self.outputType = self.typeNode.get_outputType()

        push(self.bodyNode, self.information)
        push(self.typeNode, self.information)
        push(self.formals, self.information)
        push(self.identifierNode, self.information)
示例#5
0
    def process_node(self, position=0):
        # ad-hoc means to push the ErrorNode to the parser---
        if len(function_record) > 0 and isinstance(top(function_record),
                                                   ErrorNode):
            return function_record
        # I'm unsure if this block is neccessary---#

        if position < len(
                self.information):  # if position is in bounds of information
            evaluate = self.information[position]  # take a value to evaluate

            if isinstance(evaluate, ASTnode):  # if it is a node
                if isinstance(evaluate, FunctionNode):
                    function_record.append(
                        evaluate.get_name()
                    )  # this list is used to keep track of which function is currently being processed

                nextNode = evaluate.process_node(
                    0)  # make way to the leaf of a branch

                # ad-hoc means to push the ErrorNode to the parser---
                if len(function_record) > 0 and isinstance(
                        top(function_record), ErrorNode):
                    return function_record
                # this block is used. Keep it until a more elegant solution is found---#

            # traverse each leaf of a respective branch
            nextInfo = self.process_node(position + 1)

            if isinstance(evaluate, ASTnode):
                # the aboverecursive descent will force typecheck to start at a leaf
                errorState = evaluate.typeCheck()
                if errorState != None:
                    push(ErrorNode(errorState), function_record)

            # ad-hocery! ---
            if isinstance(top(function_record), ErrorNode):
                return function_record
            #this block is used, simillar to previous block---#
            return self.information[position]
示例#6
0
 def __init__(self, ifExpression, thenExpression, elseExpression):
     ASTnode.__init__(self)
     self.expr2 = elseExpression
     self.expr1 = thenExpression
     self.condition = ifExpression
     push(self.expr2, self.information)
     push(self.expr1, self.information)
     push(self.condition, self.information)
示例#7
0
 def __init__(self, leftOperand, rightOperand):
     self.operatorType = "BinaryOperator"
     UnaryOperator.__init__(self, rightOperand)
     self.value1 = leftOperand
     push(self.value1, self.information)
     self.get_value = self.get_values
示例#8
0
 def __init__(self, value):
     ASTnode.__init__(self)
     self.value = value
     push(self.value, self.information)
示例#9
0
 def __init__(self, functionName, arguments):
     ASTnode.__init__(self)
     self.actualsNode = arguments
     self.identifierNode = FunctionIdentifierNode(functionName)
     push(self.actualsNode, self.information)
     push(self.identifierNode, self.information)
示例#10
0
 def __init__(self, expression):
     ASTnode.__init__(self)
     self.expression = expression
     push(self.expression, self.information)
示例#11
0
def nodeBuilder(semantic_stack, nodeType):
    if nodeType == ExpressionNode:
        expression = top(semantic_stack)
        pop(semantic_stack)
        return nodeType(expression)

    elif issubclass(nodeType, ValueNode):
        value = top(semantic_stack)
        pop(semantic_stack)
        if issubclass(nodeType, BinaryOperator):
            # right hand side is popped first...
            rightHandSide = value
            leftHandSide = top(semantic_stack)
            pop(semantic_stack)
            return nodeType(leftHandSide, rightHandSide)
        else:
            return nodeType(value)

    elif nodeType == PrintStatementNode:
        expressions = []
        while isinstance(top(semantic_stack), ExpressionNode):
            push(top(semantic_stack), expressions)
            pop(semantic_stack)
        return nodeType(expressions)

    elif nodeType == IfNode:  # rename these vars...[?]
        elseStatement = top(semantic_stack)
        pop(semantic_stack)
        thenStatement = top(semantic_stack)
        pop(semantic_stack)
        ifCondition = top(semantic_stack)
        pop(semantic_stack)
        return nodeType(ifCondition, thenStatement, elseStatement)

    elif nodeType == ActualsNode:
        actuals = []
        while isinstance(top(semantic_stack), ExpressionNode):
            push(top(semantic_stack), actuals)
            pop(semantic_stack)
        return nodeType(actuals)

    elif nodeType == FunctionCallNode:
        if isinstance(top(semantic_stack), ActualsNode):
            actualsNode = top(semantic_stack)
            pop(semantic_stack)
        else:
            # create empty actualsNode
            actualsNode = ActualsNode([])
        functionName = top(semantic_stack)
        pop(semantic_stack)
        return nodeType(functionName, actualsNode)

    elif nodeType == FormalsNode:  # getting parameter and argument switched up here...?
        parameters = []
        while True:
            if isinstance(top(semantic_stack), TypeNode):
                parameterType = top(semantic_stack)
                pop(semantic_stack)
                identifier = top(semantic_stack)
                pop(semantic_stack)
                push((identifier, parameterType), parameters)
            else:
                break
        return nodeType(parameters)

    elif nodeType == FunctionNode:
        body = top(semantic_stack)
        pop(semantic_stack)
        returnType = top(semantic_stack)
        pop(semantic_stack)
        if (isinstance(top(semantic_stack), FormalsNode)):
            parameters = top(semantic_stack)
            pop(semantic_stack)
        else:
            # create empty formalsNode
            parameters = FormalsNode([])
        name = top(semantic_stack)
        pop(semantic_stack)
        return nodeType(name, parameters, returnType, body)

    elif nodeType == BodyNode:
        expressions = []
        while isinstance(top(semantic_stack), ExpressionNode) or isinstance(
                top(semantic_stack), PrintStatementNode) or isinstance(
                    top(semantic_stack), BodyNode):
            push(top(semantic_stack), expressions)
            pop(semantic_stack)
        return nodeType(expressions)

    elif nodeType == DefinitionsNode:
        functions = []
        while True:
            if len(semantic_stack) > 0 and isinstance(top(semantic_stack),
                                                      FunctionNode):
                push(top(semantic_stack), functions)
                pop(semantic_stack)
            else:
                break
        return nodeType(functions)
    elif nodeType == ProgramNode:
        #hand the DefinitionsNode to the ProgramNode
        functionDefinitions = top(semantic_stack)
        pop(semantic_stack)
        return nodeType(functionDefinitions)
    else:
        raise ValueError("Invalid node type in nodeBuilder")
示例#12
0
 def __init__(self, functionDefinitions):
     ASTnode.__init__(self)
     self.definitionsNode = functionDefinitions
     push(self.definitionsNode, self.information)