Exemple #1
0
def analyzeRead():
    global token
    global hasCallReturn

    token = lexical.getToken()
    if token["Symbol"] == "sabre_parenteses":
        token = lexical.getToken()
        if token["Symbol"] == "sidentificador":
            check, position, type = symboltable.isVariable(token["Lexeme"])
            if check:
                if hasCallReturn == False:
                    codegeneration.generate(None, "RD", None, None)
                    codegeneration.generate(None, "STR", position, None)
                token = lexical.getToken()
                if token["Symbol"] == "sfecha_parenteses":
                    token = lexical.getToken()
                else:
                    error("a )", token["Line"])
            else:
                print("Found an error: " + token["Lexeme"] +
                      " was not declared as variable")
                exit()
        else:
            error("an identifier", token["Line"])
    else:
        error("a (", token["Line"])
Exemple #2
0
def analyzeWhile():
    global token
    global label
    global expression
    global hasCallReturn

    auxLabel1 = label
    if hasCallReturn == False:
        codegeneration.generate(label, "NULL", None, None)
    label += 1
    token = lexical.getToken()
    analyzeExpression()
    expressionPosFixa = posfixa.getPosFixa(expression)
    validateContentExpression(expressionPosFixa, "booleano")
    if hasCallReturn == False:
        codegeneration.generatePos(expressionPosFixa)
    expression.clear()
    if token["Symbol"] == "sfaca":
        auxLabel2 = label
        if hasCallReturn == False:
            codegeneration.generate(None, "JMPF", label, None)
        label += 1
        token = lexical.getToken()
        analyzeSimpleCommand()
        if hasCallReturn == False:
            codegeneration.generate(None, "JMP", auxLabel1, None)
            codegeneration.generate(auxLabel2, "NULL", None, None)
    else:
        error("'faca'", token["Line"])
Exemple #3
0
def analyzeWrite():
    global token
    global hasCallReturn

    token = lexical.getToken()
    if token["Symbol"] == "sabre_parenteses":
        token = lexical.getToken()
        if token["Symbol"] == "sidentificador":
            check, name, position = symboltable.hasIdentifier(token["Lexeme"])
            if check:
                if hasCallReturn == False:
                    if name == "function":
                        codegeneration.generate(None, "CALL", position, None)
                        codegeneration.generate(None, "PRN", None, None)
                    else:
                        codegeneration.generate(None, "LDV", position, None)
                        codegeneration.generate(None, "PRN", None, None)
                token = lexical.getToken()
                if token["Symbol"] == "sfecha_parenteses":
                    token = lexical.getToken()
                else:
                    error("a )", token["Line"])
            else:
                print("Found an error: " + token["Lexeme"] +
                      " was not declared as variable or function")
                exit()
        else:
            error("an identifier", token["Line"])
    else:
        error("a (", token["Line"])
Exemple #4
0
def analyzeFunctionDeclaration():
    global token
    global scope
    global label
    global toAlloc
    global listFunctionName
    global hasCallReturn

    token = lexical.getToken()
    if token["Symbol"] == "sidentificador":
        listFunctionName.append(token["Lexeme"])
        if not symboltable.searchDuplicity(token["Lexeme"], scope):
            symboltable.insert(token["Lexeme"], "function", scope, label, None)
            scope += 1
            codegeneration.generate(label, "NULL", None, None)
            label += 1
            token = lexical.getToken()
            if token["Symbol"] == "sdoispontos":
                token = lexical.getToken()
                if token["Symbol"] == "sinteiro" or token[
                        "Symbol"] == "sbooleano":
                    if token["Symbol"] == "sinteiro":
                        symboltable.insertFuncType("inteiro")
                    else:
                        symboltable.insertFuncType("booleano")
                    token = lexical.getToken()
                    if token["Symbol"] == "sponto_virgula":
                        analyzeBlock()
                    else:
                        error("an ;", token["Line"])
                else:
                    error("'inteiro' or 'booleano'", token["Line"])
            else:
                error("an :", token["Line"])
        else:
            errorTable(token["Lexeme"], token["Line"])
    else:
        error("a Identifier", token["Line"])

    if hasCallReturn == False:
        errorFunction(listFunctionName[-1])
    listFunctionName.pop()
    dalloc = symboltable.restoreLevel(scope)
    if dalloc != 0:
        toAlloc = toAlloc - dalloc
    else:
        codegeneration.generate(None, "RETURNF", None, None)
    hasCallReturn = False
    toAlloc -= 1
    scope -= 1
Exemple #5
0
def analyzeVarDeclaration():
    global token
    global toAlloc
    global countAlloc

    countAlloc = 0
    if token["Symbol"] == "svar":
        old = toAlloc
        token = lexical.getToken()
        if token["Symbol"] == "sidentificador":
            while token["Symbol"] == "sidentificador":
                analyzeVar()
                if token["Symbol"] == "sponto_virgula":
                    token = lexical.getToken()
                else:
                    error("a ;", token["Line"])
            codegeneration.generate(None, "ALLOC", old, countAlloc)
        else:
            error("an Identifier", token["Line"])
Exemple #6
0
def main():
    filePath, objPath = argsParser()
    lexical.getFile(filePath)

    token = lexical.getToken()
    codegeneration.generate(None, "START", None, None)
    if token["Symbol"] == "sprograma":
        token = lexical.getToken()
        if token["Symbol"] == "sidentificador":
            symboltable.insert(token["Lexeme"], "program", 0, None, None)
            token = lexical.getToken()
            if token["Symbol"] == "sponto_virgula":
                syntactic.analyzeBlock()
                token = lexical.getToken()
                if token != "End":
                    if token["Symbol"] == "sponto":
                        token = lexical.getToken()
                        if token == "End":
                            codegeneration.generate(None, "DALLOC", 0,
                                                    syntactic.toAlloc)
                            codegeneration.generate(None, "HLT", None, None)
                        else:
                            error("the end of file", token["Line"])
                    else:
                        error(".", token["Line"])
                else:
                    print("Found an error: Expected . to finish the program!")
            else:
                error("an ;", token["Line"])
        else:
            error("an Identifier", token["Line"])
    else:
        error("programa", token["Line"])
    codegeneration.makeObject(objPath)
Exemple #7
0
def analyzeProcedureDeclaration():
    global token
    global scope
    global label
    global toAlloc

    token = lexical.getToken()
    if token["Symbol"] == "sidentificador":
        if not symboltable.searchDuplicity(token["Lexeme"], scope):
            symboltable.insert(token["Lexeme"], "procedure", scope, label,
                               None)
            scope += 1
            codegeneration.generate(label, "NULL", None, None)
            label += 1
            token = lexical.getToken()
            if token["Symbol"] == "sponto_virgula":
                analyzeBlock()
            else:
                error("an ;", token["Line"])
        else:
            errorTable(token["Lexeme"], token["Line"])
    else:
        error("a Identifier", token["Line"])
    dalloc = symboltable.restoreLevel(scope)
    if dalloc != 0:
        codegeneration.generate(None, "DALLOC", (toAlloc - dalloc), dalloc)
        toAlloc = toAlloc - dalloc
    codegeneration.generate(None, "RETURN", None, None)
    toAlloc -= 1
    scope -= 1
Exemple #8
0
def analyzeProcedureAssignment():
    global token
    global listFunctionName
    global hasCallReturn
    global toAlloc
    global countAlloc
    global hasCallReturn

    check, name, position, type, lexeme = symboltable.hasAssignment(
        token["Lexeme"])
    if check:
        if name == "function":
            if len(listFunctionName) != 0 and lexeme == listFunctionName[-1]:
                token = lexical.getToken()
                if token["Symbol"] == "satribuicao":
                    analyzeAssignment(type)
                    hasCallReturn = True
                    if countAlloc != 0:
                        codegeneration.generate(None, "RETURNF",
                                                (toAlloc - countAlloc),
                                                countAlloc)
                    else:
                        codegeneration.generate(None, "RETURNF", None, None)
                else:
                    error("a :=", token["Line"])
            else:
                print("Found an error: It isn't possible assignment in line " +
                      str(token["Line"]))
                exit()
        else:
            token = lexical.getToken()
            if token["Symbol"] == "satribuicao":
                analyzeAssignment(type)
                if hasCallReturn == False:
                    codegeneration.generate(None, "STR", position, None)
            else:
                error("a :=", token["Line"])
    else:
        check, label = symboltable.isProcedure(token["Lexeme"])
        if check:
            token = lexical.getToken()
            if hasCallReturn == False:
                codegeneration.generate(None, "CALL", label, None)
        else:
            print("Found an error: " + token["Lexeme"] +
                  " was not declared as variable or procedure")
            exit()
Exemple #9
0
def analyzeSubRoutine():
    global token
    global label
    global toAlloc

    flag = 0
    if token["Symbol"] == "sprocedimento" or token["Symbol"] == "sfuncao":
        auxLabel = label
        codegeneration.generate(None, "JMP", label, None)
        label += 1
        flag = 1
    while token["Symbol"] == "sprocedimento" or token["Symbol"] == "sfuncao":
        toAlloc += 1
        if token["Symbol"] == "sprocedimento":
            analyzeProcedureDeclaration()
        else:
            analyzeFunctionDeclaration()
        token = lexical.getToken()
        if token["Symbol"] == "sponto_virgula":
            token = lexical.getToken()
        else:
            error("an ;", token["Line"])
    if flag == 1:
        codegeneration.generate(auxLabel, "NULL", None, None)