~oriansj/M3-Preprocess

3226e45b9e4494f7558134806e4fd092b75f657d — Jeremiah Orians 1 year, 5 months ago 06c5b74 main
harmonized struct used for core operations
5 files changed, 47 insertions(+), 89 deletions(-)

M c_stage1.c
M cc.h
M cc_macro.c
M cc_reader.c
M makefile
M c_stage1.c => c_stage1.c +14 -12
@@ 85,19 85,20 @@ void replace_trigraphs(struct token_list* token)
	}
}

void collapse_keyword(struct token_list* token, char* replace, char* replacement)
int collapse_keyword(struct token_list* token, char* replace, char* replacement)
{
	require(NULL != token, "passed incorrect token in collapse_keyword\n");
	require(NULL != token->next, "# EOF is not a valid pattern\n");
	require(NULL != replace, "We can't replace a null string\n");
	require(NULL != replacement, "The replacement can't be null either\n");
	if(!match(token->next->s, replace)) return;
	if(!match(token->next->s, replace)) return FALSE;
	free(token->s);
	struct token_list* hold = token->next;
	token->next = token->next->next;
	token->s = replacement;
	free(hold->s);
	free(hold);
	return TRUE;
}

/* Because the reader doesn't understand trigraphs, we have to fixup some keywords */


@@ 110,7 111,7 @@ void fixup_trigraph_garbage(struct token_list* token)
		if(NULL == i->next) break;
		if(match("#", i->s))
		{
			while(match(" ", i->next->s) || match("\t", i->next->s) || match("\n", i->next->s))
			while(match(" ", i->next->s) || match("\t", i->next->s))
			{
				hold = i->next;
				i->next = i->next->next;


@@ 118,15 119,16 @@ void fixup_trigraph_garbage(struct token_list* token)
				free(hold);
				if(NULL == i->next) break;
			}
			collapse_keyword(i, "if", "#if");
			collapse_keyword(i, "ifdef", "#ifdef");
			collapse_keyword(i, "ifndef", "#ifndef");
			collapse_keyword(i, "else", "#else");
			collapse_keyword(i, "define", "#define");
			collapse_keyword(i, "include", "#include");
			collapse_keyword(i, "undef", "#undef");
			collapse_keyword(i, "line", "#line");
			collapse_keyword(i, "pragma", "#pragma");
			if(collapse_keyword(i, "if", "#if")) break;
			else if(collapse_keyword(i, "ifdef", "#ifdef")) break;
			else if(collapse_keyword(i, "ifndef", "#ifndef")) break;
			else if(collapse_keyword(i, "else", "#else")) break;
			else if(collapse_keyword(i, "define", "#define")) break;
			else if(collapse_keyword(i, "include", "#include")) break;
			else if(collapse_keyword(i, "undef", "#undef")) break;
			else if(collapse_keyword(i, "line", "#line")) break;
			else if(collapse_keyword(i, "pragma", "#pragma")) break;
			else i = i->next;
		}
		else i = i->next;
	}

M cc.h => cc.h +6 -17
@@ 48,26 48,15 @@ struct type
struct token_list
{
	struct token_list* next;
	union
	{
		struct token_list* locals;
		struct token_list* prev;
	};
	struct token_list* prev;
	char* s;
	union
	{
		struct type* type;
		char* filename;
	};
	union
	{
		struct token_list* arguments;
		struct token_list* expansion;
		int depth;
		int linenumber;
	};
	char* filename;
	int linenumber;
	struct token_list* arguments;
	struct token_list* expansion;
};


struct pattern
{
	char* s;

M cc_macro.c => cc_macro.c +26 -24
@@ 34,15 34,7 @@ struct conditional_inclusion
	int previous_condition_matched; /* 1 == all subsequent conditions treated as FALSE */
};

struct macro_list
{
	struct macro_list* next;
	char* symbol;
	struct token_list* expansion;
	struct token_list* arguments;
};

struct macro_list* macro_env;
struct token_list* macro_env;
struct conditional_inclusion* conditional_inclusion_top;

/* point where we are currently modifying the global_token list */


@@ 50,9 42,9 @@ struct token_list* macro_token;

void init_macro_env(char* sym, char* value, char* source, int num)
{
	struct macro_list* hold = macro_env;
	macro_env = calloc(1, sizeof(struct macro_list));
	macro_env->symbol = sym;
	struct token_list* hold = macro_env;
	macro_env = calloc(1, sizeof(struct token_list));
	macro_env->s = sym;
	macro_env->next = hold;
	macro_env->expansion = calloc(1, sizeof(struct token_list));
	macro_env->expansion->s = value;


@@ 171,7 163,7 @@ struct token_list* copy_list(struct token_list* token)
	return copy;
}

struct macro_list* lookup_macro(struct token_list* token)
struct token_list* lookup_macro(struct token_list* token)
{
	if(NULL == token)
	{


@@ 180,11 172,11 @@ struct macro_list* lookup_macro(struct token_list* token)
		exit(EXIT_FAILURE);
	}

	struct macro_list* hold = macro_env;
	struct token_list* hold = macro_env;

	while (NULL != hold)
	{
		if (match(token->s, hold->symbol))
		if (match(token->s, hold->s))
		{
			/* found! */
			return hold;


@@ 206,11 198,11 @@ void remove_macro(struct token_list* token)
		exit(EXIT_FAILURE);
	}

	struct macro_list* hold = macro_env;
	struct macro_list* temp;
	struct token_list* hold = macro_env;
	struct token_list* temp;

	/* Deal with the first element */
	if (match(token->s, hold->symbol)) {
	if (match(token->s, hold->s)) {
		macro_env = hold->next;
		free(hold);
		return;


@@ 219,7 211,7 @@ void remove_macro(struct token_list* token)
	/* Remove element form the middle of linked list */
	while (NULL != hold->next)
	{
		if (match(token->s, hold->next->symbol))
		if (match(token->s, hold->next->s))
		{
			temp = hold->next;
			hold->next = hold->next->next;


@@ 238,7 230,7 @@ int macro_expression();
int macro_variable()
{
	int value = 0;
	struct macro_list* hold = lookup_macro(macro_token);
	struct token_list* hold = lookup_macro(macro_token);
	if (NULL != hold)
	{
		if(NULL == hold->expansion)


@@ 483,7 475,7 @@ int macro_expression()

void handle_define()
{
	struct macro_list* hold;
	struct token_list* hold;
	struct token_list* arg;
	struct token_list* expansion_end = NULL;



@@ 503,8 495,8 @@ void handle_define()
	require('\n' != macro_token->s[0], "unexpected newline after #define\n");

	/* insert new macro */
	hold = calloc(1, sizeof(struct macro_list));
	hold->symbol = macro_token->s;
	hold = calloc(1, sizeof(struct token_list));
	hold->s = macro_token->s;
	hold->next = macro_env;
	/* provided it isn't in a non-included block */
	if(conditional_define) macro_env = hold;


@@ 744,6 736,11 @@ void macro_directive()
	else if(match("#else", macro_token->s))
	{
		eat_current_token();
		if(('/' == macro_token->s[0]) && (('/' == macro_token->s[1]) || ('*' == macro_token->s[1])))
		{
			/* Delete any comments after an else */
			macro_token = macro_token->next;
		}
		require(NULL != conditional_inclusion_top, "#else without leading #if\n");
		conditional_inclusion_top->include = !conditional_inclusion_top->previous_condition_matched;
	}


@@ 761,6 758,11 @@ void macro_directive()
		t = conditional_inclusion_top;
		conditional_inclusion_top = conditional_inclusion_top->prev;
		free(t);
		if(('/' == macro_token->s[0]) && (('/' == macro_token->s[1]) || ('*' == macro_token->s[1])))
		{
			/* Delete any comments after an else */
			macro_token = macro_token->next;
		}
	}
	else if(match("#define", macro_token->s))
	{


@@ 937,7 939,7 @@ struct token_list* maybe_expand(struct token_list* token)
		exit(EXIT_FAILURE);
	}

	struct macro_list* hold = lookup_macro(token);
	struct token_list* hold = lookup_macro(token);
	struct token_list* hold2;
	struct token_list* hold3;
	struct token_list* hold4;

M cc_reader.c => cc_reader.c +0 -1
@@ 168,7 168,6 @@ int get_token(int c)
				c = consume_byte(c);
				require(EOF != c, "Hit EOF inside of line comment\n");
			}
			c = consume_byte(c);
		}
		else if(c == '=')
		{

M makefile => makefile +1 -35
@@ 25,7 25,7 @@ CFLAGS:=$(CFLAGS) -D_GNU_SOURCE -D VERSION=\"Release_0.0.0-0-000000\" -O0 -std=c

all: M3-Preprocess
.NOTPARALLEL:
M3-Preprocess: bin cpp.c cc_reader.c cc_globals.c dump.c c_stage1.c include_expansion.c cc_macro.c M2libc/bootstrappable.c | bin
M3-Preprocess: bin cc.h cpp.c cc_reader.c cc_globals.c dump.c c_stage1.c include_expansion.c cc_macro.c M2libc/bootstrappable.c | bin
	$(CC) $(CFLAGS) \
	M2libc/bootstrappable.c \
	cpp.c \


@@ 41,40 41,6 @@ M3-Preprocess: bin cpp.c cc_reader.c cc_globals.c dump.c c_stage1.c include_expa
.PHONY: clean
clean:
	rm -rf bin/ test/results/
	./test/cleanup_test.sh 0000
	./test/cleanup_test.sh 0001
	./test/cleanup_test.sh 0002
	./test/cleanup_test.sh 0003
	./test/cleanup_test.sh 0004
	./test/cleanup_test.sh 0005
	./test/cleanup_test.sh 0006
	./test/cleanup_test.sh 0007
	./test/cleanup_test.sh 0008
	./test/cleanup_test.sh 0009
	./test/cleanup_test.sh 0010
	./test/cleanup_test.sh 0011
	./test/cleanup_test.sh 0012
	./test/cleanup_test.sh 0013
	./test/cleanup_test.sh 0014
	./test/cleanup_test.sh 0015
	./test/cleanup_test.sh 0016
	./test/cleanup_test.sh 0017
	./test/cleanup_test.sh 0018
	./test/cleanup_test.sh 0019
	./test/cleanup_test.sh 0020
	./test/cleanup_test.sh 0021
	./test/cleanup_test.sh 0022
	./test/cleanup_test.sh 0023
	./test/cleanup_test.sh 0024
	./test/cleanup_test.sh 0025
	./test/cleanup_test.sh 0100
	./test/cleanup_test.sh 0101
	./test/cleanup_test.sh 0102
	./test/cleanup_test.sh 0103
	./test/cleanup_test.sh 0104
	./test/cleanup_test.sh 0105
	./test/cleanup_test.sh 0106
	./test/cleanup_test.sh 1000

# Directories
bin: