Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.24 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include "ast.h"
  6.  
  7. ////////////////////AST/////////////////////////
  8.  
  9. s_token* create_token(char *tok, int line, int col){ //esta comentado pq so interessa para a parte dos erros
  10. s_token* new = (s_token*)malloc(sizeof(s_token));
  11.  
  12. if(tok != NULL){
  13. new->token = (char*)strdup(tok);
  14. }
  15. else{
  16. new->token = NULL;
  17. }
  18.  
  19. new->line = line;
  20. new->col = col;
  21. return new;
  22. }
  23.  
  24. node* create_node(char* node_type, char *token, int line, int col){ //CHANGED LINE COL BC OF ERRORS
  25. node* new = (node*)malloc(sizeof(node));
  26.  
  27. new->node_type = (char*)strdup(node_type);
  28. if(token != NULL){
  29. new->token = (char*)strdup(token);
  30. }
  31. else{
  32. new->token = NULL;
  33. }
  34.  
  35. new->son = NULL;
  36. new->brother = NULL;
  37. new->token_annotated = NULL;
  38. new->line = line;
  39. new->col = col;
  40. return new;
  41. }
  42.  
  43. void add_brother(node *current, node *new_brother){
  44. if(current==NULL || new_brother==NULL){
  45. return;
  46. }
  47. node *aux = current;
  48. while(aux->brother != NULL){
  49. aux = aux->brother;
  50. }
  51. aux->brother = new_brother;
  52. }
  53.  
  54. void add_son(node *current, node *new_son){
  55. if(current==NULL || new_son==NULL){
  56. return;
  57. }
  58.  
  59. current->son = new_son;
  60. }
  61.  
  62. void atribute_type(node *type, node *var_d){
  63. node *aux = var_d;
  64. node *new = NULL;
  65.  
  66. while(aux != NULL){
  67. new = create_node(type->node_type, NULL, 0, 0);
  68. new->brother = aux->son;
  69. aux->son = new;
  70. aux = aux->brother;
  71. }
  72. }
  73.  
  74. int n_block(node *current) {
  75. if(current == NULL){
  76. return 0;
  77. }
  78. int counter = 0;
  79. if(current->brother != NULL){
  80. counter = 1;
  81. }
  82.  
  83. while(current->brother != NULL){
  84. if (strcmp((current->brother)->node_type, "NULL")!=0){
  85. counter++;
  86. }
  87. current = current->brother;
  88. }
  89. return counter;
  90. }
  91.  
  92. void print_ast(node *current, int n){
  93. if(current == NULL){
  94. return;
  95. }
  96. if(strcmp(current->node_type, "NULL") == 0){
  97. print_ast(current->brother, n);
  98. return;
  99. }
  100.  
  101. int i;
  102. if(strcmp(current->node_type, "NULL") != 0){
  103. for(i=0;i<n;i++){
  104. printf("..");
  105. }
  106.  
  107. if(current->token != NULL){
  108. printf("%s(%s)\n",current->node_type, current->token);
  109. }
  110. else{
  111. printf("%s\n",current->node_type);
  112. }
  113. }
  114.  
  115. print_ast(current->son, n+1);
  116. print_ast(current->brother, n);
  117. }
  118.  
  119. void clear_ast(node* current){
  120. if(current == NULL){
  121. return;
  122. }
  123.  
  124. if(current->node_type != NULL){
  125. free(current->node_type);
  126. current->node_type = NULL;
  127. }
  128. if(current->token != NULL){
  129. free(current->token);
  130. current->token = NULL;
  131. }
  132.  
  133. clear_ast(current->son);
  134. current->son = NULL;
  135. clear_ast(current->brother);
  136. current->brother = NULL;
  137.  
  138. free(current);
  139. current = NULL;
  140. }
  141.  
  142. ////////////////////SEMANTICS/////////////////////////
  143.  
  144. t_symbol *global_table;
  145.  
  146. void print_global_table(){
  147. _var *vars_list= global_table->vars_list;
  148. printf("===== %s Symbol Table =====\n", global_table->name);
  149. while(vars_list!=NULL){
  150. if(vars_list->isfunction==1){ //Função
  151. printf("%s\t(", vars_list->id);
  152. _param *param_list= vars_list->params;
  153. while(param_list!=NULL){
  154. if(param_list->isparam==1){
  155. if(param_list->next==NULL || param_list->next->isparam==0)
  156. printf("%s", param_list->type);
  157. else
  158. printf("%s,", param_list->type);
  159. }
  160. param_list= param_list->next;
  161. }
  162. printf(")\t");
  163. if(vars_list->type==NULL)
  164. printf("none\n");
  165. else
  166. printf("%s\n",vars_list->type);
  167. }
  168. else{ //Variável
  169. printf("%s\t\t%s\n",vars_list->id, vars_list->type);
  170. }
  171. vars_list=vars_list->next;
  172. }
  173. printf("\n");
  174. }
  175.  
  176. void print_local_table(){
  177. _var *vars_list= global_table->vars_list;
  178. while(vars_list!=NULL){
  179. if(vars_list->isfunction==1){
  180. printf("===== Function %s(", vars_list->id);
  181. _param *param_list= vars_list->params;
  182. while(param_list!=NULL){
  183. if(param_list->isparam==1){
  184. if(param_list->next==NULL || param_list->next->isparam==0)
  185. printf("%s", param_list->type);
  186. else
  187. printf("%s,", param_list->type);
  188. }
  189. param_list= param_list->next;
  190. }
  191. printf(") Symbol Table =====\n");
  192. printf("return\t\t%s\n",vars_list->type);
  193.  
  194. param_list= vars_list->params;
  195. while(param_list!=NULL){
  196. if(param_list->isparam==1){
  197. printf("%s\t\t%s\tparam\n",param_list->id, param_list->type);
  198. }
  199. else
  200. printf("%s\t\t%s\n",param_list->id, param_list->type);
  201. param_list= param_list->next;
  202. }
  203. printf("\n");
  204. }
  205. vars_list=vars_list->next;
  206. }
  207. }
  208.  
  209.  
  210.  
  211. _var* create_function(char *id, char *type){
  212. _var *new_function= (_var*)malloc(sizeof(_var));
  213. new_function->id = (char*)strdup(id);
  214. new_function->type = (char*)strdup(type);
  215. new_function->isfunction = 1;
  216. new_function->params = NULL;
  217. new_function->next = NULL;
  218.  
  219. return new_function;
  220. }
  221. _var* create_var(char *type, char *id){
  222. _var *new_var= (_var*)malloc(sizeof(_var));
  223. new_var->id = (char*)strdup(id);
  224. new_var->type = (char*)strdup(type);
  225. new_var->isfunction = 0;
  226. new_var->params = NULL;
  227. new_var->next = NULL;
  228.  
  229. return new_var;
  230. }
  231. _param* create_param(char *type, char *id){
  232. _param *new_param = (_param*)malloc(sizeof(_param));
  233. new_param->isparam=1;
  234. new_param->id = (char*)strdup(id);
  235. new_param->type = (char*)strdup(type);
  236. new_param->next= NULL;
  237.  
  238. return new_param;
  239. }
  240.  
  241. _param* create_local_variable(char *type, char *id){
  242. _param *new_param = (_param*)malloc(sizeof(_param));
  243. new_param->isparam=0;
  244. new_param->id = (char*)strdup(id);
  245. new_param->type = (char*)strdup(type);
  246. new_param->next= NULL;
  247.  
  248. return new_param;
  249. }
  250.  
  251. int search_var_or_function_already_exists(_var *vars_list, char* id){
  252. //0 não existe, 1 existe
  253. if(vars_list==NULL)
  254. return 0;
  255.  
  256. _var *atual = vars_list;
  257. while(atual != NULL){
  258. if(strcmp(atual->id, id) == 0){
  259. return 1;
  260. }
  261. atual = atual->next;
  262. }
  263. return 0;
  264. }
  265. char* change_type(char *type){
  266.  
  267. if(strcmp(type, "Int") == 0){
  268. type = "int";
  269. return type;
  270. }
  271. if(strcmp(type, "Float32") == 0){
  272. type = "float32";
  273. return type;
  274. }
  275. if(strcmp(type, "Bool") == 0){
  276. type = "bool";
  277. return type;
  278. }
  279. if(strcmp(type, "String") == 0){
  280. type = "string";
  281. return type;
  282. }
  283. return type;
  284. }
  285.  
  286. int search_param_already_exists(_param *params_list, char *id){
  287. if(params_list==NULL){
  288. return 0;
  289. }
  290.  
  291. _param *atual= params_list;
  292. while(atual != NULL){
  293. if(strcmp(atual->id, id) == 0){
  294. return 1;
  295. }
  296. atual = atual->next;
  297. }
  298. return 0;
  299.  
  300. }
  301. void not_used_local_symbols_error(_var *function){
  302. _param *param_list= function->params;
  303. if(param_list==NULL){
  304. return;
  305. }
  306.  
  307. _param *atual= param_list;
  308. while(atual != NULL){
  309. if(atual->isused==0){
  310. printf("Line %d, column %d: Symbol %s declared but never used\n", atual->line, atual->col, atual->id);
  311. }
  312. atual = atual->next;
  313. }
  314.  
  315. }
  316. void not_used_global_symbols_error(){
  317. _var *vars_list= global_table->vars_list;
  318. if(vars_list==NULL){
  319. return;
  320. }
  321.  
  322. _var *atual= vars_list;
  323. while(atual != NULL){
  324. if(atual->isused==0 && atual->isfunction==0){
  325. printf("Line %d, column %d: Symbol %s declared but never used\n", atual->line, atual->col, atual->id);
  326. }
  327. atual = atual->next;
  328. }
  329.  
  330. }
  331. void add_to_global(_var *new_var){
  332. if(global_table->vars_list==NULL){
  333. global_table->vars_list= new_var;
  334. return;
  335. }
  336.  
  337. _var *atual= global_table->vars_list;
  338. while(atual->next!=NULL){
  339. atual= atual->next;
  340. }
  341.  
  342. atual->next= new_var;
  343. }
  344.  
  345.  
  346. void create_table(node *node_atual){
  347. node *aux, *aux_brother;
  348.  
  349. node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6, *aux7, *aux8, *aux_funcbody;
  350.  
  351. _var *new_var, *new_function;
  352.  
  353. if(node_atual!=NULL){
  354. if(strcmp(node_atual->node_type, "Program")==0){
  355. aux = node_atual->son; //son of program is declarations
  356.  
  357. global_table = (t_symbol*)malloc(sizeof(t_symbol));
  358. global_table->name = strdup("Global");
  359. global_table->type = strdup("Global");
  360. global_table->vars_list = NULL;
  361.  
  362. aux_brother= aux->brother; //VarDecl ou FuncDecl
  363.  
  364.  
  365. while(aux_brother!=NULL){
  366. //2 brothers --> 2 ifs
  367. if(strcmp(aux_brother->node_type, "VarDecl")==0){
  368.  
  369. aux1= aux_brother->son; //Type
  370. aux2= aux1->brother; //ID
  371.  
  372.  
  373. //if varieable exists already error
  374. if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
  375. //Fica a 1 se já existir, emite erro
  376. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  377. }
  378. else{
  379. //Adiciona nova variável à tabela global
  380.  
  381. //Cria variável, dá-lhe tipo(node_type) e valor(token)
  382. new_var= create_var(change_type(aux1->node_type), aux2->token);
  383. new_var->line=aux2->line;
  384. new_var->col=aux2->col;
  385. add_to_global(new_var);
  386. }
  387. }
  388. if(strcmp(aux_brother->node_type, "FuncDecl")==0){
  389. aux1 = aux_brother->son; //FuncHeader
  390.  
  391. if(strcmp(aux1->node_type, "FuncHeader")==0){
  392. aux3 = aux1->son; //ID
  393. aux4 = aux3->brother; //Type ou FuncParams ou vazio
  394.  
  395. if(aux4!=NULL){
  396. if(strcmp(aux4->node_type, "FuncParams")==0){
  397. //Type é "none"
  398. new_function= create_function(aux3->token, "none");
  399. new_function->line= aux3->line;
  400. new_function->col=aux3->col;
  401.  
  402. aux6= aux4->son; //ParamDecl
  403. while(aux6!=NULL){
  404. if(strcmp(aux6->node_type, "NULL")==0){}
  405. else{
  406. aux7=aux6->son; //Type
  407. aux8= aux7->brother; //ID
  408. if(search_param_already_exists(new_function->params, aux8->token)==1){
  409. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  410. }
  411. else{
  412.  
  413. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  414. new_param->line= aux8->line;
  415. new_param->col=aux8->col;
  416. if(new_function->params==NULL)
  417. new_function->params=new_param;
  418. else{
  419. _param *atual= new_function->params;
  420. while(atual->next!=NULL)
  421. atual=atual->next;
  422. atual->next=new_param;
  423. }
  424. }
  425. }
  426.  
  427. aux6=aux6->brother;
  428. }
  429.  
  430. }
  431.  
  432. else{
  433. //Type diferente de "none"
  434. aux5= aux4->brother; //FuncParams ou NULL
  435. if(strcmp(aux5->node_type, "FuncParams")==0){
  436. //Com parametros
  437. new_function= create_function(aux3->token, change_type(aux4->node_type));
  438. new_function->line= aux3->line;
  439. new_function->col= aux3->col;
  440. aux6= aux5->son; //ParamDecl
  441. while(aux6!=NULL){
  442. if(strcmp(aux6->node_type, "NULL")==0){}
  443. else{
  444. aux7=aux6->son; //Type
  445. aux8= aux7->brother; //ID
  446. if(search_param_already_exists(new_function->params, aux8->token)==1){
  447. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  448. }
  449. else{
  450.  
  451. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  452. new_param->line=aux8->line;
  453. new_param->col=aux8->col;
  454. if(new_function->params==NULL)
  455. new_function->params=new_param;
  456. else{
  457. _param *atual= new_function->params;
  458. while(atual->next!=NULL)
  459. atual=atual->next;
  460. atual->next=new_param;
  461. }
  462. }
  463. }
  464.  
  465. aux6=aux6->brother;
  466. }
  467. }
  468. else{
  469. //Sem parametros
  470. new_function= create_function(aux3->token, change_type(aux4->node_type));
  471. new_function->line= aux3->line;
  472. new_function->col= aux3->col;
  473. }
  474. }
  475. }
  476.  
  477. else{
  478. //Sem parametros e Type é "none"
  479. new_function= create_function(aux3->token, "none");
  480. new_function->line= aux3->line;
  481. new_function->col= aux3->col;
  482.  
  483. }
  484.  
  485. if(search_var_or_function_already_exists(global_table->vars_list, aux3->token)==1){
  486. printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  487. }
  488.  
  489. else{
  490. add_to_global(new_function);
  491. //Tabela local
  492. aux_funcbody= aux1->brother; //FuncBody
  493. if(strcmp(aux_funcbody->node_type,"FuncBody")==0){
  494. aux1= aux_funcbody->son;
  495. while(aux1!=NULL){
  496. if(strcmp(aux1->node_type,"VarDecl")==0){
  497. aux2= aux1->son; //Type
  498. aux3= aux2->brother; //ID
  499.  
  500. if(search_param_already_exists(new_function->params, aux3->token)==1){
  501. //Fica a 1 se já existir, emite erro
  502. printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  503. //Não sei o que acontece a seguir
  504. }
  505. else{
  506. _param *new_param = create_local_variable(change_type(aux2->node_type), aux3->token);
  507. new_param->line= aux3->line;
  508. new_param->line= aux3->col;
  509. if(new_function->params==NULL)
  510. new_function->params=new_param;
  511. else{
  512. _param *atual= new_function->params;
  513. while(atual->next!=NULL)
  514. atual=atual->next;
  515. atual->next=new_param;
  516. }
  517. }
  518. }
  519. else{
  520. /*if(strcmp(aux1->node_type, "NULL")){
  521. //Verificar se as variáveis são usadas
  522. aux2= aux1->son;
  523. printf("--->Operacao %s\n", aux1->node_type);
  524. while(aux2!=NULL){
  525. printf("com filho -> %s\n",aux2->token);
  526. aux2 =aux2->brother;
  527. }
  528.  
  529. //used_param(new_function);
  530. }*/
  531. create_annotated_ast(aux1, new_function->params);
  532. //Função Maria
  533. }
  534. aux1=aux1->brother;
  535. }
  536. }
  537. //adiciona função
  538.  
  539. //not_used_local_symbols_error(new_function);
  540. }
  541.  
  542.  
  543. }
  544.  
  545. }
  546.  
  547. aux_brother= aux_brother->brother;
  548.  
  549. }
  550. //not_used_global_symbols_error();
  551. }
  552. }
  553. else{
  554. return;
  555. }
  556. }
  557. create_annotated_ast(aux1, new_function->params);
  558.  
  559. ////////////////////ANNOTATED AST/////////////////////////
  560.  
  561. char *variable_type_local(_param *p_list, char* id){
  562. if(p_list==NULL)
  563. return NULL;
  564.  
  565. _param *atual = p_list;
  566. while(atual != NULL){
  567. if(strcmp(atual->id, id) == 0){
  568. return atual->type;
  569. }
  570. atual = atual->next;
  571. }
  572. return NULL;
  573. }
  574.  
  575. char *variable_type_global(_var *vars_list, char* id){
  576. if(vars_list==NULL){
  577. return NULL;
  578. }
  579.  
  580. _var *atual = vars_list;
  581. while(atual != NULL){
  582. if(strcmp(atual->id, id) == 0){
  583. return atual->type;
  584. }
  585. atual = atual->next;
  586. }
  587.  
  588. return NULL;
  589. }
  590.  
  591. char *variable_type_final(_param *p_list, char* id){
  592. char *aux = variable_type_local(p_list, id);
  593. if(aux!=NULL){
  594. return aux;
  595. }
  596.  
  597. aux = variable_type_global(global_table->vars_list, id);
  598. if(aux!=NULL){
  599. return aux;
  600. }
  601. return NULL;
  602. }
  603.  
  604. void create_annotated_ast(node *atual, _param *p){
  605. char *aux;
  606. node *aux_node, *aux_node2, *aux_node3;
  607.  
  608. if(atual == NULL){
  609. return;
  610. }
  611.  
  612. if(strcmp(atual->node_type, "NULL")==0){
  613. return;
  614. }
  615. else if(strcmp(atual->node_type, "Id")==0){
  616. aux = variable_type_final(p, atual->token);
  617. if(aux == NULL){
  618. printf("Line %d, column %d: Cannot find symbol %s\n", atual->line, atual->col, atual->token);
  619. //pseudo-tipo undef a quaisquer símbolos desconhecidos
  620. atual->token_annotated = "undef";
  621. }
  622. else{
  623. atual->token_annotated = aux;
  624. }
  625. }
  626. else if(strcmp(atual->node_type, "StrLit")==0){
  627. atual->token_annotated = "String";
  628. }
  629. else if(strcmp(atual->node_type, "IntLit")==0){
  630. char *number = atual->token;
  631.  
  632. int i=0;
  633. while(number[i]!='\0'){
  634. if(number[i] >= '7'){
  635. printf("Line %d, column %d: Invalid octal constant: %s\n", atual->line, atual->col, atual->token);
  636. }
  637. i++;
  638. }
  639.  
  640. atual->token_annotated = "int";
  641. }
  642. else if(strcmp(atual->node_type, "RealLit")==0){
  643. //printf("tokeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen\n");
  644. atual->token_annotated = "float32";
  645. }
  646. else if(strcmp(atual->node_type, "If")==0){
  647. aux_node = atual->son;
  648. while(aux_node!=NULL){
  649. create_annotated_ast(aux_node, p);
  650. aux_node = aux_node->brother;
  651. }
  652.  
  653. aux_node2 = atual->son;
  654. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  655. printf("Line %d, column %d: Incompatible type %s in if statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  656. }
  657. }
  658. else if(strcmp(atual->node_type, "For")==0){
  659. aux_node = atual->son;
  660. while(aux_node!=NULL){
  661. create_annotated_ast(aux_node, p);
  662. aux_node = aux_node->brother;
  663. }
  664.  
  665. aux_node2 = atual->son;
  666. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  667. printf("Line %d, column %d: Incompatible type %s in for statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  668. atual->token_annotated = "undef";
  669. }
  670. else{
  671. atual->token_annotated = "bool";
  672. }
  673. }
  674. else if(strcmp(atual->node_type, "Block")==0){
  675. aux_node = atual->son;
  676. while(aux_node!=NULL){
  677. create_annotated_ast(aux_node, p);
  678. aux_node = aux_node->brother;
  679. }
  680. }
  681.  
  682. /////////////////////////////////////////////////////////////////////////////////////
  683. else if(strcmp(atual->node_type, "Return")==0){
  684. aux_node = atual->son;
  685. while(aux_node!=NULL){
  686. create_annotated_ast(aux_node, p);
  687. aux_node = aux_node->brother;
  688. }
  689.  
  690. //INT AND DOUBLE??????
  691. aux_node2 = atual->son;
  692. if(aux_node2!=NULL){
  693. if(strcmp(p->type, "none")==0){
  694. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  695. }
  696. else if(strcmp(p->type, aux_node2->token_annotated)==0){
  697. return;
  698. }
  699. else{
  700. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  701. }
  702. }
  703. else if(aux_node2==NULL && (strcmp(p->type, "none")!=0)){
  704. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  705. }
  706. }
  707.  
  708. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  709. else if(strcmp(atual->node_type, "Print")==0){
  710. aux_node = atual->son;
  711. while(aux_node!=NULL){
  712. create_annotated_ast(aux_node, p);
  713. aux_node = aux_node->brother;
  714. }
  715.  
  716. aux_node2 = atual->son;
  717. if(strcmp(aux_node2->token_annotated, "undef")==0){
  718. printf("Line %d, column %d: Incompatible type %s in fmt.Println statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  719. }
  720. }
  721.  
  722. /////////////////////////////////////////////////////////////////////////////////////FAZER DO 0 SEGMENTATION FAULT
  723. else if(strcmp(atual->node_type, "Call")==0){
  724. aux_node = atual->son;
  725. while(aux_node!=NULL){
  726. create_annotated_ast(aux_node, p);
  727. aux_node = aux_node->brother;
  728. }
  729.  
  730. aux_node2 = atual->son;
  731. atual->token_annotated = aux_node2->token_annotated;
  732. }
  733.  
  734. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  735. else if(strcmp(atual->node_type, "ParseArgs")==0){
  736. aux_node = atual->son;
  737. while(aux_node!=NULL){
  738. create_annotated_ast(aux_node, p);
  739. aux_node = aux_node->brother;
  740. }
  741.  
  742. /*aux_node2 = atual->son;
  743. aux_node3 = aux_node2->brother;*/
  744. /*if(){
  745. printf("Line %d, column %d: Operator strconv.Atoi cannot be applied to types %s, %s\n", atual->line, atual->col, aux_node2->token_annotated, aux_node3->token_annotated);
  746. }*/
  747.  
  748. atual->token_annotated = "int";
  749. }
  750. else if(strcmp(atual->node_type, "Assign")==0){
  751. aux_node = atual->son;
  752. while(aux_node!=NULL){
  753. //printf("filho assign: %s\n", aux_node->node_type);
  754. create_annotated_ast(aux_node, p);
  755. aux_node = aux_node->brother;
  756. }
  757.  
  758. aux_node2 = atual->son;
  759. aux_node3 = aux_node2->brother;
  760.  
  761. atual->token_annotated = aux_node2->token_annotated;
  762.  
  763. if((strcmp(aux_node2->token_annotated, "int")==0 && strcmp(aux_node3->token_annotated, "float32")==0) || strcmp(aux_node2->token_annotated, aux_node3->token_annotated)==0){
  764. return;
  765. }
  766. else{
  767. printf("Line %d, column %d: Operator = cannot be applied to types %s, %s\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated, aux_node3->token_annotated);
  768. }
  769. }
  770. else if((strcmp(atual->node_type, "And")==0) || (strcmp(atual->node_type, "Or")==0)){
  771. aux_node = atual->son;
  772. while(aux_node!=NULL){
  773. create_annotated_ast(aux_node, p);
  774. aux_node = aux_node->brother;
  775. }
  776.  
  777. aux_node2 = atual->son;
  778. aux_node3 = aux_node2->brother;
  779. if(strcmp(aux_node2->token_annotated, aux_node2->token_annotated)!=0){
  780. if((strcmp(atual->node_type, "And")==0)){
  781. printf("Line %d, column %d: Operator && cannot be applied to types %s, %s\n", atual->line, atual->col, aux_node2->token_annotated, aux_node3->token_annotated);
  782. }
  783. if((strcmp(atual->node_type, "Or")==0)){
  784. printf("Line %d, column %d: Operator || cannot be applied to types %s, %s\n", atual->line, atual->col, aux_node2->token_annotated, aux_node3->token_annotated);
  785. }
  786. }
  787.  
  788. atual->token_annotated = "bool";
  789. }
  790.  
  791. else if((strcmp(atual->node_type, "Lt")==0) || (strcmp(atual->node_type, "Gt")==0) || (strcmp(atual->node_type, "Eq")==0) ||
  792. (strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Le")==0) || (strcmp(atual->node_type, "Ge")==0)){
  793. aux_node = atual->son;
  794. while(aux_node!=NULL){
  795. create_annotated_ast(aux_node, p);
  796. aux_node = aux_node->brother;
  797. }
  798. atual->token_annotated = "bool";
  799.  
  800. aux_node2 = atual->son;
  801. aux_node3 = aux_node2->brother;
  802. if((strcmp(atual->node_type, "Lt")==0)){
  803. aux = "<";
  804. }
  805. else if(strcmp(atual->node_type, "Gt")==0){
  806. aux = ">";
  807. }
  808. else if(strcmp(atual->node_type, "Eq")==0){
  809. aux = "==";
  810. }
  811. else if(strcmp(atual->node_type, "Ne")==0){
  812. aux = "!";
  813. }
  814. else if(strcmp(atual->node_type, "Le")==0){
  815. aux = "<=";
  816. }
  817. else if(strcmp(atual->node_type, "Ge")==0){
  818. aux = ">=";
  819. }
  820.  
  821. if((strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Eq")==0)){
  822. if((strcmp(aux_node2->token_annotated, "bool")==0) && (strcmp(aux_node3->token_annotated, "bool")==0)){
  823. return;
  824. }
  825. }
  826. else if((strcmp(aux_node2->token_annotated, aux_node3->token_annotated)!=0) || ((strcmp(aux_node2->token_annotated, "bool")==0) && (strcmp(aux_node3->token_annotated, "bool")==0))){
  827. printf("Line %d, column %d: Operator %s cannot be applied to types %s, %s\n", atual->line, atual->col, aux, aux_node2->token_annotated, aux_node3->token_annotated);
  828. }
  829. }
  830. else if((strcmp(atual->node_type, "Add")==0) || (strcmp(atual->node_type, "Sub")==0) || (strcmp(atual->node_type, "Mul")==0) || (strcmp(atual->node_type, "Div")==0) || (strcmp(atual->node_type, "Mod")==0)){
  831. aux_node = atual->son;
  832. while(aux_node!=NULL){
  833. create_annotated_ast(aux_node, p);
  834. aux_node = aux_node->brother;
  835. }
  836.  
  837. aux_node2 = atual->son;
  838. aux_node3 = aux_node2->brother;
  839. if((strcmp(atual->node_type, "Add")==0)){
  840. aux = "+";
  841. }
  842. else if(strcmp(atual->node_type, "Sub")==0){
  843. aux = "-";
  844. }
  845. else if(strcmp(atual->node_type, "Mul")==0){
  846. aux = "*";
  847. }
  848. else if(strcmp(atual->node_type, "Div")==0){
  849. aux = "/";
  850. }
  851. else if(strcmp(atual->node_type, "Mod")==0){
  852. aux = "%";
  853. }
  854.  
  855. if(strcmp(aux_node2->token_annotated, "int")==0){
  856. if(strcmp(aux_node3->token_annotated, "int")==0){
  857. atual->token_annotated = "int";
  858. }
  859. else if(strcmp(aux_node3->token_annotated, "float32")==0){
  860. atual->token_annotated = "float32";
  861. }
  862. else{
  863. printf("Line %d, column %d: Operator %s cannot be applied to types %s, %s\n", atual->line, atual->col, aux, aux_node2->token_annotated, aux_node3->token_annotated);
  864. atual->token_annotated = "undef";
  865. }
  866. }
  867. if(strcmp(aux_node2->token_annotated, "float32")==0){
  868. if((strcmp(aux_node3->token_annotated, "int")==0) || (strcmp(aux_node3->token_annotated, "float32")==0)){
  869. atual->token_annotated = "float32";
  870. }
  871. else{
  872. printf("Line %d, column %d: Operator %s cannot be applied to types %s, %s\n", atual->line, atual->col, aux, aux_node2->token_annotated, aux_node3->token_annotated);
  873. atual->token_annotated = "undef";
  874. }
  875. }
  876. }
  877.  
  878. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  879. else if((strcmp(atual->node_type, "Plus")==0) || (strcmp(atual->node_type, "Minus")==0)){
  880. aux_node = atual->son;
  881. while(aux_node!=NULL){
  882. create_annotated_ast(aux_node, p);
  883. aux_node = aux_node->brother;
  884. }
  885.  
  886. aux_node2 = atual->son;
  887. atual->token_annotated = aux_node2->token_annotated;
  888.  
  889. //??????????????????????????????????
  890. }
  891. else if((strcmp(atual->node_type, "Not")==0)){
  892. aux_node = atual->son;
  893. while(aux_node!=NULL){
  894. create_annotated_ast(aux_node, p);
  895. aux_node = aux_node->brother;
  896. }
  897.  
  898. aux_node2 = atual->son;
  899. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  900. printf("Line %d, colunm %d: Operator ! cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
  901. }
  902.  
  903. aux_node2->token_annotated = "bool";
  904. }
  905. //printf("----> saiu da função %s %s\n", atual->token, atual->node_type);
  906. }
  907.  
  908. void print_ast_annotated(node *current, int n){
  909. //printf("--------------------------->ARVORE ANOTADA\n");
  910. if(current == NULL){
  911. return;
  912. }
  913. if(strcmp(current->node_type, "NULL") == 0){
  914. print_ast_annotated(current->brother, n);
  915. return;
  916. }
  917.  
  918. int i;
  919. _var *vars_list= global_table->vars_list;
  920.  
  921. if(strcmp(current->node_type, "NULL") != 0){
  922. for(i=0;i<n;i++){
  923. printf("..");
  924. }
  925.  
  926. if(current->token != NULL){
  927. //printf("++++++++++++++ ct: %s\n", current->node_type);
  928. while(vars_list!=NULL){
  929. //printf("------>while\n");
  930. if(vars_list->isfunction==1 && strcmp(current->token, vars_list->id)==0 && current->token_annotated!=NULL){ //Função
  931. printf("%s(%s) - (",current->node_type, current->token);
  932. _param *param_list= vars_list->params;
  933. while(param_list!=NULL){
  934. if(param_list->isparam==1){
  935. if(param_list->next==NULL || param_list->next->isparam==0)
  936. printf("%s", param_list->type);
  937. else
  938. printf("%s,", param_list->type);
  939. }
  940. param_list= param_list->next;
  941. }
  942. printf(")\n");
  943. break;
  944. }
  945. else if(vars_list->isfunction==0 && strcmp(current->token, vars_list->id)==0 && current->token_annotated!=NULL){ //Variável
  946. printf("%s(%s) - %s\n",current->node_type, current->token, current->token_annotated);
  947. break;
  948.  
  949. }
  950. else{
  951. if(current->token_annotated!=NULL){
  952. printf("%s(%s) - %s\n",current->node_type, current->token, current->token_annotated);
  953. break;
  954. }
  955. else{
  956. printf("%s(%s)\n",current->node_type, current->token);
  957. break;
  958. }
  959.  
  960. }
  961. vars_list=vars_list->next;
  962. }
  963. }
  964.  
  965. else{
  966. //printf("++++++++++++++ ct: %s\n", current->node_type);
  967. if(current->token_annotated!=NULL){
  968. printf("%s - %s\n",current->node_type, current->token_annotated);
  969. }
  970. else{
  971. printf("%s\n",current->node_type);
  972. }
  973. }
  974. }
  975.  
  976. print_ast_annotated(current->son, n+1);
  977. print_ast_annotated(current->brother, n);
  978. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement