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/////////////////////////
- s_token* create_token(char *tok, int line, int col){ //esta comentado pq so interessa para a parte dos erros
- s_token* new = (s_token*)malloc(sizeof(s_token));
- if(tok != NULL){
- new->token = (char*)strdup(tok);
- }
- 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->token_annotated = 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
- add_to_global(new_function);
- 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;
- }
- }
- }
- else{
- //printf("begin tree --> %s\n", aux1->node_type);
- create_annotated_ast(aux1, new_function->params);
- //printf("end tree\n");
- }
- aux1=aux1->brother;
- }
- }
- //adiciona função
- //add_to_global(new_function);
- }
- }
- }
- aux_brother= aux_brother->brother;
- }
- }
- }
- else{
- return;
- }
- }
- ////////////////////ANNOTATED AST/////////////////////////
- char *variable_type_local(_param *p_list, char* id){
- if(p_list==NULL)
- return NULL;
- _param *atual = p_list;
- while(atual != NULL){
- if(strcmp(atual->id, id) == 0){
- return atual->type;
- }
- atual = atual->next;
- }
- return NULL;
- }
- char *variable_type_global(_var *vars_list, char* id){
- if(vars_list==NULL){
- printf("starts at nulll\n");
- return NULL;
- }
- _var *atual = vars_list;
- while(atual != NULL){
- if(strcmp(atual->id, id) == 0){
- return atual->type;
- }
- atual = atual->next;
- }
- printf("ends at nulll\n");
- return NULL;
- }
- char *variable_type_final(_param *p_list, char* id){
- char *aux = variable_type_local(p_list, id);
- if(aux!=NULL){
- return aux;
- }
- aux = variable_type_global(global_table->vars_list, id);
- if(aux!=NULL){
- return aux;
- }
- return NULL;
- }
- void create_annotated_ast(node *atual, _param *p){
- char *aux;
- node *aux_node, *aux_node2, *aux_node3;
- if(atual == NULL){
- return;
- }
- if(strcmp(atual->node_type, "NULL")==0){
- return;
- }
- else if(strcmp(atual->node_type, "Id")==0){
- aux = variable_type_final(p, atual->token);
- if(aux == NULL){
- printf("Line %d, column %d: Cannot find symbol %s\n", atual->line, atual->col, atual->token);
- //pseudo-tipo undef a quaisquer símbolos desconhecidos
- atual->token_annotated = "undef";
- }
- else{
- atual->token_annotated = aux;
- }
- }
- else if(strcmp(atual->node_type, "StrLit")==0){
- atual->token_annotated = "String";
- }
- else if(strcmp(atual->node_type, "IntLit")==0){
- char *number = atual->token;
- int i=0;
- while(number[i]!='\0'){
- if(number[i] >= '7'){
- printf("Line %d, column %d: Invalid octal constant: %s\n", atual->line, atual->col, atual->token);
- }
- i++;
- }
- atual->token_annotated = "int";
- }
- else if(strcmp(atual->node_type, "RealLit")==0){
- atual->token_annotated = "float32";
- }
- else if(strcmp(atual->node_type, "If")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- if(strcmp(aux_node2->token_annotated, "bool")!=0){
- printf("Line %d, column %d: Incompatible type %s in if statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
- }
- }
- else if(strcmp(atual->node_type, "For")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- if(strcmp(aux_node2->token_annotated, "bool")!=0){
- printf("Line %d, column %d: Incompatible type %s in for statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
- atual->token_annotated = "undef";
- }
- else{
- atual->token_annotated = "bool";
- }
- }
- else if(strcmp(atual->node_type, "Block")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////
- else if(strcmp(atual->node_type, "Return")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- //INT AND DOUBLE??????
- aux_node2 = atual->son;
- if(aux_node2!=NULL){
- if(strcmp(p->type, "none")==0){
- printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
- }
- else if(strcmp(p->type, aux_node2->token_annotated)==0){
- return;
- }
- else{
- printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
- }
- }
- else if(aux_node2==NULL && (strcmp(p->type, "none")!=0)){
- printf("Line %d, column %d: Incompatible type %s in return statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
- else if(strcmp(atual->node_type, "Print")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- if(strcmp(aux_node2->token_annotated, "undef")==0){
- printf("Line %d, column %d: Incompatible type %s in fmt.Println statement\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////FAZER DO 0 SEGMENTATION FAULT
- else if(strcmp(atual->node_type, "Call")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////DÚVIDA
- else if(strcmp(atual->node_type, "ParseArgs")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- /*aux_node2 = atual->son;
- aux_node3 = aux_node2->brother;*/
- /*if(){
- 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);
- }*/
- atual->token_annotated = "int";
- }
- else if(strcmp(atual->node_type, "Assign")==0){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- aux_node3 = aux_node2->brother;
- if((strcmp(aux_node2->token_annotated, "int")==0 && strcmp(aux_node3->token_annotated, "float32")==0) || strcmp(aux_node2->token_annotated, aux_node3->token_annotated)==0){
- return;
- }
- else{
- printf("Line %d, column %d: Operator = cannot be applied to types %s, %s\n", aux_node2->line, aux_node2->col, aux_node2->token_annotated, aux_node3->token_annotated);
- }
- }
- else if((strcmp(atual->node_type, "And")==0) || (strcmp(atual->node_type, "Or")==0)){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- aux_node3 = aux_node2->brother;
- if(strcmp(aux_node2->token_annotated, aux_node2->token_annotated)!=0){
- if((strcmp(atual->node_type, "And")==0)){
- 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);
- }
- if((strcmp(atual->node_type, "Or")==0)){
- 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);
- }
- }
- atual->token_annotated = "bool";
- }
- else if((strcmp(atual->node_type, "Lt")==0) || (strcmp(atual->node_type, "Gt")==0) || (strcmp(atual->node_type, "Eq")==0) ||
- (strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Le")==0) || (strcmp(atual->node_type, "Ge")==0)){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- atual->token_annotated = "bool";
- aux_node2 = atual->son;
- aux_node3 = aux_node2->brother;
- if((strcmp(atual->node_type, "Lt")==0)){
- aux = "<";
- }
- else if(strcmp(atual->node_type, "Gt")==0){
- aux = ">";
- }
- else if(strcmp(atual->node_type, "Eq")==0){
- aux = "==";
- }
- else if(strcmp(atual->node_type, "Ne")==0){
- aux = "!";
- }
- else if(strcmp(atual->node_type, "Le")==0){
- aux = "<=";
- }
- else if(strcmp(atual->node_type, "Ge")==0){
- aux = ">=";
- }
- if((strcmp(atual->node_type, "Ne")==0) || (strcmp(atual->node_type, "Eq")==0)){
- if((strcmp(aux_node2->token_annotated, "bool")==0) && (strcmp(aux_node3->token_annotated, "bool")==0)){
- return;
- }
- }
- else if((strcmp(aux_node2->token_annotated, aux_node3->token_annotated)!=0) || ((strcmp(aux_node2->token_annotated, "bool")==0) && (strcmp(aux_node3->token_annotated, "bool")==0))){
- 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);
- }
- }
- 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)){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- aux_node3 = aux_node2->brother;
- if((strcmp(atual->node_type, "Add")==0)){
- aux = "+";
- }
- else if(strcmp(atual->node_type, "Sub")==0){
- aux = "-";
- }
- else if(strcmp(atual->node_type, "Mul")==0){
- aux = "*";
- }
- else if(strcmp(atual->node_type, "Div")==0){
- aux = "/";
- }
- else if(strcmp(atual->node_type, "Mod")==0){
- aux = "%";
- }
- if(strcmp(aux_node2->token_annotated, "int")==0){
- if(strcmp(aux_node3->token_annotated, "int")==0){
- atual->token_annotated = "int";
- }
- else if(strcmp(aux_node3->token_annotated, "float32")==0){
- atual->token_annotated = "float32";
- }
- else{
- 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);
- atual->token_annotated = "undef";
- }
- }
- if(strcmp(aux_node2->token_annotated, "float32")==0){
- if((strcmp(aux_node3->token_annotated, "int")==0) || (strcmp(aux_node3->token_annotated, "float32")==0)){
- atual->token_annotated = "float32";
- }
- else{
- 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);
- atual->token_annotated = "undef";
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////DÚVIDA SEGMENTATION FAULT
- else if((strcmp(atual->node_type, "Plus")==0) || (strcmp(atual->node_type, "Minus")==0)){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- //aux_node2 = atual?
- //??????????????????????????????????
- }
- else if((strcmp(atual->node_type, "Not")==0)){
- aux_node = atual->son;
- while(aux_node!=NULL){
- create_annotated_ast(aux_node, p);
- aux_node = aux_node->brother;
- }
- aux_node2 = atual->son;
- if(strcmp(aux_node2->token_annotated, "bool")!=0){
- printf("Line %d, colunm %d: Operator ! cannot be applied to type %s\n", atual->line, atual->col, aux_node2->token_annotated);
- }
- aux_node2->token_annotated = "bool";
- }
- //printf("----> saiu da função %s %s\n", atual->token, atual->node_type);
- }
- void print_ast_annotated(node *current, int n){
- printf("ARVORE ANOTADA\n");
- if(current == NULL){
- return;
- }
- if(strcmp(current->node_type, "NULL") == 0){
- print_ast(current->brother, n);
- return;
- }
- int i;
- _var *vars_list= global_table->vars_list;
- if(strcmp(current->node_type, "NULL") != 0){
- for(i=0;i<n;i++){
- printf("..");
- }
- if(current->token != NULL){
- while(vars_list!=NULL){
- printf("------>while\n");
- if(vars_list->isfunction==1 && strcmp(current->token, vars_list->id)==0){ //Função
- printf("%s(%s) - (",current->node_type, current->token);
- _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(")\n");
- }
- else{ //Variável
- printf("%s\t\t%s\n",vars_list->id, vars_list->type);
- }
- vars_list=vars_list->next;
- }
- if(current->token_annotated!=NULL){
- printf("------>token token_annotated\n");
- printf("%s(%s) - %s",current->node_type, current->token, current->token_annotated);
- }
- else{
- printf("------>normal\n");
- printf("%s(%s)\n",current->node_type, current->token);
- }
- }
- else{
- printf("------>normal2\n");
- printf("%s\n",current->node_type);
- }
- }
- print_ast(current->son, n+1);
- print_ast(current->brother, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement