Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.11 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. token* create_token(char *name, int line, int col){ //esta comentado pq so interessa para a parte dos erros
  10. token *new = (token*)malloc(sizeof(token));
  11.  
  12. if(name!= NULL){
  13. new->name = (char*)strdup(name);
  14. }
  15. else{
  16. new->name = 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. int flag_error_c = 0;
  146.  
  147. void print_global_table(){
  148. _var *vars_list= global_table->vars_list;
  149. printf("===== %s Symbol Table =====\n", global_table->name);
  150. while(vars_list!=NULL){
  151. if(vars_list->isfunction==1){ //Função
  152. printf("%s\t(", vars_list->id);
  153. _param *param_list= vars_list->params;
  154. while(param_list!=NULL){
  155. if(param_list->isparam==1){
  156. if(param_list->next==NULL || param_list->next->isparam==0)
  157. printf("%s", param_list->type);
  158. else
  159. printf("%s,", param_list->type);
  160. }
  161. param_list= param_list->next;
  162. }
  163. printf(")\t");
  164. if(vars_list->type==NULL)
  165. printf("none\n");
  166. else
  167. printf("%s\n",vars_list->type);
  168. }
  169. else{ //Variável
  170. printf("%s\t\t%s\n",vars_list->id, vars_list->type);
  171. }
  172. vars_list=vars_list->next;
  173. }
  174. printf("\n");
  175. }
  176.  
  177. void print_local_table(){
  178. _var *vars_list= global_table->vars_list;
  179. while(vars_list!=NULL){
  180. if(vars_list->isfunction==1){
  181. printf("===== Function %s(", vars_list->id);
  182. _param *param_list= vars_list->params;
  183. while(param_list!=NULL){
  184. if(param_list->isparam==1){
  185. if(param_list->next==NULL || param_list->next->isparam==0)
  186. printf("%s", param_list->type);
  187. else
  188. printf("%s,", param_list->type);
  189. }
  190. param_list= param_list->next;
  191. }
  192. printf(") Symbol Table =====\n");
  193. printf("return\t\t%s\n",vars_list->type);
  194.  
  195. param_list= vars_list->params;
  196. while(param_list!=NULL){
  197. if(param_list->isparam==1){
  198. printf("%s\t\t%s\tparam\n",param_list->id, param_list->type);
  199. }
  200. else
  201. printf("%s\t\t%s\n",param_list->id, param_list->type);
  202. param_list= param_list->next;
  203. }
  204. printf("\n");
  205. }
  206. vars_list=vars_list->next;
  207. }
  208. }
  209.  
  210. _var* create_function(char *id, char *type){
  211. _var *new_function= (_var*)malloc(sizeof(_var));
  212. new_function->id = (char*)strdup(id);
  213. new_function->type = (char*)strdup(type);
  214. new_function->isfunction = 1;
  215. new_function->params = NULL;
  216. new_function->funcbody=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->funcbody=NULL;
  229. new_var->next = NULL;
  230.  
  231. return new_var;
  232. }
  233.  
  234. _param* create_param(char *type, char *id){
  235. _param *new_param = (_param*)malloc(sizeof(_param));
  236. new_param->isparam=1;
  237. new_param->isused=0;
  238. new_param->id = (char*)strdup(id);
  239. new_param->type = (char*)strdup(type);
  240. new_param->next= NULL;
  241.  
  242. return new_param;
  243. }
  244.  
  245. _param* create_local_variable(char *type, char *id){
  246. _param *new_param = (_param*)malloc(sizeof(_param));
  247. new_param->isparam=0;
  248. new_param->isused=0;
  249. new_param->id = (char*)strdup(id);
  250. new_param->type = (char*)strdup(type);
  251. new_param->next= NULL;
  252.  
  253. return new_param;
  254. }
  255.  
  256. int search_var_or_function_already_exists(_var *vars_list, char* id){
  257. //0 não existe, 1 existe
  258. if(vars_list==NULL)
  259. return 0;
  260.  
  261. _var *atual = vars_list;
  262. while(atual != NULL){
  263. if(strcmp(atual->id, id) == 0){
  264. return 1;
  265. }
  266. atual = atual->next;
  267. }
  268. return 0;
  269. }
  270. char* change_type(char *type){
  271.  
  272. if(strcmp(type, "Int") == 0){
  273. type = "int";
  274. return type;
  275. }
  276. if(strcmp(type, "Float32") == 0){
  277. type = "float32";
  278. return type;
  279. }
  280. if(strcmp(type, "Bool") == 0){
  281. type = "bool";
  282. return type;
  283. }
  284. if(strcmp(type, "String") == 0){
  285. type = "string";
  286. return type;
  287. }
  288. return type;
  289. }
  290.  
  291. int search_param_already_exists(_param *params_list, char *id){
  292. if(params_list==NULL){
  293. return 0;
  294. }
  295.  
  296. _param *atual= params_list;
  297. while(atual != NULL){
  298. if(strcmp(atual->id, id) == 0){
  299. return 1;
  300. }
  301. atual = atual->next;
  302. }
  303. return 0;
  304.  
  305. }
  306. void not_used_local_symbols_error(_var *function){
  307. _param *param_list= function->params;
  308. if(param_list==NULL){
  309. return;
  310. }
  311.  
  312. _param *atual= param_list;
  313. while(atual != NULL){
  314. if(atual->isused==0 && atual->isparam==0){
  315. flag_error_c = 1;
  316. printf("Line %d, column %d: Symbol %s declared but never used\n", atual->line, atual->col, atual->id);
  317. }
  318. atual = atual->next;
  319. }
  320.  
  321. }
  322. void not_used_global_symbols_error(){
  323. _var *vars_list= global_table->vars_list;
  324. if(vars_list==NULL){
  325. return;
  326. }
  327.  
  328. _var *atual= vars_list;
  329. while(atual != NULL){
  330. if(atual->isused==0 && atual->isfunction==0){
  331. flag_error_c = 1;
  332. printf("Line %d, column %d: Symbol %s declared but never used\n", atual->line, atual->col, atual->id);
  333. }
  334. atual = atual->next;
  335. }
  336.  
  337. }
  338. void add_to_global(_var *new_var){
  339. if(global_table->vars_list==NULL){
  340. global_table->vars_list= new_var;
  341. return;
  342. }
  343.  
  344. _var *atual= global_table->vars_list;
  345. while(atual->next!=NULL){
  346. atual= atual->next;
  347. }
  348.  
  349. atual->next= new_var;
  350. }
  351.  
  352. void annotated_ast(){
  353. _var *vars_list= global_table->vars_list;
  354. node *aux;
  355. while(vars_list!=NULL){
  356. if(vars_list->isfunction==1){
  357. aux= vars_list->funcbody->son;
  358.  
  359. while(aux!=NULL){
  360. if(strcmp(aux->node_type,"VarDecl")!=0)
  361. create_annotated_ast(aux, vars_list->params, vars_list);
  362. aux=aux->brother;
  363. }
  364. not_used_local_symbols_error(vars_list);
  365. }
  366. vars_list= vars_list->next;
  367. }
  368. }
  369.  
  370. void create_table(node *node_atual){
  371. node *aux, *aux_brother;
  372.  
  373. node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6, *aux7, *aux8, *aux_funcbody;
  374.  
  375. _var *new_var, *new_function;
  376.  
  377. if(node_atual!=NULL){
  378. if(strcmp(node_atual->node_type, "Program")==0){
  379. aux = node_atual->son; //son of program is declarations
  380.  
  381. global_table = (t_symbol*)malloc(sizeof(t_symbol));
  382. global_table->name = strdup("Global");
  383. global_table->type = strdup("Global");
  384. global_table->vars_list = NULL;
  385.  
  386. aux_brother= aux->brother; //VarDecl ou FuncDecl
  387.  
  388.  
  389. while(aux_brother!=NULL){
  390. //2 brothers --> 2 ifs
  391. if(strcmp(aux_brother->node_type, "VarDecl")==0){
  392.  
  393. aux1= aux_brother->son; //Type
  394. aux2= aux1->brother; //ID
  395.  
  396.  
  397. //if varieable exists already error
  398. if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
  399. //Fica a 1 se já existir, emite erro
  400. printf("Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  401. flag_error_c = 1;
  402. }
  403. else{
  404. //Adiciona nova variável à tabela global
  405.  
  406. //Cria variável, dá-lhe tipo(node_type) e valor(token)
  407. new_var= create_var(change_type(aux1->node_type), aux2->token);
  408. new_var->line=aux2->line;
  409. new_var->col=aux2->col;
  410. add_to_global(new_var);
  411. }
  412. }
  413. if(strcmp(aux_brother->node_type, "FuncDecl")==0){
  414. aux1 = aux_brother->son; //FuncHeader
  415.  
  416. if(strcmp(aux1->node_type, "FuncHeader")==0){
  417. aux3 = aux1->son; //ID
  418. aux4 = aux3->brother; //Type ou FuncParams ou vazio
  419.  
  420. if(aux4!=NULL){
  421. if(strcmp(aux4->node_type, "FuncParams")==0){
  422. //Type é "none"
  423. new_function= create_function(aux3->token, "none");
  424. new_function->line= aux3->line;
  425. new_function->col=aux3->col;
  426.  
  427. aux6= aux4->son; //ParamDecl
  428. while(aux6!=NULL){
  429. if(strcmp(aux6->node_type, "NULL")==0){}
  430. else{
  431. aux7=aux6->son; //Type
  432. aux8= aux7->brother; //ID
  433. if(search_param_already_exists(new_function->params, aux8->token)==1){
  434. printf("Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  435. flag_error_c = 1;
  436. }
  437. else{
  438.  
  439. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  440. new_param->line= aux8->line;
  441. new_param->col=aux8->col;
  442. if(new_function->params==NULL)
  443. new_function->params=new_param;
  444. else{
  445. _param *atual= new_function->params;
  446. while(atual->next!=NULL)
  447. atual=atual->next;
  448. atual->next=new_param;
  449. }
  450. }
  451. }
  452.  
  453. aux6=aux6->brother;
  454. }
  455.  
  456. }
  457.  
  458. else{
  459. //Type diferente de "none"
  460. aux5= aux4->brother; //FuncParams ou NULL
  461. if(strcmp(aux5->node_type, "FuncParams")==0){
  462. //Com parametros
  463. new_function= create_function(aux3->token, change_type(aux4->node_type));
  464. new_function->line= aux3->line;
  465. new_function->col= aux3->col;
  466. aux6= aux5->son; //ParamDecl
  467. while(aux6!=NULL){
  468. if(strcmp(aux6->node_type, "NULL")==0){}
  469. else{
  470. aux7=aux6->son; //Type
  471. aux8= aux7->brother; //ID
  472. if(search_param_already_exists(new_function->params, aux8->token)==1){
  473. printf("Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  474. flag_error_c = 1;
  475. }
  476. else{
  477.  
  478. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  479. new_param->line=aux8->line;
  480. new_param->col=aux8->col;
  481. if(new_function->params==NULL)
  482. new_function->params=new_param;
  483. else{
  484. _param *atual= new_function->params;
  485. while(atual->next!=NULL)
  486. atual=atual->next;
  487. atual->next=new_param;
  488. }
  489. }
  490. }
  491.  
  492. aux6=aux6->brother;
  493. }
  494. }
  495. else{
  496. //Sem parametros
  497. new_function= create_function(aux3->token, change_type(aux4->node_type));
  498. new_function->line= aux3->line;
  499. new_function->col= aux3->col;
  500. }
  501. }
  502. }
  503.  
  504. else{
  505. //Sem parametros e Type é "none"
  506. new_function= create_function(aux3->token, "none");
  507. new_function->line= aux3->line;
  508. new_function->col= aux3->col;
  509.  
  510. }
  511.  
  512. if(search_var_or_function_already_exists(global_table->vars_list, aux3->token)==1){
  513. printf("Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  514. flag_error_c = 1;
  515. }
  516.  
  517. else{
  518.  
  519. //Tabela local
  520. aux_funcbody= aux1->brother; //FuncBody
  521. new_function->funcbody= aux_funcbody;
  522. add_to_global(new_function);
  523. if(strcmp(aux_funcbody->node_type,"FuncBody")==0){
  524. aux1= aux_funcbody->son;
  525. while(aux1!=NULL){
  526. if(strcmp(aux1->node_type,"VarDecl")==0){
  527. aux2= aux1->son; //Type
  528. aux3= aux2->brother; //ID
  529.  
  530. if(search_param_already_exists(new_function->params, aux3->token)==1){
  531. //Fica a 1 se já existir, emite erro
  532. printf("Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  533. flag_error_c = 1;
  534. //Não sei o que acontece a seguir
  535. }
  536. else{
  537. _param *new_param = create_local_variable(change_type(aux2->node_type), aux3->token);
  538. new_param->line= aux3->line;
  539. new_param->col= aux3->col;
  540. if(new_function->params==NULL)
  541. new_function->params=new_param;
  542. else{
  543. _param *atual= new_function->params;
  544. while(atual->next!=NULL)
  545. atual=atual->next;
  546. atual->next=new_param;
  547. }
  548. }
  549. }
  550. aux1=aux1->brother;
  551. }
  552. }
  553. }
  554. }
  555. }
  556. aux_brother= aux_brother->brother;
  557. }
  558. }
  559. annotated_ast();
  560. }
  561. else{
  562. return;
  563. }
  564. }
  565.  
  566. ////////////////////ANNOTATED AST/////////////////////////
  567.  
  568. char *variable_type_local(_param *p_list, char* id){
  569. if(p_list==NULL)
  570. return NULL;
  571.  
  572. _param *atual = p_list;
  573. while(atual != NULL){
  574. if(strcmp(atual->id, id) == 0){
  575. return atual->type;
  576. }
  577. atual = atual->next;
  578. }
  579. return NULL;
  580. }
  581.  
  582. char *variable_type_global(_var *vars_list, char* id, int flag){
  583. if(vars_list==NULL){
  584. return NULL;
  585. }
  586.  
  587. _var *atual = vars_list;
  588. while(atual != NULL){
  589. if(strcmp(atual->id, id) == 0 && atual->isfunction==flag){
  590. return atual->type;
  591. }
  592. atual = atual->next;
  593. }
  594. return NULL;
  595. }
  596.  
  597. char *variable_type_final(_param *p_list, char* id, int flag){
  598. if(flag==0){
  599. char *aux = variable_type_local(p_list, id);
  600. if(aux!=NULL){
  601. return aux;
  602. }
  603. }
  604.  
  605. char *aux = variable_type_global(global_table->vars_list, id, flag);
  606. if(aux!=NULL){
  607. return aux;
  608. }
  609. return NULL;
  610. }
  611.  
  612. _param *search_function(char *id){
  613. _var *vars_list = global_table->vars_list;
  614. if(vars_list==NULL){
  615. return NULL;
  616. }
  617.  
  618. _var *atual = vars_list;
  619. while(atual != NULL){
  620. if(strcmp(atual->id, id) == 0 && atual->isfunction==1){
  621. return atual->params;
  622. }
  623. atual = atual->next;
  624. }
  625. return NULL;
  626. }
  627.  
  628. int is_call = 0;
  629. char aux_call[1000], aux_call_2[1000];
  630. void create_annotated_ast(node *atual, _param *p, _var *f){
  631. char *aux;
  632. node *aux_node, *aux_node2, *aux_node3, *aux_node4;
  633.  
  634. if(atual == NULL){
  635. return;
  636. }
  637.  
  638. if(strcmp(atual->node_type, "NULL")==0){
  639. return;
  640. }
  641. else if(strcmp(atual->node_type, "Id")==0){
  642. aux = variable_type_final(p, atual->token, is_call);
  643. if(aux == NULL){
  644. if(!is_call){
  645. printf("Line %d, column %d: Cannot find symbol %s\n", atual->line, atual->col, atual->token);
  646. }
  647. else{
  648. //printf("%s(%s)\n", atual->token, aux_call);
  649. printf("Line %d, column %d: Cannot find symbol %s(%s)\n", atual->line, atual->col, atual->token, aux_call);
  650. }
  651. //pseudo-tipo undef a quaisquer símbolos desconhecidos
  652. atual->token_annotated = "undef";
  653. flag_error_c = 1;
  654. }
  655. else{
  656. _var *vars= global_table->vars_list;
  657. if(vars==NULL){}
  658. else{
  659. while(vars!=NULL){
  660. if(strcmp(vars->id, atual->token)==0){
  661. vars->isused=1;
  662. break;
  663. }
  664. vars= vars->next;
  665. }
  666. }
  667. _param *aux_param= p;
  668. if(aux_param==NULL){}
  669. else{
  670. while(aux_param!=NULL){
  671. if(strcmp(aux_param->id, atual->token)==0){
  672. aux_param->isused=1;
  673. break;
  674. }
  675. aux_param= aux_param->next;
  676. }
  677. }
  678.  
  679. //se é função e existe -> verificar parametros
  680. if(is_call){
  681. atual->token_annotated = aux;
  682. //_param *func_atual = search_function(atual->token);
  683. //printf("%s\n", func_atual->);
  684. //printf("i'm function and i exist --> %s\n", atual->token);
  685. //printf("%s(%s)\n", atual->token, aux_call);
  686. }
  687. else{
  688. atual->token_annotated = aux;
  689. }
  690. }
  691. //strcpy(aux_call, "\0");
  692. is_call = 0;
  693. }
  694. else if(strcmp(atual->node_type, "StrLit")==0){
  695. atual->token_annotated = "string";
  696. }
  697. else if(strcmp(atual->node_type, "IntLit")==0){
  698. char *number = atual->token;
  699.  
  700. int i=0;
  701. while(number[i]!='\0'){
  702. if(number[i] >= '7'){
  703. flag_error_c = 1;
  704. printf("Line %d, column %d: Invalid octal constant: %s\n", atual->line, atual->col, atual->token);
  705. }
  706. i++;
  707. }
  708.  
  709. atual->token_annotated = "int";
  710. }
  711. else if(strcmp(atual->node_type, "RealLit")==0){
  712. atual->token_annotated = "float32";
  713. }
  714. else if(strcmp(atual->node_type, "If")==0){
  715. aux_node = atual->son;
  716. create_annotated_ast(aux_node, p, f);
  717. aux_node = aux_node->brother;
  718.  
  719. aux_node2 = atual->son;
  720. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  721. flag_error_c = 1;
  722. printf("Line %d, column %d: Incompatible type %s in if statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  723. }
  724.  
  725. while(aux_node!=NULL){
  726. create_annotated_ast(aux_node, p, f);
  727. aux_node = aux_node->brother;
  728. }
  729. }
  730. else if(strcmp(atual->node_type, "For")==0){
  731. aux_node = atual->son;
  732. create_annotated_ast(aux_node, p, f);
  733. aux_node = aux_node->brother;
  734.  
  735. aux_node2 = atual->son;
  736. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  737. flag_error_c = 1;
  738. printf("Line %d, column %d: Incompatible type %s in for statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  739. }
  740.  
  741. while(aux_node!=NULL){
  742. create_annotated_ast(aux_node, p, f);
  743. aux_node = aux_node->brother;
  744. }
  745. }
  746. else if(strcmp(atual->node_type, "Block")==0){
  747. aux_node = atual->son;
  748. while(aux_node!=NULL){
  749. create_annotated_ast(aux_node, p, f);
  750. aux_node = aux_node->brother;
  751. }
  752. }
  753.  
  754. else if(strcmp(atual->node_type, "Return")==0){
  755. aux_node = atual->son;
  756. while(aux_node!=NULL){
  757. create_annotated_ast(aux_node, p, f);
  758. aux_node = aux_node->brother;
  759. }
  760.  
  761. aux_node2 = atual->son;
  762. if(aux_node2!=NULL){
  763. if(strcmp(f->type, "none")==0){
  764. flag_error_c = 1;
  765. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  766. }
  767. else if(strcmp(f->type, aux_node2->token_annotated)==0){
  768. return;
  769. }
  770. else{
  771. flag_error_c = 1;
  772. printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  773. }
  774. }
  775. else if(aux_node2==NULL && (strcmp(f->type, "none")!=0)){
  776. flag_error_c = 1;
  777. printf("Line %d, column %d: Incompatible type none in return statement\n", atual->line, atual->col);
  778. }
  779. }
  780. else if(strcmp(atual->node_type, "Print")==0){
  781. aux_node = atual->son;
  782. while(aux_node!=NULL){
  783. create_annotated_ast(aux_node, p, f);
  784. aux_node = aux_node->brother;
  785. }
  786.  
  787. aux_node2 = atual->son;
  788. if(strcmp(aux_node2->token_annotated, "undef")==0){
  789. flag_error_c = 1;
  790. printf("Line %d, column %d: Incompatible type %s in fmt.Println statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
  791. }
  792. }
  793.  
  794. /////////////////////////////////////////////////////////////////////////////////////FAZER DO 0 SEGMENTATION FAULT
  795. else if(strcmp(atual->node_type, "Call")==0){
  796. aux_node4 = atual->son;
  797. //is_call = 1;
  798. //create_annotated_ast(aux_node, p, f);
  799. aux_node = aux_node4->brother;
  800. while(aux_node!=NULL && strcmp(aux_node->node_type, "NULL")!=0){
  801. create_annotated_ast(aux_node, p, f);
  802. //printf("t a: %s\n", aux_node->token_annotated);
  803. //printf("t name: %s\n", aux_node->token);
  804. strcpy(aux_call_2,aux_node->token_annotated);
  805. //printf("aux_call_2: %s\n", aux_call_2);
  806. if(aux_node->brother!=NULL && strcmp(aux_node->brother->node_type, "NULL"))
  807. strcat(aux_call, strcat(aux_call_2, ","));
  808. else{
  809. strcat(aux_call, aux_call_2);
  810. }
  811. //printf("aux_call: %s\n", aux_call);
  812. aux_node = aux_node->brother;
  813. }
  814.  
  815. is_call = 1;
  816. create_annotated_ast(aux_node4, p, f);
  817. strcpy(aux_call, "\0");
  818. aux_node2 = atual->son;
  819.  
  820. if(strcmp(aux_node2->token_annotated, "none")!=0)
  821. atual->token_annotated = aux_node2->token_annotated;
  822. }
  823. else if(strcmp(atual->node_type, "ParseArgs")==0){
  824. aux_node = atual->son;
  825. while(aux_node!=NULL){
  826. create_annotated_ast(aux_node, p, f);
  827. aux_node = aux_node->brother;
  828. }
  829.  
  830. aux_node2 = atual->son;
  831. aux_node3 = aux_node2->brother;
  832. if(strcmp(aux_node2->token_annotated, aux_node3->token_annotated)==0 && strcmp(aux_node2->token_annotated, "int")!=0){
  833. 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);
  834. }
  835. else if(strcmp(aux_node2->token_annotated, aux_node3->token_annotated)!=0){
  836. 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);
  837. }
  838.  
  839. atual->token_annotated = "int";
  840. }
  841. else if(strcmp(atual->node_type, "Assign")==0){
  842. aux_node = atual->son;
  843. while(aux_node!=NULL){
  844. create_annotated_ast(aux_node, p, f);
  845. aux_node = aux_node->brother;
  846. }
  847.  
  848. aux_node2 = atual->son;
  849. aux_node3 = aux_node2->brother;
  850.  
  851. if(strcmp(aux_node2->token_annotated, aux_node3->token_annotated)!=0){
  852. flag_error_c = 1;
  853. 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);
  854. atual->token_annotated = "undef";
  855. }
  856. else{
  857. if(strcmp(aux_node2->token_annotated, "undef")==0){
  858. 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);
  859. atual->token_annotated = "undef";
  860. }
  861. else{
  862. atual->token_annotated = aux_node2->token_annotated;
  863. }
  864. }
  865. }
  866. else if((strcmp(atual->node_type, "And")==0) || (strcmp(atual->node_type, "Or")==0)){
  867. aux_node = atual->son;
  868. while(aux_node!=NULL){
  869. create_annotated_ast(aux_node, p, f);
  870. aux_node = aux_node->brother;
  871. }
  872.  
  873. aux_node2 = atual->son;
  874. aux_node3 = aux_node2->brother;
  875. if(strcmp(aux_node2->token_annotated, "bool")!=0 && strcmp(aux_node3->token_annotated, "bool")!=0){
  876. if((strcmp(atual->node_type, "And")==0)){
  877. flag_error_c = 1;
  878. 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);
  879. }
  880. else if((strcmp(atual->node_type, "Or")==0)){
  881. flag_error_c = 1;
  882. 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);
  883. }
  884. }
  885.  
  886. atual->token_annotated = "bool";
  887. }
  888. else if((strcmp(atual->node_type, "Lt")==0) || (strcmp(atual->node_type, "Gt")==0) || (strcmp(atual->node_type, "Eq")==0) ||
  889. (strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Le")==0) || (strcmp(atual->node_type, "Ge")==0)){
  890. aux_node = atual->son;
  891. while(aux_node!=NULL){
  892. create_annotated_ast(aux_node, p, f);
  893. aux_node = aux_node->brother;
  894. }
  895.  
  896. aux_node2 = atual->son;
  897. aux_node3 = aux_node2->brother;
  898.  
  899. if((strcmp(atual->node_type, "Lt")==0)){
  900. aux = "<";
  901. }
  902. else if(strcmp(atual->node_type, "Gt")==0){
  903. aux = ">";
  904. }
  905. else if(strcmp(atual->node_type, "Eq")==0){
  906. aux = "==";
  907. }
  908. else if(strcmp(atual->node_type, "Ne")==0){
  909. aux = "!";
  910. }
  911. else if(strcmp(atual->node_type, "Le")==0){
  912. aux = "<=";
  913. }
  914. else if(strcmp(atual->node_type, "Ge")==0){
  915. aux = ">=";
  916. }
  917.  
  918. atual->token_annotated = "bool";
  919. if(strcmp(aux_node2->token_annotated, aux_node3->token_annotated)!=0){
  920. flag_error_c = 1;
  921. 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);
  922. }
  923. else{
  924. if(strcmp(atual->node_type, "Ne")==0){
  925. if(strcmp(aux_node2->token_annotated, "bool")==0){
  926. return;
  927. }
  928. }
  929. else if((strcmp(atual->node_type, "Eq")==0)){
  930. return;
  931. }
  932. else{
  933. if((strcmp(aux_node2->token_annotated, "bool")==0)){
  934. flag_error_c = 1;
  935. 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);
  936. }
  937. else{
  938. return;
  939. }
  940. }
  941. }
  942.  
  943. }
  944. 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)){
  945. aux_node = atual->son;
  946. while(aux_node!=NULL){
  947. create_annotated_ast(aux_node, p, f);
  948. aux_node = aux_node->brother;
  949. }
  950.  
  951. aux_node2 = atual->son;
  952. aux_node3 = aux_node2->brother;
  953. if(strcmp(atual->node_type, "Add")==0){
  954. aux = "+";
  955. }
  956. else if(strcmp(atual->node_type, "Sub")==0){
  957. aux = "-";
  958. }
  959. else if(strcmp(atual->node_type, "Mul")==0){
  960. aux = "*";
  961. }
  962. else if(strcmp(atual->node_type, "Div")==0){
  963. aux = "/";
  964. }
  965. else if(strcmp(atual->node_type, "Mod")==0){
  966. aux = "%";
  967. }
  968. 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){
  969. //diferentes
  970. flag_error_c = 1;
  971. 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);
  972. atual->token_annotated = "undef";
  973. }
  974. else{
  975. //string e nao add
  976. if((strcmp(aux_node2->token_annotated, "string")==0) && (strcmp(atual->node_type, "Add")!=0)){
  977. flag_error_c = 1;
  978. 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);
  979. atual->token_annotated = "undef";
  980. }
  981. //string e add
  982. else if((strcmp(aux_node2->token_annotated, "string")==0) && (strcmp(atual->node_type, "Add")==0)){
  983. atual->token_annotated = "string";
  984. }
  985. //float e mod
  986. else if((strcmp(aux_node2->token_annotated, "float32")==0) && (strcmp(atual->node_type, "Mod")==0)){
  987. flag_error_c = 1;
  988. 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);
  989. atual->token_annotated = "undef";
  990. }
  991. //float e nao mod
  992. if((strcmp(aux_node2->token_annotated, "float32")==0) && (strcmp(atual->node_type, "Mod")!=0)){
  993. atual->token_annotated = "float32";
  994. }
  995. //int
  996. else if(strcmp(aux_node2->token_annotated, "int")==0){
  997. atual->token_annotated = "int";
  998. }
  999. }
  1000. }
  1001. else if((strcmp(atual->node_type, "Plus")==0) || (strcmp(atual->node_type, "Minus")==0)){
  1002. aux_node = atual->son;
  1003. while(aux_node!=NULL){
  1004. create_annotated_ast(aux_node, p, f);
  1005. aux_node = aux_node->brother;
  1006. }
  1007.  
  1008. aux_node2 = atual->son;
  1009.  
  1010. if(strcmp(aux_node2->token_annotated, "int")==0 || strcmp(aux_node2->token_annotated, "float32")==0){
  1011. atual->token_annotated = aux_node2->token_annotated;
  1012. }
  1013. else{
  1014. atual->token_annotated = "undef";
  1015. if(strcmp(atual->node_type, "Plus")==0){
  1016. flag_error_c = 1;
  1017. printf("Line %d, column %d: Operator + cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
  1018. }
  1019. else if(strcmp(atual->node_type, "Minus")==0){
  1020. flag_error_c = 1;
  1021. printf("Line %d, column %d: Operator - cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
  1022. }
  1023. }
  1024. }
  1025. else if((strcmp(atual->node_type, "Not")==0)){
  1026. aux_node = atual->son;
  1027. while(aux_node!=NULL){
  1028. create_annotated_ast(aux_node, p, f);
  1029. aux_node = aux_node->brother;
  1030. }
  1031.  
  1032. aux_node2 = atual->son;
  1033. if(strcmp(aux_node2->token_annotated, "bool")!=0){
  1034. flag_error_c = 1;
  1035. printf("Line %d, colunm %d: Operator ! cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
  1036. }
  1037.  
  1038. atual->token_annotated = "bool";
  1039. }
  1040. }
  1041.  
  1042.  
  1043. void print_ast_annotated(node *current, int n){
  1044. if(current == NULL){
  1045. return;
  1046. }
  1047. if(strcmp(current->node_type, "NULL") == 0){
  1048. print_ast_annotated(current->brother, n);
  1049. return;
  1050. }
  1051.  
  1052. int i, is_break=0;
  1053. _var *vars_list= global_table->vars_list;
  1054.  
  1055. if(strcmp(current->node_type, "NULL") != 0){
  1056. for(i=0;i<n;i++){
  1057. printf("..");
  1058. }
  1059.  
  1060. if(current->token != NULL){
  1061. while(vars_list!=NULL){
  1062. if(vars_list->isfunction==1 && strcmp(current->token, vars_list->id)==0 && current->token_annotated!=NULL){ //Funcao
  1063. printf("%s(%s) - (",current->node_type, current->token);
  1064. _param *param_list= vars_list->params;
  1065. while(param_list!=NULL){
  1066. if(param_list->isparam==1){
  1067. if(param_list->next==NULL || param_list->next->isparam==0)
  1068. printf("%s", param_list->type);
  1069. else
  1070. printf("%s,", param_list->type);
  1071. }
  1072. param_list= param_list->next;
  1073. }
  1074. printf(")\n");
  1075. is_break = 1;
  1076. break;
  1077. }
  1078. else if(vars_list->isfunction==0 && strcmp(current->token, vars_list->id)==0 && current->token_annotated!=NULL){ //variavel
  1079. printf("%s(%s) - %s\n",current->node_type, current->token, current->token_annotated);
  1080. is_break = 1;
  1081. break;
  1082.  
  1083. }
  1084. vars_list=vars_list->next;
  1085. }
  1086. if(current->token_annotated!=NULL && !is_break){
  1087. printf("%s(%s) - %s\n",current->node_type, current->token, current->token_annotated);
  1088. }
  1089. else if(!is_break){
  1090. printf("%s(%s)\n",current->node_type, current->token);
  1091. }
  1092.  
  1093. }
  1094.  
  1095. else{
  1096. if(current->token_annotated!=NULL){
  1097. printf("%s - %s\n",current->node_type, current->token_annotated);
  1098. }
  1099. else{
  1100. printf("%s\n",current->node_type);
  1101. }
  1102. }
  1103. }
  1104.  
  1105. print_ast_annotated(current->son, n+1);
  1106. print_ast_annotated(current->brother, n);
  1107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement