void expr_resolve( struct expr *e ) { if (!e) return; expr_resolve(e->left); expr_resolve(e->right); if (e->kind == EXPR_NAME) { struct symbol *s = scope_lookup(e->name); if (s) { e->symbol = s; printf("%s resovles to ", e->name ); symbol_print( s ); printf("\n"); } else { printf("variable %s not defined\n", e->name); exit(1); } } if (e->kind == EXPR_FUNCTION_VAL) { struct symbol *s = scope_lookup(e->name); if (s) { e->symbol = s; printf("%s resovles to ", e->name ); symbol_print( s ); printf("\n"); } else { printf("variable function %s not defined\n", e->name); exit(1); } } expr_resolve(e->next); }
//Resolves the name of an expression void expr_resolve(struct expr *e){ if(!e) return; expr_resolve(e->left); expr_resolve(e->right); if(e->kind == EXPR_IDENT) { e->symbol = scope_lookup(e->name); if(e->symbol) { printf("%s resolves to "); symbol_print(e->symbol); } else { //not defined in this scope e->symbol = 0; resolve_error(e->name); } } }
void expr_resolve (struct expr *e, int quiet) { if(!e) return; expr_resolve(e->left, quiet); expr_resolve(e->right, quiet); if(e->kind == EXPR_NAME) { struct symbol *s = scope_lookup(e->name); if(s) { e->symbol = s; if(!quiet) printf("Found use of symbol %s\n", e->name); } else { if(!quiet) printf("Unfound identifier: %s\n", e->name); exit(1); } } else if(e->kind == EXPR_STRING) { push_string_front(e->string_literal); } }
void decl_resolve( struct decl *d, symbol_t sym_type ) { if (!d) return; if (scope_lookup_local(d->name)) { printf("resolve error: %s is already declared\n", d->name); exit(1); } struct symbol *sym = symbol_create(sym_type, d->type, d->name); scope_bind(d->name, sym); // resolve type num_subtype for arrays struct type *temp_t = d->type; while (temp_t) { expr_resolve(temp_t->num_subtype); temp_t = temp_t->subtype; } if (d->code) { scope_enter(); if (d->type->params) { sym->params = d->type->params; param_list_resolve(d->type->params); } stmt_resolve(d->code); scope_exit(); } else if (d->value) { expr_resolve(d->value); } // function prototype if ((!d->code) && d->type->params ) { scope_enter(); sym->params = d->type->params; param_list_resolve(d->type->params); scope_exit(); } if (d->next) decl_resolve(d->next, sym_type); }
void expr_resolve(struct expr *e) { if (!e) return; expr_resolve(e->left); expr_resolve(e->right); if (e->kind == EXPR_VARIABLE || e->kind == EXPR_FUNCTION_CALL) { struct symbol *s = scope_lookup(e->name); if (s) { e->symbol = s; printf("%s resolves to %s %d\n", e->name, symbol_kind_to_string(e->symbol->kind), e->symbol->which); } else { printf("variable %s not found\n", e->name); error_count++; } expr_resolve(e->right); } }
void expr_resolve(struct expr *e){ if (e == NULL) { return; } expr_resolve(e->left); expr_resolve(e->right); if (e->kind==EXPR_IDENTIFIER){ struct symbol *s = scope_lookup(e->name); if (s != NULL){ e->symbol = s; char *this_variable = malloc(sizeof(char) * 10); switch (s->kind){ case SYMBOL_LOCAL: sprintf(this_variable,"LOCAL %i",s->which); break; case SYMBOL_GLOBAL: sprintf(this_variable,"GLOBAL %i",s->which); break; case SYMBOL_PARAM: sprintf(this_variable,"PARAM %i",s->which); break; default: fprintf(stderr,"Scope not recognized for symbol: %s\n",e->name); error_counter += 1; break; } printf("%s resolved to %s\n",e->name,this_variable); } else { fprintf(stderr,"Symbol %s not declared\n",e->name); error_counter += 1; } } expr_resolve(e->next); }
void decl_resolve(struct decl* d,int x) { if(!d)return; if(scope_lookup_local(d->name)!=NULL && d->type->kind!=TYPE_FUNCTION) { if(x == 1)printf("Resolve error: %s already declared\n",d->name); get_incrementError(1); } else{ if(scope_level() == 1){ struct symbol *sym = symbol_create(SYMBOL_GLOBAL,d->type,d->name,0,0); d->symbol = sym; scope_bind(d->name,sym); } else { params_local_varibles++; local_variables_cont++; struct symbol *sym = symbol_create(SYMBOL_LOCAL,d->type,d->name,0,0); d->symbol = sym; scope_bind(d->name,sym); } expr_resolve(d->value,x); if(d->type->kind == TYPE_FUNCTION){ param_list_resolve(d->type->params,0); if(!parametro)parametro = hash_table_create(0,0); hash_table_insert(parametro,d->name,d->type); } if(d->code){ scope_enter(); stmt_resolve(d->code,x); d->symbol->total_locals = local_variables_cont; local_variables_cont = 0; params_local_varibles = 0; scope_leave(); } if(d->next){ decl_resolve(d->next,x); } } }
void stmt_resolve(struct stmt *s, int *which) { if(!s) return; switch(s -> kind) { case STMT_DECL: decl_resolve(s -> decl, SYMBOL_LOCAL, which); (*which)++; break; case STMT_EXPR: expr_resolve(s -> expr); break; case STMT_IF_ELSE: expr_resolve(s -> expr); stmt_resolve(s -> body, which); stmt_resolve(s -> else_body, which); break; case STMT_FOR: expr_resolve(s -> init_expr); expr_resolve(s -> expr); expr_resolve(s -> next_expr); stmt_resolve(s -> body, which); break; case STMT_WHILE: break; case STMT_PRINT: expr_resolve(s -> expr); break; case STMT_RET: expr_resolve(s -> expr); break; case STMT_BLOCK: scope_enter(); // a new block does not equal a new stack frame // (so should not have new which value of 1) stmt_resolve(s -> body, which); scope_exit(); break; } stmt_resolve(s -> next, which); }
void stmt_resolve(struct stmt *s) { if (!s) { return; } switch (s->kind) { case STMT_DECL: decl_resolve(s->decl); break; case STMT_IF_ELSE: expr_resolve(s->expr); stmt_resolve(s->body); stmt_resolve(s->else_body); break; case STMT_BLOCK: scope_enter(); stmt_resolve(s->body); scope_exit(); break; case STMT_EXPR: expr_resolve(s->expr); break; case STMT_FOR: expr_resolve(s->init_expr); expr_resolve(s->expr); expr_resolve(s->next_expr); stmt_resolve(s->body); break; case STMT_PRINT: expr_resolve(s->expr); break; case STMT_RETURN: expr_resolve(s->expr); break; case STMT_EMPTY: break; } stmt_resolve(s->next); }