Exemple #1
0
// when we see {, create a new hash_table and connect
void scope_enter(){
	int * params = malloc(sizeof(int));
	int * locals = malloc(sizeof(int));
	*params = 0;
	*locals = 0;
	struct hash_table *h_prev;  // keep track of prev
	h->next = hash_table_create(0,0);  // create new hash table
	h_prev = h;  
	h = h->next;
	h->prev = h_prev;
	if(scope_level() == 2){
		hash_table_insert(h,"0params", params);  // insert params and locals counter
		hash_table_insert(h,"0locals", locals);
	}
}
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);
		}
	}	
}