哈哈哈了啊

  • Post author:
  • Post category:其他


void program()
{
	int entry_flag  = 0;
	Symbol *p, *q;
	
	/* before parse insert read && write func */
	p = insert_global_sym(FUNCTION,"read");
	q = insert_global_sym(FUNCTION,"write");
	if(!p || !q){
		fprintf(stderr, "init read write function error\n");
		exit(0);
	}
	
	token = get_token();
	
	while(token == TOK_INT || token == TOK_PVOID
	      || token == TOK_VOID || token == TOK_CHAR
	      || token == TOK_PINT || token == TOK_PCHAR)
	{
		entry_flag = 1;
		declaration(token);
	}
	
	if(entry_flag){
		gen_code("stp\n");
		printf("succeed!!  exit()\n");
	}else{
		fprintf(stderr, "line %d: %s does not name a type\n", save_line, save_word );
	}
	
}

static void declaration(int type)
{
	
	GlobalInfo *global_var;
	char id[NAME_SIZE];
	
	type = token;
	match(type);
	match(TOK_ID);
	strncpy(id, save_word, NAME_SIZE);
	
	if(token == TOK_LPAREN){
		
	    fun_declaration(type, id);
	    
	    if(!strcmp(id,"main")){
			main_stack = current_function->u.f.total_offset;
		}
	    
	} else if(token == TOK_SEMI || token == TOK_LSQUARE) {
		
		global_var = global_var_declaration(type, id);
		global_var->seq = global_seq;
		global_var->offset = global_total_offset;
		global_total_offset += global_var->size;
		global_seq++;	
		
	} else{
		
		fprintf(stderr, "line %d: unexpected token %s\n", save_line, save_word);
	}
	
}

static void fun_declaration(int type, char * id)
{
	
	if(type != TOK_VOID)
		has_return_value = false;
			
	// init AutoOffset
	memset(&offset,0,sizeof(AutoOffset));

	current_function = insert_global_sym(FUNCTION,id);
	
	if(current_function == NULL)
	{
		//insert_func failed!
	}
	
	current_function->u.f.return_type = type;

	gen_code("ent %d\n", current_function->u.f.id);
	
	/* match ( */
	match(TOK_LPAREN);
	
	params();
	
	/*match ) */
	match(TOK_RPAREN);
	
	//add after insert auto_var
	//current_function->u.f.total_offset += current_function->u.f.param_size;
	
	compound_stmt();
	
	if(current_function->u.f.return_type == TOK_VOID && has_return_value){
		fprintf(stderr, "line %d: this fuction doesn't return value'\n", save_line);
	}
	
}


static GlobalInfo* global_var_declaration(int type, char *id)
{
	Symbol * var;
	int size = SIZE(type);
	
	var = insert_global_sym(GLOB_VAR,id);
	if(var == NULL)
	{
        fprintf(stderr, "line %d: malloc global var error\n", save_line);
	}
	var->type = type;
	
	
	/* var */
	if(token == TOK_SEMI) 
	{
		if(type == TOK_PINT || type == TOK_PCHAR)
		{
			var->u.g.is_pointer = true;
		}
		var->u.g.size = size;
		match(TOK_SEMI);
	}
	/* array */
	else if(token == TOK_LSQUARE)
	{
		match(TOK_LSQUARE);
		match(TOK_NUM);
    	match(TOK_RSQUARE);
    	match(TOK_SEMI);
    	var->u.g.is_array = true;
    	var->u.g.size = save_num * size;
	}
	else
		fprintf(stderr, "line %d



版权声明:本文为sambrown123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。