示例#1
0
bool Compiler::startCompile( )
{
    Scanner scanner( in_file );

    if (!scanner.start())
    {
        list<string> error_list = scanner.getErrors();
        list<string>::iterator errors = error_list.begin( );

        if (error_list.size() > 0)
        {
            for(;errors != error_list.end();errors++)
            {
                cout << *errors << endl;
            }
        }

        return false;
    }
    else
    {
    	#ifdef TESTING

        list<string> tok_list = scanner.getTokens();

        for(list<string>::iterator tok = tok_list.begin( );tok != tok_list.end();tok++)
        {
            cout << *tok << endl;
        }

        #endif

        // Call parser for generating native code.
        Parser parser(scanner.getTokens() , gen_code);

        if (!parser.parse(out_file.substr(out_file.find_last_of("\\") + 1,out_file.find_last_of(".") - (out_file.find_last_of("\\") + 1)),out_file.substr(0,out_file.find_last_of("\\"))))
        {
            list<string> errors = parser.getErrors( );

            for(list<string>::iterator error = errors.begin( );error != errors.end();error++)
            {
                cout << *error << endl;
            }

            return false;
        }
        else
        {
            list<string> gen_codes = parser.getCodes( );
            ofstream out_stm(out_file.c_str( ));

            for(list<string>::iterator code = gen_codes.begin( );code != gen_codes.end();code++)
            {
                out_stm << *code << endl;
            }

            return true;
        }
    }

    return false;
}
示例#2
0
void while_stm(FILE *fp) {
    int f, whilestack_num;
    long int while_begin, while_end, con_pos, sim_pos, e_con_pos;
    char str[B_SIZE + 1], st[B_SIZE + 1], con[B_SIZE + 1];

    if (while_num >= WHILE_LIMITED_NUM) {
        error_sxc(WHILE_LIMITED_ERROR, fp);
    }
    while_num += 1;

    con_pos = ftell(fp);
    f = value_stm(fp);
    while_begin = ftell(fp);

	fseek(fp, -1L, SEEK_CUR);
	e_con_pos = ftell(fp);
	fgets(con, B_SIZE, fp);
	if (!strcmp(con, "\n") == 0) {
		fseek(fp, e_con_pos, SEEK_SET);
		error_sxc(WHILE_CONDITION_ERROR, fp);
	}
	else
	{
		fseek(fp, while_begin, SEEK_SET);
	}

    fgets(str, B_SIZE, fp);
    if(strcmp(str, "{\n") == 0) {
        while_begin = ftell(fp);
    } else {
        fseek(fp, while_begin, SEEK_SET);
		error_sxc(WHILE_MISSING_1_ERROR, fp);
    }

    /*while_end = find_stm(fp);

    while(f) {
        exec_stm(while_begin, while_end, fp);
	fseek(fp, con_pos, SEEK_SET);
	f = value_stm(fp);
    }
	fseek(fp, while_end, SEEK_SET);
    while_num -= 1;*/
	
	if (f == 0){
		while_end = find_stm(fp);
	}
	while (f){
		fseek(fp, while_begin, SEEK_SET);
		whilestack_num = 1;
		while (whilestack_num){
			sim_pos = ftell(fp);
			fgets(st, B_SIZE, fp);
			if (strcmp(st, "}\n") == 0){
				whilestack_num -= 1;
			}
			else if (strcmp(st, "{\n") == 0){
				whilestack_num += 1;
			}
			else if (strcmp(st, "end\n") == 0){
				fseek(fp, sim_pos, SEEK_SET);
				error_sxc(MISSING_1_ERROR, fp);
			}
			else if (strcmp(st, "else\n") == 0){
				fseek(fp, sim_pos, SEEK_SET);
				error_sxc(MISSING_1_ERROR, fp);
			}
			else if (strcmp(st, "if\n") == 0){
				fseek(fp, sim_pos, SEEK_SET);
				next_line(fp);
				if_stm(fp);
			}
			else if (strcmp(st, "while\n") == 0){
				fseek(fp, sim_pos, SEEK_SET);
				next_line(fp);
				while_stm(fp);
			}
			else if (strcmp(st, "in\n") == 0){
				fseek(fp, sim_pos, SEEK_SET);
				next_line(fp);
				in_stm(fp);
			}
			else if (strcmp(st, "out\n") == 0){
				fseek(fp, sim_pos, SEEK_SET);
				next_line(fp);
				out_stm(fp);
			}
			else if (strcmp(st, "for\n") == 0){
				fseek(fp, sim_pos, SEEK_SET);
				next_line(fp);
				in_stm(fp);
			}
			else
			{
				fseek(fp, sim_pos, SEEK_SET);
				simple_stm(fp);
			}
			while_end = ftell(fp);
		}
		fseek(fp, con_pos, SEEK_SET);
		f = value_stm(fp);
	}

	fseek(fp, while_end, SEEK_SET);
	while_num -= 1;
}