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->isparam==1){
- if(param_list->next==NULL || param_list->next->isparam==0)
- printf("%s", param_list->type);
- else
- printf("%s,", param_list->type);
- }
- 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");
- }
- void print_local_table(){
- _var *vars_list= global_table->vars_list;
- while(vars_list!=NULL){
- if(vars_list->isfunction==1){
- printf("===== Function %s(", vars_list->id);
- _param *param_list= vars_list->params;
- while(param_list!=NULL){
- if(param_list->isparam==1){
- if(param_list->next==NULL || param_list->next->isparam==0)
- printf("%s", param_list->type);
- else
- printf("%s,", param_list->type);
- }
- param_list= param_list->next;
- }
- printf(") Symbol Table =====\n");
- printf("return\t\t%s\n",vars_list->type);
- param_list= vars_list->params;
- while(param_list!=NULL){
- if(param_list->isparam==1){
- printf("%s\t\t%s\tparam\n",param_list->id, param_list->type);
- }
- else
- printf("%s\t\t%s\n",param_list->id, param_list->type);
- param_list= param_list->next;
- }
- printf("\n");
- }
- vars_list=vars_list->next;
- }
- }
- _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->isparam=1;
- new_param->id = (char*)strdup(id);
- new_param->type = (char*)strdup(type);
- new_param->next= NULL;
- return new_param;
- }
- _param* create_local_variable(char *type, char *id){
- _param *new_param = (_param*)malloc(sizeof(_param));
- new_param->isparam=0;
- 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;
- }
- char* change_type(char *type){
- if(strcmp(type, "Int") == 0){
- type = "int";
- return type;
- }
- if(strcmp(type, "Float32") == 0){
- type = "float32";
- return type;
- }
- if(strcmp(type, "Bool") == 0){
- type = "bool";
- return type;
- }
- if(strcmp(type, "String") == 0){
- type = "string";
- return type;
- }
- return type;
- }
- 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_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_brother;
- node *aux1, *aux2, *aux3, *aux4, *aux5, *aux6, *aux7, *aux8, *aux_funcbody;
- _var *new_var, *new_function;
- 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
- //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(change_type(aux1->node_type), aux2->token);
- add_to_global(new_var);
- }
- }
- if(strcmp(aux_brother->node_type, "FuncDecl")==0){
- aux1 = aux_brother->son; //FuncHeader
- if(strcmp(aux1->node_type, "FuncHeader")==0){
- aux3 = aux1->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, "none");
- aux6= aux4->son; //ParamDecl
- while(aux6!=NULL){
- if(strcmp(aux6->node_type, "NULL")==0){}
- else{
- aux7=aux6->son; //Type
- aux8= aux7->brother; //ID
- if(search_param_already_exists(new_function->params, aux8->token)==1){
- printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
- }
- else{
- _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
- if(new_function->params==NULL)
- new_function->params=new_param;
- else{
- _param *atual= new_function->params;
- while(atual->next!=NULL)
- atual=atual->next;
- atual->next=new_param;
- }
- }
- }
- aux6=aux6->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, change_type(aux4->node_type));
- aux6= aux5->son; //ParamDecl
- while(aux6!=NULL){
- if(strcmp(aux6->node_type, "NULL")==0){}
- else{
- aux7=aux6->son; //Type
- aux8= aux7->brother; //ID
- if(search_param_already_exists(new_function->params, aux8->token)==1){
- printf("“Line %d, column %d: Symbol %s already defined\n", aux8->line, aux8->col, aux8->token);
- }
- else{
- _param *new_param = create_param(change_type(aux7->node_type), aux8->token);
- if(new_function->params==NULL)
- new_function->params=new_param;
- else{
- _param *atual= new_function->params;
- while(atual->next!=NULL)
- atual=atual->next;
- atual->next=new_param;
- }
- }
- }
- aux6=aux6->brother;
- }
- }
- else{
- //Sem parametros
- create_function(aux3->token, change_type(aux4->node_type));
- }
- }
- }
- else{
- //Sem parametros e Type é "none"
- new_function= create_function(aux3->token, "none");
- }
- 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{
- //Tabela local
- aux_funcbody= aux1->brother; //FuncBody
- if(strcmp(aux_funcbody->node_type,"FuncBody")==0){
- aux1= aux_funcbody->son;
- while(aux1!=NULL){
- if(strcmp(aux1->node_type,"VarDecl")==0){
- aux2= aux1->son; //Type
- aux3= aux2->brother; //ID
- if(search_param_already_exists(new_function->params, aux3->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{
- _param *new_param = create_local_variable(change_type(aux2->node_type), aux3->token);
- if(new_function->params==NULL)
- new_function->params=new_param;
- else{
- _param *atual= new_function->params;
- while(atual->next!=NULL)
- atual=atual->next;
- atual->next=new_param;
- }
- }
- }
- aux1=aux1->brother;
- }
- }
- //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