~oriansj/M3-Preprocess

bc179330dfd5f294feb2df2c03cf52ce9bef5088 — Jeremiah Orians 1 year, 6 months ago 85f3f0f
Add basic trigraph support and remove label logic that wasn't needed
1 files changed, 39 insertions(+), 51 deletions(-)

M c_stage1.c
M c_stage1.c => c_stage1.c +39 -51
@@ 48,6 48,43 @@ int pattern_compress(struct token_list* i, struct pattern* p)
	return TRUE;
}

/* Because we free the strings and trigraphs can be grouped */
char* get_new_string(char* s)
{
	char* r = calloc(strlen(s)+1, sizeof(char));
	strcpy(r, s);
	return r;
}

void replace_trigraphs()
{
	struct token_list* i = global_token;
	struct pattern* trigraphhash = new_pattern("?", new_pattern("?", new_pattern("=", NULL)));
	struct pattern* trigraphopenbracket = new_pattern("?", new_pattern("?", new_pattern("(", NULL)));
	struct pattern* trigraphbackslash = new_pattern("?", new_pattern("?", new_pattern("/", NULL)));
	struct pattern* trigraphclosebracket = new_pattern("?", new_pattern("?", new_pattern(")", NULL)));
	struct pattern* trigraphxor = new_pattern("?", new_pattern("?", new_pattern("'", NULL)));
	struct pattern* trigraphopencurly = new_pattern("?", new_pattern("?", new_pattern("<", NULL)));
	struct pattern* trigraphbar = new_pattern("?", new_pattern("?", new_pattern("!", NULL)));
	struct pattern* trigraphclosecurly = new_pattern("?", new_pattern("?", new_pattern(">", NULL)));
	struct pattern* trigraphtilde = new_pattern("?", new_pattern("?", new_pattern("-", NULL)));

	while(NULL != i)
	{
		if(NULL == i->next) break;
		if(pattern_compress(i, trigraphhash)) i->s = get_new_string("#");
		else if(pattern_compress(i, trigraphopenbracket)) i->s = get_new_string("[");
		else if(pattern_compress(i, trigraphbackslash)) i->s = get_new_string("\\");
		else if(pattern_compress(i, trigraphclosebracket)) i->s = get_new_string("]");
		else if(pattern_compress(i, trigraphxor)) i->s = get_new_string("^");
		else if(pattern_compress(i, trigraphopencurly)) i->s = get_new_string("{");
		else if(pattern_compress(i, trigraphbar)) i->s = get_new_string("|");
		else if(pattern_compress(i, trigraphclosecurly)) i->s = get_new_string("}");
		else if(pattern_compress(i, trigraphtilde)) i->s = get_new_string("~");
		else i = i->next;
	}
}

void combine_common()
{
	struct token_list* i = global_token;


@@ 148,55 185,6 @@ void combine_common()
	}
}

void sort_out_labels()
{
	/* This is just my guess at a heuristic for figuring out the difference between */
	/* label: and Ternary ? conditional : alt */
	/* This will fail if labels are not on their own lines alone */
	struct token_list* i = global_token;
	struct token_list* hold;
	int count;
	struct token_list* maybe = NULL;
	while(NULL != i)
	{
		count = 0;
		require(NULL != i, "hit null while trying to sort out labels\n");
		while(!match(i->s, "\n"))
		{
			if(NULL == i) break;
			if(NULL == i->next) break;
			if(match(i->next->s, ":"))
			{
				maybe = i;
			}
			else if(!match(i->s, " ") && !match(i->s, "\t") && !match(i->s, "\n"))
			{
				count = count + 1;
			}
			i = i->next;
		}
		if(1 == count)
		{
			if(NULL == maybe) continue;
			reset_hold_string();
			strcat(hold_string, maybe->s);
			strcat(hold_string, ":");
			free(maybe->s);
			maybe->s = calloc(strlen(hold_string)+1, sizeof(char));
			strcpy(maybe->s, hold_string);
			hold = maybe->next;
			maybe->next = maybe->next->next;
			free(hold->s);
			free(hold);
		}
		else
		{
			maybe = NULL;
			i = i->next;
		}
	}
}

void stage1_preprocess()
{
	if(1 == DUMP_STAGE) dump_stage_raw();


@@ 205,10 193,10 @@ void stage1_preprocess()

	if(2 == DUMP_STAGE) dump_stage_raw();
	if(3 == DUMP_STAGE) dump_stage_straight();
	combine_common();
	replace_trigraphs();
	if(4 == DUMP_STAGE) dump_stage_raw();
	if(5 == DUMP_STAGE) dump_stage_straight();
	sort_out_labels();
	combine_common();
	if(6 == DUMP_STAGE) dump_stage_raw();
	if(7 == DUMP_STAGE) dump_stage_straight();
}