Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Rodrigo Oliveira 2016241830
- António Morais 2016217816*/
- %{
- #include "estruturas.h"
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define YYDEBUG 1
- extern int yydebug;
- int yylex(void);
- void yyerror (char *s);
- typedef struct node* n1;
- typedef struct node{
- char * descricao;
- char * valor;
- char * type;
- n1 filhos;
- n1 irmaos;
- }Node;
- n1 new_node(char *, char*);
- void add_Irmao(n1, n1);
- n1 add_Irmao_2(n1, n1);
- void imprime_AST(n1,int);
- void add_Filhos(n1,n1);
- void add_FilhoInicioComIrmaos(n1,n1);
- void imprime_Irmaos(n1);
- void copyType(n1,n1);
- void change_Tipo(n1,n1);
- n1 add_Type2Irmao(n1,n1);
- int conta_filhos(n1);
- /////////////////////////// META 3 ///////////////////////////
- void percorre_AST (n1 roots);
- void adiciona_VarDecl(n1 var_decl_Root);
- int checkVarDecl(char* VarName,scope_Pointer scope);
- int checkFuncDecl(char * nome_Func);
- int checkFuncInTable(char* nome_Func);
- void addFuncToGlogal(n1 Func_Root);
- void addTypesToGlobal(n1 FuncDef,table_Pointer table, n1 root,char* return_d);
- char* transforma(char* char1, char* char2);
- char* tornaPrimeiroCharEmLowerCase(char* atual, char* final);
- void printScopes();
- void printTables(table_Pointer table, int x);
- void Impressao_Scopes();
- void imprime_Scope(char* name, char* params);
- void addPutcharGetChar();
- void add_NewScope(char* name,n1 FuncRoot, n1 parametros,char* return_d);
- void percorre_Funcao(n1 FuncRoot,scope_Pointer scope_Func,table_Pointer tabelas);
- void imprime_AST_Anotada_Main(n1 root,int depth,char* node_Type,char* curScope);
- void AST_Anotada(n1 root,int depth,char* node_Type,char* curScope);
- char* Check_Type(char* scope_Name,char* id_Name);
- char* Check_Type_2(char* id_Name);
- char* Check_Func_Type(char* name);
- char* junta_Chars(char* char1, char* char2);
- char* expression_Call_Type(n1 root);
- void imprime_AST_Pos_Anotada(n1 root, int depth,n1 pai,int flag);
- char* expression_Type(n1 root);
- char* expr_Unary_Type(n1 root);
- char* expression_Comma_Type(n1 root);
- char* assign_type(n1 root);
- /////////////////////////// META 3 ///////////////////////////
- //n1 root;
- n1 aux_1;
- n1 aux_2;
- extern int flagt;
- extern int s_error;
- int flag_state = 0;
- int cont = 0;
- scope_Pointer scope_Root;
- scope_Pointer scope_Root_aux;
- %}
- %union{
- char* valor;
- struct node* node;
- }
- %token <valor> REALLIT;
- %token <valor> INTLIT;
- %token <valor> STRLIT;
- %token <valor> ID;
- %token PACKAGE;
- %token RETURN;
- %token FUNC;
- %token INT;
- %token ELSE;
- %token FOR;
- %token IF;
- %token VAR;
- %token FLOAT32;
- %token BOOL;
- %token STRING;
- %token PRINT;
- %token PARSEINT;
- %token CMDARGS;
- %token SEMICOLON;
- %token BLANKID;
- %token AND;
- %token ASSIGN;
- %token STAR;
- %token COMMA;
- %token DIV;
- %token EQ;
- %token GE;
- %token GT;
- %token LBRACE;
- %token LE;
- %token LPAR;
- %token LSQ;
- %token LT;
- %token MINUS;
- %token MOD;
- %token NE;
- %token NOT;
- %token OR;
- %token PLUS;
- %token RBRACE;
- %token RPAR;
- %token RSQ;
- %token RESERVED;
- %type <node> Program;
- %type <node> Declarations;
- %type <node> VarDeclaration;
- %type <node> VarSpec;
- %type <node> VarSpec_2;
- %type <node> Type;
- %type <node> FuncDeclaration;
- %type <node> FuncHeader
- %type <node> Parameters;
- %type <node> ParamDecl;
- %type <node> FuncBody;
- %type <node> VarsAndStatements;
- %type <node> Statement;
- %type <node> Statement_2;
- %type <node> ParseArgs;
- %type <node> FuncInvocation;
- %type <node> FuncInvocation_2;
- %type <node> Expression;
- %left COMMA;
- %right ASSIGN;
- %left OR;
- %left AND;
- %left GE GT LE LT EQ NE;
- %left PLUS MINUS;
- %left STAR DIV MOD;
- %left NOT;
- %left LBRACE RBRACE LPAR RPAR;
- %nonassoc IF;
- %nonassoc ELSE;
- %%
- Program:
- PACKAGE ID SEMICOLON {n1 root = new_node(NULL,"Program");if(flagt == 1){imprime_AST(root,0);}else if(flagt == 2){percorre_AST(root);}}
- |PACKAGE ID SEMICOLON Declarations {n1 root = new_node(NULL,"Program");add_Filhos(root,$4);if(flagt == 1){imprime_AST(root,0);}else if(flagt == 2){percorre_AST(root);}}
- ;
- Declarations:
- VarDeclaration SEMICOLON {$$ = $1;}
- |FuncDeclaration SEMICOLON {$$ = $1;}
- |Declarations VarDeclaration SEMICOLON {if($1 != NULL){
- $$ = $1;
- add_Irmao($$,$2);
- }else{
- $$ = $2;
- }
- }
- |Declarations FuncDeclaration SEMICOLON {if($1 != NULL){
- $$ = $1;
- add_Irmao($$,$2);
- }else{
- $$ = $2;
- }
- }
- ;
- VarDeclaration:
- VAR VarSpec {$$ = $2;}
- |VAR LPAR VarSpec SEMICOLON RPAR {$$ = $3;}
- ;
- VarSpec:
- ID VarSpec_2 Type {$$ = new_node(NULL, "VarDecl");add_Filhos($$,$3);add_Irmao($$->filhos,new_node($1,"Id"));if($2 != NULL){add_Irmao($$, add_Type2Irmao($2,$3));}}
- ;
- VarSpec_2:
- COMMA ID VarSpec_2 {$$ = new_node(NULL, "VarDecl");add_Filhos($$,new_node(NULL, NULL)); add_Irmao($$->filhos, new_node($2, "Id")); if($3 != NULL){add_Irmao($$, $3);}}
- | {$$ = NULL;}
- ;
- Type:
- INT {$$ = new_node(NULL,"Int");}
- |FLOAT32 {$$ = new_node(NULL,"Float32");}
- |BOOL {$$ = new_node(NULL,"Bool");}
- |STRING {$$ = new_node(NULL,"String");}
- ;
- FuncDeclaration:
- FuncHeader FuncBody {$$=new_node(NULL,"FuncDecl");add_Filhos($$,$1);add_Irmao($$->filhos,$2);}
- ;
- FuncHeader:
- FUNC ID LPAR RPAR {$$=new_node(NULL,"FuncHeader");add_Filhos($$,new_node($2,"Id"));add_Irmao($$->filhos,new_node(NULL,"FuncParams"));}
- |FUNC ID LPAR Parameters RPAR {$$=new_node(NULL,"FuncHeader");add_Filhos($$,new_node($2,"Id"));add_Irmao($$->filhos,$4);}
- |FUNC ID LPAR RPAR Type {$$=new_node(NULL,"FuncHeader");add_Filhos($$,new_node($2,"Id"));add_Irmao($$->filhos,$5);add_Irmao($$->filhos,new_node(NULL,"FuncParams"));}
- |FUNC ID LPAR Parameters RPAR Type {$$=new_node(NULL,"FuncHeader");add_Filhos($$,new_node($2,"Id"));add_Irmao($$->filhos,$6);add_Irmao($$->filhos,$4);}
- ;
- Parameters:
- ParamDecl {$$=new_node(NULL,"FuncParams");add_Filhos($$,$1);}
- |Parameters COMMA ParamDecl {$$=$1;add_Irmao($$->filhos,$3);}
- ;
- ParamDecl:
- ID Type {$$=new_node(NULL,"ParamDecl");add_Filhos($$,$2);add_Irmao($$->filhos,new_node($1,"Id"));}
- ;
- FuncBody:
- LBRACE VarsAndStatements RBRACE {$$=new_node(NULL,"FuncBody");
- if($2!=NULL){
- add_Filhos($$,$2);
- }
- }
- ;
- VarsAndStatements:
- VarsAndStatements SEMICOLON {$$ = $1;}
- |VarsAndStatements VarDeclaration SEMICOLON {if($1 != NULL){
- $$ = $1;
- add_Irmao($$,$2);
- }else{
- $$ = $2;
- }
- }
- |VarsAndStatements Statement SEMICOLON {if($1 != NULL){
- $$ = $1;
- add_Irmao($$,$2);
- }else{
- $$ = $2;
- }
- }
- | {$$ = NULL;}
- ;
- Statement:
- ID ASSIGN Expression {$$ = new_node(NULL,"Assign");add_Filhos($$,new_node($1,"Id"));add_Irmao($$->filhos,$3);}
- |LBRACE Statement_2 RBRACE {if($2 != NULL){if(conta_filhos($2) > 1){$$ = new_node(NULL,"Block");add_Filhos($$,$2);cont = 0;}else{$$ = $2;}}else{$$ = NULL;}}
- |IF Expression LBRACE Statement_2 RBRACE {$$ = new_node(NULL,"If");add_Filhos($$,$2);aux_1 = new_node(NULL,"Block");aux_2 = new_node(NULL,"Block");add_Irmao($2,aux_1);add_Irmao(aux_1,aux_2);if($4 != NULL){add_Filhos(aux_1,$4);}}
- |IF Expression LBRACE Statement_2 RBRACE ELSE LBRACE Statement_2 RBRACE {$$ = new_node(NULL,"If");add_Filhos($$,$2);aux_1 = new_node(NULL,"Block");aux_2 = new_node(NULL,"Block");add_Irmao($2,aux_1);add_Irmao(aux_1,aux_2);if($4 != NULL){add_Filhos(aux_1,$4);}if($8 != NULL){add_Filhos(aux_2,$8);}}
- |FOR LBRACE Statement_2 RBRACE {$$ = new_node(NULL,"For");aux_1 = new_node(NULL,"Block");add_Filhos($$,aux_1);if($3 != NULL){add_Filhos(aux_1,$3);}}
- |FOR Expression LBRACE Statement_2 RBRACE {$$ = new_node(NULL,"For");add_Filhos($$,$2);aux_1 = new_node(NULL,"Block");add_Irmao($$->filhos,aux_1);if($4 != NULL){add_Filhos(aux_1,$4);}}
- |RETURN {$$ = new_node(NULL,"Return");}
- |RETURN Expression {$$ = new_node(NULL,"Return");add_Filhos($$,$2);}
- |FuncInvocation {$$ = $1;}
- |ParseArgs {$$ = $1;}
- |PRINT LPAR Expression RPAR {$$ = new_node(NULL,"Print");add_Filhos($$,$3);}
- |PRINT LPAR STRLIT RPAR {$$ = new_node(NULL,"Print");add_Filhos($$,new_node($3,"StrLit"));}
- |error {$$ = NULL;}
- ;
- Statement_2:
- Statement_2 Statement SEMICOLON {$$ = add_Irmao_2($1,$2);if($1 != NULL){cont += 1;}}
- | {$$ = NULL;}
- ;
- ParseArgs:
- ID COMMA BLANKID ASSIGN PARSEINT LPAR CMDARGS LSQ Expression RSQ RPAR {$$ = new_node(NULL,"ParseArgs");add_Filhos($$,new_node($1,"Id"));add_Irmao($$->filhos,$9);}
- |ID COMMA BLANKID ASSIGN PARSEINT LPAR error RPAR {$$ = NULL;}
- ;
- FuncInvocation:
- ID LPAR RPAR {$$ = new_node(NULL,"Call");add_Filhos($$,new_node($1,"Id"));}
- |ID LPAR Expression FuncInvocation_2 RPAR {$$ = new_node(NULL,"Call");add_Filhos($$,new_node($1,"Id"));add_Irmao($$->filhos,$3);add_Irmao($$->filhos,$4);}
- |ID LPAR Expression RPAR {$$ = new_node(NULL,"Call");add_Filhos($$,new_node($1,"Id"));add_Irmao($$->filhos,$3);}
- |ID LPAR error RPAR {$$ = NULL;}
- ;
- FuncInvocation_2:
- COMMA Expression {$$ = $2;}
- |FuncInvocation_2 COMMA Expression {if($1 != NULL){
- $$ = $1;
- add_Irmao($$,$3);
- }else{
- $$ = $3;
- }
- }
- ;
- Expression:
- INTLIT {$$ = new_node($1,"IntLit");}
- |REALLIT {$$ = new_node($1,"RealLit");}
- |ID {$$ = new_node($1,"Id");}
- |FuncInvocation {$$ = $1;}
- |LPAR Expression RPAR {$$ = $2;}
- |Expression OR Expression {$$ = new_node(NULL,"Or");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression AND Expression {$$ = new_node(NULL,"And");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression LT Expression {$$ = new_node(NULL,"Lt");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression GT Expression {$$ = new_node(NULL,"Gt");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression EQ Expression {$$ = new_node(NULL,"Eq");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression NE Expression {$$ = new_node(NULL,"Ne");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression LE Expression {$$ = new_node(NULL,"Le");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression GE Expression {$$ = new_node(NULL,"Ge");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |NOT Expression {$$ = new_node(NULL,"Not");add_Filhos($$,$2);}
- |MINUS Expression {$$ = new_node(NULL,"Minus");add_Filhos($$,$2);}
- |PLUS Expression {$$ = new_node(NULL,"Plus");add_Filhos($$,$2);}
- |Expression PLUS Expression {$$ = new_node(NULL,"Add");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression MINUS Expression {$$ = new_node(NULL,"Sub");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression STAR Expression {$$ = new_node(NULL,"Mul");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression DIV Expression {$$ = new_node(NULL,"Div");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |Expression MOD Expression {$$ = new_node(NULL,"Mod");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
- |LPAR error RPAR {$$ = NULL;}
- ;
- %%
- int main(int argc, char *argv[])
- {
- //yydebug = 0;
- if(argc == 2){
- if(strcmp(argv[1],"-l") == 0){
- flagt = 0;
- yylex();
- }
- else if(strcmp(argv[1],"-t") == 0){
- flagt = 1;
- s_error = 0;
- yyparse();
- }
- else if(strcmp(argv[1],"-s") == 0){
- flagt = 2;
- s_error = 0;
- yyparse();
- }
- }
- else{
- flagt = 1;
- s_error = 0;
- yyparse();
- }
- return 0;
- }
- //////////////////////////////////////////////////////////// META 2 ////////////////////////////////////////////////////////////////////////////////////
- n1 new_node(char* valorNew,char * descricaoNew){
- n1 node = (n1) malloc(sizeof(Node));
- node->filhos = NULL;
- node->irmaos = NULL;
- node->valor = valorNew;
- node->descricao = descricaoNew;
- node->type = NULL;
- return node;
- }
- void copyType(n1 node,n1 type){
- n1 new = new_node(NULL,type->descricao);
- while(node->irmaos != NULL){
- add_FilhoInicioComIrmaos(node,new);
- node = node->irmaos;
- }
- }
- void add_FilhoInicioComIrmaos(n1 node,n1 irmao){
- if(node != NULL && irmao != NULL){
- n1 aux;
- aux = node->filhos;
- node->filhos = irmao;
- node->filhos->irmaos = aux;
- }
- }
- void add_Irmao(n1 node, n1 irmao){
- if(node != NULL && irmao != NULL){
- while(node->irmaos != NULL){
- node = node->irmaos;
- }
- node->irmaos = irmao;
- }
- }
- n1 add_Irmao_2(n1 node, n1 irmao){
- n1 aux1 = node;
- n1 aux2 = irmao;
- if(node != NULL){
- while(node->irmaos != NULL){
- node = node->irmaos;
- }
- node->irmaos = irmao;
- return aux1;
- }
- return aux2;
- }
- void add_Filhos(n1 node,n1 filho){
- if(node != NULL && filho != NULL){
- node->filhos = filho;
- }
- }
- void imprime_AST(n1 root,int depth){
- if(s_error == 0){
- int i;
- if(root != NULL){
- for(i= 0;i<depth;i++){
- printf("..");
- }
- if (root->valor != NULL){
- printf("%s(%s)\n",root->descricao,root->valor);
- }
- else{
- printf("%s\n",root->descricao);
- }
- imprime_AST(root->filhos,depth+1);
- imprime_AST(root->irmaos,depth);
- }
- }
- }
- void imprime_Irmaos(n1 node){
- while(node->irmaos != NULL){
- printf("%s\n",node->descricao);
- }
- }
- void change_Tipo(n1 no,n1 type) {
- no->descricao = type->descricao;
- }
- n1 add_Type2Irmao(n1 lista,n1 type) {
- n1 aux = lista;
- if(lista != NULL){
- while(aux->irmaos != NULL) {
- change_Tipo(aux->filhos, type);
- aux = aux->irmaos;
- }
- change_Tipo(aux->filhos, type);
- return lista;
- }
- return lista;
- }
- int conta_filhos(n1 root){
- n1 aux;
- int soma = 0;
- aux = root;
- while(aux != NULL){
- aux = aux->irmaos;
- soma++;
- }
- return soma;
- }
- //////////////////////////////////////////////////////////// META 3 ////////////////////////////////////////////////////////////////////////////////////
- void percorre_AST (n1 roots){
- n1 root = roots;
- int verificaIrmao = 1;
- root = root->filhos;
- scope_Root = init_Scope("Global");
- addPutcharGetChar();
- while(verificaIrmao == 1){
- if(strcmp(root->descricao,"VarDecl") == 0){
- adiciona_VarDecl(root);
- }
- else if(strcmp(root->descricao,"FuncDecl") == 0){
- if(checkFuncDecl(root->filhos->filhos->valor) == 0 && checkFuncInTable(root->filhos->filhos->valor) == 0){
- addFuncToGlogal(root);
- }
- else{
- }
- }
- root = root->irmaos;
- if(root == NULL){
- verificaIrmao = 0;
- }
- }
- printScopes();
- printf("\n");
- //imprime_AST(roots,0);
- imprime_AST_Anotada_Main(roots,0,"Program","Global");
- }
- void adiciona_VarDecl(n1 var_decl_Root){
- char* descricaoLower = NULL;
- table_Pointer tableCur = scope_Root->tabelas;
- if(checkVarDecl(var_decl_Root->filhos->irmaos->valor,scope_Root) == 0){
- while(tableCur->next != NULL){
- tableCur = tableCur->next;
- }
- tableCur->next = init_Table(var_decl_Root->filhos->irmaos->valor);
- descricaoLower = tornaPrimeiroCharEmLowerCase(var_decl_Root->filhos->descricao,descricaoLower);
- tableCur->next->type = descricaoLower;
- /*printf("%s\t%s\n",var_decl_Root->filhos->descricao,descricaoLower);*/
- var_decl_Root->filhos->irmaos->type = descricaoLower;
- tableCur->next->params = "VarDecl";
- }
- else{
- }
- return;
- }
- int checkVarDecl(char* nome_Var,scope_Pointer scope){ /*Retorna 0 se nao existir nenhuma variavel igual*/
- table_Pointer tabelasAux = scope->tabelas;
- while(tabelasAux != NULL){
- if(tabelasAux->name == NULL){
- tabelasAux->name = "null";
- }
- if(strcmp(tabelasAux->name,nome_Var) == 0){
- return 1;
- }
- tabelasAux = tabelasAux->next;
- }
- return 0;
- }
- int checkFuncDecl(char * nome_Func){ /*Retorna 0 se nao existir nenhuma função igual */
- scope_Pointer scopeAux = scope_Root;
- while(scopeAux != NULL){
- if(strcmp(nome_Func,scopeAux->name) == 0){
- return 1;
- }
- scopeAux = scopeAux->next;
- }
- return 0;
- }
- int checkFuncInTable(char* nome_Func){ /*Retorna 0 se nao existir nenhuma função na tabela*/
- table_Pointer tabelasAux = scope_Root->tabelas;
- while(tabelasAux!= NULL){
- if(strcmp(nome_Func,tabelasAux->name) == 0){
- return 1;
- }
- tabelasAux = tabelasAux->next;
- }
- return 0;
- }
- void addFuncToGlogal(n1 Func_Root){
- char* descricaoLower = NULL;
- table_Pointer tableCur = scope_Root->tabelas;
- while(tableCur->next != NULL){
- tableCur = tableCur->next;
- }
- if(conta_filhos(Func_Root->filhos->filhos) <= 2){
- tableCur->next = init_Table(Func_Root->filhos->filhos->valor);
- tableCur->next->type = "none";
- addTypesToGlobal(Func_Root->filhos->filhos->irmaos,tableCur->next, Func_Root, "none");
- }
- else{
- if(strcmp(Func_Root->filhos->filhos->irmaos->descricao,"FuncParams") == 0){
- tableCur->next = init_Table(Func_Root->filhos->filhos->valor);
- descricaoLower = tornaPrimeiroCharEmLowerCase(Func_Root->filhos->filhos->irmaos->irmaos->descricao,descricaoLower);
- }
- else{
- tableCur->next = init_Table(Func_Root->filhos->filhos->valor);
- descricaoLower = tornaPrimeiroCharEmLowerCase(Func_Root->filhos->filhos->irmaos->descricao,descricaoLower);
- }
- tableCur->next->type = descricaoLower;
- addTypesToGlobal(Func_Root->filhos->filhos->irmaos->irmaos,tableCur->next, Func_Root,descricaoLower);
- }
- }
- void addTypesToGlobal(n1 FuncDef,table_Pointer table, n1 root,char* return_d){
- int verificaPrimeiroTipo = 1;
- char* descricaoLower;
- n1 params;
- if(conta_filhos(FuncDef) >= 1){
- params = FuncDef->filhos;
- }
- else{
- params = NULL;
- }
- while(params != NULL){
- if(verificaPrimeiroTipo == 1){
- descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
- table->params = descricaoLower;
- FuncDef->filhos->filhos->irmaos->type = descricaoLower;
- verificaPrimeiroTipo = 0;
- }
- else{
- char* aux;
- descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
- aux = transforma(table->params,descricaoLower);
- FuncDef->filhos->filhos->irmaos->type = descricaoLower;
- table->params = aux;
- }
- params = params->irmaos;
- }
- add_NewScope(root->filhos->filhos->descricao,root, FuncDef->filhos,return_d);
- }
- char* transforma(char* char1, char* char2){
- char* tab = ",";
- char* final;
- int size = strlen(char1) + strlen(char2) + strlen(tab);
- int i, j = 0;
- final = (char*)malloc(sizeof(char)*(size+1));
- for(i = 0; i < size + 1; i++){
- if(i < strlen(char1)){
- final[i] = char1[i];
- }
- else if(i == strlen(char1)){
- final[i] = tab[0];
- }
- else if(i < size + 1){
- final[i] = char2[j];
- j++;
- }
- }
- return final;
- }
- char* tornaPrimeiroCharEmLowerCase(char* atual, char* final){
- final = (char*)malloc(sizeof(char) * strlen(atual));
- strcpy(final,atual);
- final[0] = tolower(final[0]);
- return final;
- }
- void printScopes(){
- printf("===== Global Symbol Table =====\n");
- printTables(scope_Root->tabelas, 1);
- Impressao_Scopes();
- }
- void printTables(table_Pointer table, int x){
- int verifica_Irmao = 1;
- while(verifica_Irmao == 1){
- if(strcmp(table->name,"putchar") != 0 && strcmp(table->name,"getchar") != 0){
- if(x == 1){
- if(table->params != NULL){
- if(strcmp(table->params,"VarDecl") == 0){
- printf("%s\t\t%s\n",table->name,table->type);
- }
- else{
- printf("%s\t(%s)\t%s\n",table->name,table->params,table->type);
- }
- }
- else{
- printf("%s\t()\t%s\n",table->name,table->type);
- }
- }
- else{
- if(table->params != NULL){
- if(strcmp(table->params,"params") == 0 && x == 0){
- printf("%s\t\t%s\tparam\n",table->name,table->type);
- }
- else{
- printf("%s\t\t(%s)\t%s\n",table->name,table->params,table->type);
- }
- }
- else{
- printf("%s\t\t%s\n",table->name,table->type);
- }
- }
- }
- table = table->next;
- if(table == NULL){
- verifica_Irmao = 0;
- }
- }
- }
- void Impressao_Scopes(){
- scope_Pointer scopeRootAux = scope_Root;
- table_Pointer tabelasAux = scope_Root->tabelas;
- while(tabelasAux != NULL){
- if(tabelasAux->params != NULL){
- /*declaracao/defenicao de funcao*/
- imprime_Scope(tabelasAux->name,tabelasAux->params);
- }
- else{
- imprime_Scope(tabelasAux->name,"");
- }
- tabelasAux = tabelasAux->next;
- }
- scope_Root = scopeRootAux;
- }
- void imprime_Scope(char* name, char* params){
- scope_Pointer scopeRootAux = scope_Root;
- while(scopeRootAux != NULL){
- if(strcmp(scopeRootAux->name,name) == 0){
- printf("\n===== Function %s(%s) Symbol Table =====\n", scopeRootAux->name,params);
- printTables(scopeRootAux->tabelas,0);
- }
- scopeRootAux = scopeRootAux->next;
- }
- /*scope_Root = scopeRootAux;*/
- }
- void addPutcharGetChar(){
- scope_Root->tabelas = init_Table("putchar");
- scope_Root->tabelas->type = "int";
- scope_Root->tabelas->params = "int";
- scope_Root->tabelas->next = init_Table("getchar");
- scope_Root->tabelas->next->type = "int";
- scope_Root->tabelas->next->params = "void";
- }
- void add_NewScope(char* name,n1 FuncRoot, n1 parametros,char* return_d){
- char* descricaoLower = NULL;
- scope_Pointer ScopeRootCopy = scope_Root;
- while(scope_Root->next != NULL){
- scope_Root = scope_Root->next;
- }
- /*inicializa o return*/
- scope_Root->next = init_Scope(FuncRoot->filhos->filhos->valor);
- scope_Root->next->previous = scope_Root;
- scope_Root->next->tabelas = init_Table("return");
- if(conta_filhos(FuncRoot->filhos->filhos) < 2){
- descricaoLower = "none";
- }
- else{
- descricaoLower = return_d;
- }
- scope_Root->next->tabelas->type = descricaoLower;
- scope_Root = scope_Root->next;
- /*coloca os parametros*/
- n1 params = parametros;
- table_Pointer tabelasCopy = scope_Root->tabelas;
- table_Pointer tabelasAux;
- while(params != NULL){
- if(params->filhos->irmaos != NULL){
- tabelasAux = init_Table(params->filhos->irmaos->valor);
- descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
- tabelasAux->type = descricaoLower;
- tabelasAux->params = "params";
- }
- else{
- descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
- tabelasAux = init_Table(descricaoLower);
- tabelasAux->params = "params";
- }
- tabelasCopy->next = tabelasAux;
- tabelasCopy = tabelasCopy->next;
- params = params->irmaos;
- }
- /*coloca as variaveis declaradas*/
- percorre_Funcao(FuncRoot->filhos,scope_Root,tabelasCopy);
- scope_Root = ScopeRootCopy;
- }
- void percorre_Funcao(n1 FuncRoot,scope_Pointer scope_Func,table_Pointer tabelas){
- char* descricaoLower=NULL;
- if(FuncRoot == NULL){
- return;
- }
- if(strcmp(FuncRoot->descricao,"VarDecl") == 0){
- if(checkVarDecl(FuncRoot->filhos->irmaos->valor,scope_Func) == 0){
- tabelas->next = init_Table(FuncRoot->filhos->irmaos->valor);
- descricaoLower = tornaPrimeiroCharEmLowerCase(FuncRoot->filhos->descricao,descricaoLower);
- tabelas->next->type = descricaoLower;
- tabelas = tabelas->next;
- }
- }
- percorre_Funcao(FuncRoot->filhos,scope_Func,tabelas);
- percorre_Funcao(FuncRoot->irmaos,scope_Root,tabelas);
- return;
- }
- void imprime_AST_Anotada_Main(n1 root,int depth,char* node_Type,char* curScope){
- if(scope_Root->next != NULL)
- scope_Root->next->previous = scope_Root;
- AST_Anotada(root,depth,node_Type,curScope);
- /*printf("fim primeria funcao\n");*/
- imprime_AST_Pos_Anotada(root,0,NULL,0);
- }
- void AST_Anotada(n1 root,int depth,char* node_Type,char* curScope){
- if(root == NULL){
- /*printf("ROOT NULL\n");*/
- }
- int testeCall = 0;
- if(scope_Root == NULL){
- /*printf("SCOPE NULL\n");*/
- }
- char* node_Type_Parent = malloc(strlen(node_Type) + 1);
- strcpy(node_Type_Parent,node_Type);
- if(flagt == 2){ /*|| flagt == 1*/
- if(root != NULL){
- node_Type = root->descricao;
- if(strcmp(root->descricao,"FuncDecl") == 0){
- curScope = root->filhos->filhos->valor;
- }
- /*literias, têm valor nao null*/
- if(root->valor != NULL){
- if(strcmp(root->descricao,"Id") == 0){
- if(strcmp(node_Type_Parent,"VarDecl") != 0 && strcmp(node_Type_Parent,"FuncDecl") != 0 && strcmp(node_Type_Parent,"ParamDecl") != 0 && strcmp(node_Type_Parent,"Call") != 0){
- char* type = Check_Type(curScope,root->valor);
- if(type == NULL){
- type = Check_Type("Global",root->valor);
- if(type != NULL){
- root->type = type;
- }
- else{
- /*nada*/
- }
- }
- else{
- root->type = type;
- }
- }
- else if(strcmp(node_Type_Parent,"Call") == 0){
- testeCall = 1;
- root->type = Check_Func_Type(root->valor);
- }
- else{
- /*nada*/
- }
- }
- if(strcmp(root->descricao,"IntLit")==0){
- root->type = "int";
- }
- if(strcmp(root->descricao,"RealLit")==0){
- root->type = "float32";
- }
- }
- else if(strcmp(root->descricao,"Call") == 0){
- root->type = expression_Call_Return(root);
- }
- else if(strcmp(root->descricao,"Or") == 0 || strcmp(root->descricao,"And") == 0 || strcmp(root->descricao,"Eq") == 0 || strcmp(root->descricao,"Ne") == 0 || strcmp(root->descricao,"Lt") == 0 || strcmp(root->descricao,"Gt") == 0 || strcmp(root->descricao,"Le") == 0 || strcmp(root->descricao,"Ge") == 0 || strcmp(root->descricao,"Not") == 0){
- /*printf("HEY 1\n");*/
- root->type = "bool";
- }
- else if(strcmp(root->descricao,"Mod") == 0){
- root->type = "int";
- }
- else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus") == 0){
- root->type = expr_Unary_Type(root);
- }
- if(testeCall == 1){ /*provavelmente esta mal*/
- node_Type_Parent = "id";
- }
- AST_Anotada(root->filhos,depth + 1,node_Type,curScope);
- AST_Anotada(root->irmaos,depth,node_Type_Parent,curScope);
- }
- return;
- }
- }
- char* Check_Type(char* scope_Name,char* id_Name){
- scope_Pointer scopeRootAux = scope_Root;
- table_Pointer tabelasAux;
- while(scopeRootAux != NULL){
- if(strcmp(scopeRootAux->name,scope_Name)==0){
- tabelasAux = scopeRootAux->tabelas;
- while(tabelasAux != NULL){
- if(strcmp(tabelasAux->name,id_Name)==0){
- return tabelasAux->type;
- }
- tabelasAux = tabelasAux->next;
- }
- }
- scopeRootAux = scopeRootAux->next;
- }
- //scope_Root = scopeRootAux;
- return NULL;
- }
- char* Check_Type_2(char* id_Name){
- scope_Pointer scopeRootAux = scope_Root;
- char* type;
- while(scopeRootAux != NULL){
- type = Check_Type(scopeRootAux->name,id_Name);
- if(type != NULL){
- return type;
- }
- scopeRootAux = scopeRootAux->next;
- }
- //scope_Root = scopeRootAux;
- return NULL;
- }
- char* Check_Func_Type(char* name){
- scope_Pointer scopeRootAux = scope_Root;
- while(scopeRootAux != NULL){
- table_Pointer tabelasAux = scopeRootAux->tabelas;
- while(tabelasAux != NULL){
- if(strcmp(tabelasAux->name,name) ==0){
- return tabelasAux->params;
- }
- tabelasAux = tabelasAux->next;
- }
- scopeRootAux = scopeRootAux->next;
- }
- //scope_Root = scopeRootAux;
- return NULL;
- }
- char* Check_Func_Type_2(char* name){
- scope_Pointer scopeRootAux = scope_Root;
- while(scopeRootAux != NULL){
- if(strcmp(scopeRootAux->name,"Global") == 0){
- table_Pointer tabelasAux = scopeRootAux->tabelas;
- while(tabelasAux != NULL){
- if(strcmp(tabelasAux->name,name) ==0){
- if(tabelasAux->params != NULL){
- return tabelasAux->params;
- }
- }
- tabelasAux = tabelasAux->next;
- }
- }
- scopeRootAux = scopeRootAux->next;
- }
- //scope_Root = scopeRootAux;
- return "";
- }
- char* junta_Chars(char* char1, char* char2){
- char* final;
- char* parent1 = "(";
- char* parent2 = ")";
- int size = strlen(char1)+strlen(parent1)+strlen(char2)+strlen(parent2);
- int i,j=0;
- final = (char*)malloc(sizeof(char)*(size+1));
- for(i = 0; i < size+1; i++){
- if(i < strlen(char1)){
- final[i] = char1[i];
- }
- else if(i == strlen(char1)){
- final[i] = '(';
- }
- else if(i < size-1){
- final[i] = char2[j];
- j++;
- }
- else if(i < size){
- final[i] = ')';
- }
- }
- return final;
- }
- char* expression_Call_Return(n1 root){
- return Check_Func_Type(root->filhos->valor);
- }
- char* expression_Call_Type(n1 root){
- scope_Pointer scopeRootAux = scope_Root;
- while(scopeRootAux != NULL){
- table_Pointer tabelasAux = scopeRootAux->tabelas;
- while(tabelasAux != NULL){
- if(strcmp(tabelasAux->name,name) ==0){
- return tabelasAux->params;
- }
- tabelasAux = tabelasAux->next;
- }
- scopeRootAux = scopeRootAux->next;
- }
- //scope_Root = scopeRootAux;
- return NULL;
- }
- void check_AST_Anotada(n1 root, int depth,n1 pai, int flag){
- if(flagt == 2){ /*|| flagt == 1*/
- int i;
- char* desc_pai;
- if(root != NULL){
- if(strcmp(root->descricao,"Add") == 0 || strcmp(root->descricao,"Mul") == 0 || strcmp(root->descricao,"Sub") == 0 || strcmp(root->descricao,"Div") == 0){
- root->type = expression_Type(root);
- /*printf("%s - %s\n",root->descricao,root->type);*/
- }
- else if(strcmp(root->descricao,"Assign") == 0){
- /*Verificar se o id existe, se os filhos teem o mesmo tipo e ver se esta declarada com o mesmo tipo*/
- root->type = assign_type(root);
- /*printf("%s - %s\n",root->descricao,assign_type(root));*/
- }
- else if(strcmp(root->descricao,"Call") == 0){ /*Ver se os parametros estao todos coorrespondentes*/
- if(strcmp(Check_Type(root->filhos->valor,"return"),"none") == 0){
- root->type = "none";
- }
- else{
- root->type = Check_Type(root->filhos->valor,"return");
- }
- }
- else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus")==0){
- /*Ver se é int ou float32*/
- root->type = expr_Unary_Type(root);
- }
- else if(strcmp(root->descricao,"Comma") == 0){
- root->type = expression_Comma_Type(root);
- }
- else if(strcmp(root->descricao,"ParseArgs") == 0){
- root->type = root->filhos->type;
- }
- check_AST_Anotada(root->filhos, depth + 1, root, 0);
- check_AST_Anotada(root->irmaos, depth, pai , 1);
- }
- }
- }
- void imprime_AST_Pos_Anotada(n1 root, int depth,n1 pai, int flag){
- if(flagt == 2){ /*|| flagt == 1*/
- int i;
- char* desc_pai;
- if(root != NULL){
- for(i= 0;i < depth;i++){
- printf("..");
- }
- if(strcmp(root->descricao,"Add") == 0 || strcmp(root->descricao,"Mul") == 0 || strcmp(root->descricao,"Sub") == 0 || strcmp(root->descricao,"Div") == 0){
- root->type = expression_Type(root);
- printf("%s - %s\n",root->descricao,root->type);
- }
- else if(strcmp(root->descricao,"Assign") == 0){
- printf("%s - %s\n",root->descricao,assign_type(root));
- }
- else if(strcmp(root->descricao,"Call") == 0){
- /*printf("%s\n",Check_Type(root->filhos->valor,"return"));*/
- if(strcmp(Check_Type(root->filhos->valor,"return"),"none") == 0){
- printf("%s\n",root->descricao);
- }
- else{
- printf("%s - %s\n",root->descricao,Check_Type(root->filhos->valor,"return"));
- }
- }
- else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus")==0){
- printf("%s - %s\n",root->descricao,expr_Unary_Type(root));
- }
- else if(strcmp(root->descricao,"Comma") == 0){
- /*printf("Entrei Comma3\n");*/
- printf("%s - %s\n",root->descricao,expression_Comma_Type(root));
- }
- else if(strcmp(root->descricao,"ParseArgs") == 0){
- printf("%s - %s\n",root->descricao,root->filhos->type);
- }
- else{
- if(pai != NULL){
- desc_pai = pai->descricao;
- }
- else{
- desc_pai = "null";
- }
- if (root->valor != NULL && root->type != NULL && strcmp(desc_pai,"FuncHeader") != 0 && strcmp(desc_pai,"ParamDecl") != 0 && flag == 1 && strcmp(desc_pai,"VarDecl") != 0){
- printf("%s(%s) - %s\n",root->descricao,root->valor,root->type);
- }
- else if(root->valor != NULL && root->type != NULL && strcmp(desc_pai,"FuncHeader") != 0 && strcmp(desc_pai,"ParamDecl") != 0 && strcmp(desc_pai,"Call") != 0 && strcmp(desc_pai,"VarDecl") != 0){
- printf("%s(%s) - %s\n",root->descricao,root->valor,root->type);
- }
- else if(root->valor != NULL){
- if(strcmp(desc_pai,"Call") == 0){
- printf("%s(%s) - (%s)\n",root->descricao,root->valor, Check_Func_Type_2(root->valor));
- }
- else{
- printf("%s(%s)\n",root->descricao,root->valor);
- }
- }
- else if(root->descricao != NULL && root->type != NULL){
- printf("%s - %s\n",root->descricao,root->type );
- }
- else{
- printf("%s\n",root->descricao);
- }
- }
- imprime_AST_Pos_Anotada(root->filhos, depth + 1, root, 0);
- imprime_AST_Pos_Anotada(root->irmaos, depth, pai , 1);
- }
- }
- }
- char* expression_Type(n1 root){
- char* type_1 = "aux1";
- char* type_2 = "aux2";
- if(root != NULL){
- if(strcmp(root->filhos->descricao,"Add")==0 || strcmp(root->filhos->descricao,"Mul")==0 || strcmp(root->filhos->descricao,"Sub")==0 || strcmp(root->filhos->descricao,"Div")==0 || strcmp(root->filhos->descricao,"Mod") == 0){
- /*printf("OH YEAH 2\n");*/
- type_1 = expression_Type(root->filhos);
- }
- else if(strcmp(root->filhos->descricao,"Minus") ==0 || strcmp(root->filhos->descricao,"Plus") ==0){
- type_1 = expr_Unary_Type(root->filhos);
- }
- else if(strcmp(root->filhos->descricao,"Comma") ==0){
- type_1 = expression_Comma_Type(root->filhos);
- }
- else if(strcmp(root->filhos->descricao,"Or") ==0 || strcmp(root->filhos->descricao,"And") == 0 || strcmp(root->filhos->descricao,"Not") == 0){
- root->filhos->type = "bool";
- type_1 = "bool";
- }
- else if(strcmp(root->filhos->descricao,"Eq") == 0 || strcmp(root->filhos->descricao,"Ne") == 0 || strcmp(root->filhos->descricao,"Lt") == 0 || strcmp(root->filhos->descricao,"Gt") == 0 || strcmp(root->filhos->descricao,"Le") == 0 || strcmp(root->filhos->descricao,"Ge") == 0){
- root->filhos->type = "bool";
- type_1 = "int";
- }
- else if(strcmp(root->filhos->descricao,"Call")==0){
- type_1 = Check_Type(root->filhos->filhos->valor, "return");
- }
- else if(strcmp(root->filhos->descricao,"Id") == 0){
- /*printf("#%s\n",root->filhos->valor);*/
- type_1 = Check_Type_2(root->filhos->valor);
- }
- else{
- type_1 = root->filhos->type;
- }
- if(strcmp(root->filhos->irmaos->descricao,"Add")==0 || strcmp(root->filhos->irmaos->descricao,"Mul")==0 || strcmp(root->filhos->irmaos->descricao,"Sub")==0 || strcmp(root->filhos->irmaos->descricao,"Div")==0 || strcmp(root->filhos->descricao,"Mod") == 0){
- type_2 = expression_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Minus") ==0 || strcmp(root->filhos->irmaos->descricao,"Plus") ==0){
- type_2 = expr_Unary_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Comma") ==0){
- type_2 = expression_Comma_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->descricao,"Or") ==0 || strcmp(root->filhos->descricao,"And") == 0 || strcmp(root->filhos->descricao,"Not") == 0){
- root->filhos->type = "bool";
- type_2 = "bool";
- }
- else if(strcmp(root->filhos->descricao,"Eq") == 0 || strcmp(root->filhos->descricao,"Ne") == 0 || strcmp(root->filhos->descricao,"Lt") == 0 || strcmp(root->filhos->descricao,"Gt") == 0 || strcmp(root->filhos->descricao,"Le") == 0 || strcmp(root->filhos->descricao,"Ge") == 0){
- root->filhos->type = "bool";
- type_2 = "int";
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Call") == 0){
- type_2 = Check_Type(root->filhos->irmaos->filhos->valor, "return");
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Id") == 0){
- type_2 = Check_Type_2(root->filhos->irmaos->valor);
- }
- else{
- type_2 = root->filhos->irmaos->type;
- }
- }
- if(strcmp(root->descricao,"Add") == 0){
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"int")==0){
- /*printf("ola4\n");*/
- return "int";
- }
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"float32")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- if(strcmp(type_1,"float32")==0 && strcmp(type_2,"int")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- if(strcmp(type_1,"float32")==0 && strcmp(type_2,"float32")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"string")==0){
- /*printf("ola4\n");*/
- return "int";
- }
- if(strcmp(type_1,"string")==0 && strcmp(type_2,"int")==0){
- /*printf("ola4\n");*/
- return "int";
- }
- if(strcmp(type_1,"string")==0 && strcmp(type_2,"string")==0){
- /*printf("ola4\n");*/
- return "string";
- }
- else{
- return "undef";
- /*ERRO???????????*/
- }
- }
- else if(strcmp(root->descricao,"Mul")== 0 || strcmp(root->descricao,"Sub") == 0 || strcmp(root->descricao,"Div") == 0){
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"int")==0){
- /*printf("ola4\n");*/
- return "int";
- }
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"float32")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- if(strcmp(type_1,"float32")==0 && strcmp(type_2,"int")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- if(strcmp(type_1,"float32")==0 && strcmp(type_2,"float32")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- else{
- return "undef";
- /*ERRO???????????*/
- }
- }
- /*else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus") == 0){
- }*/
- else if(strcmp(root->descricao,"Or") ==0 || strcmp(root->descricao,"And") == 0 || strcmp(root->descricao,"Not") == 0){
- if(strcmp(type_1,"bool")==0 && strcmp(type_2,"bool")==0){
- /*printf("ola4\n");*/
- return "bool";
- }
- else{
- return "undef";
- /*ERRO???????????*/
- }
- }
- else if(strcmp(root->descricao,"Eq") == 0 || strcmp(root->descricao,"Ne") == 0 || strcmp(root->descricao,"Lt") == 0 || strcmp(root->descricao,"Gt") == 0 || strcmp(root->descricao,"Le") == 0 || strcmp(root->descricao,"Ge") == 0){
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"int")==0){
- /*printf("ola4\n");*/
- return "int";
- }
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"float32")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- if(strcmp(type_1,"float32")==0 && strcmp(type_2,"int")==0){
- /*printf("ola4\n");*/
- return "float32";
- }
- if(strcmp(type_1,"int")==0 && strcmp(type_2,"string")==0){
- /*printf("ola4\n");*/
- return "int";
- }
- else{
- return "undef";
- /*flag e print*/
- /*ERRO???????????*/
- }
- }
- return "undef";
- }
- char* expr_Unary_Type(n1 root){
- char* type_1;
- if(strcmp(root->filhos->descricao,"Add")==0 || strcmp(root->filhos->descricao,"Mul")==0 || strcmp(root->filhos->descricao,"Sub")==0 || strcmp(root->filhos->descricao,"Div")==0){
- type_1 = expression_Type(root->filhos);
- }
- else if(strcmp(root->filhos->descricao,"Or") ==0 || strcmp(root->filhos->descricao,"And")==0 || strcmp(root->filhos->descricao,"Eq")==0 || strcmp(root->filhos->descricao,"Ne")==0 || strcmp(root->filhos->descricao,"Lt")==0 || strcmp(root->filhos->descricao,"Gt")==0 || strcmp(root->filhos->descricao,"Le")==0 || strcmp(root->filhos->descricao,"Ge")==0 || strcmp(root->filhos->descricao,"Mod")==0 || strcmp(root->filhos->descricao,"Not")==0){
- type_1 = "bool";
- }
- else if(strcmp(root->filhos->descricao,"Minus") ==0 || strcmp(root->filhos->descricao,"Plus") ==0){
- type_1 = expr_Unary_Type(root->filhos);
- }
- else if(strcmp(root->filhos->descricao,"Comma") ==0){
- /*printf("Entrei Comma1\n");*/
- type_1 = expression_Comma_Type(root->filhos);
- }
- else if(strcmp(root->filhos->descricao,"Call")==0){
- type_1 = expression_Call_Type(root->filhos);
- /*printf("TIPO FUNCAO2: %s\n",type_1);*/
- }
- else{
- type_1 = root->filhos->type;
- }
- return type_1;
- }
- char* expression_Comma_Type(n1 root){
- char* Type_1;
- if(strcmp(root->filhos->irmaos->descricao,"Add")==0 || strcmp(root->filhos->irmaos->descricao,"Mul")==0 || strcmp(root->filhos->irmaos->descricao,"Sub")==0 || strcmp(root->filhos->irmaos->descricao,"Div")==0){
- Type_1 = expression_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Or") ==0 || strcmp(root->filhos->irmaos->descricao,"And")==0 || strcmp(root->filhos->irmaos->descricao,"Eq")==0 || strcmp(root->filhos->irmaos->descricao,"Ne")==0 || strcmp(root->filhos->irmaos->descricao,"Lt")==0 || strcmp(root->filhos->irmaos->descricao,"Gt")==0 || strcmp(root->filhos->irmaos->descricao,"Le")==0 || strcmp(root->filhos->irmaos->descricao,"Ge")==0 || strcmp(root->filhos->irmaos->descricao,"Mod")==0 || strcmp(root->filhos->irmaos->descricao,"Not")==0){
- Type_1 = "bool";
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Minus") ==0 || strcmp(root->filhos->irmaos->descricao,"Plus") ==0){
- Type_1 = expr_Unary_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Comma") ==0){
- /*printf("Entrei Comma2\n");*/
- Type_1 = expression_Comma_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Call")==0){
- Type_1 = expression_Call_Type(root->filhos->irmaos);
- /*printf("TIPO FUNCAO: %s\n",Type_1);*/
- }
- else{
- Type_1 = root->filhos->irmaos->type;
- /*printf("TIPO: %s\n",Type_1);*/
- }
- return Type_1;
- }
- char* assign_type(n1 root){
- char * type = NULL;
- char* type_1 = "aux_1";
- char* type_2 = "aux_2";
- type_1 = Check_Type_2(root->filhos->valor);
- if(strcmp(root->filhos->irmaos->descricao,"Add")==0 || strcmp(root->filhos->irmaos->descricao,"Mul")==0 || strcmp(root->filhos->irmaos->descricao,"Sub")==0 || strcmp(root->filhos->irmaos->descricao,"Div")==0 || strcmp(root->filhos->irmaos->descricao,"Mod") == 0){
- type_2 = expression_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Minus") ==0 || strcmp(root->filhos->irmaos->descricao,"Plus") ==0){
- type_2 = expr_Unary_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Or") ==0 || strcmp(root->filhos->irmaos->descricao,"And")==0 || strcmp(root->filhos->irmaos->descricao,"Eq")==0 || strcmp(root->filhos->irmaos->descricao,"Ne")==0 || strcmp(root->filhos->irmaos->descricao,"Lt")==0 || strcmp(root->filhos->irmaos->descricao,"Gt")==0 || strcmp(root->filhos->irmaos->descricao,"Le")==0 || strcmp(root->filhos->irmaos->descricao,"Ge")==0 || strcmp(root->filhos->irmaos->descricao,"Mod")==0 || strcmp(root->filhos->irmaos->descricao,"Not")==0){
- type_2 = "bool";
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Call")==0){
- printf("HEEEEEEY \n");
- printf("%s\n",expression_Call_Type(root->filhos->irmaos));
- type_2 = expression_Call_Type(root->filhos->irmaos);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"Id") == 0){
- type_2 = Check_Type_2(root->filhos->irmaos->valor);
- }
- else if(strcmp(root->filhos->irmaos->descricao,"IntLit")==0){
- type_2 = "int";
- }
- else if(strcmp(root->filhos->irmaos->descricao,"RealLit")==0){
- type_2 = "float32";
- }
- else{
- type_2 = "undef";
- }
- if(type_1 == NULL){
- /*ERRO -> Nao existe declarado*/
- }
- else if(strcmp(type_1,type_2) != 0){
- /*ERRO -> Nao teem o mesmo tipo*/
- }
- else{
- type = type_1;
- }
- return type;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement