Exemple #1
0
main()
{
	int choice;
	char buffer[32];

	scope_t *top_scope = NULL;
	node_t *tmp = NULL;
	
	fprintf(stderr, "0.search 1.search_all 2.insert 3.new_scope 4.del_scope \n");
	while (1) {
		scanf("%d", &choice);
		switch(choice) {
		case 0:
			scanf("%s", buffer);
			tmp = scope_search(top_scope, buffer);
			if (tmp != NULL) {
				fprintf(stderr, "Found[%s]\n", tmp->name);
			}
			else {
				fprintf(stderr, "Not found[%s]\n", buffer);
			}
			break;

		case 1:
			scanf("%s", buffer);
			tmp = scope_search_all(top_scope, buffer);
			if (tmp != NULL) {
				fprintf(stderr, "Found[%s]\n", tmp->name);
			}
			else {
				fprintf(stderr, "Not found[%s]\n", buffer);
			}
			break;

		case 2:
			scanf("%s", buffer);
			tmp = scope_insert(top_scope, buffer);
			if (tmp != NULL) {
				fprintf(stderr, "Insert[%s]\n", tmp->name);
			}
			else {
				fprintf(stderr, "Cannot Insert[%s]\n", buffer);
			}
			break;

		case 3:
			top_scope = scope_push(top_scope);
			break;

		case 4:
			top_scope = scope_pop(top_scope);
			break;
		}
	}
}
Exemple #2
0
int check_func_def_arg(is_func_def_arg* node)
{
	int errors = 0;
	SYMBOL* symbol;

	errors += check_type_decl(node->type);
	if (errors == 0)
	{
		symbol = scope_local_lookup(symtab, node->id->name, t_symbol_var);
		if (symbol)
		{
			pretty_error(node->line, "argument \"%s\" colides with already defined variable (previous declaration was here: %d)",
				node->id->name, symbol->line);
			errors++;
		} else
		{
			symbol = scope_insert(symtab, symbol_new_var(node->id->name, node->line, node->type, false, symtab->framepos++));
			symbol->data.var_data.initialized = true;
		}
	}

	return errors;
}
Exemple #3
0
int check_class_def(is_class_def* node)
{
	int errors = 0;
	SYMBOL* symbol;

	symbol = scope_lookup(symtab, node->id->name, t_symbol_class);
	if (symbol)
	{
		errors++;
		pretty_error(node->line, "class \"%s\" is already defined (previous declaration was here: %d)", node->id->name, symbol->line);
	} else
		scope_insert(symtab, symbol = symbol_new_class(node->id->name, node->line));

	node->scope = scope_new(symbol, true);

	/*
		FIXME:
		this should be in application and not in class def,
		because theoretically class should be able to access other classes
	*/
	scope_push(node->scope);
		errors += check_class_stmt_list(node->body, true);

		if (errors == 0)
		{
			symbol = scope_lookup(symtab, "main", t_symbol_func);
			if (!symbol)
			{
				errors++;
				pretty_error(node->line, "missing main entry point");
			}
		}
		errors += check_class_stmt_list(node->body, false);
	scope_pop();

	return errors;
}
Exemple #4
0
int check_func_def(is_func_def* node, bool first_pass)
{
	SYMBOL* symbol;
	SCOPE* tempscope;
	int label;
	int errors = 0;
	is_type_decl *tmpType = NULL;

	if (first_pass)
	{
		errors += check_type_decl(node->type);

		symbol = scope_lookup(symtab, node->id->name, t_symbol_func);
		if (symbol)
		{
			pretty_error(node->line, "symbol \"%s\" is already defined (previous declaration was here: %d)", node->id->name, symbol->line);
			errors++;
		} else
		{
			tempscope = scope_new(NULL, false);
			scope_push(tempscope);
				errors += check_func_def_args(node->args);
			scope_pop();
			
			if (strcmp(node->id->name, "main") == 0)
			{
				label = 0;

				if (node->args) /* no args is valid */
				{
					if (node->args->length == 1) /* main may must not have more than two arguments */
					{
						tmpType = insert_type_decl_array(insert_array_decl(insert_type_object(t_type_native_string), new_dims_empty_list(0, 1))); /* must be an array of Strings */

						if (!type_type_equal(tmpType,node->args->node->type))
						{
							errors++;
							pretty_error(node->line, "main function arguments do not match any valid prototypes");
						}

						free_type_decl(tmpType);
					} else
					{
						errors++;
						pretty_error(node->line, "main function arguments do not match any valid prototypes");
					}
				}

				tmpType = insert_type_decl_object(insert_type_object(t_type_native_int)); /* return value must be int or void */

				if (!type_type_equal(tmpType,node->type))
				{
					free_type_decl(tmpType);

					tmpType = new_type_decl_void(0); /* check for void */

					if (!type_type_equal(tmpType,node->type)) /* none; errors occurred */
					{
						errors++;
						pretty_error(node->line, "main function return type does not match any valid types (nor int nor void)");
					}
				}
				
				free_type_decl(tmpType);
			} else
				label = ++label_counter;

			symbol = symbol_new_func(node->id->name, node->line, node->type, node->args, label);
			scope_delete(tempscope);

			scope_insert(symtab, symbol);

			node->scope = scope_new(symbol, false);
		}
	} else
	{
		scope_push(node->scope);
			errors += check_func_def_args(node->args); /* this will not give errors */
			errors += check_stmt_list(node->body);
			
			if ((!node->body || !node->body->terminated) &&
				!(node->type->type == t_type_decl_type_object && node->type->data.type_object->type == t_type_native_void))
			{
				pretty_error(node->line, "reached end of non void function");
				errors++;		
			}
		scope_pop();
	}

	return errors;
}
Exemple #5
0
int check_var_defs(is_var_defs* node, bool first_pass)
{
	int errors = 0;

	SYMBOL *symbol;

	is_type_decl *type = NULL;

	is_var_def_list *it;

	errors += check_type_decl(node->type);
	errors += check_var_def_list(node->list);

	for (it = node->list; it != NULL; it = it->next)
	{
		symbol = scope_local_lookup(symtab, it->node->left->id->name, t_symbol_var);
		if (symbol)
		{
			errors++;
			pretty_error(it->node->line, "symbol \"%s\" is already defined (previous declaration was here: %d)", it->node->left->id->name, symbol->line);
		} else
		{
			if (it->node->left->empty->size != 0)
			{
				if (node->type->type != t_type_decl_array_decl)
				{
					/* create a new type_decl (array) with it->node->left->data.empty->size dimensions */
					type = insert_type_decl_array(insert_array_decl(insert_type_object(node->type->data.type_object->type), new_dims_empty_list(node->type->line, it->node->left->empty->size)));
				} else
				{
					/* type is already a type_decl_array, only dims update is needed */
					type = duplicate_type_decl(node->type);
					type->data.array->dims->size += it->node->left->empty->size;
				}

			} else /* no dims updates are needed; even if the type is "dimmed" this is only a copy of the type */
				type = duplicate_type_decl(node->type);

			if (first_pass)
				symbol = scope_insert(symtab, symbol_new_var(it->node->left->id->name, node->line, type, true, globalpos++));
			else
				symbol = scope_insert(symtab, symbol_new_var(it->node->left->id->name, node->line, type, false, symtab->framepos++));

			it->node->left->symbol = symbol;

			if (it->node->var_init) /* check initialization if it exists */
			{
				if (type_var_init_assign_able(type, (type->type == t_type_decl_array_decl ? type->data.array->dims->size : 0), it->node->var_init))
					/* if initialization is valid */
					symbol->data.var_data.initialized = true;
				else
				{
					errors++;
					pretty_error(it->node->line, "variable \"%s\" initalization is invalid", it->node->left->id->name);
				}
			}
		}

		free_type_decl(type);
	}
	
	return errors;
}