Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include "ast.h"
- ////////////////////AST/////////////////////////
- /*node* create_token(char *token, int line, int col){ //esta comentado pq so interessa para a parte dos erros
- token* new = (token*)malloc(sizeof(token));
- if(token != NULL){
- new->token = (char*)strdup(token);
- }
- else{
- new->token = NULL;
- }
- new->line = line;
- new->col = col;
- return new;
- }*/
- node* create_node(char* node_type, char *token, int line, int col){ //CHANGED LINE COL BC OF ERRORS
- node* new = (node*)malloc(sizeof(node));
- new->node_type = (char*)strdup(node_type);
- if(token != NULL){
- new->token = (char*)strdup(token);
- }
- else{
- new->token = NULL;
- }
- new->son = NULL;
- new->brother = NULL;
- new->line = line;
- new->col = col;
- return new;
- }
- void add_brother(node *current, node *new_brother){
- if(current==NULL || new_brother==NULL){
- return;
- }
- node *aux = current;
- while(aux->brother != NULL){
- aux = aux->brother;
- }
- aux->brother = new_brother;
- }
- void add_son(node *current, node *new_son){
- if(current==NULL || new_son==NULL){
- return;
- }
- current->son = new_son;
- }
- void atribute_type(node *type, node *var_d){
- node *aux = var_d;
- node *new = NULL;
- while(aux != NULL){
- new = create_node(type->node_type, NULL, 0, 0);
- new->brother = aux->son;
- aux->son = new;
- aux = aux->brother;
- }
- }
- int n_block(node *current) {
- if(current == NULL){
- return 0;
- }
- int counter = 0;
- if(current->brother != NULL){
- counter = 1;
- }
- while(current->brother != NULL){
- if (strcmp((current->brother)->node_type, "NULL")!=0){
- counter++;
- }
- current = current->brother;
- }
- return counter;
- }
- void print_ast(node *current, int n){
- if(current == NULL){
- return;
- }
- if(strcmp(current->node_type, "NULL") == 0){
- print_ast(current->brother, n);
- return;
- }
- int i;
- if(strcmp(current->node_type, "NULL") != 0){
- for(i=0;i<n;i++){
- printf("..");
- }
- if(current->token != NULL){
- printf("%s(%s)\n",current->node_type, current->token);
- }
- else{
- printf("%s\n",current->node_type);
- }
- }
- print_ast(current->son, n+1);
- print_ast(current->brother, n);
- }
- void clear_ast(node* current){
- if(current == NULL){
- return;
- }
- if(current->node_type != NULL){
- free(current->node_type);
- current->node_type = NULL;
- }
- if(current->token != NULL){
- free(current->token);
- current->token = NULL;
- }
- clear_ast(current->son);
- current->son = NULL;
- clear_ast(current->brother);
- current->brother = NULL;
- free(current);
- current = NULL;
- }
- ////////////////////SEMANTICS/////////////////////////
- t_symbol *global_table;
- void print_global_table(){
- _var *vars_list= global_table->vars_list;
- printf("===== %s Symbol Table =====\n", global_table->name);
- while(vars_list!=NULL){
- if(vars_list->isfunction==1){ //Função
- printf("%s\t(", vars_list->id);
- _param *param_list= vars_list->params;
- while(param_list!=NULL){
- if(param_list->next==NULL)
- printf("%s", param_list->id);
- else
- printf("%s,", param_list->id);
- param_list= param_list->next;
- }
- printf(")\t");
- if(vars_list->type==NULL)
- printf("none\n");
- else
- printf("%s\n",vars_list->type);
- }
- else{ //Variável
- printf("%s\t\t%s\n",vars_list->id, vars_list->type);
- }
- vars_list=vars_list->next;
- }
- printf("\n");
- }
- _var* create_function(char *id, char *type){
- _var *new_function= (_var*)malloc(sizeof(_var));
- new_function->id = (char*)strdup(id);
- new_function->type = (char*)strdup(type);
- new_function->isfunction = 1;
- new_function->params = NULL;
- new_function->next = NULL;
- return new_function;
- }
- _var* create_var(char *type, char *id){
- _var *new_var= (_var*)malloc(sizeof(_var));
- new_var->id = (char*)strdup(id);
- new_var->type = (char*)strdup(type);
- new_var->isfunction = 0;
- new_var->params = NULL;
- new_var->next = NULL;
- return new_var;
- }
- _param* create_param(char *type, char *id){
- _param *new_param = (_param*)malloc(sizeof(_param));
- new_param->id = (char*)strdup(id);
- new_param->type = (char*)strdup(type);
- new_param->next= NULL;
- return new_param;
- }
- int search_var_or_function_already_exists(_var *vars_list, char* id){
- //0 não existe, 1 existe
- if(vars_list==NULL)
- return 0;
- _var *atual = vars_list;
- while(atual != NULL){
- if(strcmp(atual->id, id) == 0){
- return 1;
- }
- atual = atual->next;
- }
- return 0;
- }
- int search_param_already_exists(_param *params_list, char *id){
- if(params_list==NULL){
- return 0;
- }
- _param *atual= params_list;
- while(atual != NULL){
- if(strcmp(atual->id, id) == 0){
- return 1;
- }
- atual = atual->next;
- }
- return 0;
- }
- void add_param_to_function(_param *new_param, _param *list_params){
- if(list_params==NULL){
- list_params= new_param;
- return;
- }
- _param *atual= list_params;
- while(atual->next != NULL){
- atual = atual->next;
- }
- atual->next= new_param;
- }
- void add_to_global(_var *new_var){
- if(global_table->vars_list==NULL){
- global_table->vars_list= new_var;
- return;
- }
- _var *atual= global_table->vars_list;
- while(atual->next!=NULL){
- atual= atual->next;
- }
- atual->next= new_var;
- }
- void create_table(node *node_atual){
- node *aux, *aux_gravar, *aux_brother;
- node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6;
- _var *new_var, *new_function;
- _param *new_param;
- if(node_atual!=NULL){
- if(strcmp(node_atual->node_type, "Program")==0){
- aux = node_atual->son; //son of program is declarations
- global_table = (t_symbol*)malloc(sizeof(t_symbol));
- global_table->name = strdup("Global");
- global_table->type = strdup("Global");
- global_table->vars_list = NULL;
- aux_brother= aux->brother; //VarDecl ou FuncDecl
- while(aux_brother!=NULL){
- //2 brothers --> 2 ifs
- if(strcmp(aux_brother->node_type, "VarDecl")==0){
- aux1= aux_brother->son; //Type
- aux2= aux1->brother; //ID
- aux3= aux_brother->brother; //auxVS
- //if varieable exists already error
- if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
- //Fica a 1 se já existir, emite erro
- printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
- //Não sei o que acontece a seguir
- }
- else{
- //Adiciona nova variável à tabela global
- //Cria variável, dá-lhe tipo(node_type) e valor(token)
- new_var= create_var(aux1->node_type, aux2->token);
- add_to_global(new_var);
- }
- if(aux3!=NULL){ //tem variáveis no auxVS
- aux2= aux3->son;
- if(search_var_or_function_already_exists(global_table->vars_list, aux2->token)==1){
- //Fica a 1 se já existir, emite erro
- printf("“Line %d, column %d: Symbol %s already defined\n", aux2->line, aux2->col, aux2->token);
- //Não sei o que acontece a seguir
- }
- else{
- //Adiciona nova variável à tabela global
- //Cria variável, dá-lhe tipo(node_type) e valor(token)
- new_var= create_var(aux1->node_type, aux2->token);
- add_to_global(new_var);
- }
- aux3= aux3->brother;
- }
- }
- /*
- if(strcmp(aux_brother->node_type, "FuncDecl")==0){
- aux1 = aux_brother->son; //FuncHeader ou FuncBody
- if(strcmp(aux1->node_type, "FuncHeader")==0){
- aux3 = aux2->son; //ID
- aux4 = aux3->brother; //Type ou FuncParams ou vazio
- if(aux4!=NULL){
- if(strcmp(aux4->node_type, "FuncParams")==0){
- //Type é "none"
- new_function= create_function(aux3->token, NULL);
- aux5= aux4->son; //ParamDecl
- while(aux5!=NULL){
- aux3=aux5->son; //Type
- aux4= aux3->brother; //ID
- if(search_param_already_exists(new_function->params, aux4->token)==1){
- printf("“Line %d, column %d: Symbol %s already defined\n", aux4->line, aux4->col, aux4->token);
- }
- else{
- _param *new_param = create_param(aux3->node_type, aux4->token);
- add_param_to_function(new_param, new_function->params);
- }
- aux5=aux5->brother;
- }
- }
- else{
- //Type diferente de "none"
- aux5= aux4->brother; //FuncParams ou NULL
- if(strcmp(aux5->node_type, "FuncParams")==0){
- //Com parametros
- new_function= create_function(aux3->token, aux4->node_type);
- aux6= aux5->son; //ParamDecl
- while(aux6!=NULL){
- aux3=aux6->son; //Type
- aux4= aux3->brother; //ID
- if(search_param_already_exists(new_function->params, aux4->token)==1){
- printf("“Line %d, column %d: Symbol %s already defined\n", aux4->line, aux4->col, aux4->token);
- }
- else{
- _param *new_param = create_param(aux3->node_type, aux4->token);
- add_param_to_function(new_param, new_function->params);
- }
- aux6=aux6->brother;
- }
- }
- else{
- //Sem parametros
- create_function(aux3->token, aux4->node_type);
- }
- }
- }
- else{
- //Sem parametros e Type é "none"
- create_function(aux3->token, NULL);
- }
- if(search_var_or_function_already_exists(global_table->vars_list, aux3->token)==1){
- printf("“Line %d, column %d: Symbol %s already defined\n", aux3->line, aux3->col, aux3->token);
- }
- else{
- //adiciona função
- add_to_global(new_function);
- }
- }
- }*/
- aux_brother= aux_brother->brother;
- }
- }
- }
- else{
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement