diff --git a/AssemblyCode.obj b/AssemblyCode.obj index ae2cf5d..f4c6853 100644 --- a/AssemblyCode.obj +++ b/AssemblyCode.obj @@ -1,47 +1,37 @@ START ALLOC 0 1 - ALLOC 1 3 + ALLOC 1 1 JMP 1 2 NULL - ALLOC 4 3 - ALLOC 7 1 - JMP 3 -4 NULL - RD - STR 4 - RD - STR 5 + ALLOC 2 1 + LDV 1 + LDC 1 + CEQ + JMPF 3 + LDC 1 + JMP 4 +3 NULL + LDV 1 + STR 2 + LDV 1 + LDC 1 + SUB + STR 1 + LDV 2 CALL 2 LDV 0 - STR 6 - LDV 6 - PRN - RETURN -5 NULL - ALLOC 8 1 - RD - STR 8 - LDV 8 - LDC 10 - CME - JMPF 6 - CALL 4 -6 NULL - DALLOC 8 1 - RETURN -3 NULL - CALL 5 - LDV 4 - LDV 5 - ADD + MULT +4 NULL STR 0 - DALLOC 4 4 + DALLOC 2 1 RETURNF 1 NULL + RD + STR 1 CALL 2 LDV 0 - STR 3 - LDV 3 + STR 1 + LDV 1 PRN - DALLOC 0 4 + DALLOC 0 2 HLT \ No newline at end of file diff --git a/src/codeGeneration.c b/src/codeGeneration.c index 62fe231..72aa726 100644 --- a/src/codeGeneration.c +++ b/src/codeGeneration.c @@ -239,7 +239,6 @@ void generateExpressionCode(ExpressionAnalyzer *posFix, Symbol *symbol) { generateAssembly("INV ", 0, 0); break; case UnarioP: - continue; break; case OpMultDiv: if(isEqualString(posFix->lexeme, "*")) @@ -264,7 +263,7 @@ void generateExpressionCode(ExpressionAnalyzer *posFix, Symbol *symbol) { generateAssembly("CMAQ ", 0, 0); else if(isEqualString(posFix->lexeme, "!=")) generateAssembly("CDIF ", 0, 0); - else if(isEqualString(posFix->lexeme, "==")) + else if(isEqualString(posFix->lexeme, "=")) generateAssembly("CEQ ", 0, 0); break; case Nao: diff --git a/src/symbol.c b/src/symbol.c index b163fe8..0b4f4c3 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -7,6 +7,7 @@ #include "verifyChar.h" +// Insert a new symbol into stack void insertSymbol(Symbol **stack, char *lexeme, bool scope, SymbolType type, int memory) { Symbol *new = (Symbol *)malloc(sizeof(Symbol)); @@ -19,6 +20,7 @@ void insertSymbol(Symbol **stack, char *lexeme, bool scope, SymbolType type, int (*stack) = new; } +// Insert the type of symbol of variables void putType(Symbol **stack, SymbolType type) { Symbol *l = *stack; for(; l != NULL && l->type == Var; l = l->next) { @@ -26,17 +28,20 @@ void putType(Symbol **stack, SymbolType type) { } } +// Insert the type of symbol of function void putTypeFunc(Symbol **stack, SymbolType type) { (*stack)->type = type; } -void printStack(Symbol *l){ +// Print all symbols in stack +void printStack(Symbol *l) { while(l != NULL){ printf("\nLexeme: %-30s \t\t Type: %s\nScope: %s Memory: %d\n\n", l->lexeme, symbolTypeToString(l->type), l->scope ? "Sim" : "Nao", l->memory); l = l->next; } } +// Free memory void freeSymbol(Symbol **l) { Symbol *aux = (*l), *aux2; while(aux != NULL){ @@ -47,7 +52,8 @@ void freeSymbol(Symbol **l) { (*l) = NULL; } -char* symbolTypeToString(SymbolType type){ +// Convert enum to string +char* symbolTypeToString(SymbolType type) { switch(type) { case Var: return "Var"; case Func: return "Func"; @@ -60,7 +66,8 @@ char* symbolTypeToString(SymbolType type){ } } -bool searchDuplicity(Symbol *l, char *lexeme){ +// Verify variable duplicity (need to check if duplicity on global variables are allowed) +bool searchDuplicity(Symbol *l, char *lexeme) { bool sameScope = true; for(Symbol *aux = l; aux != NULL; aux = aux->next) { if(aux->scope) @@ -71,6 +78,7 @@ bool searchDuplicity(Symbol *l, char *lexeme){ return false; } +// Returns the address of the declared procedure int searchProcAddr(Symbol *symbol, char *lexeme) { for(Symbol *l = symbol; l != NULL; l = l->next) if(isEqualString(l->lexeme, lexeme) && (l->type == Procedimento)) @@ -78,14 +86,15 @@ int searchProcAddr(Symbol *symbol, char *lexeme) { return -1; } -int searchVarFuncAddress(Symbol *symbol, char *lexeme){ +// Returns the address of the declared function or variable +int searchVarFuncAddress(Symbol *symbol, char *lexeme) { for(Symbol *l = symbol; l != NULL; l = l->next) if(isEqualString(l->lexeme, lexeme) && (l->type == VarBooleana || l->type == VarInteira || l->type == FuncInteira || l->type == FuncBooleana)) return l->memory; return -1; } -//Return the type of the var/func (Used on expression analyzer) +// Return the type of the var/func (Used on expression analyzer) SymbolType searchVarFuncType(Symbol *l, char *lexeme) { for(Symbol *aux = l; aux != NULL; aux = aux->next) { if(isEqualString(aux->lexeme, lexeme)) @@ -93,6 +102,7 @@ SymbolType searchVarFuncType(Symbol *l, char *lexeme) { } } +// Returns the type of the specified function or variable name bool verifyProcedureFunctionDuplicity(Symbol *symbol, char *lexeme) { for(Symbol *l = symbol; l != NULL; l = l->next) if(isEqualString(l->lexeme, lexeme)) @@ -100,6 +110,7 @@ bool verifyProcedureFunctionDuplicity(Symbol *symbol, char *lexeme) { return false; } +// Verify if the procedure or function have been already declared bool verifyProcedureDeclaration(Symbol *symbol, char *lexeme) { for(Symbol *l = symbol; l != NULL; l = l->next) if(isEqualString(l->lexeme, lexeme) && l->type == Procedimento) @@ -107,6 +118,7 @@ bool verifyProcedureDeclaration(Symbol *symbol, char *lexeme) { return false; } +// Verify if the procedure was already declared (check if search duplicity can overwrite this) bool verifyFunctionDeclaration(Symbol *symbol, char *lexeme) { for(Symbol *l = symbol; l != NULL; l = l->next) if(isEqualString(l->lexeme, lexeme) && (l->type == FuncBooleana || l->type == FuncInteira)){ @@ -115,6 +127,7 @@ bool verifyFunctionDeclaration(Symbol *symbol, char *lexeme) { return false; } +// Verify if the function was already declared (check if search duplicity can overwrite this) bool verifyVarFuncDeclaration(Symbol *symbol, char *lexeme) { for(Symbol *l = symbol; l != NULL; l = l->next) if(isEqualString(l->lexeme, lexeme) && (l->type == VarBooleana || l->type == FuncBooleana || l->type == VarInteira || l->type == FuncInteira)) @@ -122,6 +135,7 @@ bool verifyVarFuncDeclaration(Symbol *symbol, char *lexeme) { return false; } +// verify if the var/func was declared bool verifyVarDeclaration(Symbol *symbol, char *lexeme, int *memory){ for(Symbol *l = symbol; l != NULL; l = l->next) { if(isEqualString(l->lexeme, lexeme) && l->type == VarInteira) { @@ -132,6 +146,7 @@ bool verifyVarDeclaration(Symbol *symbol, char *lexeme, int *memory){ return false; } +// Unstack until the next scope (local variable region) int unStack(Symbol **symbol) { int countVars = 0; Symbol *aux = (*symbol), *aux2; @@ -150,7 +165,7 @@ int unStack(Symbol **symbol) { //################################################################# Posfix conversion -//Simple stack functions (Posfix conversion) +// Push stack (Used on expression analyzer) void push(simpleStack **stack, ExpressionAnalyzer *op){ simpleStack *new = (simpleStack *)malloc(sizeof(simpleStack)); ExpressionAnalyzer *ex = (ExpressionAnalyzer *)malloc(sizeof(ExpressionAnalyzer)); @@ -161,6 +176,7 @@ void push(simpleStack **stack, ExpressionAnalyzer *op){ *stack = new; } +// Pop stack (Used on expression analyzer) ExpressionAnalyzer pop(simpleStack **stack) { ExpressionAnalyzer ret; @@ -182,6 +198,7 @@ ExpressionAnalyzer pop(simpleStack **stack) { return ret; } +// Print the simple stack used to analyze the expression void printSimpleStack(simpleStack *s) { simpleStack *pointer = s; printf("\n\nSTACK:\n"); @@ -191,6 +208,7 @@ void printSimpleStack(simpleStack *s) { } } +// Free the simple stack memory allocated used to analyze the expression void freeSimpleStack(simpleStack **st) { simpleStack *aux = (*st), *aux2; while(aux != NULL){ @@ -201,6 +219,7 @@ void freeSimpleStack(simpleStack **st) { *st = aux; } +// Free the expression memory allocated used on semantic to analyze expressions void freeExpression(ExpressionAnalyzer **l) { ExpressionAnalyzer *aux = (*l), *aux2; while(aux != NULL){ @@ -211,6 +230,7 @@ void freeExpression(ExpressionAnalyzer **l) { (*l) = NULL; } +// Print the expression (Used on DEBUG mode to POS_FIX conversion) void printExpression(ExpressionAnalyzer *ex, char *ty, bool type) { ExpressionAnalyzer *aux = ex; printf("\n\nDEBUG - Semantico - %s\n",ty); @@ -224,6 +244,7 @@ void printExpression(ExpressionAnalyzer *ex, char *ty, bool type) { printf("\n\n"); } +// Make a copy of a expression (Used to make a copy from POS_FIX expression to analyze the expression type (Semantic Analayzer)) void copyExpression(ExpressionAnalyzer **dest, ExpressionAnalyzer *src) { if(debug) printf("DEBUG - Semantico - Copy Expression"); @@ -251,9 +272,10 @@ void copyExpression(ExpressionAnalyzer **dest, ExpressionAnalyzer *src) { } } +// Get the variable type (integer or boolean) LexemeType getVarType(Symbol *l, char *lexeme) { bool sameScope = true; - for(Symbol *aux = l; aux != NULL; aux = aux->next) { + for(Symbol *aux = l; aux != NULL; aux = aux->next) { //Search in the Symbol Table if(isEqualString(aux->lexeme, lexeme)){ if(aux->type == VarBooleana || aux->type == VarInteira) return aux->type == VarBooleana ? Booleano : Inteiro; @@ -266,9 +288,10 @@ LexemeType getVarType(Symbol *l, char *lexeme) { sameScope = false; } } - detectError(22, lineCount, '\0');//Não encontrou a variável na tabela de simbolos(Nao esta declarada ou não é visivel) + detectError(22, lineCount, '\0');// Did't find the variable in the Symbol Table - variable undeclared or not in the same scope } +// Check if the lexeme informed is a function and returns it's type LexemeType isFunction(Symbol *l, char *lexeme) { for(Symbol *aux = l; aux != NULL; aux = aux->next) { if(isEqualString(aux->lexeme, lexeme)){ @@ -283,6 +306,7 @@ LexemeType isFunction(Symbol *l, char *lexeme) { } } +// Used on syntatic to insert the found expression part void insertInFix(ExpressionAnalyzer **list, char lexeme[maxIdentifierLength], LexemeType type) { if(list == NULL) return; @@ -308,6 +332,7 @@ void insertInFix(ExpressionAnalyzer **list, char lexeme[maxIdentifierLength], Le aux->next = new; } +// Used on semantic to insert the IN_FIX convertion of the expression void insertPosFix(ExpressionAnalyzer **PosFix, ExpressionAnalyzer *Expression) { ExpressionAnalyzer *new = (ExpressionAnalyzer *)malloc(sizeof(ExpressionAnalyzer)); @@ -329,7 +354,7 @@ void insertPosFix(ExpressionAnalyzer **PosFix, ExpressionAnalyzer *Expression) { } -//searchStackMorePrecedence(&stack, inFix, PosFix) +// Find the next operator with more precedence to insert correctly in the POS_FIX expression void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, ExpressionAnalyzer **PosFix){ if(debug) printf("DEBUG - Semantico - Search Stack more precedence"); @@ -340,9 +365,9 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr switch (auxType){ case OpMaisMenos: while(aux != NULL){ - if(aux->c->type == AbreP || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E || aux->c->type == Rel)//Até encontrar (, final da pilha ou primeiro operador com precedência menor + if(aux->c->type == AbreP || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E || aux->c->type == Rel) // Util find '(' OR empty stack or the first operator with less procedence return; - if(aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP) {//copiando na saída todos os operadores com precedência maior ou igual ao que será empilhado + if(aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP) { // Insert POS_FIX all operators with precedence grather than or equals to the one that will be inserted in the STACK aux = aux->next; ret = pop(stack); insertPosFix(PosFix, &ret); @@ -352,9 +377,9 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr break; case OpMultDiv: while(aux != NULL){ - if(aux->c->type == AbreP || aux->c->type == OpMaisMenos || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E || aux->c->type == Rel)//Até encontrar (, final da pilha ou primeiro operador com precedência menor + if(aux->c->type == AbreP || aux->c->type == OpMaisMenos || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E || aux->c->type == Rel) // Util find '(' OR empty stack or the first operator with less procedence return; - if(aux->c->type == OpMultDiv || aux->c->type == UnarioN || aux->c->type == UnarioP) { //copiando na saída todos os operadores com precedência maior ou igual ao que será empilhado + if(aux->c->type == OpMultDiv || aux->c->type == UnarioN || aux->c->type == UnarioP) { // Insert POS_FIX all operators with precedence grather than or equals to the one that will be inserted in the STACK aux = aux->next; ret = pop(stack); insertPosFix(PosFix, &ret);; @@ -364,9 +389,9 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr break; case E: while(aux != NULL){ - if(aux->c->type == AbreP || aux->c->type == OU)//Até encontrar (, final da pilha ou primeiro operador com precedência menor + if(aux->c->type == AbreP || aux->c->type == OU) // Util find '(' OR empty stack or the first operator with less procedence return; - if(aux->c->type == Nao || aux->c->type == E || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP) { //copiando na saída todos os operadores com precedência maior ou igual ao que será empilhado + if(aux->c->type == Nao || aux->c->type == E || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP) { //Insert POS_FIX all operators with precedence grather than or equals to the one that will be inserted in the STACK aux = aux->next; ret = pop(stack); insertPosFix(PosFix, &ret); @@ -376,9 +401,9 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr break; case Nao: while(aux != NULL){ - if(aux->c->type == AbreP || aux->c->type == E || aux->c->type == OU)//Até encontrar (, final da pilha ou primeiro operador com precedência menor + if(aux->c->type == AbreP || aux->c->type == E || aux->c->type == OU) // Util find '(' OR empty stack or the first operator with less procedence return; - if(aux->c->type == Nao || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP) { //copiando na saída todos os operadores com precedência maior ou igual ao que será empilhado + if(aux->c->type == Nao || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP) { //Insert POS_FIX all operators with precedence grather than or equals to the one that will be inserted in the STACK aux = aux->next; ret = pop(stack); insertPosFix(PosFix, &ret); @@ -388,9 +413,9 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr break; case OU: while(aux != NULL){ - if(aux->c->type == AbreP)//Até encontrar (, final da pilha ou primeiro operador com precedência menor + if(aux->c->type == AbreP) // Util find '(' OR empty stack or the first operator with less procedence return; - if(aux->c->type == E || aux->c->type == Nao || aux->c->type == OU || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP){ //copiando na saída todos os operadores com precedência maior ou igual ao que será empilhado + if(aux->c->type == E || aux->c->type == Nao || aux->c->type == OU || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP){ //Insert POS_FIX all operators with precedence grather than or equals to the one that will be inserted in the STACK aux = aux->next; ret = pop(stack); insertPosFix(PosFix, &ret); @@ -400,9 +425,9 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr break; case Rel: while(aux != NULL){ - if(aux->c->type == AbreP || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E)//Até encontrar (, final da pilha ou primeiro operador com precedência menor + if(aux->c->type == AbreP || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E) // Util find '(' OR empty stack or the first operator with less procedence return; - if(aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP){ //copiando na saída todos os operadores com precedência maior ou igual ao que será empilhado + if(aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos || aux->c->type == UnarioN || aux->c->type == UnarioP){ //Insert POS_FIX all operators with precedence grather than or equals to the one that will be inserted in the STACK aux = aux->next; ret = pop(stack); insertPosFix(PosFix, &ret); @@ -413,9 +438,9 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr case UnarioN: case UnarioP: while(aux != NULL){ - if(aux->c->type == AbreP || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos)//Até encontrar (, final da pilha ou primeiro operador com precedência menor + if(aux->c->type == AbreP || aux->c->type == Nao || aux->c->type == OU || aux->c->type == E || aux->c->type == Rel || aux->c->type == OpMultDiv || aux->c->type == OpMaisMenos)//Util find '(' OR empty stack or the first operator with less procedence return; - if(aux->c->type == UnarioN || aux->c->type == UnarioP){ //copiando na saída todos os operadores com precedência maior ou igual ao que será empilhado + if(aux->c->type == UnarioN || aux->c->type == UnarioP){ //Insert POS_FIX all operators with precedence grather than or equals to the one that will be inserted in the STACK aux = aux->next; ret = pop(stack); insertPosFix(PosFix, &ret); @@ -428,32 +453,32 @@ void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, Expr } } +// Used to identify Unary operators in the IN_FIX expression and change their type to 'UnarioP' or 'UnarioN' (Used before POS_FIX conversion) void verifyUnaryOperators(ExpressionAnalyzer **inFix) { /* - Se é o primeiro: - Se é +, descarta - Se é -, atribui unario - loop - Se atual for + ou - e atual não for varint/funcint/num, atribui unario + If is the first and the lexeme is + or -: + Attribute unary + If actual is + or - and previous token is not varint/funcInt/num: + Attribute unary */ if(debug) printf("DEBUG - Semantico - Verify Unary Operators"); ExpressionAnalyzer *aux = (*inFix), *auxFree = NULL, *last = NULL, *aux2 = NULL; while(aux != NULL) { - if(aux->next != NULL){ + if(aux->next != NULL) { if(aux->type == OpMaisMenos) { - if(last == NULL && isEqualString(aux->lexeme, "-")) { //unário negativo + if(last == NULL && isEqualString(aux->lexeme, "-")) { // Negative unary aux->type = UnarioN; - } else if(last == NULL && isEqualString(aux->lexeme, "+")) { //unário positivo (remove) + } else if(last == NULL && isEqualString(aux->lexeme, "+")) { // Positive unary aux->type = UnarioP; - } else if((last->type != FuncInt && last->type != VarInt && last->type != Inteiro)) { //unário positivo (remove) - if(isEqualString(aux->lexeme, "+")){ + } else if((last->type != FuncInt && last->type != VarInt && last->type != Inteiro)) { // Positive unary + if(isEqualString(aux->lexeme, "+")) { aux->type = UnarioP; - } else if(isEqualString(aux->lexeme, "-")) { //unário negativo + } else if(isEqualString(aux->lexeme, "-")) { // Negative unary aux->type = UnarioN; } } - } else if(aux->type == Nao && aux->next != NULL && aux->next->type == Nao){ //(NAO NAO) (Se anulam) + } else if(aux->type == Nao && aux->next != NULL && aux->next->type == Nao) { // (NAO NAO) anulate themselves if(last != NULL) last->next = aux->next->next; else @@ -462,7 +487,7 @@ void verifyUnaryOperators(ExpressionAnalyzer **inFix) { free(aux); if(last != NULL) aux = last; - else{ + else { aux = aux2; (*inFix) = aux2; continue; @@ -476,30 +501,33 @@ void verifyUnaryOperators(ExpressionAnalyzer **inFix) { printExpression(*inFix, "Type VerifyUn",true); } +// Convert the IN_FIX expression to POS_FIX expression void convertPosFix(ExpressionAnalyzer **inFixIn, ExpressionAnalyzer **PosFix){ - verifyUnaryOperators(inFixIn); - //printExpression(*inFixIn, "IN_FIX_DEPOIS", false); - if(debug) + verifyUnaryOperators(inFixIn); // Check and convert the types of proper unary operators + + if(debug) { printf("DEBUG - Semantico - ENTROU POSFIX\n"); + printExpression(*inFixIn, "IN_FIX_DEPOIS", false); + } simpleStack *stack = NULL; /* - Se num ou var ou func, copia - Se (, empilha - Se ), desempilha até o primeiro ( - Se o tipo for OpMultDiv, OpMaisMenos, Rel, Nao, E, OU + If number or variable or function, copy + If (, push on stack + If ), unstack (pop) until the first ( + If the type is OpMultDiv, OpMaisMenos, Rel, Nao, E, OU, UnarioN, UnarioP analyze the stack to search the operators with more precedence and stack the current operator */ ExpressionAnalyzer aux; for(ExpressionAnalyzer *inFix = (*inFixIn); inFix != NULL; inFix = inFix->next) { if(inFix->type == VarInt || inFix->type == VarBool || inFix->type == Inteiro || inFix->type == FuncBool || inFix->type == FuncInt || inFix->type == Booleano)//variable or number or function insertPosFix(PosFix, inFix); - else if(inFix->type == AbreP){//Empilha + else if(inFix->type == AbreP){// Insert Stack push(&stack, inFix); } - else if(inFix->type == FechaP){//Desempilha + else if(inFix->type == FechaP){// Unstack do{ - aux = pop(&stack);//Joga na PosFix até achar o '(' + aux = pop(&stack);// Insert POS_FIX until find '(' if(aux.type != AbreP) insertPosFix(PosFix, &aux); } while(aux.type != AbreP); @@ -509,17 +537,17 @@ void convertPosFix(ExpressionAnalyzer **inFixIn, ExpressionAnalyzer **PosFix){ push(&stack, inFix); } else{ - searchStackMorePrecedence(&stack, inFix, PosFix); + searchStackMorePrecedence(&stack, inFix, PosFix); // Unstack operators with equal or more precendece push(&stack,inFix); } } } - if(stack != NULL) { + if(stack != NULL) { // Unstack the operators that remains in the stack do{ aux = pop(&stack); if(aux.type != AbreP || aux.type != FechaP) insertPosFix(PosFix, &aux); - }while(stack != NULL); + } while(stack != NULL); } } diff --git a/src/symbol.h b/src/symbol.h index 4f38d10..9becf12 100644 --- a/src/symbol.h +++ b/src/symbol.h @@ -30,20 +30,32 @@ typedef struct symbol { //################################################################# typedef enum LexemeType{ - VarInt, VarBool, FuncInt, FuncBool, AbreP, FechaP, - UnarioN, UnarioP, - OpMultDiv, OpMaisMenos, Rel, - Nao, E, OU, - Inteiro, Booleano + VarInt, // 0 + VarBool, // 1 + FuncInt, // 2 + FuncBool, // 3 + AbreP, // 4 + FechaP, // 5 + UnarioN, // 6 + UnarioP, // 7 + OpMultDiv, // 8 + OpMaisMenos, // 9 + Rel, // 10 + Nao, // 11 + E, // 12 + OU, // 13 + Inteiro, // 14 + Booleano // 15 }LexemeType; +// Used on expression analyzer type typedef struct expressionAnalyzer { char lexeme[maxIdentifierLength]; LexemeType type; struct expressionAnalyzer *next; }ExpressionAnalyzer; -//Simple stack functions (Posfix conversion) +// Simple stack functions (POS_FIX conversion) typedef struct simpleStack { ExpressionAnalyzer *c; struct simpleStack *next; @@ -72,12 +84,16 @@ char* symbolTypeToString(SymbolType type); // Verify variable duplicity (need to check if duplicity on global variables are allowed) bool searchDuplicity(Symbol *stack, char *lexeme); +// Returns the address of the declared procedure int searchProcAddr(Symbol *symbol, char *lexeme); +// Returns the address of the declared function or variable int searchVarFuncAddress(Symbol *symbol, char *lexeme); +// Returns the type of the specified function or variable name SymbolType searchVarFuncType(Symbol *l, char *lexeme); +// Verify if the procedure or function have been already declared bool verifyProcedureFunctionDuplicity(Symbol *symbol, char *lexeme); // Verify if the procedure was already declared (check if search duplicity can overwrite this) @@ -97,32 +113,46 @@ int unStack(Symbol **symbol); //################################################################# +// Push stack (Used on expression analyzer) void push(simpleStack **stack, ExpressionAnalyzer *c); +// Pop stack (Used on expression analyzer) ExpressionAnalyzer pop(simpleStack **stack); +// Print the simple stack used to analyze the expression void printSimpleStack(simpleStack *s); +// Free the simple stack memory allocated used to analyze the expression void freeSimpleStack(simpleStack **st); +// Free the expression memory allocated used on semantic to analyze expressions void freeExpression(ExpressionAnalyzer **l); +// Print the expression (Used on DEBUG mode to POS_FIX conversion) void printExpression(ExpressionAnalyzer *ex, char *ty, bool type); +//Make a copy of a expression (Used to make a copy from POS_FIX expression to analyze the expression type (Semantic Analayzer)) void copyExpression(ExpressionAnalyzer **dest, ExpressionAnalyzer *src); +// Get the variable type (integer or boolean) LexemeType getVarType(Symbol *l, char *lexeme); +// Check if the lexeme informed is a function and returns it's type LexemeType isFunction(Symbol *l, char *lexeme); +// Used on syntatic to insert the found expression part void insertInFix(ExpressionAnalyzer **list, char lexeme[maxIdentifierLength], LexemeType type); +// Used on semantic to insert the IN_FIX convertion of the expression void insertPosFix(ExpressionAnalyzer **PosFix, ExpressionAnalyzer *Expression); +// Find the next operator with more precedence to insert correctly in the POS_FIX expression void searchStackMorePrecedence(simpleStack **stack, ExpressionAnalyzer *op, ExpressionAnalyzer **PosFix); +// Used to identify Unary operators in the IN_FIX expression and change their type to 'UnarioP' or 'UnarioN' (Used before POS_FIX conversion) void verifyUnaryOperators(ExpressionAnalyzer **inFix); +// Convert the IN_FIX expression to POS_FIX expression void convertPosFix(ExpressionAnalyzer **inFixIn, ExpressionAnalyzer **PosFix); #endif \ No newline at end of file diff --git a/src/syntatic.c b/src/syntatic.c index 340ad25..c3da2db 100644 --- a/src/syntatic.c +++ b/src/syntatic.c @@ -183,7 +183,7 @@ void semanticAnalyzer(ExpressionAnalyzer **inFix, LexemeType type, Symbol *symbo analyzeExpressionType(&analyze, type); generateExpressionCode(posFix, symbol); //TODO - + freeExpression(inFix); freeExpression(&posFix); freeExpression(&analyze); @@ -456,11 +456,13 @@ void analyzeConditional(char *c, Token **token, Symbol **symbol, ExpressionAnaly if (isEqualString((*token)->symbol, "ssenao")) { auxrot2 = label++; generateAssembly("JMP ", auxrot2, 0); + } + generateAssembly("NULL ", auxrot1, 0); + if (isEqualString((*token)->symbol, "ssenao")) { getNewToken(c, token, *symbol, inFix); analyzeSimpleCommand(c, token, symbol, inFix); - generateAssembly("NULL ", auxrot2, 0); + generateAssembly("NULL ", auxrot2, 0); } - generateAssembly("NULL ", auxrot1, 0); } else errorSintax(token, 18, '\0'); } diff --git a/src/testes/GeraRafa.txt b/src/testes/GeraRafa.txt new file mode 100644 index 0000000..e0f59a5 --- /dev/null +++ b/src/testes/GeraRafa.txt @@ -0,0 +1,44 @@ + + +programa testefinal; {ERRO variavel nao declarada} + +var opcao,x,y,total:inteiro; + + +procedimento numeros; +var x,y: inteiro; + +procedimento dobro; +var media: inteiro; +inicio + media:=(x+y)*2; + escreva(media) +fim; + +inicio + leia(x); + leia(y); + total:= x+y; + escreva(total); {soma dos numeros lidos} + dobro {Calcula a media aritmetica dos numeros lidos} +fim; + +procedimento p; {calcula fatouial de um numero lido} +var total,z: inteiro; +inicio + z:= x; x:=x-1; + se z>1 entao p {recursivo} + senao y:=1; + y:=y*z +fim { p }; + +inicio + leia(opcao); + se opcao = 1 + entao numeros + senao inicio + leia(x); + p; + escreva(y) + fim +fim. \ No newline at end of file diff --git a/src/testes/gerahenrique.txt b/src/testes/gerahenrique.txt new file mode 100644 index 0000000..428b087 --- /dev/null +++ b/src/testes/gerahenrique.txt @@ -0,0 +1,31 @@ + +{% teste 3} +programa leitura; +var x,r:booleano; + a,y,b:booleano; +funcao func1: inteiro; +var a,b,c: inteiro; +inicio + func1:= +a-(-b); + fim; +procedimento ver; +var i: inteiro; +inicio + i:= 3; + x:=verdadeiro; + enquanto (i>1) ou (+i*(-100) >(-func1) ) e (nao x) + faca inicio + i:= i-1; + se i = 2 + entao a:= b; + escreva(i) + fim; + escreva(i); +fim; + + +inicio + x:= verdadeiro; + y:= falso; + ver; +fim. \ No newline at end of file