Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.76 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.  
  302. void add_to_global(_var *new_var){
  303. if(global_table->vars_list==NULL){
  304. global_table->vars_list= new_var;
  305. return;
  306. }
  307.  
  308. _var *atual= global_table->vars_list;
  309. while(atual->next!=NULL){
  310. atual= atual->next;
  311. }
  312.  
  313. atual->next= new_var;
  314. }
  315.  
  316.  
  317. void create_table(node *node_atual){
  318. node *aux, *aux_brother;
  319.  
  320. node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6, *aux7, *aux8, *aux_funcbody;
  321.  
  322. _var *new_var, *new_function;
  323.  
  324. if(node_atual!=NULL){
  325. if(strcmp(node_atual->node_type, "Program")==0){
  326. aux = node_atual->son; //son of program is declarations
  327.  
  328. global_table = (t_symbol*)malloc(sizeof(t_symbol));
  329. global_table->name = strdup("Global");
  330. global_table->type = strdup("Global");
  331. global_table->vars_list = NULL;
  332.  
  333. aux_brother= aux->brother; //VarDecl ou FuncDecl
  334.  
  335.  
  336. while(aux_brother!=NULL){
  337. //2 brothers --> 2 ifs
  338. if(strcmp(aux_brother->node_type, "VarDecl")==0){
  339.  
  340. aux1= aux_brother->son; //Type
  341. aux2= aux1->brother; //ID
  342.  
  343.  
  344. //if varieable exists already error
  345. if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
  346. //Fica a 1 se já existir, emite erro
  347. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  348. //Não sei o que acontece a seguir
  349. }
  350. else{
  351. //Adiciona nova variável à tabela global
  352.  
  353. //Cria variável, dá-lhe tipo(node_type) e valor(token)
  354. new_var= create_var(change_type(aux1->node_type), aux2->token);
  355. add_to_global(new_var);
  356. }
  357. }
  358. if(strcmp(aux_brother->node_type, "FuncDecl")==0){
  359. aux1 = aux_brother->son; //FuncHeader
  360.  
  361. if(strcmp(aux1->node_type, "FuncHeader")==0){
  362. aux3 = aux1->son; //ID
  363. aux4 = aux3->brother; //Type ou FuncParams ou vazio
  364.  
  365. if(aux4!=NULL){
  366. if(strcmp(aux4->node_type, "FuncParams")==0){
  367. //Type é "none"
  368. new_function= create_function(aux3->token, "none");
  369. aux6= aux4->son; //ParamDecl
  370. while(aux6!=NULL){
  371. if(strcmp(aux6->node_type, "NULL")==0){}
  372. else{
  373. aux7=aux6->son; //Type
  374. aux8= aux7->brother; //ID
  375. if(search_param_already_exists(new_function->params, aux8->token)==1){
  376. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  377. }
  378. else{
  379.  
  380. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  381. if(new_function->params==NULL)
  382. new_function->params=new_param;
  383. else{
  384. _param *atual= new_function->params;
  385. while(atual->next!=NULL)
  386. atual=atual->next;
  387. atual->next=new_param;
  388. }
  389. }
  390. }
  391.  
  392. aux6=aux6->brother;
  393. }
  394.  
  395. }
  396.  
  397. else{
  398. //Type diferente de "none"
  399. aux5= aux4->brother; //FuncParams ou NULL
  400. if(strcmp(aux5->node_type, "FuncParams")==0){
  401. //Com parametros
  402. new_function= create_function(aux3->token, change_type(aux4->node_type));
  403. aux6= aux5->son; //ParamDecl
  404. while(aux6!=NULL){
  405. if(strcmp(aux6->node_type, "NULL")==0){}
  406. else{
  407. aux7=aux6->son; //Type
  408. aux8= aux7->brother; //ID
  409. if(search_param_already_exists(new_function->params, aux8->token)==1){
  410. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  411. }
  412. else{
  413.  
  414. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  415. if(new_function->params==NULL)
  416. new_function->params=new_param;
  417. else{
  418. _param *atual= new_function->params;
  419. while(atual->next!=NULL)
  420. atual=atual->next;
  421. atual->next=new_param;
  422. }
  423. }
  424. }
  425.  
  426. aux6=aux6->brother;
  427. }
  428. }
  429. else{
  430. //Sem parametros
  431. create_function(aux3->token, change_type(aux4->node_type));
  432. }
  433. }
  434. }
  435.  
  436. else{
  437. //Sem parametros e Type é "none"
  438. new_function= create_function(aux3->token, "none");
  439.  
  440. }
  441.  
  442. if(search_var_or_function_already_exists(global_table->vars_list, aux3->token)==1){
  443. printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  444. }
  445. else{
  446. //Tabela local
  447. add_to_global(new_function);
  448. aux_funcbody= aux1->brother; //FuncBody
  449. if(strcmp(aux_funcbody->node_type,"FuncBody")==0){
  450. aux1= aux_funcbody->son;
  451. while(aux1!=NULL){
  452. if(strcmp(aux1->node_type,"VarDecl")==0){
  453. aux2= aux1->son; //Type
  454. aux3= aux2->brother; //ID
  455.  
  456. if(search_param_already_exists(new_function->params, aux3->token)==1){
  457. //Fica a 1 se já existir, emite erro
  458. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  459. //Não sei o que acontece a seguir
  460. }
  461. else{
  462. _param *new_param = create_local_variable(change_type(aux2->node_type), aux3->token);
  463. if(new_function->params==NULL)
  464. new_function->params=new_param;
  465. else{
  466. _param *atual= new_function->params;
  467. while(atual->next!=NULL)
  468. atual=atual->next;
  469. atual->next=new_param;
  470. }
  471. }
  472. }
  473. else{
  474. //printf("begin tree --> %s\n", aux1->node_type);
  475. create_annotated_ast(aux1, new_function->params);
  476. //printf("end tree\n");
  477. }
  478. aux1=aux1->brother;
  479. }
  480. }
  481. //adiciona função
  482. //add_to_global(new_function);
  483. }
  484. }
  485. }
  486. aux_brother= aux_brother->brother;
  487. }
  488. }
  489. }
  490. else{
  491. return;
  492. }
  493. }
  494.  
  495. ////////////////////ANNOTATED AST/////////////////////////
  496.  
  497. char *variable_type_local(_param *p_list, char* id){
  498. if(p_list==NULL)
  499. return NULL;
  500.  
  501. _param *atual = p_list;
  502. while(atual != NULL){
  503. if(strcmp(atual->id, id) == 0){
  504. return atual->type;
  505. }
  506. atual = atual->next;
  507. }
  508. return NULL;
  509. }
  510.  
  511. char *variable_type_global(_var *vars_list, char* id){
  512. if(vars_list==NULL){
  513. printf("starts at nulll\n");
  514. return NULL;
  515. }
  516.  
  517. _var *atual = vars_list;
  518. while(atual != NULL){
  519. if(strcmp(atual->id, id) == 0){
  520. return atual->type;
  521. }
  522. atual = atual->next;
  523. }
  524.  
  525. printf("ends at nulll\n");
  526. return NULL;
  527. }
  528.  
  529. char *variable_type_final(_param *p_list, char* id){
  530. char *aux = variable_type_local(p_list, id);
  531. if(aux!=NULL){
  532. return aux;
  533. }
  534.  
  535. aux = variable_type_global(global_table->vars_list, id);
  536. if(aux!=NULL){
  537. return aux;
  538. }
  539. return NULL;
  540. }
  541.  
  542. void create_annotated_ast(node *atual, _param *p){
  543. char *aux;
  544. node *aux_node, *aux_node2, *aux_node3;
  545.  
  546. if(atual == NULL){
  547. return;
  548. }
  549.  
  550. if(strcmp(atual->node_type, "NULL")==0){
  551. return;
  552. }
  553. else if(strcmp(atual->node_type, "Id")==0){
  554. aux = variable_type_final(p, atual->token);
  555. if(aux == NULL){
  556. printf("Line %d, column %d: Cannot find symbol %s\n", atual->line, atual->col, atual->token);
  557. //pseudo-tipo undef a quaisquer símbolos desconhecidos
  558. atual->token_annotated = "undef";
  559. }
  560. else{
  561. atual->token_annotated = aux;
  562. }
  563. }
  564. else if(strcmp(atual->node_type, "StrLit")==0){
  565. atual->token_annotated = "String";
  566. }
  567. else if(strcmp(atual->node_type, "IntLit")==0){
  568. char *number = atual->token;
  569.  
  570. int i=0;
  571. while(number[i]!='\0'){
  572. if(number[i] >= '7'){
  573. printf("Line %d, column %d: Invalid octal constant: %s\n", atual->line, atual->col, atual->token);
  574. }
  575. i++;
  576. }
  577.  
  578. atual->token_annotated = "int";
  579. }
  580. else if(strcmp(atual->node_type, "RealLit")==0){
  581. atual->token_annotated = "float32";
  582. }
  583. else if(strcmp(atual->node_type, "If")==0){
  584. aux_node = atual->son;
  585. while(aux_node!=NULL){
  586. create_annotated_ast(aux_node, p);
  587. aux_node = aux_node->brother;
  588. }
  589.  
  590. aux_node2 = atual->son;
  591. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  592. printf("Line %d, column %d: Incompatible type %s in if statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  593. }
  594. }
  595. else if(strcmp(atual->node_type, "For")==0){
  596. aux_node = atual->son;
  597. while(aux_node!=NULL){
  598. create_annotated_ast(aux_node, p);
  599. aux_node = aux_node->brother;
  600. }
  601.  
  602. aux_node2 = atual->son;
  603. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  604. printf("Line %d, column %d: Incompatible type %s in for statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  605. atual->token_annotated = "undef";
  606. }
  607. else{
  608. atual->token_annotated = "bool";
  609. }
  610. }
  611. else if(strcmp(atual->node_type, "Block")==0){
  612. aux_node = atual->son;
  613. while(aux_node!=NULL){
  614. create_annotated_ast(aux_node, p);
  615. aux_node = aux_node->brother;
  616. }
  617. }
  618.  
  619. /////////////////////////////////////////////////////////////////////////////////////
  620. else if(strcmp(atual->node_type, "Return")==0){
  621. aux_node = atual->son;
  622. while(aux_node!=NULL){
  623. create_annotated_ast(aux_node, p);
  624. aux_node = aux_node->brother;
  625. }
  626.  
  627. //INT AND DOUBLE??????
  628. aux_node2 = atual->son;
  629. if(aux_node2!=NULL){
  630. if(strcmp(p->type, "none")==0){
  631. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  632. }
  633. else if(strcmp(p->type, aux_node2->token_annotated)==0){
  634. return;
  635. }
  636. else{
  637. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  638. }
  639. }
  640. else if(aux_node2==NULL && (strcmp(p->type, "none")!=0)){
  641. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  642. }
  643. }
  644.  
  645. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  646. else if(strcmp(atual->node_type, "Print")==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, "undef")==0){
  655. printf("Line %d, column %d: Incompatible type %s in fmt.Println statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  656. }
  657. }
  658.  
  659. /////////////////////////////////////////////////////////////////////////////////////FAZER DO 0 SEGMENTATION FAULT
  660. else if(strcmp(atual->node_type, "Call")==0){
  661. aux_node = atual->son;
  662. while(aux_node!=NULL){
  663. create_annotated_ast(aux_node, p);
  664. aux_node = aux_node->brother;
  665. }
  666. }
  667.  
  668. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  669. else if(strcmp(atual->node_type, "ParseArgs")==0){
  670. aux_node = atual->son;
  671. while(aux_node!=NULL){
  672. create_annotated_ast(aux_node, p);
  673. aux_node = aux_node->brother;
  674. }
  675.  
  676. /*aux_node2 = atual->son;
  677. aux_node3 = aux_node2->brother;*/
  678. /*if(){
  679. 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);
  680. }*/
  681.  
  682. atual->token_annotated = "int";
  683. }
  684. else if(strcmp(atual->node_type, "Assign")==0){
  685. aux_node = atual->son;
  686. while(aux_node!=NULL){
  687. create_annotated_ast(aux_node, p);
  688. aux_node = aux_node->brother;
  689. }
  690.  
  691. aux_node2 = atual->son;
  692. aux_node3 = aux_node2->brother;
  693. 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){
  694. return;
  695. }
  696. else{
  697. 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);
  698. }
  699. }
  700.  
  701. else if((strcmp(atual->node_type, "And")==0) || (strcmp(atual->node_type, "Or")==0)){
  702. aux_node = atual->son;
  703. while(aux_node!=NULL){
  704. create_annotated_ast(aux_node, p);
  705. aux_node = aux_node->brother;
  706. }
  707.  
  708. aux_node2 = atual->son;
  709. aux_node3 = aux_node2->brother;
  710. if(strcmp(aux_node2->token_annotated, aux_node2->token_annotated)!=0){
  711. if((strcmp(atual->node_type, "And")==0)){
  712. 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);
  713. }
  714. if((strcmp(atual->node_type, "Or")==0)){
  715. 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);
  716. }
  717. }
  718.  
  719. atual->token_annotated = "bool";
  720. }
  721. else if((strcmp(atual->node_type, "Lt")==0) || (strcmp(atual->node_type, "Gt")==0) || (strcmp(atual->node_type, "Eq")==0) ||
  722. (strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Le")==0) || (strcmp(atual->node_type, "Ge")==0)){
  723. aux_node = atual->son;
  724. while(aux_node!=NULL){
  725. create_annotated_ast(aux_node, p);
  726. aux_node = aux_node->brother;
  727. }
  728. atual->token_annotated = "bool";
  729.  
  730. aux_node2 = atual->son;
  731. aux_node3 = aux_node2->brother;
  732. if((strcmp(atual->node_type, "Lt")==0)){
  733. aux = "<";
  734. }
  735. else if(strcmp(atual->node_type, "Gt")==0){
  736. aux = ">";
  737. }
  738. else if(strcmp(atual->node_type, "Eq")==0){
  739. aux = "==";
  740. }
  741. else if(strcmp(atual->node_type, "Ne")==0){
  742. aux = "!";
  743. }
  744. else if(strcmp(atual->node_type, "Le")==0){
  745. aux = "<=";
  746. }
  747. else if(strcmp(atual->node_type, "Ge")==0){
  748. aux = ">=";
  749. }
  750.  
  751. if((strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Eq")==0)){
  752. if((strcmp(aux_node2->token_annotated, "bool")==0) && (strcmp(aux_node3->token_annotated, "bool")==0)){
  753. return;
  754. }
  755. }
  756. 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))){
  757. 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);
  758. }
  759. }
  760. 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)){
  761. aux_node = atual->son;
  762. while(aux_node!=NULL){
  763. create_annotated_ast(aux_node, p);
  764. aux_node = aux_node->brother;
  765. }
  766.  
  767. aux_node2 = atual->son;
  768. aux_node3 = aux_node2->brother;
  769. if((strcmp(atual->node_type, "Add")==0)){
  770. aux = "+";
  771. }
  772. else if(strcmp(atual->node_type, "Sub")==0){
  773. aux = "-";
  774. }
  775. else if(strcmp(atual->node_type, "Mul")==0){
  776. aux = "*";
  777. }
  778. else if(strcmp(atual->node_type, "Div")==0){
  779. aux = "/";
  780. }
  781. else if(strcmp(atual->node_type, "Mod")==0){
  782. aux = "%";
  783. }
  784.  
  785. if(strcmp(aux_node2->token_annotated, "int")==0){
  786. if(strcmp(aux_node3->token_annotated, "int")==0){
  787. atual->token_annotated = "int";
  788. }
  789. else if(strcmp(aux_node3->token_annotated, "float32")==0){
  790. atual->token_annotated = "float32";
  791. }
  792. else{
  793. 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);
  794. atual->token_annotated = "undef";
  795. }
  796. }
  797. if(strcmp(aux_node2->token_annotated, "float32")==0){
  798. if((strcmp(aux_node3->token_annotated, "int")==0) || (strcmp(aux_node3->token_annotated, "float32")==0)){
  799. atual->token_annotated = "float32";
  800. }
  801. else{
  802. 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);
  803. atual->token_annotated = "undef";
  804. }
  805. }
  806. }
  807.  
  808. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA SEGMENTATION FAULT
  809. else if((strcmp(atual->node_type, "Plus")==0) || (strcmp(atual->node_type, "Minus")==0)){
  810. aux_node = atual->son;
  811. while(aux_node!=NULL){
  812. create_annotated_ast(aux_node, p);
  813. aux_node = aux_node->brother;
  814. }
  815.  
  816. aux_node2 = atual->son;
  817. //aux_node2 = atual?
  818. //??????????????????????????????????
  819. }
  820. else if((strcmp(atual->node_type, "Not")==0)){
  821. aux_node = atual->son;
  822. while(aux_node!=NULL){
  823. create_annotated_ast(aux_node, p);
  824. aux_node = aux_node->brother;
  825. }
  826.  
  827. aux_node2 = atual->son;
  828. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  829. printf("Line %d, colunm %d: Operator ! cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
  830. }
  831.  
  832. aux_node2->token_annotated = "bool";
  833. }
  834. //printf("----> saiu da função %s %s\n", atual->token, atual->node_type);
  835. }
  836.  
  837. void print_ast_annotated(node *current, int n){
  838. printf("ARVORE ANOTADA\n");
  839. if(current == NULL){
  840. return;
  841. }
  842. if(strcmp(current->node_type, "NULL") == 0){
  843. print_ast(current->brother, n);
  844. return;
  845. }
  846.  
  847. int i;
  848. _var *vars_list= global_table->vars_list;
  849. if(strcmp(current->node_type, "NULL") != 0){
  850. for(i=0;i<n;i++){
  851. printf("..");
  852. }
  853.  
  854. if(current->token != NULL){
  855. while(vars_list!=NULL){
  856. printf("------>while\n");
  857. if(vars_list->isfunction==1 && strcmp(current->token, vars_list->id)==0){ //Função
  858. printf("%s(%s) - (",current->node_type, current->token);
  859. _param *param_list= vars_list->params;
  860. while(param_list!=NULL){
  861. if(param_list->isparam==1){
  862. if(param_list->next==NULL || param_list->next->isparam==0)
  863. printf("%s", param_list->type);
  864. else
  865. printf("%s,", param_list->type);
  866. }
  867. param_list= param_list->next;
  868. }
  869. printf(")\n");
  870. }
  871. else{ //Variável
  872. printf("%s\t\t%s\n",vars_list->id, vars_list->type);
  873. }
  874. vars_list=vars_list->next;
  875. }
  876.  
  877. if(current->token_annotated!=NULL){
  878. printf("------>token token_annotated\n");
  879. printf("%s(%s) - %s",current->node_type, current->token, current->token_annotated);
  880. }
  881. else{
  882. printf("------>normal\n");
  883. printf("%s(%s)\n",current->node_type, current->token);
  884. }
  885. }
  886. else{
  887. printf("------>normal2\n");
  888. printf("%s\n",current->node_type);
  889. }
  890. }
  891.  
  892. print_ast(current->son, n+1);
  893. print_ast(current->brother, n);
  894. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement