Advertisement
Guest User

Untitled

a guest
May 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 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->next==NULL)
  153. printf("%s", param_list->id);
  154. else
  155. printf("%s,", param_list->id);
  156. param_list= param_list->next;
  157. }
  158. printf(")\t");
  159. if(vars_list->type==NULL)
  160. printf("none\n");
  161. else
  162. printf("%s\n",vars_list->type);
  163. }
  164. else{ //Variável
  165. printf("%s\t\t%s\n",vars_list->id, vars_list->type);
  166. }
  167. vars_list=vars_list->next;
  168. }
  169. printf("\n");
  170. }
  171.  
  172. _var* create_function(char *id, char *type){
  173. _var *new_function= (_var*)malloc(sizeof(_var));
  174. new_function->id = (char*)strdup(id);
  175. new_function->type = (char*)strdup(type);
  176. new_function->isfunction = 1;
  177. new_function->params = NULL;
  178. new_function->next = NULL;
  179.  
  180. return new_function;
  181. }
  182. _var* create_var(char *type, char *id){
  183. _var *new_var= (_var*)malloc(sizeof(_var));
  184. new_var->id = (char*)strdup(id);
  185. new_var->type = (char*)strdup(type);
  186. new_var->isfunction = 0;
  187. new_var->params = NULL;
  188. new_var->next = NULL;
  189.  
  190. return new_var;
  191. }
  192. _param* create_param(char *type, char *id){
  193. _param *new_param = (_param*)malloc(sizeof(_param));
  194. new_param->id = (char*)strdup(id);
  195. new_param->type = (char*)strdup(type);
  196. new_param->next= NULL;
  197.  
  198. return new_param;
  199. }
  200. int search_var_or_function_already_exists(_var *vars_list, char* id){
  201. //0 não existe, 1 existe
  202. if(vars_list==NULL)
  203. return 0;
  204.  
  205. _var *atual = vars_list;
  206. while(atual != NULL){
  207. if(strcmp(atual->id, id) == 0){
  208. return 1;
  209. }
  210. atual = atual->next;
  211. }
  212. return 0;
  213. }
  214.  
  215. int search_param_already_exists(_param *params_list, char *id){
  216. if(params_list==NULL){
  217. return 0;
  218. }
  219.  
  220. _param *atual= params_list;
  221. while(atual != NULL){
  222. if(strcmp(atual->id, id) == 0){
  223. return 1;
  224. }
  225. atual = atual->next;
  226. }
  227. return 0;
  228.  
  229. }
  230. void add_param_to_function(_param *new_param, _param *list_params){
  231. if(list_params==NULL){
  232. list_params= new_param;
  233. return;
  234. }
  235.  
  236. _param *atual= list_params;
  237. while(atual->next != NULL){
  238. atual = atual->next;
  239. }
  240. atual->next= new_param;
  241.  
  242. }
  243. void add_to_global(_var *new_var){
  244. if(global_table->vars_list==NULL){
  245. global_table->vars_list= new_var;
  246. return;
  247. }
  248.  
  249. _var *atual= global_table->vars_list;
  250. while(atual->next!=NULL){
  251. atual= atual->next;
  252. }
  253.  
  254. atual->next= new_var;
  255. }
  256.  
  257. void create_table(node *node_atual){
  258. node *aux, *aux_gravar, *aux_brother;
  259.  
  260. node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6;
  261.  
  262. _var *new_var, *new_function;
  263. _param *new_param;
  264.  
  265. if(node_atual!=NULL){
  266. if(strcmp(node_atual->node_type, "Program")==0){
  267. aux = node_atual->son; //son of program is declarations
  268.  
  269. global_table = (t_symbol*)malloc(sizeof(t_symbol));
  270. global_table->name = strdup("Global");
  271. global_table->type = strdup("Global");
  272. global_table->vars_list = NULL;
  273.  
  274. aux_brother= aux->brother; //VarDecl ou FuncDecl
  275.  
  276.  
  277. while(aux_brother!=NULL){
  278. //2 brothers --> 2 ifs
  279. if(strcmp(aux_brother->node_type, "VarDecl")==0){
  280.  
  281. aux1= aux_brother->son; //Type
  282. aux2= aux1->brother; //ID
  283. aux3= aux_brother->brother; //auxVS
  284.  
  285. //if varieable exists already error
  286. if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
  287. //Fica a 1 se já existir, emite erro
  288. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  289. //Não sei o que acontece a seguir
  290. }
  291. else{
  292. //Adiciona nova variável à tabela global
  293.  
  294. //Cria variável, dá-lhe tipo(node_type) e valor(token)
  295. new_var= create_var(aux1->node_type, aux2->token);
  296. add_to_global(new_var);
  297. }
  298. if(aux3!=NULL){ //tem variáveis no auxVS
  299. aux2= aux3->son;
  300.  
  301. if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
  302. //Fica a 1 se já existir, emite erro
  303. printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
  304. //Não sei o que acontece a seguir
  305. }
  306. else{
  307. //Adiciona nova variável à tabela global
  308. //Cria variável, dá-lhe tipo(node_type) e valor(token)
  309. new_var= create_var(aux1->node_type, aux2->token);
  310. add_to_global(new_var);
  311. }
  312. aux3= aux3->brother;
  313.  
  314. }
  315. }
  316. /*
  317. if(strcmp(aux_brother->node_type, "FuncDecl")==0){
  318. aux1 = aux_brother->son; //FuncHeader ou FuncBody
  319.  
  320. if(strcmp(aux1->node_type, "FuncHeader")==0){
  321. aux3 = aux2->son; //ID
  322. aux4 = aux3->brother; //Type ou FuncParams ou vazio
  323.  
  324. if(aux4!=NULL){
  325. if(strcmp(aux4->node_type, "FuncParams")==0){
  326. //Type é "none"
  327. new_function= create_function(aux3->token, NULL);
  328. aux5= aux4->son; //ParamDecl
  329. while(aux5!=NULL){
  330. aux3=aux5->son; //Type
  331. aux4= aux3->brother; //ID
  332. if(search_param_already_exists(new_function->params, aux4->token)==1){
  333. printf("“Line %d, column %d: Symbol %s already defined\n", aux4->line, aux4->col, aux4->token);
  334. }
  335. else{
  336. _param *new_param = create_param(aux3->node_type, aux4->token);
  337. add_param_to_function(new_param, new_function->params);
  338. }
  339.  
  340. aux5=aux5->brother;
  341. }
  342.  
  343. }
  344. else{
  345. //Type diferente de "none"
  346. aux5= aux4->brother; //FuncParams ou NULL
  347. if(strcmp(aux5->node_type, "FuncParams")==0){
  348. //Com parametros
  349. new_function= create_function(aux3->token, aux4->node_type);
  350. aux6= aux5->son; //ParamDecl
  351. while(aux6!=NULL){
  352. aux3=aux6->son; //Type
  353. aux4= aux3->brother; //ID
  354. if(search_param_already_exists(new_function->params, aux4->token)==1){
  355. printf("“Line %d, column %d: Symbol %s already defined\n", aux4->line, aux4->col, aux4->token);
  356. }
  357. else{
  358. _param *new_param = create_param(aux3->node_type, aux4->token);
  359. add_param_to_function(new_param, new_function->params);
  360. }
  361.  
  362. aux6=aux6->brother;
  363. }
  364. }
  365. else{
  366. //Sem parametros
  367. create_function(aux3->token, aux4->node_type);
  368. }
  369. }
  370. }
  371. else{
  372. //Sem parametros e Type é "none"
  373. create_function(aux3->token, NULL);
  374.  
  375. }
  376.  
  377. if(search_var_or_function_already_exists(global_table->vars_list, aux3->token)==1){
  378. printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
  379. }
  380. else{
  381. //adiciona função
  382. add_to_global(new_function);
  383. }
  384.  
  385. }
  386.  
  387. }*/
  388.  
  389. aux_brother= aux_brother->brother;
  390.  
  391. }
  392. }
  393. }
  394. else{
  395. return;
  396. }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement