Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.28 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. /*node* create_token(char *token, int line, int col){ //esta comentado pq so interessa para a parte dos erros
  10. token* new = (token*)malloc(sizeof(token));
  11.  
  12. if(token != NULL){
  13. new->token = (char*)strdup(token);
  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->line = line;
  38. new->col = col;
  39. return new;
  40. }
  41.  
  42. void add_brother(node *current, node *new_brother){
  43. if(current==NULL || new_brother==NULL){
  44. return;
  45. }
  46. node *aux = current;
  47. while(aux->brother != NULL){
  48. aux = aux->brother;
  49. }
  50. aux->brother = new_brother;
  51. }
  52.  
  53. void add_son(node *current, node *new_son){
  54. if(current==NULL || new_son==NULL){
  55. return;
  56. }
  57.  
  58. current->son = new_son;
  59. }
  60.  
  61. void atribute_type(node *type, node *var_d){
  62. node *aux = var_d;
  63. node *new = NULL;
  64.  
  65. while(aux != NULL){
  66. new = create_node(type->node_type, NULL, 0, 0);
  67. new->brother = aux->son;
  68. aux->son = new;
  69. aux = aux->brother;
  70. }
  71. }
  72.  
  73. int n_block(node *current) {
  74. if(current == NULL){
  75. return 0;
  76. }
  77. int counter = 0;
  78. if(current->brother != NULL){
  79. counter = 1;
  80. }
  81.  
  82. while(current->brother != NULL){
  83. if (strcmp((current->brother)->node_type, "NULL")!=0){
  84. counter++;
  85. }
  86. current = current->brother;
  87. }
  88. return counter;
  89. }
  90.  
  91. void print_ast(node *current, int n){
  92. if(current == NULL){
  93. return;
  94. }
  95. if(strcmp(current->node_type, "NULL") == 0){
  96. print_ast(current->brother, n);
  97. return;
  98. }
  99.  
  100. int i;
  101. if(strcmp(current->node_type, "NULL") != 0){
  102. for(i=0;i<n;i++){
  103. printf("..");
  104. }
  105.  
  106. if(current->token != NULL){
  107. printf("%s(%s)\n",current->node_type, current->token);
  108. }
  109. else{
  110. printf("%s\n",current->node_type);
  111. }
  112. }
  113.  
  114. print_ast(current->son, n+1);
  115. print_ast(current->brother, n);
  116. }
  117.  
  118. void clear_ast(node* current){
  119. if(current == NULL){
  120. return;
  121. }
  122.  
  123. if(current->node_type != NULL){
  124. free(current->node_type);
  125. current->node_type = NULL;
  126. }
  127. if(current->token != NULL){
  128. free(current->token);
  129. current->token = NULL;
  130. }
  131.  
  132. clear_ast(current->son);
  133. current->son = NULL;
  134. clear_ast(current->brother);
  135. current->brother = NULL;
  136.  
  137. free(current);
  138. current = NULL;
  139. }
  140.  
  141. ////////////////////SEMANTICS/////////////////////////
  142. t_symbol *global_table;
  143.  
  144. void print_global_table(){
  145. _var *vars_list= global_table->vars_list;
  146. printf("===== %s Symbol Table =====\n", global_table->name);
  147. while(vars_list!=NULL){
  148. if(vars_list->isfunction==1){ //Função
  149. printf("%s\t(", vars_list->id);
  150. _param *param_list= vars_list->params;
  151. while(param_list!=NULL){
  152. if(param_list->isparam==1){
  153. if(param_list->next==NULL || param_list->next->isparam==0)
  154. printf("%s", param_list->type);
  155. else
  156. printf("%s,", param_list->type);
  157. }
  158. param_list= param_list->next;
  159. }
  160. printf(")\t");
  161. if(vars_list->type==NULL)
  162. printf("none\n");
  163. else
  164. printf("%s\n",vars_list->type);
  165. }
  166. else{ //Variável
  167. printf("%s\t\t%s\n",vars_list->id, vars_list->type);
  168. }
  169. vars_list=vars_list->next;
  170. }
  171. printf("\n");
  172. }
  173.  
  174. void print_local_table(){
  175. _var *vars_list= global_table->vars_list;
  176. while(vars_list!=NULL){
  177. if(vars_list->isfunction==1){
  178. printf("===== Function %s(", vars_list->id);
  179. _param *param_list= vars_list->params;
  180. while(param_list!=NULL){
  181. if(param_list->isparam==1){
  182. if(param_list->next==NULL || param_list->next->isparam==0)
  183. printf("%s", param_list->type);
  184. else
  185. printf("%s,", param_list->type);
  186. }
  187. param_list= param_list->next;
  188. }
  189. printf(") Symbol Table =====\n");
  190. printf("return\t\t%s\n",vars_list->type);
  191.  
  192. param_list= vars_list->params;
  193. while(param_list!=NULL){
  194. if(param_list->isparam==1){
  195. printf("%s\t\t%s\tparam\n",param_list->id, param_list->type);
  196. }
  197. else
  198. printf("%s\t\t%s\n",param_list->id, param_list->type);
  199. param_list= param_list->next;
  200. }
  201. printf("\n");
  202. }
  203. vars_list=vars_list->next;
  204. }
  205.  
  206. }
  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->next = NULL;
  217.  
  218. return new_function;
  219. }
  220. _var* create_var(char *type, char *id){
  221. _var *new_var= (_var*)malloc(sizeof(_var));
  222. new_var->id = (char*)strdup(id);
  223. new_var->type = (char*)strdup(type);
  224. new_var->isfunction = 0;
  225. new_var->params = NULL;
  226. new_var->next = NULL;
  227.  
  228. return new_var;
  229. }
  230. _param* create_param(char *type, char *id){
  231. _param *new_param = (_param*)malloc(sizeof(_param));
  232. new_param->isparam=1;
  233. new_param->id = (char*)strdup(id);
  234. new_param->type = (char*)strdup(type);
  235. new_param->next= NULL;
  236.  
  237. return new_param;
  238. }
  239.  
  240. _param* create_local_variable(char *type, char *id){
  241. _param *new_param = (_param*)malloc(sizeof(_param));
  242. new_param->isparam=0;
  243. new_param->id = (char*)strdup(id);
  244. new_param->type = (char*)strdup(type);
  245. new_param->next= NULL;
  246.  
  247. return new_param;
  248. }
  249.  
  250. int search_var_or_function_already_exists(_var *vars_list, char* id){
  251. //0 não existe, 1 existe
  252. if(vars_list==NULL)
  253. return 0;
  254.  
  255. _var *atual = vars_list;
  256. while(atual != NULL){
  257. if(strcmp(atual->id, id) == 0){
  258. return 1;
  259. }
  260. atual = atual->next;
  261. }
  262. return 0;
  263. }
  264. char* change_type(char *type){
  265.  
  266. if(strcmp(type, "Int") == 0){
  267. type = "int";
  268. return type;
  269. }
  270. if(strcmp(type, "Float32") == 0){
  271. type = "float32";
  272. return type;
  273. }
  274. if(strcmp(type, "Bool") == 0){
  275. type = "bool";
  276. return type;
  277. }
  278. if(strcmp(type, "String") == 0){
  279. type = "string";
  280. return type;
  281. }
  282. return type;
  283. }
  284.  
  285. int search_param_already_exists(_param *params_list, char *id){
  286. if(params_list==NULL){
  287. return 0;
  288. }
  289.  
  290. _param *atual= params_list;
  291. while(atual != NULL){
  292. if(strcmp(atual->id, id) == 0){
  293. return 1;
  294. }
  295. atual = atual->next;
  296. }
  297. return 0;
  298.  
  299. }
  300.  
  301. void add_to_global(_var *new_var){
  302. if(global_table->vars_list==NULL){
  303. global_table->vars_list= new_var;
  304. return;
  305. }
  306.  
  307. _var *atual= global_table->vars_list;
  308. while(atual->next!=NULL){
  309. atual= atual->next;
  310. }
  311.  
  312. atual->next= new_var;
  313. }
  314.  
  315.  
  316. void create_table(node *node_atual){
  317. node *aux, *aux_brother;
  318.  
  319. node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6, *aux7, *aux8, *aux_funcbody;
  320.  
  321. _var *new_var, *new_function;
  322.  
  323. if(node_atual!=NULL){
  324. if(strcmp(node_atual->node_type, "Program")==0){
  325. aux = node_atual->son; //son of program is declarations
  326.  
  327. global_table = (t_symbol*)malloc(sizeof(t_symbol));
  328. global_table->name = strdup("Global");
  329. global_table->type = strdup("Global");
  330. global_table->vars_list = NULL;
  331.  
  332. aux_brother= aux->brother; //VarDecl ou FuncDecl
  333.  
  334.  
  335. while(aux_brother!=NULL){
  336. //2 brothers --> 2 ifs
  337. if(strcmp(aux_brother->node_type, "VarDecl")==0){
  338.  
  339. aux1= aux_brother->son; //Type
  340. aux2= aux1->brother; //ID
  341.  
  342.  
  343. //if varieable exists already error
  344. if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
  345. //Fica a 1 se já existir, emite erro
  346. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  347. //Não sei o que acontece a seguir
  348. }
  349. else{
  350. //Adiciona nova variável à tabela global
  351.  
  352. //Cria variável, dá-lhe tipo(node_type) e valor(token)
  353. new_var= create_var(change_type(aux1->node_type), aux2->token);
  354. add_to_global(new_var);
  355. }
  356. }
  357. if(strcmp(aux_brother->node_type, "FuncDecl")==0){
  358. aux1 = aux_brother->son; //FuncHeader
  359.  
  360. if(strcmp(aux1->node_type, "FuncHeader")==0){
  361. aux3 = aux1->son; //ID
  362. aux4 = aux3->brother; //Type ou FuncParams ou vazio
  363.  
  364. if(aux4!=NULL){
  365. if(strcmp(aux4->node_type, "FuncParams")==0){
  366. //Type é "none"
  367. new_function= create_function(aux3->token, "none");
  368. aux6= aux4->son; //ParamDecl
  369. while(aux6!=NULL){
  370. if(strcmp(aux6->node_type, "NULL")==0){}
  371. else{
  372. aux7=aux6->son; //Type
  373. aux8= aux7->brother; //ID
  374. if(search_param_already_exists(new_function->params, aux8->token)==1){
  375. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  376. }
  377. else{
  378.  
  379. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  380. if(new_function->params==NULL)
  381. new_function->params=new_param;
  382. else{
  383. _param *atual= new_function->params;
  384. while(atual->next!=NULL)
  385. atual=atual->next;
  386. atual->next=new_param;
  387. }
  388. }
  389. }
  390.  
  391. aux6=aux6->brother;
  392. }
  393.  
  394. }
  395.  
  396. else{
  397. //Type diferente de "none"
  398. aux5= aux4->brother; //FuncParams ou NULL
  399. if(strcmp(aux5->node_type, "FuncParams")==0){
  400. //Com parametros
  401. new_function= create_function(aux3->token, change_type(aux4->node_type));
  402. aux6= aux5->son; //ParamDecl
  403. while(aux6!=NULL){
  404. if(strcmp(aux6->node_type, "NULL")==0){}
  405. else{
  406. aux7=aux6->son; //Type
  407. aux8= aux7->brother; //ID
  408. if(search_param_already_exists(new_function->params, aux8->token)==1){
  409. printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
  410. }
  411. else{
  412.  
  413. _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
  414. if(new_function->params==NULL)
  415. new_function->params=new_param;
  416. else{
  417. _param *atual= new_function->params;
  418. while(atual->next!=NULL)
  419. atual=atual->next;
  420. atual->next=new_param;
  421. }
  422. }
  423. }
  424.  
  425. aux6=aux6->brother;
  426. }
  427. }
  428. else{
  429. //Sem parametros
  430. create_function(aux3->token, change_type(aux4->node_type));
  431. }
  432. }
  433. }
  434.  
  435. else{
  436. //Sem parametros e Type é "none"
  437. new_function= create_function(aux3->token, "none");
  438.  
  439. }
  440.  
  441. if(search_var_or_function_already_exists(global_table->vars_list, aux3->token)==1){
  442. printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  443. }
  444. else{
  445. //Tabela local
  446. aux_funcbody= aux1->brother; //FuncBody
  447. if(strcmp(aux_funcbody->node_type,"FuncBody")==0){
  448. aux1= aux_funcbody->son;
  449. while(aux1!=NULL){
  450. if(strcmp(aux1->node_type,"VarDecl")==0){
  451. aux2= aux1->son; //Type
  452. aux3= aux2->brother; //ID
  453.  
  454. if(search_param_already_exists(new_function->params, aux3->token)==1){
  455. //Fica a 1 se já existir, emite erro
  456. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  457. //Não sei o que acontece a seguir
  458. }
  459. else{
  460. _param *new_param = create_local_variable(change_type(aux2->node_type), aux3->token);
  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. aux1=aux1->brother;
  472. }
  473. }
  474. //adiciona função
  475. add_to_global(new_function);
  476. }
  477.  
  478.  
  479. }
  480.  
  481. }
  482.  
  483. aux_brother= aux_brother->brother;
  484.  
  485. }
  486. }
  487. }
  488. else{
  489. return;
  490. }
  491. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement