Beispiel #1
0
def p_func_else(p):
    'func_else : '
    quadruple = Quad("operator_goto", "-1", "-1", "pending")
    globalScope.quads.append(quadruple)
    globalScope.quadCount += 1
    # Adds quad after else goto to if gotoF
    endIf = globalScope.pendingJumps.pop()
    globalScope.quads[endIf - 1].setResult(globalScope.quadCount)
    # Adds else goto pending jump
    globalScope.pendingJumps.push(globalScope.quadCount - 1)
Beispiel #2
0
def p_func_endWhile(p):
    'func_endWhile : '
    endWhile = globalScope.pendingJumps.pop()
    returnWhile = globalScope.pendingJumps.pop()

    quadruple = Quad("operator_goto", "-1", "-1", returnWhile)
    globalScope.quads.append(quadruple)
    globalScope.quadCount += 1

    globalScope.quads[endWhile - 1].setResult(globalScope.quadCount)
Beispiel #3
0
def p_func_start(p):
    'func_start :'
    quadruple = Quad("operator_goto", -1, -1, "pending")
    globalScope.quads.append(quadruple)
    globalScope.quadCount += 1
    globalScope.pendingJumps.push(globalScope.quadCount - 1)
    if globalScope.functionDirectory.addFunction("Main", "void", -1):
        globalScope.functionName = "Main"
    else:
        sys.exit("Error: Function ID already exists")
Beispiel #4
0
def p_func_logicOP(p):
    'func_logicOP : '
    if not globalScope.pendingOperators.isEmpty() and (
            globalScope.pendingOperators.top() == "operator_and"
            or globalScope.pendingOperators.top() == "operator_or"):
        rightOp = globalScope.pendingOperands.pop()
        rightType = globalScope.operandTypes.pop()
        leftOp = globalScope.pendingOperands.pop()
        leftType = globalScope.operandTypes.pop()
        operator = globalScope.pendingOperators.pop()

        resultType = globalScope.semanticCube.verification(
            operator, leftType, rightType)

        if resultType != None:
            result = "t" + str(globalScope.tempCount)
            globalScope.tempCount += 1
            globalScope.pendingOperands.push(result)
            globalScope.operandTypes.push(resultType)
            quadruple = Quad(operator, leftOp, rightOp, result)
            globalScope.quads.append(quadruple)
            globalScope.quadCount += 1
        else:
            sys.exit("Type " + leftType + " and type " + rightType +
                     " can't be combined with a logical operator")

    elif not globalScope.pendingOperators.isEmpty(
    ) and globalScope.pendingOperators.top() == "operator_not":
        rightOp = globalScope.pendingOperands.pop()
        rightType = globalScope.operandTypes.pop()
        operator = globalScope.pendingOperators.pop()

        if rightType == "bool":
            result = "t" + str(globalScope.tempCount)
            globalScope.tempCount += 1
            globalScope.pendingOperands.push(result)
            globalScope.operandTypes.push("bool")
            quadruple = Quad(operator, "-1", rightOp, result)
            globalScope.quads.append(quadruple)
            globalScope.quadCount += 1
        else:
            sys.exit(
                "Operator not can only be applied to operands of type bool")
Beispiel #5
0
def p_func_endCallFunction(p):
    'func_endCallFunction : '
    if globalScope.functionCalled == globalScope.pendingOperands.pop():
        quadruple = Quad("operator_gosub", globalScope.functionCalled, "-1",
                         "-1")
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1

        globalScope.functionCalled = ""
        globalScope.parameterCount = 1
Beispiel #6
0
def p_func_callFunc(p):
    'func_callFunc : '
    if globalScope.functionDirectory.functionExists(p[-1]):
        globalScope.functionCalled = p[-1]

        globalScope.pendingOperands.push(p[-1])
        quadruple = Quad("operator_era", p[-1], "-1", "-1")
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1
    else:
        sys.exit("Function ID doesn't exist in directory")
Beispiel #7
0
def p_func_read(p):
    'func_read : '
    input_id = p[-1]

    if globalScope.functionDirectory.varExists(
            globalScope.functionName, input_id) or (
                globalScope.functionName != "Main"
                and globalScope.functionDirectory.varExists("Main", input_id)):
        quadruple = Quad("operator_read", "-1", "-1", input_id)
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1
    else:
        sys.exit("Input ID " + input_id + " does not exist")
Beispiel #8
0
def p_func_whileExp(p):
    'func_whileExp : '
    whileType = globalScope.operandTypes.pop()

    if whileType == "bool":
        result = globalScope.pendingOperands.pop()
        quadruple = Quad("operator_gotoF", result, "-1", "pending")
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1
        globalScope.pendingJumps.push(globalScope.quadCount - 1)
    else:
        sys.exit("While expression can't be evaluated with value of type " +
                 whileType)
Beispiel #9
0
def p_func_if(p):
    'func_if : '
    expressionType = globalScope.operandTypes.pop()

    if expressionType == "bool":
        result = globalScope.pendingOperands.pop()
        quadruple = Quad("operator_gotoF", result, "-1", "pending")
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1
        globalScope.pendingJumps.push(globalScope.quadCount - 1)
    else:
        sys.exit("Cannot evaluate if condition with expression of type " +
                 expressionType)
Beispiel #10
0
def p_func_clear(p):
    'func_clear : '
    quadruple = Quad("operator_endfunc", "-1", "-1", "-1")
    globalScope.quads.append(quadruple)
    globalScope.quadCount += 1

    globalScope.pendingOperators.empty()
    globalScope.pendingOperands.empty()
    globalScope.operandTypes.empty()
    globalScope.isVarFlag = True
    globalScope.functionName = ""
    globalScope.varName = ""
    globalScope.varType = ""
    globalScope.varSize = ""
Beispiel #11
0
def p_func_endDoWhile(p):
    'func_endDoWhile : '
    whileType = globalScope.operandTypes.pop()

    if whileType == "bool":
        result = globalScope.pendingOperands.pop()
        returnLoop = globalScope.pendingJumps.pop()

        quadruple = Quad("operator_gotoT", result, "-1", returnLoop)
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1
    else:
        sys.exit("Do while expression can't be evaluated with value of type " +
                 whileType)
Beispiel #12
0
def p_func_callFuncParameter(p):
    'func_callFuncParameter : '
    parameterVar = globalScope.pendingOperands.pop()
    parameterType = globalScope.operandTypes.pop()

    if parameterType == globalScope.functionDirectory.functions[
            globalScope.functionCalled][2][globalScope.parameterCount - 1]:
        quadruple = Quad("operator_param", parameterVar, "-1",
                         "param" + str(globalScope.parameterCount))
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1
        globalScope.parameterCount += 1
    else:
        sys.exit("Parameter incorrect for function " +
                 globalScope.pendingOperands.top())
Beispiel #13
0
def p_func_endCallFunction(p):
    'func_endCallFunction : '
    if globalScope.functionCalled == globalScope.pendingOperands.pop():
        quadruple = Quad("operator_gosub", globalScope.functionCalled, "-1",
                         "-1")
        globalScope.quads.append(quadruple)
        globalScope.quadCount += 1

        print("Local Memory for: " + globalScope.functionCalled)
        globalScope.functionDirectory.local_memory.print_Memory()
        print("-----------------------------")

        globalScope.functionCalled = ""
        globalScope.parameterCount = 1

        # CLEAR LOCAL MEMORY
        globalScope.functionDirectory.local_memory.clear_Memory()
Beispiel #14
0
def p_func_assign_value(p):
    'func_assign_value : '
    if not globalScope.pendingOperators.isEmpty(
    ) and globalScope.pendingOperators.top() == "operator_assign":
        # value
        rightOp = globalScope.pendingOperands.pop()
        rightType = globalScope.operandTypes.pop()
        # id with assigned value
        leftOp = globalScope.pendingOperands.pop()
        leftType = globalScope.operandTypes.pop()

        operator = globalScope.pendingOperators.pop()
        resultType = globalScope.semanticCube.verification(
            operator, leftType, rightType)

        if resultType != None:
            quadruple = Quad(operator, rightOp, "-1", leftOp)
            globalScope.quads.append(quadruple)
            globalScope.quadCount += 1
        else:
            sys.exit("Unable to assign value of type " + rightType +
                     " to ID of type " + leftType)
Beispiel #15
0
def p_func_term(p):
    'func_term : '
    if not globalScope.pendingOperators.isEmpty() and (
            globalScope.pendingOperators.top() == "operator_add"
            or globalScope.pendingOperators.top() == "operator_minus"):
        rightOp = globalScope.pendingOperands.pop()
        rightType = globalScope.operandTypes.pop()
        leftOp = globalScope.pendingOperands.pop()
        leftType = globalScope.operandTypes.pop()
        operator = globalScope.pendingOperators.pop()

        resultType = globalScope.semanticCube.verification(
            operator, leftType, rightType)

        if resultType != None:
            result = "t" + str(globalScope.quadCount + 1)
            globalScope.pendingOperands.push(result)
            globalScope.operandTypes.push(resultType)
            quadruple = Quad(operator, leftOp, rightOp, result)
            globalScope.quads.append(quadruple)
            globalScope.quadCount += 1
        else:
            sys.exit("Unable to add/subtract term of type " + leftType +
                     " with term of type " + rightType)
Beispiel #16
0
def p_func_factor(p):
    'func_factor : '
    if not globalScope.pendingOperators.isEmpty() and (
            globalScope.pendingOperators.top() == "operator_mult"
            or globalScope.pendingOperators.top() == "operator_div"):
        rightOp = globalScope.pendingOperands.pop()
        rightType = globalScope.operandTypes.pop()
        leftOp = globalScope.pendingOperands.pop()
        leftType = globalScope.operandTypes.pop()
        operator = globalScope.pendingOperators.pop()

        resultType = globalScope.semanticCube.verification(
            operator, leftType, rightType)

        if resultType != None:
            result = "t" + str(globalScope.quadCount + 1)
            globalScope.pendingOperands.push(result)
            globalScope.operandTypes.push(resultType)
            quadruple = Quad(operator, leftOp, rightOp, result)
            globalScope.quads.append(quadruple)
            globalScope.quadCount += 1
        else:
            sys.exit("Unable to multiply/divide factor of type " + leftType +
                     " with factor of type " + rightType)
Beispiel #17
0
def p_func_print(p):
    'func_print : '
    output_exp = globalScope.pendingOperands.pop()
    quadruple = Quad("operator_print", output_exp, "", "")
    globalScope.quads.append(quadruple)
    globalScope.quadCount += 1
Beispiel #18
0
def p_func_constantIDArray(p):
    'func_constantIDArray : '
    functionName = globalScope.functionName
    globalScope.varName = p[-4]

    if globalScope.functionDirectory.varExists(functionName, p[-4]):
        index_val = globalScope.pendingOperands.pop()
        index_type = globalScope.operandTypes.pop()

        if index_type == "number":
            if globalScope.functionDirectory.varExists(functionName,
                                                       globalScope.index):
                #crea una direccion temporal para el result del quad de address
                address = globalScope.functionDirectory.constant_memory.get_nextAddress(
                    "number")
                globalScope.functionDirectory.constant_memory.set_AddressValue(
                    address, p[-2])

                #pushea esa en el stack
                globalScope.pendingOperands.push(address)
                globalScope.operandTypes.push(
                    globalScope.functionDirectory.getVarType(
                        functionName, p[-4]))

                quadruple = Quad(
                    "operator_verify", "0",
                    globalScope.functionDirectory.functions[functionName][1][
                        p[-4]][1], index_val)
                globalScope.quads.append(quadruple)

                quadruple = Quad(
                    "operator_address", index_val,
                    globalScope.functionDirectory.getVarAddress(
                        functionName, p[-4]), address)
                globalScope.quads.append(quadruple)

                globalScope.quadCount += 2

            elif int(
                    float(globalScope.index)
            ) < globalScope.functionDirectory.functions[functionName][1][
                    p[-4]][1] and int(float(globalScope.index)) > -1:
                address = globalScope.functionDirectory.getVarAddress(
                    functionName, p[-4]) + int(float(globalScope.index))
                globalScope.pendingOperands.push(address)
                globalScope.operandTypes.push(
                    globalScope.functionDirectory.getVarType(
                        functionName, p[-4]))

                quadruple = Quad(
                    "operator_verify", "0",
                    globalScope.functionDirectory.functions[functionName][1][
                        p[-4]][1], globalScope.index)
                globalScope.quads.append(quadruple)

                quadruple = Quad(
                    "operator_address", globalScope.index,
                    globalScope.functionDirectory.getVarAddress(
                        functionName, p[-4]), address)
                globalScope.quads.append(quadruple)

                globalScope.quadCount += 2

            else:
                sys.exit("Out of range for size of variable " + p[-4])
        else:
            sys.exit("Wrong type of variable for index.")
    else:
        sys.exit("ID " + p[-4] + " does not exist")