Advertisement
Guest User

Untitled

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