/*
 *	update a value of an identifier in symbol table
		$$.tuple = symbol_init_declarator( $1.tuple, $3.tuple);
 */
TUPLE	*symbol_init_declarator( TUPLE *declarator, TUPLE *initializer)
{
	TUPLE	*lists;
/*
 *	can be a list of identifiers
 */
	for( lists = declarator; lists; lists = lists->next)
	{
/*
 *	create a symbol with this value and copy it to identifier list
 */
		if( initializer)
		{
			create_symbol( 0, initializer->value, lists->buffer, lists->length);
			lists->value = initializer->value;
		}
		else
			create_symbol( 0, 0, lists->buffer, lists->length);
	}
/*
 *	free the initializer linked list
 */
	free_tuple_list( initializer);
	return( declarator);
}
Exemplo n.º 2
0
extern ACE_err_t
CLI_parse(CLI_parser_t *p, char *buf)
{
    p->buf = buf;
    p->lookahead = CLI_TOKEN_START;

    match(p, CLI_TOKEN_START);

    if (p->lookahead == CLI_TOKEN_END) {
        return CLI_ERR_LINEEMPTY;
    }

    if (p->lookahead == CLI_TOKEN_NAME) {
        create_symbol(p->cmd, p->sc.token, sizeof (p->cmd));
        match(p, CLI_TOKEN_NAME);
    } else {
        return CLI_ERR_SYNTAX;
    }

    if (p->lookahead == CLI_TOKEN_IN) {
        create_symbol(p->in, p->cmd, sizeof (p->in));
        match(p, CLI_TOKEN_IN);
        create_symbol(p->cmd, p->sc.token, sizeof (p->cmd));
        if (match(p, CLI_TOKEN_NAME) != ACE_OK) {
            return CLI_ERR_SYNTAX;
        }
    } else {
        create_symbol(p->in, NULL, 1);
    }

    if (p->lookahead == CLI_TOKEN_ARG) {
        create_symbol(p->arg, p->sc.token, sizeof (p->arg));
        match(p, CLI_TOKEN_ARG);
    } else {
        create_symbol(p->arg, NULL, 1);
    }

    if (p->lookahead == CLI_TOKEN_OUT) {
        match(p, CLI_TOKEN_OUT);
        create_symbol(p->out, p->sc.token, sizeof (p->out));
        if (match(p, CLI_TOKEN_NAME) != ACE_OK) {
            return CLI_ERR_SYNTAX;
        }
    } else {
        create_symbol(p->out, NULL, 1);
    }

    if (p->lookahead == CLI_TOKEN_RUN) {
        p->run = TRUE;
        match(p, CLI_TOKEN_RUN);
    } else {
        p->run = FALSE;
    }

    if (p->lookahead != CLI_TOKEN_END) {
        return CLI_ERR_SYNTAX;
    }

    return ACE_OK;
}
Exemplo n.º 3
0
void create_env()
{
  //env must be ((dummy . dummy) (a . 1) (b . 3) ...)
  env = create_cons();
  set_car(env, create_cons());
  set_cdr(env, create_empty_list());

  add_bind_to_env(env, create_symbol("car"), create_subr(LF_car));
  add_bind_to_env(env, create_symbol("cdr"), create_subr(LF_cdr));
  add_bind_to_env(env, create_symbol("atom?"), create_subr(LF_cons));
  add_bind_to_env(env, create_symbol("eq?"), create_subr(LF_eq));
  add_bind_to_env(env, create_symbol("quote"), create_fsubr(LF_quote));
  return;
}
Exemplo n.º 4
0
t_bool			test_sym_prod(
		char const **str,
		size_t nb_prods,
		size_t nb_symbols,
		t_bool (*test)(t_prod **prod, t_symbol **syms, ...))
{
	t_prod		**prods;
	t_symbol	**syms;
	t_fifo		*sym_lists[2];
	t_bool		result;

	sym_lists[0] = f_fifo_create();
	sym_lists[1] = f_fifo_create();
	f_fifo_add(sym_lists[1], create_symbol("END_OF_INPUT"));
	prods = parse_prods(str, nb_prods, sym_lists);
	syms = parse_symbols(str + nb_prods, nb_symbols, sym_lists);
	if (prods != NULL && syms != NULL
			&& (fifo_len(sym_lists[0]) == 0
				|| compute_sets_all_syms(sym_lists[1], sym_lists[0])))
		result = test(prods, syms, sym_lists[0], sym_lists[1],
				nb_prods, nb_symbols);
	else
		result = FALSE;
	destroy_prods(&prods, nb_prods);
	free(syms);
	f_fifo_destroy(&sym_lists[0], sym_del);
	f_fifo_destroy(&sym_lists[1], sym_del);
	return (result);
}
Exemplo n.º 5
0
symbol_table * create_symbol_table(){
	char * lib_functions[] = {
					"print","input","objectmemberkeys","objecttotalmembers",
				  	"objectcopy","totalarguments","argument","typeof","strtonum",
				  	"sqrt","cos","sin" };
	
	unsigned int i;
	st_entry * symbol;
	symbol_table * st = (symbol_table *)malloc(sizeof(symbol_table));
	if(memerror(st,"symbol table"))
		return NULL;

	for(i=0;i<BUCKET_SIZE;i++)
		st->hash_table[i] = NULL;
	st->scope_list = NULL;

	// We add all the library functions from the beginning in the symbol table.
	for(i=0;i<12;i++){
		symbol = create_symbol(lib_functions[i],1,0,0,LIBFUNC,0,0);
		if(memerror(symbol,"initalize lib func")){
			free(st);
			return NULL;
		}
		st_insert(&st,&symbol);
	}

	return st;
}
Exemplo n.º 6
0
int search_node_var_decl_function(Node *node, Table *function_table){
	if(DEBUG_SYMBOL_TABLE){
		printf("#ST: search_node_var_decl_function\n");
	}

	int errorcount = 0;
	Node *aux, *previous;
	Symbol *symbol;

	for(aux=node->first_child; aux; previous=aux, aux=aux->brother);

	Symbol_type type = get_symbol_type(previous->value);

	/*
	if(error_semantic == 0 && type == _bad_symbol_type_){
		error_semantic = 1;
		printf("Line %d, col %d: Type identifier expected\n", previous->line, previous->column);
		errorcount++;
	}
	*/

	for(aux=node->first_child; aux->brother; aux=aux->brother){
		/* TO DO - verificar se variavel ja existe !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
		symbol = create_symbol(aux->value, type, _none_, NULL, aux->line, aux->column);
		insert_symbol_in_table(function_table, symbol);
	}

	return errorcount;
}
Exemplo n.º 7
0
int search_node_func_def(Node *node){
	if(DEBUG_SYMBOL_TABLE){
		printf("#ST: search_node_func_def\n");
	}

	int errorcount=0;
	Table *new_table;
	Symbol *symbol;
	Node *aux, *previous;

	/* creates the symbol element for the Program Table and adds it */
	/* TO DO - verificar se variavel ja existe !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	symbol = create_symbol(node->first_child->value, _function_, _none_, NULL, -1, -1);
	insert_symbol_in_table(program_table, symbol);

	/* gets the function return type */
	for(aux=node->first_child; aux->brother->node_type != NODE_VAR_PART; aux=aux->brother);

	Symbol_type function_return_type = get_symbol_type(aux->value);
	
	/*
	if(error_semantic == 0 && function_return_type == _bad_symbol_type_){
		error_semantic = 1;
		printf("Line %d, col %d: Type identifier expected\n", previous->line, previous->column);
		errorcount++;
	}
	*/
	
	/* creates the new Function table */
	new_table = insert_table(_function_table_);

	/* creates the symbol element function and adds it to the table */
	/* TO DO - verificar se variavel ja existe !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	symbol = create_symbol(node->first_child->value, function_return_type, _return_, NULL, -1, -1);
	insert_symbol_in_table(new_table, symbol);

	/* travels to the NODE_FUNC_PARAMS node and runs through their childrens and grandchildrens */	
	errorcount += search_node_func_params(new_table, node->first_child->brother);

	for(aux=node->first_child; aux->node_type != NODE_VAR_PART; aux = aux->brother);

	errorcount += search_node_var_part_function(aux, new_table);

	return errorcount;
}
Exemplo n.º 8
0
/* create an instance of a string object */
void add_symbol(interp_core_type *interp, char *str) {
    object_type *obj=0;

    TRACE("Sy");

    obj=create_symbol(interp, str);
    
    add_object(interp,obj);
}
Exemplo n.º 9
0
pointer parse_quote(parser* parse)
{
    if(is_whitespace(*parse->curr))
        return parser_error(parse, "unexpected whitespace after quote.");

    switch(*parse->curr)
    {
    case '(':
        return create_pair(create_symbol(parse->symbols, "QUOTE"), parse_expr(parse));
    default:
        if(is_symbol_char(*parse->curr) && !is_number_char(*parse->curr))
            return create_pair(create_symbol(parse->symbols, "QUOTE"), parse_symbol(parse));
        else
            return parser_error(parse, "Unexpected token after quote.");

    }

}
Exemplo n.º 10
0
void add_symbol (address a, char *name, add_symbol_params *params)
{
    module *m=params->m;
    rbtree *symtbl=m->symbols;
    oassert(symtbl && "symbols=NULL in module");
    MemoryCache *mc=params->mc;

    if (one_time_int3_bp_re && params->t==SYM_TYPE_PE_EXPORT && module_adr_in_executable_section (m, a))
    {
        strbuf sb=STRBUF_INIT;
        strbuf_addstr (&sb, get_module_name(m));
        strbuf_addc (&sb, '!');
        strbuf_addstr (&sb, name);

        if (regexec (one_time_int3_bp_re, sb.buf, 0, NULL, 0)==0)
            set_onetime_INT3_BP(a, params->p, m, name, mc);

        strbuf_deinit (&sb);
    };

    if (dump_seh && string_is_ends_with (name, "security_cookie"))
    {
        m->security_cookie_adr=a;
        m->security_cookie_adr_known=true;
        if (symbol_c_debug)
            L ("%s() got address of security_cookie (0x" PRI_REG_HEX ") for %s!%s\n", __FUNCTION__, a, get_module_name(m), name);
    };

    bool dump_symbol=false;
    if (dump_all_symbols_re)
    {
        strbuf sb=STRBUF_INIT;
        strbuf_addstr (&sb, get_module_name(m));
        strbuf_addc (&sb, '!');
        strbuf_addstr (&sb, name);

        if (regexec (dump_all_symbols_re, sb.buf, 0, NULL, 0)==0)
            dump_symbol=true;

        strbuf_deinit (&sb);
    };

    if (dump_symbol || (dump_all_symbols_re==NULL && dump_all_symbols))
    {
        dump_PID_if_need(params->p);
        L("New symbol. Module=[%s], address=[0x" PRI_ADR_HEX "], name=[%s]\n", get_module_name(m), a, name);
    };

    symbol *new_sym=create_symbol(params->t, name);
    symbol *first_sym=(symbol*)rbtree_lookup(symtbl, (void*)a);

    if (first_sym)
        new_sym->next=first_sym; // insert at beginning of list

    rbtree_insert(symtbl, (void*)a, (void*)new_sym);
};
Exemplo n.º 11
0
void start_symbol_table(){
	if(DEBUG_SYMBOL_TABLE){
		printf("#ST - start_symbol_table\n");
	}

	Table *outer;
	Table *func;
	Symbol *new_symbol;
	Symbol *aux;

	/* ----- creates Outer Symbol Table ----- */
	outer=insert_table(_outer_table_);

	new_symbol = create_symbol("boolean", _type_, _constant_, "_boolean_", -1, -1);
	outer->child = new_symbol;
	aux = new_symbol;

	new_symbol = create_symbol("integer", _type_, _constant_, "_integer_", -1, -1);
	aux->next = new_symbol;
	aux = new_symbol;

	new_symbol = create_symbol("real", _type_, _constant_, "_real_", -1, -1);
	aux->next = new_symbol;
	aux = new_symbol;

	new_symbol = create_symbol("false", _boolean_, _constant_, "_false_", -1, -1);
	aux->next = new_symbol;
	aux = new_symbol;

	new_symbol = create_symbol("true", _boolean_, _constant_, "_true_", -1, -1);
	aux->next = new_symbol;
	aux = new_symbol;

	new_symbol = create_symbol("paramcount", _function_, _none_, NULL, -1, -1);
	aux->next = new_symbol;
	aux = new_symbol;

	new_symbol = create_symbol("program", _program_, _none_, NULL, -1, -1);
	aux->next = new_symbol;
	aux = new_symbol;

	/* ----- creates Function Symbol Table for 'paramcount' ----- */
	func = insert_table(_function_table_);

	new_symbol = create_symbol("paramcount", _integer_, _return_, NULL, -1, -1);

	func->child = new_symbol;

	program_table = insert_table(_program_table_);
}
/*
 *	update the value from last identifier and attach the tuple
		$$.tuple = symbol_init_declarator_list( $1.tuple, $3.tuple);
 */
TUPLE	*symbol_init_declarator_list( TUPLE *init_declarator_list, TUPLE *init_declarator)
{
	TUPLE	*lists;
/*
 *	can be a list of identifiers
 */
	for( lists = init_declarator_list; lists; lists = lists->next)
	{
/*
 *	create a symbol with this value
 */
		if( init_declarator)
		    create_symbol( 0, init_declarator->value, lists->buffer, lists->length);
		else
		    create_symbol( 0, 0, lists->buffer, lists->length);
	}
/*
 *	link in the last init_declarator tuple, return head of list
 */
	tuple_tail_to_head( init_declarator_list, init_declarator);
	return( init_declarator_list);
}
Exemplo n.º 13
0
void bind_symbol_list(interp_core_type *interp, binding_type *binding_list,
                      object_type **env) {
    object_type *obj=0;
    int i=0;

    /* bind every symbol in the list of primitives */
    for(i=0; binding_list[i].symbol!=0; i++) {
        obj=create_symbol(interp, binding_list[i].symbol);
        bind_symbol(interp, obj,
                    create_primitive(interp, binding_list[i].primitive,
                                     binding_list[i].eval_first,
                                     binding_list[i].eval_end), env);
    }
}
Exemplo n.º 14
0
static void create_icon (WindowckPlugin *wckp)
{
    wckp->icon = g_slice_new0 (WindowIcon);
    wckp->icon->eventbox = GTK_EVENT_BOX (gtk_event_box_new());
    wckp->icon->symbol = NULL;

    gtk_widget_set_can_focus (GTK_WIDGET(wckp->icon->eventbox), TRUE);

    gtk_event_box_set_visible_window (wckp->icon->eventbox, FALSE);

    gtk_box_pack_start (GTK_BOX (wckp->hvbox), GTK_WIDGET(wckp->icon->eventbox), FALSE, FALSE, 0);

    create_symbol (wckp);
}
Exemplo n.º 15
0
pointer parse_symbol(parser* parse)
{
    const char* C = parse->curr;

    while(!is_delimiter(*parse->curr))
    {
        if(is_symbol_char(*parse->curr))
            parse->curr++;
        else
            return parser_error(parse, "Unexpected char '%c' in symbol.", *parse->curr);
    }

    return create_symbol(parse->symbols, C, parse->curr-C);
}
Exemplo n.º 16
0
int st_insert(symbol_table ** st, st_entry ** symbol) {
	unsigned int key = generate_key((*symbol)->name);
	scope_entry * temp = (*st)->scope_list;
	scope_entry * previous = NULL;

	// Making a copy of the symbol to link it to the scope list.
	st_entry * symbol_cpy = create_symbol((*symbol)->name,(*symbol)->active,(*symbol)->scope,(*symbol)->line,(*symbol)->type,(*symbol)->offset,(*symbol)->space);
	if((*symbol)->value_type.varVal->used_in_func != NULL)
		symbol_cpy =  set_var_func(symbol_cpy,(*symbol)->value_type.varVal->used_in_func);

	// Insertion in the hash table.
	if ((*st)->hash_table[key])
		(*symbol)->next = (*st)->hash_table[key];
	(*st)->hash_table[key] = *symbol;

	// Insertion in the scope list.
	while (temp && temp->scope<(*symbol)->scope){
		previous = temp;
		temp = temp->next;
	}

	if (temp == NULL || temp->scope != (*symbol)->scope) {
		temp = (scope_entry *)malloc(sizeof(scope_entry));
		if (memerror(temp,"scope entry"))
			return 0;
		temp->scope = (*symbol)->scope;

		if(previous!=NULL){
			temp->next = previous->next;
			previous->next = temp;
		}
		else {
			temp->next = (*st)->scope_list;
			(*st)->scope_list = temp;
		}
		temp->symbols = symbol_cpy;
	}
	else 
		symbol_cpy->next = temp->symbols;
	 
	temp->symbols = symbol_cpy;

	// Seting the pointer to the last symbol that was added
	if(symbol_cpy->type!=FORMAL)
		(*st)->last_symbol = symbol_cpy;

	return 1;
}
Exemplo n.º 17
0
object_type * call_load(interp_core_type *interp, char *filename) {
    struct stat stat_buf;
    object_type *load_call=0;

    /* check the current directory */
    if(stat(filename, &stat_buf)) {
	fail("Unable to locate libraries");
    }

    /* build a call to load */
    load_call=cons(interp, create_symbol(interp, "load"),
		   cons(interp, create_string(interp, filename),
			interp->empty_list));


    return eval(interp, load_call);
}
/*
 *	type an identifier in symbol table
		$$.tuple = symbol_declaration( $1.token, $2.tuple);
 */
TUPLE	*symbol_declaration( int type_specifier, TUPLE *init_declarator_list)
{
	TUPLE	*lists;
/*
 *	can be a list of identifiers
 */
	for( lists = init_declarator_list; lists; lists = lists->next)
	{
/*
 *	create a symbol with this type
 */
		create_symbol( type_specifier, 0, lists->buffer, lists->length);
	}
/*
 *	return init_declarator_list linked list
 */
	return( init_declarator_list);
}
Exemplo n.º 19
0
symbol* lookup(char* id, scope* s) {
    scope* leaf = s;

    // Starting at the given scope, walk up the tree until we find a symbol.
    // If no symbol is found, we add a new symbol to the current scope and return it
    while(s != NULL) {
        node* n = list_search(s->symbol_list, find_symbol, id);
        if (n != NULL) {
            return n->data;
        }
        s = s->parent;
    }
    // If we got here, then we didn't find a symbol anywhere, so we'll add it in the given
    // scope.
    symbol* sym = create_symbol(id);
    add_symbol_to_scope(leaf, sym);
    return sym;
}
Exemplo n.º 20
0
/* Da de alta una nueva variable en la tabla de simbolos y la retorna */
Element *new_variable() {
	Element *e = (Element *)malloc(sizeof(Element));
	static int numero_va = 0;
	char *first_part = "$aux";
	char aux_num[6];
	numero_va++;
	sprintf(aux_num, "%d", numero_va);
	char *name = (char *)calloc(strlen(aux_num) + strlen(first_part) + 1, sizeof(char));
	strcat(name, first_part);
	strcat(name, aux_num);
	Symbol *s = create_symbol(name);
	s->type = INT;
	add_symbol(s);
	e->code = IDENTIFICADOR;
	e->name = name;
	
	return e;
}
Exemplo n.º 21
0
/*
 *	update the value from last identifier and attach the clip
		$$.clips = symbol_init_declarator_list( $1.clips, $3.clips);
 */
CLIPS	*symbol_init_declarator_list( CLIPS *init_declarator_list, CLIPS *init_declarator)
{
	CLIPS	*lists;
/*
 *	can be a list of identifiers
 */
	for( lists = init_declarator_list; lists; lists = lists->next)
	{
/*
 *	create a symbol with this value
 */
		create_symbol( 0, init_declarator->value, lists->buffer, lists->length);
	}
/*
 *	link in the last init_declarator clip, return head of list
 */
	clips_tail_to_head( init_declarator_list, init_declarator);
	return( init_declarator_list);
}
Exemplo n.º 22
0
/*
 *	update a value of an identifier in symbol table
		$$.clips = symbol_init_declarator( $1.clips, $3.clips);
 */
CLIPS	*symbol_init_declarator( CLIPS *declarator, CLIPS *initializer)
{
	CLIPS	*lists;
/*
 *	can be a list of identifiers
 */
	for( lists = declarator; lists; lists = lists->next)
	{
/*
 *	create a symbol with this value and copy it to identifier list
 */
		create_symbol( 0, initializer->value, lists->buffer, lists->length);
		lists->value = initializer->value;
	}
/*
 *	free the initializer linked list
 */
	de_clips_list( initializer);
	return( declarator);
}
Exemplo n.º 23
0
symbol *parse_file (char *name, symbol *anchor)
{
	FILE *input;
	char *word;
	bool at_eof;
	int line;
	symbol *current;
	symbol *previous;

	at_eof = false;
	line = 1;
	current = NULL;
	previous = NULL;

	if ((input = fopen (name, "r")))
	{		
		while (! at_eof)
		{			
			if (current)
				previous = current;

			current = create_symbol (input, &at_eof, &line);

			if (! anchor)
			{
				anchor = current;
				current->prev = NULL;		
			}

			if (previous)
			{
				current->prev = previous;
				previous->next = current;
			}
		}

		fclose (input);
	}

	return (anchor);	
}
Exemplo n.º 24
0
int search_node_func_params(Table *function_table, Node *node){
	if(DEBUG_SYMBOL_TABLE){
		printf("#ST: search_node_func_params\n");
	}

	int errorcount = 0;
	Node *aux_node_params, *aux_node_id, *previous;
	Symbol_type var_type;
	Symbol *symbol;
	Flag flag_type;

	/* runs over each NODE_VAR_PARAMS or NODE_PARAMS */
	for(aux_node_params=node->first_child; aux_node_params; aux_node_params=aux_node_params->brother){
		/* gets the symbol_type of the vars */
		for(aux_node_id=aux_node_params->first_child; aux_node_id; previous=aux_node_id, aux_node_id=aux_node_id->brother);
		var_type = get_symbol_type(previous->value);

		/*
		if(error_semantic == 0 && var_type == _bad_symbol_type_){
			error_semantic = 1;
		printf("Line %d, col %d: Type identifier expected\n", previous->line, previous->column);
			errorcount++;
		}
		*/

		if(aux_node_params->node_type == NODE_VAR_PARAMS)
			flag_type = _varparam_;
		else if(aux_node_params->node_type == NODE_PARAMS)
			flag_type = _param_;

		/* creates a new symbol element and adds it to the function table */
		for(aux_node_id=aux_node_params->first_child; aux_node_id->brother; aux_node_id=aux_node_id->brother){
			/* TO DO - verificar se variavel ja existe !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
			symbol = create_symbol(aux_node_id->value, var_type, flag_type, NULL, aux_node_id->line, aux_node_id->column);
			insert_symbol_in_table(function_table, symbol);
		}
	}

	return errorcount;
}
Exemplo n.º 25
0
/*
 * Description: Process the extern definition in first transition.
 * 				Adds it into the symbol table
 * Input:		1. Current transition
 */
void first_transition_process_extern(transition_data* transition) {
	symbol_node_ptr p_symbol = NULL;

	char* extern_name = get_next_word(transition);

	/* If we have an extern*/
	if (extern_name) {
		symbol_node_ptr p_searched_symbol = search_symbol(extern_name);

		/* Extern does not exists in symbol table */
		if (!p_searched_symbol) {
			/* Create new symbol in table or die */
			p_symbol = create_symbol(extern_name, NO_ADDRESS, true, true);
			if (p_symbol) {
				add_symbol_to_list(p_symbol);

				/* Make sure that the line does not contain left overs */
				if (!is_end_of_data_in_line(transition->current_line_information)) {
					print_compiler_error("Invalid tokens after extern definition", transition->current_line_information);
					transition->is_compiler_error = true;
					return;
				}
			}
			/* Could not allocat memory, die*/
			else {
				transition->is_runtimer_error = true;
				free(extern_name);
				return;
			}
		}
		/* Extern is already in table */
		else {
			print_compiler_error("Each extern can be defined only once", transition->current_line_information);
			transition->is_compiler_error = true;
			return;
		}
	}
}
Exemplo n.º 26
0
void travVardecl(astree* root) {
    astree* node = root->children[0];
    astree* node1 = root->children[1];
    root->block = blockcount;
    int sym = node->symbol;
    int otherSym = getReturnType(node1);
    switch(sym) {
    case TOK_INT:
        checkDecl(node);
        if(otherSym==TOK_INTCON || otherSym==TOK_NULL) {
            symbol *newSym = create_symbol(node->children[0]);
            newSym->attributes.set(ATTR_int);
            newSym->attributes.set(ATTR_lval);
            newSym->attributes.set(ATTR_variable);
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(node->children[0]->lexinfo,newSym));
                symbol_stack.pop_back();
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(node->children[0]->
                                            lexinfo,newSym));
            }
            dumpToFile(file_sym, newSym, node->children[0]);
        } else if(otherSym == TOK_NEWARRAY) {
            insertArr(node, node1);
        }
        break;

    case TOK_CHAR:
        checkDecl(node);
        if(otherSym==TOK_CHARCON || otherSym==TOK_NULL) {
            symbol *newSym = create_symbol(node->children[0]);
            newSym->attributes.set(ATTR_char);
            newSym->attributes.set(ATTR_lval);
            newSym->attributes.set(ATTR_variable);
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(node->children[0]->lexinfo,newSym));
                symbol_stack.pop_back();
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(node->children[0]->
                                            lexinfo,newSym));
            }
            dumpToFile(file_sym, newSym, node->children[0]);
        } else if(otherSym == TOK_NEWARRAY) {
            insertArr(node, node1);
        }
        break;

    case TOK_BOOL:
        checkDecl(node);
        if(otherSym==TOK_TRUE ||
                otherSym==TOK_FALSE || otherSym==TOK_NULL) {
            symbol *newSym = create_symbol(node->children[0]);
            newSym->attributes.set(ATTR_bool);
            newSym->attributes.set(ATTR_lval);
            newSym->attributes.set(ATTR_variable);
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(node->children[0]->lexinfo,newSym));
                symbol_stack.pop_back();
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(node->children[0]->
                                            lexinfo,newSym));
            }
            dumpToFile(file_sym, newSym, node->children[0]);
        } else if (otherSym == TOK_NEWARRAY) {
            insertArr(node, node1);
        }
        break;
    case TOK_STRING:
        checkDecl(node);
        if(otherSym == TOK_NEWSTRING) {
            if(node1->children[0]->symbol == TOK_INTCON ||
                    otherSym==TOK_NULL) {
                symbol *newSym = create_symbol(node->children[0]);
                newSym->attributes.set(ATTR_string);
                newSym->attributes.set(ATTR_lval);
                newSym->attributes.set(ATTR_variable);
                if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                    symbol_table* tab = new symbol_table();
                    tab->insert(symbol_entry(node->children[0]
                                             ->lexinfo,newSym));
                    symbol_stack.pop_back();
                    symbol_stack.push_back(tab);
                } else {
                    symbol_stack.back()->insert(symbol_entry(node->children[0]->
                                                lexinfo,newSym));
                }
                dumpToFile(file_sym, newSym, node->children[0]);
            } else {
                errprintf("%zu.%zu.%zu String size allocator not of "
                          "type int.\n",
                          node1->children[1]->filenr, node1->children[1]->linenr,
                          node1->children[1]->offset);
            }
        } else if (otherSym == TOK_STRINGCON || otherSym==TOK_NULL) {
            if(otherSym!=TOK_NULL)
                strvec.push_back(node1->lexinfo);
            symbol *newSym = create_symbol(node->children[0]);
            newSym->attributes.set(ATTR_string);
            newSym->attributes.set(ATTR_lval);
            newSym->attributes.set(ATTR_variable);
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(node->children[0]->lexinfo,newSym));
                symbol_stack.pop_back();
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(node->children[0]->
                                            lexinfo,newSym));
            }

            dumpToFile(file_sym, newSym, node->children[0]);
        } else if(otherSym == TOK_NEWARRAY) {
            insertArr(node, node1);
        }
        break;
    case TOK_TYPEID:
        if(otherSym == TOK_TYPEID || otherSym==TOK_NULL
                || otherSym == TOK_STRUCT) {
            if(node1->symbol != TOK_CALL) {
                string leftStr = *node->lexinfo;
                string rightStr = *node1->children[0]->lexinfo;
                if(leftStr != rightStr) {
                    errprintf("%zu.%zu.%zu Type mismatch between constructor "
                              "and declaration.\n",
                              node1->children[1]->filenr, node1->children[1]->linenr,
                              node1->children[1]->offset);
                }
            } else {
                if(lookup(node1->children[0]->lexinfo)) {
                    symbol* funcSym = lookupSym(node1->children[0]->lexinfo);
                    string leftStr = *node->lexinfo;
                    string rightStr = funcSym->type_id;
                    if(leftStr != rightStr) {
                        errprintf("%zu.%zu.%zu Type mismatch between constructor "
                                  "and declaration.\n",
                                  node1->children[1]->filenr, node1->children[1]->linenr,
                                  node1->children[1]->offset);
                    }
                }
            }
            symbol *newSym = create_symbol(node->children[0]);
            newSym->attributes.set(ATTR_struct);
            newSym->attributes.set(ATTR_lval);
            newSym->attributes.set(ATTR_variable);
            newSym->type_id = *node->lexinfo;
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(node->children[0]->lexinfo,newSym));
                symbol_stack.pop_back();
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(node->children[0]->
                                            lexinfo,newSym));
            }

            dumpToFile(file_sym, newSym, node->children[0]);
        } else {
            errprintf("%zu.%zu.%zu Variable not allocated correctly.\n",
                      node1->children[1]->filenr, node1->children[1]->linenr,
                      node1->children[1]->offset);
        }
        break;
    case TOK_IDENT:
        if(lookup(node->lexinfo)) {
            symbol* newSym = lookupSym(node->lexinfo);
            int type = getIdentReturn(newSym);
            if(type==TOK_STRUCT&&otherSym==TOK_TYPEID) {
                if(newSym->type_id!=*node1->children[0]->lexinfo) {
                    errprintf("%zu.%zu.%zu Variable being reassigned wrong "
                              "type",node->filenr,node->linenr,node->offset);
                }
            }
        } else {
            errprintf("%zu.%zu.%zu"
                      " Variable being referenced is undefined\n",node->filenr,
                      node->linenr,node->offset);
        }
    }
}
Exemplo n.º 27
0
symbol* create_root_type(char* name) {
    symbol* s = create_symbol(name);
    s->type = s;
    return s;
}
Exemplo n.º 28
0
symbol* create_symbol_with_type(char* name, symbol* type) {
    symbol* m = create_symbol(name);
    m->type = type;
    return m;
}
Exemplo n.º 29
0
void create_primitives() {
  make_primitive(create_symbol("car"), prim_car);
  make_primitive(create_symbol("cdr"), prim_cdr);
  make_primitive(create_symbol("cons"), prim_cons);
  make_primitive(create_symbol("+"), prim_add);
  make_primitive(create_symbol("-"), prim_sub);
  make_primitive(create_symbol("*"), prim_mul);
  make_primitive(create_symbol("fixnum?"), prim_fixnump);
  make_primitive(create_symbol("character?"), prim_characterp);
  make_primitive(create_symbol("boolean?"), prim_booleanp);
  make_primitive(create_symbol("symbol?"), prim_symbolp);
  make_primitive(create_symbol("string?"), prim_stringp);
  make_primitive(create_symbol("cons?"), prim_consp);
  make_primitive(create_symbol("function?"), prim_functionp);
}
Exemplo n.º 30
0
void traverseAstree(astree* root) {
    if( flag == 0 ) {
        symbol_stack.push_back(nullptr);
        flag++;
    }
    for (size_t a = 0; a < root->children.size(); ++a) {
        int sym = root->children[a]->symbol;
        astree* curr = root->children[a];
        switch(sym) {
        case TOK_VARDECL:
        case '=':
            travVardecl(curr);
            break;
        case TOK_IF:
        case TOK_WHILE:
            validCompare(curr);
            symbol_stack.push_back(nullptr);
            blockcount++;
            traverseAstree(curr->children[1]);
            blockcount--;
            symbol_stack.pop_back();
            break;
        case TOK_IFELSE:
            validCompare(curr);
            for(size_t i = 0; i<curr->children.size(); i++) {
                symbol_stack.push_back(nullptr);
                blockcount++;
                traverseAstree(curr->children[i]);
                blockcount--;
                symbol_stack.pop_back();
            }
            break;
        case TOK_FUNCTION:
        {
            root->children[a]->children[1]->block = blockcount+1;
            symbol* newFunc = create_symbol(root->children[a]);
            blockcount++;
            newFunc->attributes.set(ATTR_function);
            switch(curr->children[0]->symbol) {
            case TOK_INT:
                newFunc->attributes.set(ATTR_int);
                break;
            case TOK_STRING:
                newFunc->attributes.set(ATTR_string);
                break;
            case TOK_CHAR:
                newFunc->attributes.set(ATTR_char);
                break;
            case TOK_BOOL:
                newFunc->attributes.set(ATTR_bool);
                break;
            case TOK_TYPEID:
                newFunc->attributes.set(ATTR_struct);
                newFunc->attributes.set(ATTR_typeid);
                newFunc->type_id =*root->children[a]->children[0]->lexinfo;
                break;
            }
            newFunc->parameters = new vector<symbol*>;
            for(size_t i = 0; i<curr->children[1]->children.size(); i++) {
                astree* param_node = curr->children[1]->children[i];
                symbol* param = create_symbol(param_node->children[0]);
                param->attributes.set(ATTR_variable);
                param->attributes.set(ATTR_lval);
                param->attributes.set(ATTR_param);
                int paramSym = param_node->symbol;
                switch(paramSym) {
                case TOK_INT:
                    param->attributes.set(ATTR_int);
                    break;
                case TOK_CHAR:
                    param->attributes.set(ATTR_char);
                    break;
                case TOK_STRING:
                    param->attributes.set(ATTR_string);
                    break;
                case TOK_BOOL:
                    param->attributes.set(ATTR_bool);
                    break;
                }
                newFunc->parameters->push_back(param);
            }
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(curr->children[0]->children[0]->
                                         lexinfo,newFunc));
                symbol_stack.pop_back();
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(curr->children[0]->
                                            children[0]->lexinfo,newFunc));
            }
            dumpToFile(file_sym, newFunc, curr->children[0]->children[0]);
            symbol_stack.push_back(nullptr);
            if (curr->children[1]->children.size() != 0) {
                dumpParams(newFunc, curr->children[1]);
            }
            int funcSym;
            switch (curr->children[0]->symbol) {
            case TOK_INT:
                funcSym = TOK_INTCON;
                break;
            case TOK_CHAR:
                funcSym = TOK_CHARCON;
                break;
            case TOK_STRING:
                funcSym = TOK_STRINGCON;
                break;
            case TOK_BOOL:
                funcSym = TOK_BOOL;
                break;
            }
            traverseFunc(curr->children[2], funcSym);
            blockcount--;
            symbol_stack.pop_back();
            break;
        }
        case TOK_STRUCT:
        {
            astree *temp = root->children[a];
            symbol *newSym = create_symbol(temp->children[0]);
            newSym->attributes.set(ATTR_struct);
            newSym->attributes.set(ATTR_typeid);
            newSym->type_id = *temp->children[0]->lexinfo;
            dumpToFile(file_sym,newSym,temp->children[0]);
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(temp->children[0]->lexinfo,newSym));
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(temp->children[0]->
                                            lexinfo,newSym));
            }
            newSym->fields = new symbol_table();
            for(size_t i = 1; i<temp->children.size(); i++) {
                symbol *tempSym = create_symbol(temp->children[i]->
                                                children[0]);
                tempSym->attributes.set(ATTR_field);
                switch(temp->children[i]->symbol) {
                case TOK_INT:
                    tempSym->attributes.set(ATTR_int);
                    break;
                case TOK_CHAR:
                    tempSym->attributes.set(ATTR_char);
                    break;
                case TOK_BOOL:
                    tempSym->attributes.set(ATTR_bool);
                    break;
                case TOK_STRING:
                    tempSym->attributes.set(ATTR_string);
                    break;
                case TOK_TYPEID:
                    tempSym->attributes.set(ATTR_typeid);
                    tempSym->attributes.set(ATTR_struct);
                    break;
                }
                newSym->fields->insert(symbol_entry(temp->children[i]->
                                                    children[0]->lexinfo,tempSym));
            }
            dumpFields(newSym,temp);
            break;
        }
        case TOK_CALL:
            checkCall(curr);
            break;
        case TOK_PROTOTYPE:
        {
            symbol* newFunc = create_symbol(root->children[a]);
            blockcount++;
            newFunc->attributes.set(ATTR_function);
            switch(curr->children[0]->symbol) {
            case TOK_INT:
                newFunc->attributes.set(ATTR_int);
                break;
            case TOK_STRING:
                newFunc->attributes.set(ATTR_string);
                break;
            case TOK_CHAR:
                newFunc->attributes.set(ATTR_char);
                break;
            case TOK_BOOL:
                newFunc->attributes.set(ATTR_bool);
                break;
            case TOK_TYPEID:
                newFunc->attributes.set(ATTR_struct);
                newFunc->attributes.set(ATTR_typeid);
                newFunc->type_id =*root->children[a]->children[0]->lexinfo;
                break;
            }
            newFunc->parameters = new vector<symbol*>;
            for(size_t i = 0; i<curr->children[1]->children.size(); i++) {
                astree* param_node = curr->children[1]->children[i];
                symbol* param = create_symbol(param_node->children[0]);
                param->attributes.set(ATTR_variable);
                param->attributes.set(ATTR_lval);
                param->attributes.set(ATTR_param);
                int paramSym = param_node->symbol;
                switch(paramSym) {
                case TOK_INT:
                    param->attributes.set(ATTR_int);
                    break;
                case TOK_CHAR:
                    param->attributes.set(ATTR_char);
                    break;
                case TOK_STRING:
                    param->attributes.set(ATTR_string);
                    break;
                case TOK_BOOL:
                    param->attributes.set(ATTR_bool);
                    break;
                }
                newFunc->parameters->push_back(param);
            }
            if(symbol_stack.back() == nullptr || symbol_stack.empty()) {
                symbol_table* tab = new symbol_table();
                tab->insert(symbol_entry(curr->children[0]->children[0]->
                                         lexinfo,newFunc));
                symbol_stack.pop_back();
                symbol_stack.push_back(tab);
            } else {
                symbol_stack.back()->insert(symbol_entry(curr->children[0]->
                                            children[0]->lexinfo,newFunc));
            }
            dumpToFile(file_sym, newFunc, curr->children[0]->children[0]);
            symbol_stack.push_back(nullptr);
            if (curr->children[1]->children.size() != 0) {
                dumpParams(newFunc, curr->children[1]);
            }
            int funcSym;
            switch (curr->children[0]->symbol) {
            case TOK_INT:
                funcSym = TOK_INTCON;
                break;
            case TOK_CHAR:
                funcSym = TOK_CHARCON;
                break;
            case TOK_STRING:
                funcSym = TOK_STRINGCON;
                break;
            case TOK_BOOL:
                funcSym = TOK_BOOL;
                break;
            }
            blockcount--;
            break;
        }
        }
    }
}