Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.01 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. aux_funcbody= aux1->brother; //FuncBody
  448. if(strcmp(aux_funcbody->node_type,"FuncBody")==0){
  449. aux1= aux_funcbody->son;
  450. while(aux1!=NULL){
  451. if(strcmp(aux1->node_type,"VarDecl")==0){
  452. aux2= aux1->son; //Type
  453. aux3= aux2->brother; //ID
  454.  
  455. if(search_param_already_exists(new_function->params, aux3->token)==1){
  456. //Fica a 1 se já existir, emite erro
  457. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  458. //Não sei o que acontece a seguir
  459. }
  460. else{
  461. _param *new_param = create_local_variable(change_type(aux2->node_type), aux3->token);
  462. if(new_function->params==NULL)
  463. new_function->params=new_param;
  464. else{
  465. _param *atual= new_function->params;
  466. while(atual->next!=NULL)
  467. atual=atual->next;
  468. atual->next=new_param;
  469. }
  470. }
  471. }
  472. else{
  473. create_annotated_ast(aux1, new_function->params);
  474. }
  475. aux1=aux1->brother;
  476. }
  477. }
  478. //adiciona função
  479. add_to_global(new_function);
  480. }
  481. }
  482. }
  483. aux_brother= aux_brother->brother;
  484. }
  485. }
  486. }
  487. else{
  488. return;
  489. }
  490. }
  491.  
  492. ////////////////////ANNOTATED AST/////////////////////////
  493.  
  494. char *variable_type_local(_param *p_list, char* id){
  495. if(p_list==NULL)
  496. return NULL;
  497.  
  498. _var *atual = p_list;
  499. while(atual->next != NULL){
  500. if(strcmp(atual->id, id) == 0){
  501. return atual->type;
  502. }
  503. atual = atual->next;
  504. }
  505. return NULL;
  506. }
  507.  
  508. char *variable_type_global(_var *vars_list, char* id){
  509. if(vars_list==NULL)
  510. return NULL;
  511.  
  512. _var *atual = vars_list;
  513. while(atual->next != NULL){
  514. if(strcmp(atual->id, id) == 0){
  515. return atual->type;
  516. }
  517. atual = atual->next;
  518. }
  519. return NULL;
  520. }
  521.  
  522.  
  523.  
  524. char *variable_type_final(_param *p_list, char* id){
  525. char *aux = variable_type_local(p_list);
  526. if(aux!=NULL){
  527. return aux;
  528. }
  529. *aux = varieable_type_global(global_table->vars_list);
  530. else(aux!=NULL){
  531. return aux;
  532. }
  533. return NULL;
  534. }
  535.  
  536. void create_annotated_ast(node *atual, _param *p){
  537. char *aux;
  538. node *aux_node, *aux_node2, *aux_node3;
  539.  
  540. if(strcmp(atual->node_type, "NULL")==0 || atual == NULL){
  541. return;
  542. }
  543. else if(strcmp(atual->node_type, "Id")==0){
  544. aux = variable_type_final(p, atual->token);
  545. if(aux == NULL){
  546. printf("Line %d, column %d: Cannot find symbol %s", atual->line, atual->col, atual->token);
  547. //pseudo-tipo undef a quaisquer símbolos desconhecidos
  548. atual->token_annotated = "undef";
  549. }
  550. else{
  551. atual->token_annotated = aux;
  552. }
  553. }
  554. else if(strcmp(atual->node_type, "StrLit")==0){
  555. atual->token_annotated = "String";
  556. }
  557. else if(strcmp(atual->node_type, "IntLit")==0){
  558. char *number = atual->token;
  559.  
  560. int i=0;
  561. while(number[i]!='\0'){
  562. if(number[i] >= '7'){
  563. printf("Line %d, column %d: Invalid octal constant: %s\n", atual->line, atual->col, atual->token);
  564. }
  565. i++;
  566. }
  567.  
  568. atual->token_annotated = "int";
  569. }
  570. else if(strcmp(atual->node_type, "RealLit")==0){
  571. atual->token_annotated = "float32";
  572. }
  573. else if(strcmp(atual->node_type, "If")==0){
  574. aux_node = atual->son;
  575. while(aux_node!=NULL){
  576. create_annotated_ast(aux_node, p);
  577. aux_node = aux_node->brother;
  578. }
  579.  
  580. aux_node2 = atual->son;
  581. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  582. printf("Line %d, column %d: Incompatible type %s in if statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  583. }
  584. }
  585. else if(strcmp(atual->node_type, "For")==0){
  586. aux_node = atual->son;
  587. while(aux_node!=NULL){
  588. create_annotated_ast(aux_node, p);
  589. aux_node = aux_node->brother;
  590. }
  591.  
  592. aux_node2 = atual->son;
  593. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  594. printf("Line %d, column %d: Incompatible type %s in for statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  595. atual->token_annotated = "undef";
  596. }
  597. else{
  598. atual->token_annotated = "bool";
  599. }
  600. }
  601. else if(strcmp(atual->node_type, "Block")==0){
  602. aux_node = atual->son;
  603. while(aux_node!=NULL){
  604. create_annotated_ast(aux_node);
  605. aux_node = aux_node->brother;
  606. }
  607. }
  608.  
  609. /////////////////////////////////////////////////////////////////////////////////////
  610. else if(strcmp(atual->node_type, "Return")==0){
  611. aux_node = atual->son;
  612. while(aux_node!=NULL){
  613. create_annotated_ast(aux_node);
  614. aux_node = aux_node->brother;
  615. }
  616.  
  617. //INT AND DOUBLE??????
  618. aux_node2 = atual->son;
  619. if(aux_node2!=NULL){
  620. if(strcmp(p->type, "none")==0){
  621. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  622. }
  623. else if(strcmp(func type &&, aux_node2->token_annotated)==0){
  624. return;
  625. }
  626. else{
  627. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  628. }
  629. }
  630. else if(aux_node2==NULL && (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. }
  634.  
  635. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  636. else if(strcmp(atual->node_type, "Print")==0){
  637. aux_node = atual->son;
  638. while(aux_node!=NULL){
  639. create_annotated_ast(aux_node);
  640. aux_node = aux_node->brother;
  641. }
  642.  
  643. aux_node2 = atual->son;
  644. if(strcmp(aux_node2->token_annotated, "undef")==0){
  645. printf("Line %d, column %d: Incompatible type %s in fmt.Println statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  646. }
  647. }
  648.  
  649. /////////////////////////////////////////////////////////////////////////////////////FAZER DO 0
  650. else if(strcmp(atual->node_type, "Call")==0){
  651. aux_node = atual->son;
  652. while(aux_node!=NULL){
  653. create_annotated_ast(aux_node);
  654. aux_node = aux_node->brother;
  655. }
  656. }
  657.  
  658. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  659. else if(strcmp(atual->node_type, "ParseArgs")==0){
  660. aux_node = atual->son;
  661. while(aux_node!=NULL){
  662. create_annotated_ast(aux_node);
  663. aux_node = aux_node->brother;
  664. }
  665.  
  666. /*aux_node2 = atual->son;
  667. aux_node3 = aux_node2->brother;*/
  668. /*if(){
  669. 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);
  670. }*/
  671.  
  672. atual->token_annotated = "int";
  673. }
  674. else if(strcmp(atual->node_type, "Assign")==0){
  675. aux_node = atual->son;
  676. while(aux_node!=NULL){
  677. create_annotated_ast(aux_node);
  678. aux_node = aux_node->brother;
  679. }
  680.  
  681. aux_node2 = atual->son;
  682. aux_node3 = aux_node2->brother;
  683. 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){
  684. return;
  685. }
  686. else{
  687. 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);
  688. }
  689. }
  690.  
  691. else if((strcmp(atual->node_type, "And")==0) || (strcmp(atual->node_type, "Or")==0)){
  692. aux_node = atual->son;
  693. while(aux_node!=NULL){
  694. create_annotated_ast(aux_node);
  695. aux_node = aux_node->brother;
  696. }
  697.  
  698. aux_node2 = atual->son;
  699. aux_node3 = aux_node2->brother;
  700. if(strcmp(aux_node2->token_annotated, aux_node2->token_annotated)!=0){
  701. if((strcmp(atual->node_type, "And")==0)){
  702. 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);
  703. }
  704. if((strcmp(atual->node_type, "Or")==0)){
  705. 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);
  706. }
  707. }
  708.  
  709. atual->token_annotated = "bool";
  710. }
  711. else if((strcmp(atual->node_type, "Lt")==0) || (strcmp(atual->node_type, "Gt")==0) || (strcmp(atual->node_type, "Eq")==0) ||
  712. (strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Le")==0) || (strcmp(atual->node_type, "Ge")==0)){
  713. aux_node = atual->son;
  714. while(aux_node!=NULL){
  715. create_annotated_ast(aux_node);
  716. aux_node = aux_node->brother;
  717. }
  718. atual->token_annotated = "bool";
  719.  
  720. aux_node2 = atual->son;
  721. aux_node3 = aux_node2->brother;
  722. if((strcmp(atual->node_type, "Lt")==0)){
  723. aux = "<";
  724. }
  725. else if(strcmp(atual->node_type, "Gt")==0){
  726. aux = ">";
  727. }
  728. else if(strcmp(atual->node_type, "Eq")==0){
  729. aux = "==";
  730. }
  731. else if(strcmp(atual->node_type, "Ne")==0){
  732. aux = "!";
  733. }
  734. else if(strcmp(atual->node_type, "Le")==0){
  735. aux = "<=";
  736. }
  737. else if(strcmp(atual->node_type, "Ge")==0){
  738. aux = ">=";
  739. }
  740.  
  741. if((strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Eq")==0)){
  742. if((strcmp(aux_node2->token_annotated, "bool")==0) && (strcmp(aux_node3->token_annotated, "bool")==0)){
  743. return;
  744. }
  745. }
  746. 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))){
  747. 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);
  748. }
  749. }
  750. 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)){
  751. aux_node = atual->son;
  752. while(aux_node!=NULL){
  753. create_annotated_ast(aux_node);
  754. aux_node = aux_node->brother;
  755. }
  756.  
  757. aux_node2 = atual->son;
  758. aux_node3 = aux_node2->brother;
  759. if((strcmp(atual->node_type, "Add")==0)){
  760. aux = "+";
  761. }
  762. else if(strcmp(atual->node_type, "Sub")==0){
  763. aux = "-";
  764. }
  765. else if(strcmp(atual->node_type, "Mul")==0){
  766. aux = "*";
  767. }
  768. else if(strcmp(atual->node_type, "Div")==0){
  769. aux = "/";
  770. }
  771. else if(strcmp(atual->node_type, "Mod")==0){
  772. aux = "%";
  773. }
  774.  
  775. if(strcmp(aux_node2->token_annotated, "int")==0){
  776. if(strcmp(aux_node3->token_annotated, "int")==0){
  777. atual->token_annotated = "int";
  778. }
  779. else if(strcmp(aux_node3->token_annotated, "float32")==0){
  780. atual->token_annotated = "float32";
  781. }
  782. else{
  783. 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);
  784. atual->token_annotated = "undef";
  785. }
  786. }
  787. if(strcmp(aux_node2->token_annotated, "float32")==0){
  788. if((strcmp(aux_node3->token_annotated, "int")==0) || (strcmp(aux_node3->token_annotated, "float32")==0)){
  789. atual->token_annotated = "float32";
  790. }
  791. else{
  792. 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);
  793. atual->token_annotated = "undef";
  794. }
  795. }
  796. }
  797.  
  798. /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
  799. else if((strcmp(atual->node_type, "Plus")==0) || (strcmp(atual->node_type, "Minus")==0)){
  800. aux_node = atual->son;
  801. while(aux_node!=NULL){
  802. create_annotated_ast(aux_node);
  803. aux_node = aux_node->brother;
  804. }
  805.  
  806. aux_node2 = atual->son;
  807. //aux_node2 = atual?
  808. //??????????????????????????????????
  809. }
  810. else if((strcmp(atual->node_type, "Not")==0)){
  811. aux_node = atual->son;
  812. while(aux_node!=NULL){
  813. create_annotated_ast(aux_node);
  814. aux_node = aux_node->brother;
  815. }
  816.  
  817. aux_node2 = atual->son;
  818. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  819. printf("Line %d, colunm %d: Operator ! cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
  820. }
  821.  
  822. aux_node2->token_annotated = "bool";
  823. }
  824. }
  825.  
  826. void print_ast_annotated(node *current, int n){
  827. if(current == NULL){
  828. return;
  829. }
  830. if(strcmp(current->node_type, "NULL") == 0){
  831. print_ast(current->brother, n);
  832. return;
  833. }
  834.  
  835. int i;
  836. if(strcmp(current->node_type, "NULL") != 0){
  837. for(i=0;i<n;i++){
  838. printf("..");
  839. }
  840.  
  841. if(current->token != NULL){
  842. if(current->token_annotated!=NULL){
  843.  
  844. }
  845. else{
  846. printf("%s(%s)\n",current->node_type, current->token);
  847.  
  848. }
  849. }
  850. else{
  851. printf("%s\n",current->node_type);
  852. }
  853. }
  854.  
  855. print_ast(current->son, n+1);
  856. print_ast(current->brother, n);
  857. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement