From: Dale Weiler Date: Wed, 11 Apr 2012 16:13:31 +0000 (-0400) Subject: Implemented support for #include, but it doesn't add an include to the lexer yet. X-Git-Tag: 0.1-rc1~683 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=51e2f2b73184f6679829104ced9887665f8f5b4c;p=xonotic%2Fgmqcc.git Implemented support for #include, but it doesn't add an include to the lexer yet. --- diff --git a/code.c b/code.c index 3297301..b796b13 100644 --- a/code.c +++ b/code.c @@ -151,6 +151,7 @@ VECTOR_MAKE(char, code_strings ); prog_header code_header; void code_write() { + #if 0 /* Add test program */ code_strings_add('\0'); @@ -188,7 +189,7 @@ void code_write() { code_functions_add((prog_section_function){0, 0, 0, 0, .name=0, 0, 0, {0}}); /* NULL */ code_functions_add((prog_section_function){1, 0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */ - code_functions_add((prog_section_function){-2, 0, 0, 0, .name=8, 0, 0, {0}}); /* print */ + code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print */ code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13, 0,0, {0}}); /* m_keydown */ code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13+10, 0,0, {0}}); code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13+10+7, 0,0, {0}}); @@ -208,8 +209,10 @@ void code_write() { code_header.globals = (prog_section){code_header.functions.offset + sizeof(prog_section_function) *code_functions_elements, code_globals_elements }; code_header.strings = (prog_section){code_header.globals.offset + sizeof(int) *code_globals_elements, code_strings_elements }; code_header.entfield = 0; /* TODO: */ - + #endif + + /* write out everything one SHOT! */ FILE *fp = fopen("program.dat", "wb"); fwrite(&code_header, 1, sizeof(prog_header), fp); fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp); diff --git a/lex.c b/lex.c index 3be0045..37e914f 100644 --- a/lex.c +++ b/lex.c @@ -302,6 +302,7 @@ int lex_token(struct lex_file *file) { if (typedef_find(file->lastok)) TEST_TYPE(typedef_find(file->lastok)->name); + #undef TEST_TYPE return LEX_IDENT; } return ch; diff --git a/main.c b/main.c index 8aa73a0..00f8411 100644 --- a/main.c +++ b/main.c @@ -20,10 +20,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include -#include -#include -#include "gmqcc.h" +# include + #include +# include + # include "gmqcc.h" int main(int argc, char **argv) { argc--; diff --git a/parse.c b/parse.c index 2274df9..38c3332 100644 --- a/parse.c +++ b/parse.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "gmqcc.h" /* @@ -278,7 +279,18 @@ int parse_tree(struct lex_file *file) { case TOKEN_CONTINUE: PARSE_TREE_ADD(PARSE_TYPE_CONTINUE); break; - + + /* + * DO loops work like so: + * __do_loop_beg: + * IFNOT __do_loop_beg# + * GOTO __do_loop_end + * INSTR1 + * INSTR2 + * ...... + * GOTO __do_loop_beg + */ + case TOKEN_DO: PARSE_PERFORM(PARSE_TYPE_DO, {}); case TOKEN_WHILE: PARSE_PERFORM(PARSE_TYPE_WHILE, {}); case TOKEN_BREAK: PARSE_PERFORM(PARSE_TYPE_BREAK, {}); @@ -297,14 +309,36 @@ int parse_tree(struct lex_file *file) { * which are higer than the ascii table.) */ case '#': + token = lex_token(file); /* skip '#' */ + while (isspace(token)) { + if (token == '\n') { + return error(ERROR_PARSE, "Expected valid preprocessor directive after `#` %s\n"); + } + token = lex_token(file); /* try again */ + } /* - * Skip the preprocessor for now: We'll implement our own - * eventually. For now we need to make sure directives are - * not accidently tokenized. + * If we make it here we found a directive, the supported + * directives so far are #include. */ - token = lex_token(file); - token = lex_token(file); - + if (strncmp(file->lastok, "include", sizeof("include")) == 0) { + //lex_include("file"); + /* + * We need to compose a name till we find either a + * '"',> or a <, (for includes), if we hit a '\n' then + * clearly the person miswrote the include. + */ + while (*file->lastok != '"' && token != '\n') + token = lex_token(file); + + if (token == '\n') + return error(ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n"); + + + //lex_token(file); + //lex_token(file); + printf("include: %s\n", file->lastok); + } + /* skip all tokens to end of directive */ while (token != '\n') token = lex_token(file); diff --git a/test/include.qc b/test/include.qc new file mode 100644 index 0000000..39aa583 --- /dev/null +++ b/test/include.qc @@ -0,0 +1,13 @@ +/* + * all of these includes should work. No matter what the spacing + * is, we rely on it. + */ +#include "include.h" +#include "include.h" + #include "include.h" + #include "include.h" + #include "include.h" +# include "include.h" + # include "include.h" +#include "include.h" +# include "include.h" diff --git a/typedef.c b/typedef.c index dcc4e20..ecd8b8b 100644 --- a/typedef.c +++ b/typedef.c @@ -80,7 +80,7 @@ int typedef_add(const char *from, const char *to) { strncmp(from, "void", sizeof("void")) == 0) { typedef_table[hash] = mem_a(sizeof(typedef_node)); - typedef_table[hash]->name = strdup(from); + typedef_table[hash]->name = util_strdup(from); return -100; } else { /* search the typedefs for it (typedef-a-typedef?) */