Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.27 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->isused=0;
  227. new_var->params = NULL;
  228. new_var->next = NULL;
  229.  
  230. return new_var;
  231. }
  232. _param* create_param(char *type, char *id){
  233. _param *new_param = (_param*)malloc(sizeof(_param));
  234. new_param->isparam=1;
  235. new_param->isused=0;
  236. new_param->id = (char*)strdup(id);
  237. new_param->type = (char*)strdup(type);
  238. new_param->next= NULL;
  239.  
  240. return new_param;
  241. }
  242.  
  243. _param* create_local_variable(char *type, char *id){
  244. _param *new_param = (_param*)malloc(sizeof(_param));
  245. new_param->isparam=0;
  246. new_param->isused=0;
  247. new_param->id = (char*)strdup(id);
  248. new_param->type = (char*)strdup(type);
  249. new_param->next= NULL;
  250.  
  251. return new_param;
  252. }
  253.  
  254. int search_var_or_function_already_exists(_var *vars_list, char* id){
  255. //0 não existe, 1 existe
  256. if(vars_list==NULL)
  257. return 0;
  258.  
  259. _var *atual = vars_list;
  260. while(atual != NULL){
  261. if(strcmp(atual->id, id) == 0){
  262. return 1;
  263. }
  264. atual = atual->next;
  265. }
  266. return 0;
  267. }
  268. char* change_type(char *type){
  269.  
  270. if(strcmp(type, "Int") == 0){
  271. type = "int";
  272. return type;
  273. }
  274. if(strcmp(type, "Float32") == 0){
  275. type = "float32";
  276. return type;
  277. }
  278. if(strcmp(type, "Bool") == 0){
  279. type = "bool";
  280. return type;
  281. }
  282. if(strcmp(type, "String") == 0){
  283. type = "string";
  284. return type;
  285. }
  286. return type;
  287. }
  288.  
  289. int search_param_already_exists(_param *params_list, char *id){
  290. if(params_list==NULL){
  291. return 0;
  292. }
  293.  
  294. _param *atual= params_list;
  295. while(atual != NULL){
  296. if(strcmp(atual->id, id) == 0){
  297. return 1;
  298. }
  299. atual = atual->next;
  300. }
  301. return 0;
  302.  
  303. }
  304. void not_used_local_symbols_error(_var *function){
  305. _param *param_list= function->params;
  306. if(param_list==NULL){
  307. return;
  308. }
  309.  
  310. _param *atual= param_list;
  311. while(atual != NULL){
  312. if(atual->isused==0){
  313. printf("Line %d, column %d: Symbol %s declared but never used\n", atual->line, atual->col, atual->id);
  314. }
  315. atual = atual->next;
  316. }
  317.  
  318. }
  319. void not_used_global_symbols_error(){
  320. _var *vars_list= global_table->vars_list;
  321. if(vars_list==NULL){
  322. return;
  323. }
  324.  
  325. _var *atual= vars_list;
  326. while(atual != NULL){
  327. if(atual->isused==0 && atual->isfunction==0){
  328. printf("Line %d, column %d: Symbol %s declared but never used\n", atual->line, atual->col, atual->id);
  329. }
  330. atual = atual->next;
  331. }
  332.  
  333. }
  334. void add_to_global(_var *new_var){
  335. if(global_table->vars_list==NULL){
  336. global_table->vars_list= new_var;
  337. return;
  338. }
  339.  
  340. _var *atual= global_table->vars_list;
  341. while(atual->next!=NULL){
  342. atual= atual->next;
  343. }
  344.  
  345. atual->next= new_var;
  346. }
  347.  
  348.  
  349. void create_table(node *node_atual){
  350. node *aux, *aux_brother;
  351.  
  352. node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6, *aux7, *aux8, *aux_funcbody;
  353.  
  354. _var *new_var, *new_function;
  355.  
  356. if(node_atual!=NULL){
  357. if(strcmp(node_atual->node_type, "Program")==0){
  358. aux = node_atual->son; //son of program is declarations
  359.  
  360. global_table = (t_symbol*)malloc(sizeof(t_symbol));
  361. global_table->name = strdup("Global");
  362. global_table->type = strdup("Global");
  363. global_table->vars_list = NULL;
  364.  
  365. aux_brother= aux->brother; //VarDecl ou FuncDecl
  366.  
  367.  
  368. while(aux_brother!=NULL){
  369. //2 brothers --> 2 ifs
  370. if(strcmp(aux_brother->node_type, "VarDecl")==0){
  371.  
  372. aux1= aux_brother->son; //Type
  373. aux2= aux1->brother; //ID
  374.  
  375.  
  376. //if varieable exists already error
  377. if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
  378. //Fica a 1 se já existir, emite erro
  379. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  380. }
  381. else{
  382. //Adiciona nova variável à tabela global
  383.  
  384. //Cria variável, dá-lhe tipo(node_type) e valor(token)
  385. new_var= create_var(change_type(aux1->node_type), aux2->token);
  386. new_var->line=aux2->line;
  387. new_var->col=aux2->col;
  388. add_to_global(new_var);
  389. }
  390. }
  391. if(strcmp(aux_brother->node_type, "FuncDecl")==0){
  392. aux1 = aux_brother->son; //FuncHeader
  393.  
  394. if(strcmp(aux1->node_type, "FuncHeader")==0){
  395. aux3 = aux1->son; //ID
  396. aux4 = aux3->brother; //Type ou FuncParams ou vazio
  397.  
  398. if(aux4!=NULL){
  399. if(strcmp(aux4->node_type, "FuncParams")==0){
  400. //Type é "none"
  401. new_function= create_function(aux3->token, "none");
  402. new_function->line= aux3->line;
  403. new_function->col=aux3->col;
  404.  
  405. aux6= aux4->son; //ParamDecl
  406. while(aux6!=NULL){
  407. if(strcmp(aux6->node_type, "NULL")==0){}
  408. else{
  409. aux7=aux6->son; //Type
  410. aux8= aux7->brother; //ID
  411. if(search_param_already_exists(new_function->params, aux8->token)==1){
  412. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  413. }
  414. else{
  415.  
  416. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  417. new_param->line= aux8->line;
  418. new_param->col=aux8->col;
  419. if(new_function->params==NULL)
  420. new_function->params=new_param;
  421. else{
  422. _param *atual= new_function->params;
  423. while(atual->next!=NULL)
  424. atual=atual->next;
  425. atual->next=new_param;
  426. }
  427. }
  428. }
  429.  
  430. aux6=aux6->brother;
  431. }
  432.  
  433. }
  434.  
  435. else{
  436. //Type diferente de "none"
  437. aux5= aux4->brother; //FuncParams ou NULL
  438. if(strcmp(aux5->node_type, "FuncParams")==0){
  439. //Com parametros
  440. new_function= create_function(aux3->token, change_type(aux4->node_type));
  441. new_function->line= aux3->line;
  442. new_function->col= aux3->col;
  443. aux6= aux5->son; //ParamDecl
  444. while(aux6!=NULL){
  445. if(strcmp(aux6->node_type, "NULL")==0){}
  446. else{
  447. aux7=aux6->son; //Type
  448. aux8= aux7->brother; //ID
  449. if(search_param_already_exists(new_function->params, aux8->token)==1){
  450. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  451. }
  452. else{
  453.  
  454. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  455. new_param->line=aux8->line;
  456. new_param->col=aux8->col;
  457. if(new_function->params==NULL)
  458. new_function->params=new_param;
  459. else{
  460. _param *atual= new_function->params;
  461. while(atual->next!=NULL)
  462. atual=atual->next;
  463. atual->next=new_param;
  464. }
  465. }
  466. }
  467.  
  468. aux6=aux6->brother;
  469. }
  470. }
  471. else{
  472. //Sem parametros
  473. new_function= create_function(aux3->token, change_type(aux4->node_type));
  474. new_function->line= aux3->line;
  475. new_function->col= aux3->col;
  476. }
  477. }
  478. }
  479.  
  480. else{
  481. //Sem parametros e Type é "none"
  482. new_function= create_function(aux3->token, "none");
  483. new_function->line= aux3->line;
  484. new_function->col= aux3->col;
  485.  
  486. }
  487.  
  488. if(search_var_or_function_already_exists(global_table->vars_list, aux3->token)==1){
  489. printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  490. }
  491.  
  492. else{
  493. add_to_global(new_function);
  494. //Tabela local
  495. aux_funcbody= aux1->brother; //FuncBody
  496. if(strcmp(aux_funcbody->node_type,"FuncBody")==0){
  497. aux1= aux_funcbody->son;
  498. while(aux1!=NULL){
  499. if(strcmp(aux1->node_type,"VarDecl")==0){
  500. aux2= aux1->son; //Type
  501. aux3= aux2->brother; //ID
  502.  
  503. if(search_param_already_exists(new_function->params, aux3->token)==1){
  504. //Fica a 1 se já existir, emite erro
  505. printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  506. //Não sei o que acontece a seguir
  507. }
  508. else{
  509. _param *new_param = create_local_variable(change_type(aux2->node_type), aux3->token);
  510. new_param->line= aux3->line;
  511. new_param->line= aux3->col;
  512. if(new_function->params==NULL)
  513. new_function->params=new_param;
  514. else{
  515. _param *atual= new_function->params;
  516. while(atual->next!=NULL)
  517. atual=atual->next;
  518. atual->next=new_param;
  519. }
  520. }
  521. }
  522. else{
  523. /*if(strcmp(aux1->node_type, "NULL")){
  524. //Verificar se as variáveis são usadas
  525. aux2= aux1->son;
  526. printf("--->Operacao %s\n", aux1->node_type);
  527. while(aux2!=NULL){
  528. printf("com filho -> %s\n",aux2->token);
  529. aux2 =aux2->brother;
  530. }
  531.  
  532. //used_param(new_function);
  533. }*/
  534. create_annotated_ast(aux1, new_function->params);
  535. //Função Maria
  536. }
  537. aux1=aux1->brother;
  538. }
  539. }
  540. //adiciona função
  541.  
  542. //not_used_local_symbols_error(new_function);
  543. }
  544.  
  545.  
  546. }
  547.  
  548. }
  549.  
  550. aux_brother= aux_brother->brother;
  551.  
  552. }
  553. //not_used_global_symbols_error();
  554. }
  555. }
  556. else{
  557. return;
  558. }
  559. }
  560.  
  561.  
  562. ////////////////////ANNOTATED AST/////////////////////////
  563.  
  564. char *variable_type_local(_param *p_list, char* id){
  565. if(p_list==NULL)
  566. return NULL;
  567.  
  568. _param *atual = p_list;
  569. while(atual != NULL){
  570. if(strcmp(atual->id, id) == 0){
  571. return atual->type;
  572. }
  573. atual = atual->next;
  574. }
  575. return NULL;
  576. }
  577.  
  578. char *variable_type_global(_var *vars_list, char* id){
  579. if(vars_list==NULL){
  580. return NULL;
  581. }
  582.  
  583. _var *atual = vars_list;
  584. while(atual != NULL){
  585. if(strcmp(atual->id, id) == 0){
  586. return atual->type;
  587. }
  588. atual = atual->next;
  589. }
  590.  
  591. return NULL;
  592. }
  593.  
  594. char *variable_type_final(_param *p_list, char* id){
  595. char *aux = variable_type_local(p_list, id);
  596. if(aux!=NULL){
  597. return aux;
  598. }
  599.  
  600. aux = variable_type_global(global_table->vars_list, id);
  601. if(aux!=NULL){
  602. return aux;
  603. }
  604. return NULL;
  605. }
  606.  
  607. void create_annotated_ast(node *atual, _param *p){
  608. char *aux;
  609. node *aux_node, *aux_node2, *aux_node3;
  610.  
  611. if(atual == NULL){
  612. return;
  613. }
  614.  
  615. if(strcmp(atual->node_type, "NULL")==0){
  616. return;
  617. }
  618. else if(strcmp(atual->node_type, "Id")==0){
  619. aux = variable_type_final(p, atual->token);
  620. if(aux == NULL){
  621. printf("Line %d, column %d: Cannot find symbol %s\n", atual->line, atual->col, atual->token);
  622. //pseudo-tipo undef a quaisquer símbolos desconhecidos
  623. atual->token_annotated = "undef";
  624. }
  625. else{
  626. atual->token_annotated = aux;
  627. }
  628. }
  629. else if(strcmp(atual->node_type, "StrLit")==0){
  630. atual->token_annotated = "String";
  631. }
  632. else if(strcmp(atual->node_type, "IntLit")==0){
  633. char *number = atual->token;
  634.  
  635. int i=0;
  636. while(number[i]!='\0'){
  637. if(number[i] >= '7'){
  638. printf("Line %d, column %d: Invalid octal constant: %s\n", atual->line, atual->col, atual->token);
  639. }
  640. i++;
  641. }
  642.  
  643. atual->token_annotated = "int";
  644. }
  645. else if(strcmp(atual->node_type, "RealLit")==0){
  646. //printf("tokeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen\n");
  647. atual->token_annotated = "float32";
  648. }
  649. else if(strcmp(atual->node_type, "If")==0){
  650. aux_node = atual->son;
  651. while(aux_node!=NULL){
  652. create_annotated_ast(aux_node, p);
  653. aux_node = aux_node->brother;
  654. }
  655.  
  656. aux_node2 = atual->son;
  657. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  658. printf("Line %d, column %d: Incompatible type %s in if statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  659. }
  660. }
  661. else if(strcmp(atual->node_type, "For")==0){
  662. aux_node = atual->son;
  663. while(aux_node!=NULL){
  664. create_annotated_ast(aux_node, p);
  665. aux_node = aux_node->brother;
  666. }
  667.  
  668. aux_node2 = atual->son;
  669. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  670. printf("Line %d, column %d: Incompatible type %s in for statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  671. atual->token_annotated = "undef";
  672. }
  673. else{
  674. atual->token_annotated = "bool";
  675. }
  676. }
  677. else if(strcmp(atual->node_type, "Block")==0){
  678. aux_node = atual->son;
  679. while(aux_node!=NULL){
  680. create_annotated_ast(aux_node, p);
  681. aux_node = aux_node->brother;
  682. }
  683. }
  684.  
  685. /////////////////////////////////////////////////////////////////////////////////////
  686. else if(strcmp(atual->node_type, "Return")==0){
  687. aux_node = atual->son;
  688. while(aux_node!=NULL){
  689. create_annotated_ast(aux_node, p);
  690. aux_node = aux_node->brother;
  691. }
  692.  
  693. //INT AND DOUBLE??????
  694. aux_node2 = atual->son;
  695. if(aux_node2!=NULL){
  696. if(strcmp(p->type, "none")==0){
  697. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  698. }
  699. else if(strcmp(p->type, aux_node2->token_annotated)==0){
  700. return;
  701. }
  702. else{
  703. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  704. }
  705. }
  706. else if(aux_node2==NULL && (strcmp(p->type, "none")!=0)){
  707. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  708. }
  709. }
  710.  
  711. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  712. else if(strcmp(atual->node_type, "Print")==0){
  713. aux_node = atual->son;
  714. while(aux_node!=NULL){
  715. create_annotated_ast(aux_node, p);
  716. aux_node = aux_node->brother;
  717. }
  718.  
  719. aux_node2 = atual->son;
  720. if(strcmp(aux_node2->token_annotated, "undef")==0){
  721. printf("Line %d, column %d: Incompatible type %s in fmt.Println statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  722. }
  723. }
  724.  
  725. /////////////////////////////////////////////////////////////////////////////////////FAZER DO 0 SEGMENTATION FAULT
  726. else if(strcmp(atual->node_type, "Call")==0){
  727. aux_node = atual->son;
  728. while(aux_node!=NULL){
  729. create_annotated_ast(aux_node, p);
  730. aux_node = aux_node->brother;
  731. }
  732.  
  733. aux_node2 = atual->son;
  734. atual->token_annotated = aux_node2->token_annotated;
  735. }
  736.  
  737. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  738. else if(strcmp(atual->node_type, "ParseArgs")==0){
  739. aux_node = atual->son;
  740. while(aux_node!=NULL){
  741. create_annotated_ast(aux_node, p);
  742. aux_node = aux_node->brother;
  743. }
  744.  
  745. /*aux_node2 = atual->son;
  746. aux_node3 = aux_node2->brother;*/
  747. /*if(){
  748. 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);
  749. }*/
  750.  
  751. atual->token_annotated = "int";
  752. }
  753. else if(strcmp(atual->node_type, "Assign")==0){
  754. aux_node = atual->son;
  755. while(aux_node!=NULL){
  756. //printf("filho assign: %s\n", aux_node->node_type);
  757. create_annotated_ast(aux_node, p);
  758. aux_node = aux_node->brother;
  759. }
  760.  
  761. aux_node2 = atual->son;
  762. aux_node3 = aux_node2->brother;
  763.  
  764. atual->token_annotated = aux_node2->token_annotated;
  765.  
  766. 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){
  767. return;
  768. }
  769. else{
  770. 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);
  771. }
  772. }
  773. else if((strcmp(atual->node_type, "And")==0) || (strcmp(atual->node_type, "Or")==0)){
  774. aux_node = atual->son;
  775. while(aux_node!=NULL){
  776. create_annotated_ast(aux_node, p);
  777. aux_node = aux_node->brother;
  778. }
  779.  
  780. aux_node2 = atual->son;
  781. aux_node3 = aux_node2->brother;
  782. if(strcmp(aux_node2->token_annotated, aux_node2->token_annotated)!=0){
  783. if((strcmp(atual->node_type, "And")==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. if((strcmp(atual->node_type, "Or")==0)){
  787. 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);
  788. }
  789. }
  790.  
  791. atual->token_annotated = "bool";
  792. }
  793.  
  794. else if((strcmp(atual->node_type, "Lt")==0) || (strcmp(atual->node_type, "Gt")==0) || (strcmp(atual->node_type, "Eq")==0) ||
  795. (strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Le")==0) || (strcmp(atual->node_type, "Ge")==0)){
  796. aux_node = atual->son;
  797. while(aux_node!=NULL){
  798. create_annotated_ast(aux_node, p);
  799. aux_node = aux_node->brother;
  800. }
  801. atual->token_annotated = "bool";
  802.  
  803. aux_node2 = atual->son;
  804. aux_node3 = aux_node2->brother;
  805. if((strcmp(atual->node_type, "Lt")==0)){
  806. aux = "<";
  807. }
  808. else if(strcmp(atual->node_type, "Gt")==0){
  809. aux = ">";
  810. }
  811. else if(strcmp(atual->node_type, "Eq")==0){
  812. aux = "==";
  813. }
  814. else if(strcmp(atual->node_type, "Ne")==0){
  815. aux = "!";
  816. }
  817. else if(strcmp(atual->node_type, "Le")==0){
  818. aux = "<=";
  819. }
  820. else if(strcmp(atual->node_type, "Ge")==0){
  821. aux = ">=";
  822. }
  823.  
  824. if((strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Eq")==0)){
  825. if((strcmp(aux_node2->token_annotated, "bool")==0) && (strcmp(aux_node3->token_annotated, "bool")==0)){
  826. return;
  827. }
  828. }
  829. 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))){
  830. 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);
  831. }
  832. }
  833. 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)){
  834. aux_node = atual->son;
  835. while(aux_node!=NULL){
  836. create_annotated_ast(aux_node, p);
  837. aux_node = aux_node->brother;
  838. }
  839.  
  840. aux_node2 = atual->son;
  841. aux_node3 = aux_node2->brother;
  842. if((strcmp(atual->node_type, "Add")==0)){
  843. aux = "+";
  844. }
  845. else if(strcmp(atual->node_type, "Sub")==0){
  846. aux = "-";
  847. }
  848. else if(strcmp(atual->node_type, "Mul")==0){
  849. aux = "*";
  850. }
  851. else if(strcmp(atual->node_type, "Div")==0){
  852. aux = "/";
  853. }
  854. else if(strcmp(atual->node_type, "Mod")==0){
  855. aux = "%";
  856. }
  857.  
  858. if(strcmp(aux_node2->token_annotated, "int")==0){
  859. if(strcmp(aux_node3->token_annotated, "int")==0){
  860. atual->token_annotated = "int";
  861. }
  862. else if(strcmp(aux_node3->token_annotated, "float32")==0){
  863. atual->token_annotated = "float32";
  864. }
  865. else{
  866. 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);
  867. atual->token_annotated = "undef";
  868. }
  869. }
  870. if(strcmp(aux_node2->token_annotated, "float32")==0){
  871. if((strcmp(aux_node3->token_annotated, "int")==0) || (strcmp(aux_node3->token_annotated, "float32")==0)){
  872. atual->token_annotated = "float32";
  873. }
  874. else{
  875. 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);
  876. atual->token_annotated = "undef";
  877. }
  878. }
  879. }
  880.  
  881. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  882. else if((strcmp(atual->node_type, "Plus")==0) || (strcmp(atual->node_type, "Minus")==0)){
  883. aux_node = atual->son;
  884. while(aux_node!=NULL){
  885. create_annotated_ast(aux_node, p);
  886. aux_node = aux_node->brother;
  887. }
  888.  
  889. aux_node2 = atual->son;
  890. atual->token_annotated = aux_node2->token_annotated;
  891.  
  892. //??????????????????????????????????
  893. }
  894. else if((strcmp(atual->node_type, "Not")==0)){
  895. aux_node = atual->son;
  896. while(aux_node!=NULL){
  897. create_annotated_ast(aux_node, p);
  898. aux_node = aux_node->brother;
  899. }
  900.  
  901. aux_node2 = atual->son;
  902. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  903. printf("Line %d, colunm %d: Operator ! cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
  904. }
  905.  
  906. aux_node2->token_annotated = "bool";
  907. }
  908. //printf("----> saiu da função %s %s\n", atual->token, atual->node_type);
  909. }
  910.  
  911. void print_ast_annotated(node *current, int n){
  912. //printf("--------------------------->ARVORE ANOTADA\n");
  913. if(current == NULL){
  914. return;
  915. }
  916. if(strcmp(current->node_type, "NULL") == 0){
  917. print_ast_annotated(current->brother, n);
  918. return;
  919. }
  920.  
  921. int i;
  922. _var *vars_list= global_table->vars_list;
  923.  
  924. if(strcmp(current->node_type, "NULL") != 0){
  925. for(i=0;i<n;i++){
  926. printf("..");
  927. }
  928.  
  929. if(current->token != NULL){
  930. //printf("++++++++++++++ ct: %s\n", current->node_type);
  931. while(vars_list!=NULL){
  932. //printf("------>while\n");
  933. if(vars_list->isfunction==1 && strcmp(current->token, vars_list->id)==0 && current->token_annotated!=NULL){ //Função
  934. printf("%s(%s) - (",current->node_type, current->token);
  935. _param *param_list= vars_list->params;
  936. while(param_list!=NULL){
  937. if(param_list->isparam==1){
  938. if(param_list->next==NULL || param_list->next->isparam==0)
  939. printf("%s", param_list->type);
  940. else
  941. printf("%s,", param_list->type);
  942. }
  943. param_list= param_list->next;
  944. }
  945. printf(")\n");
  946. break;
  947. }
  948. else if(vars_list->isfunction==0 && strcmp(current->token, vars_list->id)==0 && current->token_annotated!=NULL){ //Variável
  949. printf("%s(%s) - %s\n",current->node_type, current->token, current->token_annotated);
  950. break;
  951.  
  952. }
  953. else{
  954. if(current->token_annotated!=NULL){
  955. printf("%s(%s) - %s\n",current->node_type, current->token, current->token_annotated);
  956. break;
  957. }
  958. else{
  959. printf("%s(%s)\n",current->node_type, current->token);
  960. break;
  961. }
  962.  
  963. }
  964. vars_list=vars_list->next;
  965. }
  966. }
  967.  
  968. else{
  969. //printf("++++++++++++++ ct: %s\n", current->node_type);
  970. if(current->token_annotated!=NULL){
  971. printf("%s - %s\n",current->node_type, current->token_annotated);
  972. }
  973. else{
  974. printf("%s\n",current->node_type);
  975. }
  976. }
  977. }
  978.  
  979. print_ast_annotated(current->son, n+1);
  980. print_ast_annotated(current->brother, n);
  981. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement