Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 55.32 KB | None | 0 0
  1. /*Rodrigo Oliveira 2016241830
  2. António Morais 2016217816*/
  3.  
  4. %{
  5. #include "estruturas.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <ctype.h>
  10. #define YYDEBUG 1
  11.  
  12. extern int yydebug;
  13. int yylex(void);
  14. void yyerror (char *s);
  15.  
  16. typedef struct node* n1;
  17.  
  18. typedef struct node{
  19.     char * descricao;
  20.     char * valor;
  21.     char * type;
  22.     n1 filhos;
  23.     n1 irmaos;
  24.  
  25. }Node;
  26.  
  27. n1  new_node(char *, char*);
  28. void add_Irmao(n1, n1);
  29. n1 add_Irmao_2(n1, n1);
  30. void imprime_AST(n1,int);
  31. void add_Filhos(n1,n1);
  32. void add_FilhoInicioComIrmaos(n1,n1);
  33. void imprime_Irmaos(n1);
  34. void copyType(n1,n1);
  35. void change_Tipo(n1,n1);
  36. n1 add_Type2Irmao(n1,n1);
  37. int conta_filhos(n1);
  38.  
  39. /////////////////////////// META 3 ///////////////////////////
  40.  
  41. void percorre_AST (n1 roots);
  42. void adiciona_VarDecl(n1 var_decl_Root);
  43. int checkVarDecl(char* VarName,scope_Pointer scope);
  44. int checkFuncDecl(char * nome_Func);
  45. int checkFuncInTable(char* nome_Func);
  46. void addFuncToGlogal(n1 Func_Root);
  47. void addTypesToGlobal(n1 FuncDef,table_Pointer table, n1 root,char* return_d);
  48. char* transforma(char* char1, char* char2);
  49. char* tornaPrimeiroCharEmLowerCase(char* atual, char* final);
  50. void printScopes();
  51. void printTables(table_Pointer table, int x);
  52. void Impressao_Scopes();
  53. void imprime_Scope(char* name, char* params);
  54. void addPutcharGetChar();
  55. void add_NewScope(char* name,n1 FuncRoot, n1 parametros,char* return_d);
  56. void percorre_Funcao(n1 FuncRoot,scope_Pointer scope_Func,table_Pointer tabelas);
  57.  
  58. void imprime_AST_Anotada_Main(n1 root,int depth,char* node_Type,char* curScope);
  59. void AST_Anotada(n1 root,int depth,char* node_Type,char* curScope);
  60. char* Check_Type(char* scope_Name,char* id_Name);
  61. char* Check_Type_2(char* id_Name);
  62. char* Check_Func_Type(char* name);
  63. char* junta_Chars(char* char1, char* char2);
  64. char* expression_Call_Type(n1 root);
  65. void imprime_AST_Pos_Anotada(n1 root, int depth,n1 pai,int flag);
  66. char* expression_Type(n1 root);
  67. char* expr_Unary_Type(n1 root);
  68. char* expression_Comma_Type(n1 root);
  69. char* assign_type(n1 root);
  70.  
  71. /////////////////////////// META 3 ///////////////////////////
  72.  
  73. //n1 root;
  74. n1 aux_1;
  75. n1 aux_2;
  76. extern int flagt;
  77. extern int s_error;
  78. int flag_state = 0;
  79. int cont = 0;
  80.  
  81. scope_Pointer scope_Root;
  82. scope_Pointer scope_Root_aux;
  83.  
  84. %}
  85.  
  86. %union{
  87.     char* valor;
  88.     struct node* node;
  89. }
  90.  
  91. %token <valor> REALLIT;
  92. %token <valor> INTLIT;
  93. %token <valor> STRLIT;
  94. %token <valor> ID;
  95.  
  96. %token PACKAGE;
  97. %token RETURN;
  98. %token FUNC;
  99. %token INT;
  100. %token ELSE;
  101. %token FOR;
  102. %token IF;
  103. %token VAR;
  104. %token FLOAT32;
  105. %token BOOL;
  106. %token STRING;
  107. %token PRINT;
  108. %token PARSEINT;
  109. %token CMDARGS;
  110. %token SEMICOLON;
  111. %token BLANKID;
  112. %token AND;
  113. %token ASSIGN;
  114. %token STAR;
  115. %token COMMA;
  116. %token DIV;
  117. %token EQ;
  118. %token GE;
  119. %token GT;
  120. %token LBRACE;
  121. %token LE;
  122. %token LPAR;
  123. %token LSQ;
  124. %token LT;
  125. %token MINUS;
  126. %token MOD;
  127. %token NE;
  128. %token NOT;
  129. %token OR;
  130. %token PLUS;
  131. %token RBRACE;
  132. %token RPAR;
  133. %token RSQ;
  134. %token RESERVED;
  135.  
  136. %type <node> Program;
  137. %type <node> Declarations;
  138. %type <node> VarDeclaration;
  139. %type <node> VarSpec;
  140. %type <node> VarSpec_2;
  141. %type <node> Type;
  142. %type <node> FuncDeclaration;
  143. %type <node> FuncHeader
  144. %type <node> Parameters;
  145. %type <node> ParamDecl;
  146. %type <node> FuncBody;
  147. %type <node> VarsAndStatements;
  148. %type <node> Statement;
  149. %type <node> Statement_2;
  150. %type <node> ParseArgs;
  151. %type <node> FuncInvocation;
  152. %type <node> FuncInvocation_2;
  153. %type <node> Expression;
  154.  
  155. %left COMMA;
  156. %right ASSIGN;
  157. %left OR;
  158. %left AND;
  159. %left GE GT LE LT EQ NE;
  160. %left PLUS MINUS;
  161. %left STAR DIV MOD;
  162. %left NOT;
  163. %left LBRACE RBRACE LPAR RPAR;
  164.  
  165. %nonassoc IF;
  166. %nonassoc ELSE;
  167.  
  168. %%
  169.  
  170. Program:
  171.         PACKAGE ID SEMICOLON                                                            {n1 root = new_node(NULL,"Program");if(flagt == 1){imprime_AST(root,0);}else if(flagt == 2){percorre_AST(root);}}
  172.         |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);}}
  173.         ;
  174.  
  175. Declarations:
  176.         VarDeclaration SEMICOLON                                                        {$$ = $1;}
  177.         |FuncDeclaration SEMICOLON                                                      {$$ = $1;}
  178.         |Declarations VarDeclaration SEMICOLON                                          {if($1 != NULL){
  179.                                                                                             $$ = $1;
  180.                                                                                             add_Irmao($$,$2);
  181.                                                                                         }else{
  182.                                                                                             $$ = $2;
  183.  
  184.                                                                                         }
  185.                                                                                         }
  186.         |Declarations FuncDeclaration SEMICOLON                                         {if($1 != NULL){
  187.                                                                                             $$ = $1;
  188.                                                                                             add_Irmao($$,$2);
  189.                                                                                         }else{
  190.                                                                                             $$ = $2;
  191.  
  192.                                                                                         }
  193.                                                                                         }
  194.         ;
  195.  
  196. VarDeclaration:
  197.         VAR VarSpec                                                                     {$$ = $2;}
  198.         |VAR LPAR VarSpec SEMICOLON RPAR                                                {$$ = $3;}
  199.         ;
  200.  
  201. VarSpec:
  202.         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));}}
  203.         ;
  204.  
  205. VarSpec_2:                                                                              
  206.         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);}}
  207.         |                                    {$$ = NULL;}
  208.         ;
  209.  
  210. Type:
  211.         INT                                                                             {$$ = new_node(NULL,"Int");}
  212.         |FLOAT32                                                                        {$$ = new_node(NULL,"Float32");}
  213.         |BOOL                                                                           {$$ = new_node(NULL,"Bool");}
  214.         |STRING                                                                         {$$ = new_node(NULL,"String");}
  215.         ;
  216.  
  217. FuncDeclaration:
  218.         FuncHeader FuncBody                                                             {$$=new_node(NULL,"FuncDecl");add_Filhos($$,$1);add_Irmao($$->filhos,$2);}
  219.         ;
  220.  
  221. FuncHeader:
  222.         FUNC ID LPAR RPAR                                                               {$$=new_node(NULL,"FuncHeader");add_Filhos($$,new_node($2,"Id"));add_Irmao($$->filhos,new_node(NULL,"FuncParams"));}
  223.         |FUNC ID LPAR Parameters RPAR                                                   {$$=new_node(NULL,"FuncHeader");add_Filhos($$,new_node($2,"Id"));add_Irmao($$->filhos,$4);}
  224.         |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"));}
  225.         |FUNC ID LPAR Parameters RPAR Type                                              {$$=new_node(NULL,"FuncHeader");add_Filhos($$,new_node($2,"Id"));add_Irmao($$->filhos,$6);add_Irmao($$->filhos,$4);}
  226.         ;
  227.  
  228.  
  229. Parameters:
  230.         ParamDecl                                                                       {$$=new_node(NULL,"FuncParams");add_Filhos($$,$1);}
  231.         |Parameters COMMA ParamDecl                                                     {$$=$1;add_Irmao($$->filhos,$3);}
  232.         ;
  233.  
  234. ParamDecl:
  235.         ID Type                                                                         {$$=new_node(NULL,"ParamDecl");add_Filhos($$,$2);add_Irmao($$->filhos,new_node($1,"Id"));}
  236.         ;
  237.  
  238.  
  239. FuncBody:
  240.         LBRACE VarsAndStatements RBRACE                                                 {$$=new_node(NULL,"FuncBody");
  241.                                                                                         if($2!=NULL){
  242.                                                                                             add_Filhos($$,$2);
  243.                                                                                         }
  244.                                                                                         }
  245.         ;
  246.  
  247. VarsAndStatements:
  248.         VarsAndStatements SEMICOLON                                                     {$$ = $1;}
  249.         |VarsAndStatements VarDeclaration SEMICOLON                                     {if($1 != NULL){
  250.                                                                                             $$ = $1;
  251.                                                                                             add_Irmao($$,$2);
  252.                                                                                         }else{
  253.                                                                                             $$ = $2;
  254.  
  255.                                                                                         }
  256.                                                                                         }
  257.         |VarsAndStatements Statement SEMICOLON                                          {if($1 != NULL){
  258.                                                                                             $$ = $1;
  259.                                                                                             add_Irmao($$,$2);
  260.                                                                                         }else{
  261.                                                                                             $$ = $2;
  262.  
  263.                                                                                         }
  264.                                                                                         }
  265.         |                                                                               {$$ = NULL;}
  266.         ;
  267.  
  268. Statement:
  269.         ID ASSIGN Expression                                                            {$$ = new_node(NULL,"Assign");add_Filhos($$,new_node($1,"Id"));add_Irmao($$->filhos,$3);}
  270.         |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;}}
  271.         |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);}}
  272.         |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);}}
  273.         |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);}}
  274.         |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);}}
  275.         |RETURN                                                                         {$$ = new_node(NULL,"Return");}
  276.         |RETURN Expression                                                              {$$ = new_node(NULL,"Return");add_Filhos($$,$2);}
  277.         |FuncInvocation                                                                 {$$ = $1;}
  278.         |ParseArgs                                                                      {$$ = $1;}
  279.         |PRINT LPAR Expression RPAR                                                     {$$ = new_node(NULL,"Print");add_Filhos($$,$3);}
  280.         |PRINT LPAR STRLIT RPAR                                                         {$$ = new_node(NULL,"Print");add_Filhos($$,new_node($3,"StrLit"));}
  281.         |error                                                                          {$$ = NULL;}
  282.         ;
  283.  
  284. Statement_2:
  285.         Statement_2 Statement SEMICOLON                                                 {$$ = add_Irmao_2($1,$2);if($1 != NULL){cont += 1;}}
  286.         |                                                                               {$$ = NULL;}
  287.         ;
  288.  
  289. ParseArgs:
  290.         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);}
  291.         |ID COMMA BLANKID ASSIGN PARSEINT LPAR error RPAR                               {$$ = NULL;}
  292.         ;
  293.  
  294. FuncInvocation:
  295.         ID LPAR RPAR                                                                    {$$ = new_node(NULL,"Call");add_Filhos($$,new_node($1,"Id"));}
  296.         |ID LPAR Expression FuncInvocation_2 RPAR                                       {$$ = new_node(NULL,"Call");add_Filhos($$,new_node($1,"Id"));add_Irmao($$->filhos,$3);add_Irmao($$->filhos,$4);}
  297.         |ID LPAR Expression RPAR                                                        {$$ = new_node(NULL,"Call");add_Filhos($$,new_node($1,"Id"));add_Irmao($$->filhos,$3);}
  298.         |ID LPAR error RPAR                                                             {$$ = NULL;}
  299.         ;
  300.  
  301. FuncInvocation_2:
  302.         COMMA Expression                                                                {$$ = $2;}
  303.         |FuncInvocation_2 COMMA Expression                                              {if($1 != NULL){
  304.                                                                                             $$ = $1;
  305.                                                                                             add_Irmao($$,$3);
  306.                                                                                         }else{
  307.                                                                                             $$ = $3;
  308.  
  309.                                                                                         }
  310.                                                                                         }
  311.         ;
  312.  
  313. Expression:
  314.         INTLIT                                                                          {$$ = new_node($1,"IntLit");}
  315.         |REALLIT                                                                        {$$ = new_node($1,"RealLit");}
  316.         |ID                                                                             {$$ = new_node($1,"Id");}
  317.         |FuncInvocation                                                                 {$$ = $1;}
  318.         |LPAR Expression RPAR                                                           {$$ = $2;}
  319.         |Expression OR Expression                                                       {$$ = new_node(NULL,"Or");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  320.         |Expression AND Expression                                                      {$$ = new_node(NULL,"And");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  321.         |Expression LT Expression                                                       {$$ = new_node(NULL,"Lt");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  322.         |Expression GT Expression                                                       {$$ = new_node(NULL,"Gt");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  323.         |Expression EQ Expression                                                       {$$ = new_node(NULL,"Eq");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  324.         |Expression NE Expression                                                       {$$ = new_node(NULL,"Ne");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  325.         |Expression LE Expression                                                       {$$ = new_node(NULL,"Le");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  326.         |Expression GE Expression                                                       {$$ = new_node(NULL,"Ge");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  327.         |NOT Expression                                                                 {$$ = new_node(NULL,"Not");add_Filhos($$,$2);}
  328.         |MINUS Expression                                                               {$$ = new_node(NULL,"Minus");add_Filhos($$,$2);}
  329.         |PLUS Expression                                                                {$$ = new_node(NULL,"Plus");add_Filhos($$,$2);}
  330.         |Expression PLUS Expression                                                     {$$ = new_node(NULL,"Add");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  331.         |Expression MINUS Expression                                                    {$$ = new_node(NULL,"Sub");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  332.         |Expression STAR Expression                                                     {$$ = new_node(NULL,"Mul");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  333.         |Expression DIV Expression                                                      {$$ = new_node(NULL,"Div");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  334.         |Expression MOD Expression                                                      {$$ = new_node(NULL,"Mod");add_Filhos($$,$1);add_Irmao($$->filhos,$3);}
  335.         |LPAR error RPAR                                                                {$$ = NULL;}
  336.         ;
  337.  
  338. %%
  339.  
  340. int main(int argc, char *argv[])
  341. {
  342.     //yydebug  = 0;
  343.     if(argc == 2){
  344.         if(strcmp(argv[1],"-l") == 0){
  345.             flagt = 0;
  346.             yylex();
  347.         }
  348.         else if(strcmp(argv[1],"-t") == 0){
  349.             flagt = 1;
  350.             s_error = 0;
  351.             yyparse();
  352.         }
  353.         else if(strcmp(argv[1],"-s") == 0){
  354.             flagt = 2;
  355.             s_error = 0;
  356.             yyparse();
  357.         }
  358.     }
  359.     else{
  360.         flagt = 1;
  361.         s_error = 0;
  362.         yyparse();
  363.     }
  364.  
  365.     return 0;
  366. }
  367.  
  368. //////////////////////////////////////////////////////////// META 2 ////////////////////////////////////////////////////////////////////////////////////
  369.  
  370. n1 new_node(char* valorNew,char * descricaoNew){
  371.     n1 node = (n1) malloc(sizeof(Node));
  372.     node->filhos = NULL;
  373.     node->irmaos = NULL;
  374.     node->valor = valorNew;
  375.     node->descricao = descricaoNew;
  376.     node->type = NULL;
  377.     return node;
  378.  
  379. }
  380.  
  381. void copyType(n1 node,n1 type){
  382.     n1 new = new_node(NULL,type->descricao);
  383.     while(node->irmaos != NULL){
  384.         add_FilhoInicioComIrmaos(node,new);
  385.         node = node->irmaos;
  386.     }
  387. }
  388.  
  389. void add_FilhoInicioComIrmaos(n1 node,n1 irmao){
  390.     if(node != NULL && irmao != NULL){
  391.         n1 aux;
  392.         aux = node->filhos;
  393.         node->filhos = irmao;
  394.         node->filhos->irmaos = aux;
  395.     }
  396. }
  397.  
  398. void add_Irmao(n1 node, n1 irmao){
  399.     if(node != NULL && irmao != NULL){
  400.         while(node->irmaos != NULL){
  401.             node = node->irmaos;
  402.         }
  403.         node->irmaos = irmao;
  404.     }
  405.  
  406. }
  407.  
  408. n1 add_Irmao_2(n1 node, n1 irmao){
  409.     n1 aux1 = node;
  410.     n1 aux2 = irmao;
  411.     if(node != NULL){
  412.         while(node->irmaos != NULL){
  413.             node = node->irmaos;
  414.         }
  415.         node->irmaos = irmao;
  416.         return aux1;
  417.     }
  418.     return aux2;
  419. }
  420.  
  421. void add_Filhos(n1 node,n1 filho){
  422.     if(node != NULL && filho != NULL){
  423.         node->filhos = filho;
  424.     }
  425. }
  426.  
  427. void imprime_AST(n1 root,int depth){
  428.     if(s_error == 0){
  429.         int i;
  430.         if(root != NULL){
  431.             for(i= 0;i<depth;i++){
  432.                 printf("..");
  433.             }
  434.             if (root->valor != NULL){
  435.                 printf("%s(%s)\n",root->descricao,root->valor);
  436.             }
  437.             else{
  438.                 printf("%s\n",root->descricao);
  439.             }
  440.             imprime_AST(root->filhos,depth+1);
  441.             imprime_AST(root->irmaos,depth);
  442.  
  443.        
  444.         }
  445.     }
  446. }
  447.  
  448. void imprime_Irmaos(n1 node){
  449.     while(node->irmaos != NULL){
  450.         printf("%s\n",node->descricao);
  451.     }
  452. }
  453.  
  454. void change_Tipo(n1 no,n1 type) {
  455.     no->descricao = type->descricao;
  456. }
  457.  
  458. n1 add_Type2Irmao(n1 lista,n1 type) {
  459.     n1 aux = lista;
  460.     if(lista != NULL){
  461.         while(aux->irmaos != NULL) {
  462.             change_Tipo(aux->filhos, type);
  463.             aux = aux->irmaos;
  464.         }
  465.         change_Tipo(aux->filhos, type);  
  466.         return lista;
  467.     }
  468.     return lista;
  469.    
  470. }
  471.  
  472. int conta_filhos(n1 root){
  473.     n1 aux;
  474.     int soma = 0;
  475.    
  476.     aux = root;
  477.    
  478.     while(aux != NULL){
  479.         aux = aux->irmaos;
  480.         soma++;
  481.     }
  482.     return soma;
  483. }
  484.  
  485.  
  486. //////////////////////////////////////////////////////////// META 3 ////////////////////////////////////////////////////////////////////////////////////
  487.  
  488. void percorre_AST (n1 roots){
  489.     n1 root = roots;
  490.     int verificaIrmao = 1;
  491.     root =  root->filhos;
  492.     scope_Root = init_Scope("Global");
  493.     addPutcharGetChar();
  494.     while(verificaIrmao == 1){
  495.         if(strcmp(root->descricao,"VarDecl") == 0){
  496.             adiciona_VarDecl(root);
  497.         }
  498.         else if(strcmp(root->descricao,"FuncDecl") == 0){
  499.             if(checkFuncDecl(root->filhos->filhos->valor) == 0 && checkFuncInTable(root->filhos->filhos->valor) == 0){
  500.                 addFuncToGlogal(root);
  501.             }
  502.             else{
  503.  
  504.             }
  505.         }
  506.  
  507.         root = root->irmaos;
  508.         if(root == NULL){
  509.             verificaIrmao = 0;
  510.         }
  511.     }
  512.  
  513.     printScopes();
  514.     printf("\n");
  515.     //imprime_AST(roots,0);
  516.     imprime_AST_Anotada_Main(roots,0,"Program","Global");
  517. }
  518.  
  519. void adiciona_VarDecl(n1 var_decl_Root){
  520.     char* descricaoLower = NULL;
  521.     table_Pointer tableCur = scope_Root->tabelas;
  522.     if(checkVarDecl(var_decl_Root->filhos->irmaos->valor,scope_Root) == 0){
  523.         while(tableCur->next != NULL){
  524.             tableCur = tableCur->next;
  525.            
  526.         }
  527.         tableCur->next = init_Table(var_decl_Root->filhos->irmaos->valor);
  528.         descricaoLower = tornaPrimeiroCharEmLowerCase(var_decl_Root->filhos->descricao,descricaoLower);
  529.         tableCur->next->type = descricaoLower;
  530.         /*printf("%s\t%s\n",var_decl_Root->filhos->descricao,descricaoLower);*/
  531.         var_decl_Root->filhos->irmaos->type = descricaoLower;
  532.         tableCur->next->params = "VarDecl";
  533.     }  
  534.     else{
  535.  
  536.     }
  537.     return;
  538. }
  539.  
  540. int checkVarDecl(char* nome_Var,scope_Pointer scope){ /*Retorna 0 se nao existir nenhuma variavel igual*/
  541.     table_Pointer tabelasAux = scope->tabelas;
  542.     while(tabelasAux != NULL){
  543.         if(tabelasAux->name == NULL){
  544.            tabelasAux->name = "null";
  545.         }
  546.         if(strcmp(tabelasAux->name,nome_Var) == 0){
  547.             return 1;
  548.         }
  549.         tabelasAux = tabelasAux->next;
  550.     }
  551.     return 0;
  552.  
  553. }
  554.  
  555. int checkFuncDecl(char * nome_Func){ /*Retorna 0 se nao existir nenhuma função igual */
  556.     scope_Pointer scopeAux = scope_Root;
  557.     while(scopeAux != NULL){
  558.         if(strcmp(nome_Func,scopeAux->name) == 0){
  559.             return 1;
  560.         }
  561.         scopeAux = scopeAux->next;
  562.     }
  563.     return 0;
  564. }
  565.  
  566. int checkFuncInTable(char* nome_Func){ /*Retorna 0 se nao existir nenhuma função na tabela*/
  567.     table_Pointer tabelasAux = scope_Root->tabelas;
  568.  
  569.     while(tabelasAux!= NULL){
  570.  
  571.         if(strcmp(nome_Func,tabelasAux->name) == 0){
  572.             return 1;
  573.         }
  574.         tabelasAux = tabelasAux->next;
  575.     }
  576.     return 0;
  577. }
  578.  
  579. void addFuncToGlogal(n1 Func_Root){
  580.     char* descricaoLower = NULL;
  581.     table_Pointer tableCur = scope_Root->tabelas;
  582.     while(tableCur->next != NULL){
  583.         tableCur = tableCur->next;
  584.     }
  585.     if(conta_filhos(Func_Root->filhos->filhos) <= 2){
  586.         tableCur->next = init_Table(Func_Root->filhos->filhos->valor);
  587.         tableCur->next->type = "none";
  588.  
  589.         addTypesToGlobal(Func_Root->filhos->filhos->irmaos,tableCur->next, Func_Root, "none");
  590.     }
  591.     else{
  592.         if(strcmp(Func_Root->filhos->filhos->irmaos->descricao,"FuncParams") == 0){
  593.             tableCur->next = init_Table(Func_Root->filhos->filhos->valor);
  594.             descricaoLower = tornaPrimeiroCharEmLowerCase(Func_Root->filhos->filhos->irmaos->irmaos->descricao,descricaoLower);
  595.         }
  596.         else{
  597.             tableCur->next = init_Table(Func_Root->filhos->filhos->valor);
  598.             descricaoLower = tornaPrimeiroCharEmLowerCase(Func_Root->filhos->filhos->irmaos->descricao,descricaoLower);
  599.         }
  600.         tableCur->next->type = descricaoLower;
  601.        
  602.         addTypesToGlobal(Func_Root->filhos->filhos->irmaos->irmaos,tableCur->next, Func_Root,descricaoLower);
  603.     }  
  604. }
  605.  
  606. void addTypesToGlobal(n1 FuncDef,table_Pointer table, n1 root,char* return_d){
  607.     int verificaPrimeiroTipo = 1;
  608.     char* descricaoLower;
  609.     n1 params;
  610.     if(conta_filhos(FuncDef) >= 1){
  611.         params = FuncDef->filhos;
  612.     }
  613.     else{
  614.         params = NULL;
  615.     }
  616.     while(params != NULL){
  617.         if(verificaPrimeiroTipo == 1){
  618.             descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
  619.             table->params = descricaoLower;
  620.             FuncDef->filhos->filhos->irmaos->type = descricaoLower;
  621.             verificaPrimeiroTipo = 0;
  622.         }
  623.         else{
  624.             char* aux;
  625.             descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
  626.             aux = transforma(table->params,descricaoLower);
  627.             FuncDef->filhos->filhos->irmaos->type = descricaoLower;
  628.             table->params = aux;
  629.         }
  630.         params = params->irmaos;
  631.     }
  632.  
  633.     add_NewScope(root->filhos->filhos->descricao,root, FuncDef->filhos,return_d);
  634. }
  635.  
  636. char* transforma(char* char1, char* char2){
  637.     char* tab = ",";
  638.     char* final;
  639.  
  640.     int size = strlen(char1) + strlen(char2) + strlen(tab);
  641.     int i, j = 0;
  642.  
  643.     final = (char*)malloc(sizeof(char)*(size+1));
  644.    
  645.     for(i = 0; i < size + 1; i++){
  646.         if(i < strlen(char1)){
  647.             final[i] = char1[i];
  648.         }
  649.         else if(i == strlen(char1)){
  650.             final[i] = tab[0];
  651.         }
  652.         else if(i < size + 1){
  653.             final[i] = char2[j];
  654.             j++;
  655.         }
  656.     }
  657.     return final;
  658. }
  659.  
  660. char* tornaPrimeiroCharEmLowerCase(char* atual, char* final){
  661.     final = (char*)malloc(sizeof(char) * strlen(atual));
  662.     strcpy(final,atual);
  663.     final[0] = tolower(final[0]);
  664.     return final;
  665. }
  666.  
  667. void printScopes(){
  668.     printf("===== Global Symbol Table =====\n");
  669.     printTables(scope_Root->tabelas, 1);
  670.     Impressao_Scopes();
  671. }
  672.  
  673. void printTables(table_Pointer table, int x){
  674.     int verifica_Irmao = 1;
  675.  
  676.     while(verifica_Irmao == 1){
  677.         if(strcmp(table->name,"putchar") != 0 && strcmp(table->name,"getchar") != 0){
  678.             if(x == 1){
  679.                 if(table->params != NULL){
  680.                     if(strcmp(table->params,"VarDecl") == 0){
  681.                         printf("%s\t\t%s\n",table->name,table->type);
  682.                     }
  683.                     else{
  684.                         printf("%s\t(%s)\t%s\n",table->name,table->params,table->type);
  685.                     }
  686.                    
  687.                 }
  688.                 else{
  689.                     printf("%s\t()\t%s\n",table->name,table->type);
  690.                 }
  691.             }
  692.             else{
  693.                 if(table->params != NULL){
  694.                     if(strcmp(table->params,"params") == 0 && x == 0){
  695.                         printf("%s\t\t%s\tparam\n",table->name,table->type);
  696.                     }
  697.                     else{
  698.                         printf("%s\t\t(%s)\t%s\n",table->name,table->params,table->type);
  699.                     }
  700.                 }
  701.                 else{
  702.                     printf("%s\t\t%s\n",table->name,table->type);
  703.                 }
  704.                            
  705.             }
  706.         }
  707.         table = table->next;
  708.         if(table == NULL){
  709.             verifica_Irmao = 0;
  710.         }
  711.     }
  712. }
  713.  
  714. void Impressao_Scopes(){
  715.     scope_Pointer scopeRootAux = scope_Root;
  716.     table_Pointer tabelasAux = scope_Root->tabelas;
  717.     while(tabelasAux != NULL){
  718.  
  719.         if(tabelasAux->params != NULL){
  720.             /*declaracao/defenicao de funcao*/
  721.             imprime_Scope(tabelasAux->name,tabelasAux->params);
  722.         }
  723.         else{
  724.             imprime_Scope(tabelasAux->name,"");
  725.         }
  726.         tabelasAux = tabelasAux->next;
  727.     }
  728.     scope_Root = scopeRootAux;
  729. }
  730.  
  731.  
  732. void imprime_Scope(char* name, char* params){
  733.     scope_Pointer scopeRootAux = scope_Root;
  734.  
  735.     while(scopeRootAux != NULL){
  736.  
  737.         if(strcmp(scopeRootAux->name,name) == 0){
  738.             printf("\n===== Function %s(%s) Symbol Table =====\n", scopeRootAux->name,params);
  739.             printTables(scopeRootAux->tabelas,0);
  740.         }
  741.         scopeRootAux = scopeRootAux->next;
  742.     }
  743.     /*scope_Root = scopeRootAux;*/
  744. }
  745.  
  746. void addPutcharGetChar(){
  747.     scope_Root->tabelas = init_Table("putchar");
  748.     scope_Root->tabelas->type = "int";
  749.     scope_Root->tabelas->params = "int";
  750.     scope_Root->tabelas->next = init_Table("getchar");
  751.     scope_Root->tabelas->next->type = "int";
  752.     scope_Root->tabelas->next->params = "void";
  753. }
  754.  
  755. void add_NewScope(char* name,n1 FuncRoot, n1 parametros,char* return_d){
  756.     char* descricaoLower = NULL;
  757.     scope_Pointer ScopeRootCopy = scope_Root;
  758.    
  759.     while(scope_Root->next != NULL){
  760.         scope_Root = scope_Root->next;
  761.     }
  762.     /*inicializa o return*/
  763.     scope_Root->next = init_Scope(FuncRoot->filhos->filhos->valor);
  764.     scope_Root->next->previous = scope_Root;
  765.     scope_Root->next->tabelas = init_Table("return");
  766.     if(conta_filhos(FuncRoot->filhos->filhos) < 2){
  767.         descricaoLower = "none";
  768.     }
  769.     else{
  770.         descricaoLower = return_d;
  771.     }
  772.     scope_Root->next->tabelas->type = descricaoLower;
  773.     scope_Root = scope_Root->next;
  774.  
  775.     /*coloca os parametros*/
  776.     n1 params = parametros;
  777.     table_Pointer tabelasCopy = scope_Root->tabelas;
  778.     table_Pointer tabelasAux;
  779.     while(params != NULL){  
  780.         if(params->filhos->irmaos != NULL){
  781.             tabelasAux = init_Table(params->filhos->irmaos->valor);
  782.             descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
  783.             tabelasAux->type = descricaoLower;
  784.             tabelasAux->params = "params";
  785.         }
  786.         else{
  787.             descricaoLower = tornaPrimeiroCharEmLowerCase(params->filhos->descricao,descricaoLower);
  788.             tabelasAux = init_Table(descricaoLower);
  789.             tabelasAux->params = "params";
  790.         }
  791.         tabelasCopy->next = tabelasAux;
  792.         tabelasCopy = tabelasCopy->next;
  793.         params = params->irmaos;
  794.     }
  795.  
  796.     /*coloca as variaveis declaradas*/
  797.     percorre_Funcao(FuncRoot->filhos,scope_Root,tabelasCopy);
  798.  
  799.     scope_Root = ScopeRootCopy;
  800. }
  801.  
  802. void percorre_Funcao(n1 FuncRoot,scope_Pointer scope_Func,table_Pointer tabelas){
  803.     char* descricaoLower=NULL;
  804.     if(FuncRoot == NULL){
  805.         return;
  806.     }
  807.     if(strcmp(FuncRoot->descricao,"VarDecl") == 0){
  808.         if(checkVarDecl(FuncRoot->filhos->irmaos->valor,scope_Func) == 0){
  809.             tabelas->next = init_Table(FuncRoot->filhos->irmaos->valor);
  810.             descricaoLower = tornaPrimeiroCharEmLowerCase(FuncRoot->filhos->descricao,descricaoLower);
  811.             tabelas->next->type = descricaoLower;
  812.             tabelas = tabelas->next;
  813.         }
  814.     }
  815.     percorre_Funcao(FuncRoot->filhos,scope_Func,tabelas);
  816.     percorre_Funcao(FuncRoot->irmaos,scope_Root,tabelas);
  817.     return;
  818. }
  819.  
  820. void imprime_AST_Anotada_Main(n1 root,int depth,char* node_Type,char* curScope){
  821.     if(scope_Root->next != NULL)
  822.         scope_Root->next->previous = scope_Root;
  823.  
  824.  
  825.     AST_Anotada(root,depth,node_Type,curScope);
  826.    
  827.     /*printf("fim primeria funcao\n");*/
  828.  
  829.     imprime_AST_Pos_Anotada(root,0,NULL,0);
  830.  
  831. }
  832.  
  833. void AST_Anotada(n1 root,int depth,char* node_Type,char* curScope){
  834.     if(root == NULL){
  835.         /*printf("ROOT NULL\n");*/
  836.     }
  837.  
  838.     int testeCall = 0;
  839.     if(scope_Root == NULL){
  840.         /*printf("SCOPE NULL\n");*/
  841.     }
  842.  
  843.     char* node_Type_Parent = malloc(strlen(node_Type) + 1);
  844.     strcpy(node_Type_Parent,node_Type);
  845.  
  846.     if(flagt == 2){ /*|| flagt == 1*/
  847.         if(root != NULL){
  848.             node_Type = root->descricao;
  849.  
  850.             if(strcmp(root->descricao,"FuncDecl") == 0){
  851.                 curScope = root->filhos->filhos->valor;
  852.             }
  853.  
  854.             /*literias, têm valor nao null*/
  855.             if(root->valor != NULL){
  856.                 if(strcmp(root->descricao,"Id") == 0){
  857.                     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){
  858.                         char* type = Check_Type(curScope,root->valor);
  859.  
  860.                         if(type == NULL){
  861.                             type = Check_Type("Global",root->valor);
  862.                             if(type != NULL){
  863.                                 root->type = type;
  864.                             }
  865.                             else{  
  866.                                 /*nada*/
  867.                             }
  868.                         }
  869.                         else{
  870.                             root->type = type;      
  871.                         }
  872.                     }
  873.                     else if(strcmp(node_Type_Parent,"Call") == 0){
  874.                         testeCall = 1;
  875.  
  876.                         root->type = Check_Func_Type(root->valor);    
  877.                     }
  878.                     else{  
  879.                         /*nada*/        
  880.                     }
  881.                 }
  882.  
  883.                 if(strcmp(root->descricao,"IntLit")==0){
  884.                     root->type = "int";
  885.                    
  886.                 }
  887.                 if(strcmp(root->descricao,"RealLit")==0){
  888.                     root->type = "float32";  
  889.                    
  890.                 }
  891.             }
  892.  
  893.             else if(strcmp(root->descricao,"Call") == 0){
  894.                 root->type = expression_Call_Return(root);
  895.             }
  896.             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){
  897.                 /*printf("HEY 1\n");*/
  898.                 root->type = "bool";
  899.             }
  900.  
  901.             else if(strcmp(root->descricao,"Mod") == 0){
  902.                 root->type = "int";
  903.             }
  904.  
  905.             else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus") == 0){
  906.                 root->type = expr_Unary_Type(root);
  907.             }
  908.  
  909.             if(testeCall == 1){ /*provavelmente esta mal*/
  910.                 node_Type_Parent = "id";
  911.             }
  912.  
  913.             AST_Anotada(root->filhos,depth + 1,node_Type,curScope);
  914.  
  915.             AST_Anotada(root->irmaos,depth,node_Type_Parent,curScope);
  916.         }
  917.         return;
  918.     }
  919. }
  920.  
  921. char* Check_Type(char* scope_Name,char* id_Name){
  922.     scope_Pointer scopeRootAux = scope_Root;
  923.     table_Pointer tabelasAux;
  924.  
  925.     while(scopeRootAux != NULL){
  926.         if(strcmp(scopeRootAux->name,scope_Name)==0){
  927.             tabelasAux = scopeRootAux->tabelas;
  928.             while(tabelasAux != NULL){
  929.                 if(strcmp(tabelasAux->name,id_Name)==0){
  930.                     return tabelasAux->type;
  931.                 }
  932.                 tabelasAux = tabelasAux->next;
  933.             }
  934.         }
  935.         scopeRootAux = scopeRootAux->next;
  936.     }
  937.     //scope_Root = scopeRootAux;
  938.    
  939.     return NULL;
  940. }
  941.  
  942. char* Check_Type_2(char* id_Name){
  943.     scope_Pointer scopeRootAux = scope_Root;
  944.     char* type;
  945.  
  946.     while(scopeRootAux != NULL){
  947.         type = Check_Type(scopeRootAux->name,id_Name);
  948.         if(type != NULL){
  949.             return type;
  950.         }
  951.         scopeRootAux = scopeRootAux->next;
  952.     }
  953.     //scope_Root = scopeRootAux;
  954.  
  955.     return NULL;
  956. }
  957.  
  958. char* Check_Func_Type(char* name){
  959.     scope_Pointer scopeRootAux = scope_Root;
  960.  
  961.     while(scopeRootAux != NULL){
  962.         table_Pointer tabelasAux = scopeRootAux->tabelas;
  963.         while(tabelasAux != NULL){
  964.             if(strcmp(tabelasAux->name,name) ==0){
  965.                 return tabelasAux->params;
  966.             }
  967.             tabelasAux = tabelasAux->next;
  968.         }
  969.         scopeRootAux = scopeRootAux->next;
  970.     }
  971.     //scope_Root = scopeRootAux;
  972.  
  973.     return NULL;
  974. }
  975.  
  976. char* Check_Func_Type_2(char* name){
  977.     scope_Pointer scopeRootAux = scope_Root;
  978.  
  979.     while(scopeRootAux != NULL){
  980.         if(strcmp(scopeRootAux->name,"Global") == 0){
  981.             table_Pointer tabelasAux = scopeRootAux->tabelas;
  982.             while(tabelasAux != NULL){
  983.                 if(strcmp(tabelasAux->name,name) ==0){
  984.                     if(tabelasAux->params != NULL){
  985.                         return tabelasAux->params;
  986.                     }
  987.                 }
  988.                 tabelasAux = tabelasAux->next;
  989.             }
  990.         }
  991.         scopeRootAux = scopeRootAux->next;
  992.     }
  993.     //scope_Root = scopeRootAux;
  994.  
  995.     return "";
  996. }
  997.  
  998. char* junta_Chars(char* char1, char* char2){
  999.     char* final;
  1000.     char* parent1 = "(";
  1001.     char* parent2 = ")";
  1002.     int size = strlen(char1)+strlen(parent1)+strlen(char2)+strlen(parent2);
  1003.     int i,j=0;
  1004.  
  1005.     final = (char*)malloc(sizeof(char)*(size+1));
  1006.    
  1007.     for(i = 0; i < size+1; i++){
  1008.         if(i < strlen(char1)){
  1009.             final[i] = char1[i];
  1010.         }
  1011.         else if(i == strlen(char1)){
  1012.             final[i] = '(';
  1013.         }
  1014.         else if(i < size-1){
  1015.             final[i] = char2[j];
  1016.             j++;
  1017.         }
  1018.         else if(i < size){
  1019.             final[i] = ')';
  1020.         }
  1021.     }
  1022.     return final;
  1023. }
  1024.  
  1025. char* expression_Call_Return(n1 root){
  1026.     return Check_Func_Type(root->filhos->valor);
  1027. }
  1028.  
  1029. char* expression_Call_Type(n1 root){
  1030.     scope_Pointer scopeRootAux = scope_Root;
  1031.  
  1032.     while(scopeRootAux != NULL){
  1033.         table_Pointer tabelasAux = scopeRootAux->tabelas;
  1034.         while(tabelasAux != NULL){
  1035.             if(strcmp(tabelasAux->name,name) ==0){
  1036.                 return tabelasAux->params;
  1037.             }
  1038.             tabelasAux = tabelasAux->next;
  1039.         }
  1040.         scopeRootAux = scopeRootAux->next;
  1041.     }
  1042.     //scope_Root = scopeRootAux;
  1043.  
  1044.     return NULL;
  1045. }
  1046.  
  1047. void check_AST_Anotada(n1 root, int depth,n1 pai, int flag){
  1048.     if(flagt == 2){ /*|| flagt == 1*/
  1049.         int i;
  1050.         char* desc_pai;
  1051.         if(root != NULL){
  1052.             if(strcmp(root->descricao,"Add") == 0 || strcmp(root->descricao,"Mul") == 0 || strcmp(root->descricao,"Sub") == 0 || strcmp(root->descricao,"Div") == 0){
  1053.                 root->type = expression_Type(root);
  1054.                 /*printf("%s - %s\n",root->descricao,root->type);*/
  1055.             }
  1056.             else if(strcmp(root->descricao,"Assign") == 0){
  1057.                 /*Verificar se o id existe, se os filhos teem o mesmo tipo e ver se esta declarada com o mesmo tipo*/
  1058.                 root->type = assign_type(root);
  1059.                 /*printf("%s - %s\n",root->descricao,assign_type(root));*/
  1060.             }
  1061.             else if(strcmp(root->descricao,"Call") == 0){ /*Ver se os parametros estao todos coorrespondentes*/
  1062.                 if(strcmp(Check_Type(root->filhos->valor,"return"),"none") == 0){
  1063.                     root->type = "none";
  1064.                 }
  1065.                 else{
  1066.                     root->type = Check_Type(root->filhos->valor,"return");
  1067.                 }
  1068.                
  1069.             }
  1070.             else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus")==0){
  1071.                 /*Ver se é int ou float32*/
  1072.                 root->type = expr_Unary_Type(root);
  1073.             }
  1074.             else if(strcmp(root->descricao,"Comma") == 0){
  1075.                 root->type = expression_Comma_Type(root);
  1076.             }
  1077.             else if(strcmp(root->descricao,"ParseArgs") == 0){
  1078.                 root->type = root->filhos->type;
  1079.             }
  1080.  
  1081.             check_AST_Anotada(root->filhos, depth + 1, root, 0);
  1082.  
  1083.             check_AST_Anotada(root->irmaos, depth, pai , 1);
  1084.         }
  1085.     }
  1086. }
  1087.  
  1088. void imprime_AST_Pos_Anotada(n1 root, int depth,n1 pai, int flag){
  1089.     if(flagt == 2){ /*|| flagt == 1*/
  1090.         int i;
  1091.         char* desc_pai;
  1092.         if(root != NULL){
  1093.             for(i= 0;i < depth;i++){
  1094.                 printf("..");
  1095.             }
  1096.             if(strcmp(root->descricao,"Add") == 0 || strcmp(root->descricao,"Mul") == 0 || strcmp(root->descricao,"Sub") == 0 || strcmp(root->descricao,"Div") == 0){
  1097.                 root->type = expression_Type(root);
  1098.                 printf("%s - %s\n",root->descricao,root->type);
  1099.             }
  1100.             else if(strcmp(root->descricao,"Assign") == 0){
  1101.                 printf("%s - %s\n",root->descricao,assign_type(root));
  1102.             }
  1103.             else if(strcmp(root->descricao,"Call") == 0){
  1104.                 /*printf("%s\n",Check_Type(root->filhos->valor,"return"));*/
  1105.                 if(strcmp(Check_Type(root->filhos->valor,"return"),"none") == 0){
  1106.                     printf("%s\n",root->descricao);
  1107.                 }
  1108.                 else{
  1109.                     printf("%s - %s\n",root->descricao,Check_Type(root->filhos->valor,"return"));
  1110.                 }
  1111.                
  1112.             }
  1113.             else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus")==0){
  1114.                 printf("%s - %s\n",root->descricao,expr_Unary_Type(root));
  1115.             }
  1116.             else if(strcmp(root->descricao,"Comma") == 0){
  1117.                 /*printf("Entrei Comma3\n");*/
  1118.                 printf("%s - %s\n",root->descricao,expression_Comma_Type(root));
  1119.             }
  1120.             else if(strcmp(root->descricao,"ParseArgs") == 0){
  1121.                 printf("%s - %s\n",root->descricao,root->filhos->type);
  1122.             }
  1123.             else{
  1124.                 if(pai != NULL){
  1125.                     desc_pai = pai->descricao;
  1126.                 }
  1127.                 else{
  1128.                     desc_pai = "null";
  1129.                 }
  1130.  
  1131.                 if (root->valor != NULL && root->type != NULL && strcmp(desc_pai,"FuncHeader") != 0 && strcmp(desc_pai,"ParamDecl") != 0 && flag == 1 && strcmp(desc_pai,"VarDecl") != 0){
  1132.                     printf("%s(%s) - %s\n",root->descricao,root->valor,root->type);
  1133.                 }
  1134.                 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){
  1135.                     printf("%s(%s) - %s\n",root->descricao,root->valor,root->type);
  1136.                 }
  1137.                 else if(root->valor != NULL){
  1138.                     if(strcmp(desc_pai,"Call") == 0){
  1139.                         printf("%s(%s) - (%s)\n",root->descricao,root->valor, Check_Func_Type_2(root->valor));
  1140.                     }
  1141.                     else{
  1142.                         printf("%s(%s)\n",root->descricao,root->valor);
  1143.                     }
  1144.                    
  1145.                 }
  1146.                 else if(root->descricao != NULL && root->type != NULL){
  1147.                     printf("%s - %s\n",root->descricao,root->type );
  1148.                 }
  1149.                 else{
  1150.                     printf("%s\n",root->descricao);
  1151.                 }
  1152.             }
  1153.             imprime_AST_Pos_Anotada(root->filhos, depth + 1, root, 0);
  1154.  
  1155.             imprime_AST_Pos_Anotada(root->irmaos, depth, pai , 1);
  1156.         }
  1157.     }
  1158. }
  1159.  
  1160. char* expression_Type(n1 root){
  1161.     char* type_1 = "aux1";
  1162.     char* type_2 = "aux2";
  1163.  
  1164.     if(root != NULL){
  1165.         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){
  1166.             /*printf("OH YEAH 2\n");*/
  1167.             type_1 = expression_Type(root->filhos);
  1168.         }
  1169.         else if(strcmp(root->filhos->descricao,"Minus") ==0 || strcmp(root->filhos->descricao,"Plus") ==0){
  1170.             type_1 = expr_Unary_Type(root->filhos);
  1171.         }
  1172.         else if(strcmp(root->filhos->descricao,"Comma") ==0){
  1173.             type_1 = expression_Comma_Type(root->filhos);
  1174.         }
  1175.         else if(strcmp(root->filhos->descricao,"Or") ==0 || strcmp(root->filhos->descricao,"And") == 0 || strcmp(root->filhos->descricao,"Not") == 0){
  1176.             root->filhos->type = "bool";
  1177.             type_1 = "bool";
  1178.         }
  1179.         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){
  1180.             root->filhos->type = "bool";
  1181.             type_1 = "int";
  1182.         }
  1183.         else if(strcmp(root->filhos->descricao,"Call")==0){
  1184.             type_1 = Check_Type(root->filhos->filhos->valor, "return");
  1185.         }
  1186.         else if(strcmp(root->filhos->descricao,"Id") == 0){
  1187.             /*printf("#%s\n",root->filhos->valor);*/
  1188.             type_1 = Check_Type_2(root->filhos->valor);        
  1189.         }
  1190.         else{
  1191.             type_1 = root->filhos->type;
  1192.         }
  1193.         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){
  1194.             type_2 = expression_Type(root->filhos->irmaos);
  1195.         }
  1196.         else if(strcmp(root->filhos->irmaos->descricao,"Minus") ==0 || strcmp(root->filhos->irmaos->descricao,"Plus") ==0){
  1197.             type_2 = expr_Unary_Type(root->filhos->irmaos);
  1198.         }
  1199.         else if(strcmp(root->filhos->irmaos->descricao,"Comma") ==0){
  1200.             type_2 = expression_Comma_Type(root->filhos->irmaos);
  1201.         }
  1202.         else if(strcmp(root->filhos->descricao,"Or") ==0 || strcmp(root->filhos->descricao,"And") == 0 || strcmp(root->filhos->descricao,"Not") == 0){
  1203.             root->filhos->type = "bool";
  1204.             type_2 = "bool";
  1205.         }
  1206.         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){
  1207.             root->filhos->type = "bool";
  1208.             type_2 = "int";
  1209.         }
  1210.         else if(strcmp(root->filhos->irmaos->descricao,"Call") == 0){
  1211.             type_2 = Check_Type(root->filhos->irmaos->filhos->valor, "return");
  1212.         }
  1213.         else if(strcmp(root->filhos->irmaos->descricao,"Id") == 0){
  1214.             type_2 = Check_Type_2(root->filhos->irmaos->valor);
  1215.         }
  1216.         else{
  1217.             type_2 = root->filhos->irmaos->type;
  1218.         }
  1219.     }
  1220.  
  1221.     if(strcmp(root->descricao,"Add") == 0){
  1222.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"int")==0){
  1223.         /*printf("ola4\n");*/
  1224.         return "int";
  1225.         }
  1226.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"float32")==0){
  1227.             /*printf("ola4\n");*/
  1228.             return "float32";
  1229.         }
  1230.         if(strcmp(type_1,"float32")==0 && strcmp(type_2,"int")==0){
  1231.             /*printf("ola4\n");*/
  1232.             return "float32";
  1233.         }
  1234.         if(strcmp(type_1,"float32")==0 && strcmp(type_2,"float32")==0){
  1235.             /*printf("ola4\n");*/
  1236.             return "float32";
  1237.         }
  1238.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"string")==0){
  1239.             /*printf("ola4\n");*/
  1240.             return "int";
  1241.         }
  1242.         if(strcmp(type_1,"string")==0 && strcmp(type_2,"int")==0){
  1243.             /*printf("ola4\n");*/
  1244.             return "int";
  1245.         }
  1246.         if(strcmp(type_1,"string")==0 && strcmp(type_2,"string")==0){
  1247.             /*printf("ola4\n");*/
  1248.             return "string";
  1249.         }
  1250.         else{
  1251.             return "undef";
  1252.             /*ERRO???????????*/
  1253.         }
  1254.     }
  1255.     else if(strcmp(root->descricao,"Mul")== 0 || strcmp(root->descricao,"Sub") == 0 || strcmp(root->descricao,"Div") == 0){
  1256.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"int")==0){
  1257.         /*printf("ola4\n");*/
  1258.         return "int";
  1259.         }
  1260.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"float32")==0){
  1261.             /*printf("ola4\n");*/
  1262.             return "float32";
  1263.         }
  1264.         if(strcmp(type_1,"float32")==0 && strcmp(type_2,"int")==0){
  1265.             /*printf("ola4\n");*/
  1266.             return "float32";
  1267.         }
  1268.         if(strcmp(type_1,"float32")==0 && strcmp(type_2,"float32")==0){
  1269.             /*printf("ola4\n");*/
  1270.             return "float32";
  1271.         }
  1272.         else{
  1273.             return "undef";
  1274.             /*ERRO???????????*/
  1275.         }
  1276.     }
  1277.     /*else if(strcmp(root->descricao,"Minus") == 0 || strcmp(root->descricao,"Plus") == 0){
  1278.  
  1279.     }*/
  1280.     else if(strcmp(root->descricao,"Or") ==0 || strcmp(root->descricao,"And") == 0 || strcmp(root->descricao,"Not") == 0){
  1281.         if(strcmp(type_1,"bool")==0 && strcmp(type_2,"bool")==0){
  1282.         /*printf("ola4\n");*/
  1283.         return "bool";
  1284.         }
  1285.         else{
  1286.             return "undef";
  1287.             /*ERRO???????????*/
  1288.         }
  1289.     }
  1290.     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){
  1291.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"int")==0){
  1292.         /*printf("ola4\n");*/
  1293.         return "int";
  1294.         }
  1295.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"float32")==0){
  1296.             /*printf("ola4\n");*/
  1297.             return "float32";
  1298.         }
  1299.         if(strcmp(type_1,"float32")==0 && strcmp(type_2,"int")==0){
  1300.             /*printf("ola4\n");*/
  1301.             return "float32";
  1302.         }
  1303.         if(strcmp(type_1,"int")==0 && strcmp(type_2,"string")==0){
  1304.             /*printf("ola4\n");*/
  1305.             return "int";
  1306.         }
  1307.         else{
  1308.             return "undef";
  1309.             /*flag e print*/
  1310.             /*ERRO???????????*/
  1311.         }
  1312.     }
  1313.  
  1314.     return "undef";
  1315.  
  1316.  
  1317. }
  1318.  
  1319. char* expr_Unary_Type(n1 root){
  1320.     char* type_1;
  1321.  
  1322.     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){      
  1323.         type_1 = expression_Type(root->filhos);
  1324.     }
  1325.     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){
  1326.         type_1 = "bool";
  1327.     }
  1328.     else if(strcmp(root->filhos->descricao,"Minus") ==0 || strcmp(root->filhos->descricao,"Plus") ==0){
  1329.         type_1 = expr_Unary_Type(root->filhos);
  1330.     }
  1331.     else if(strcmp(root->filhos->descricao,"Comma") ==0){
  1332.        /*printf("Entrei Comma1\n");*/
  1333.         type_1 = expression_Comma_Type(root->filhos);
  1334.     }
  1335.     else if(strcmp(root->filhos->descricao,"Call")==0){
  1336.         type_1 = expression_Call_Type(root->filhos);
  1337.         /*printf("TIPO FUNCAO2: %s\n",type_1);*/
  1338.     }
  1339.     else{
  1340.         type_1 = root->filhos->type;
  1341.     }
  1342.  
  1343.     return type_1;
  1344. }
  1345.  
  1346.  
  1347. char* expression_Comma_Type(n1 root){
  1348.     char* Type_1;
  1349.  
  1350.     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){      
  1351.         Type_1 = expression_Type(root->filhos->irmaos);
  1352.     }
  1353.     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){
  1354.         Type_1 = "bool";
  1355.     }
  1356.     else if(strcmp(root->filhos->irmaos->descricao,"Minus") ==0 || strcmp(root->filhos->irmaos->descricao,"Plus") ==0){
  1357.         Type_1 = expr_Unary_Type(root->filhos->irmaos);
  1358.     }
  1359.     else if(strcmp(root->filhos->irmaos->descricao,"Comma") ==0){
  1360.         /*printf("Entrei Comma2\n");*/
  1361.         Type_1 = expression_Comma_Type(root->filhos->irmaos);
  1362.     }
  1363.     else if(strcmp(root->filhos->irmaos->descricao,"Call")==0){
  1364.         Type_1 = expression_Call_Type(root->filhos->irmaos);
  1365.         /*printf("TIPO FUNCAO: %s\n",Type_1);*/
  1366.     }
  1367.     else{
  1368.         Type_1 = root->filhos->irmaos->type;
  1369.         /*printf("TIPO: %s\n",Type_1);*/
  1370.     }
  1371.     return Type_1;
  1372.  
  1373. }
  1374.  
  1375. char* assign_type(n1 root){
  1376.     char * type = NULL;
  1377.     char* type_1 = "aux_1";
  1378.     char* type_2 = "aux_2";
  1379.  
  1380.     type_1 = Check_Type_2(root->filhos->valor);
  1381.  
  1382.     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){
  1383.         type_2 = expression_Type(root->filhos->irmaos);
  1384.     }
  1385.     else if(strcmp(root->filhos->irmaos->descricao,"Minus") ==0 || strcmp(root->filhos->irmaos->descricao,"Plus") ==0){
  1386.         type_2 = expr_Unary_Type(root->filhos->irmaos);
  1387.     }
  1388.     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){
  1389.         type_2 = "bool";
  1390.     }
  1391.     else if(strcmp(root->filhos->irmaos->descricao,"Call")==0){
  1392.         printf("HEEEEEEY \n");
  1393.         printf("%s\n",expression_Call_Type(root->filhos->irmaos));
  1394.         type_2 = expression_Call_Type(root->filhos->irmaos);
  1395.     }
  1396.     else if(strcmp(root->filhos->irmaos->descricao,"Id") == 0){
  1397.         type_2 = Check_Type_2(root->filhos->irmaos->valor);
  1398.     }
  1399.     else if(strcmp(root->filhos->irmaos->descricao,"IntLit")==0){
  1400.         type_2 = "int";
  1401.     }
  1402.     else if(strcmp(root->filhos->irmaos->descricao,"RealLit")==0){
  1403.         type_2 = "float32";
  1404.     }  
  1405.     else{
  1406.         type_2 = "undef";
  1407.     }
  1408.  
  1409.     if(type_1 == NULL){
  1410.         /*ERRO -> Nao existe declarado*/
  1411.     }
  1412.     else if(strcmp(type_1,type_2) != 0){
  1413.         /*ERRO -> Nao teem o mesmo tipo*/
  1414.     }
  1415.     else{
  1416.         type = type_1;
  1417.     }
  1418.    
  1419.     return type;
  1420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement