From a5e44c66313134810eeb4e62c236bcc33a8403f3 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Mon, 9 Apr 2012 22:44:06 -0400 Subject: [PATCH] More parse tree stuff --- lex.c | 3 ++- main.c | 6 +----- parse.c | 55 +++++++++++++++++++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/lex.c b/lex.c index 6c381c9..01b2ec7 100644 --- a/lex.c +++ b/lex.c @@ -295,7 +295,8 @@ int lex_token(struct lex_file *file) { for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++) if (!strncmp(typedef_find(file->lastok)->name, lex_keywords[it], sizeof(lex_keywords[it]))) return it; - + + return LEX_IDENT; } return ch; diff --git a/main.c b/main.c index 3e8b8b6..c796d7c 100644 --- a/main.c +++ b/main.c @@ -25,13 +25,10 @@ #include #include "gmqcc.h" -//typedef int foo; - int usage(const char *name) { printf("Usage: %s -f infile -o outfile\n", name); return 0; } - int main(int argc, char **argv) { const char *ofile = NULL; const char *ifile = NULL; @@ -60,10 +57,9 @@ int main(int argc, char **argv) { printf("ifile: %s\n", ifile); printf("ofile: %s\n", ofile); + /* Open file */ FILE *fp = fopen(ifile, "r"); - - /* run the preprocessor */ if (!fp) { fclose(fp); return error(ERROR_COMPILER, "Source file: %s not found\n", ifile); diff --git a/parse.c b/parse.c index 68aa2c9..b46df13 100644 --- a/parse.c +++ b/parse.c @@ -68,6 +68,7 @@ #define PARSE_TYPE_BAND 35 #define PARSE_TYPE_BOR 36 #define PARSE_TYPE_DONE 37 +#define PARSE_TYPE_IDENT 38 /* * Adds a parse type to the parse tree, this is where all the hard @@ -119,10 +120,12 @@ void parse_debug(struct parsenode *tree) { case PARSE_TYPE_RETURN: STORE("STATEMENT: RETURN\n"); case PARSE_TYPE_DONE: STORE("STATEMENT: DONE\n"); - + case PARSE_TYPE_VOID: STORE("DECLTYPE: VOID\n"); + case PARSE_TYPE_STRING: STORE("DECLTYPE: STRING\n"); case PARSE_TYPE_ELIP: STORE("DECLTYPE: VALIST\n"); case PARSE_TYPE_ENTITY: STORE("DECLTYPE: ENTITY\n"); case PARSE_TYPE_FLOAT: STORE("DECLTYPE: FLOAT\n"); + case PARSE_TYPE_VECTOR: STORE("DECLTYPE: VECTOR\n"); case PARSE_TYPE_GT: STORE("TEST: GREATER THAN\n"); case PARSE_TYPE_LT: STORE("TEST: LESS THAN\n"); @@ -147,7 +150,7 @@ void parse_debug(struct parsenode *tree) { case PARSE_TYPE_FOR: STORE("LOOP: FOR\n"); case PARSE_TYPE_DO: STORE("LOOP: DO\n"); - + case PARSE_TYPE_IDENT: STORE("IDENT: ???\n"); } tree = tree->next; } @@ -190,32 +193,28 @@ int parse(struct lex_file *file) { switch (token) { case TOKEN_IF: token = lex_token(file); - while ((token == ' ' || token == '\n') && file->length >= 0) - token = lex_token(file); + //while ((token == ' ' || token == '\n') && file->length >= 0) + // token = lex_token(file); - if (token != '(') - error(ERROR_PARSE, "Expected `(` after if\n", ""); + //if (token != '(') + // error(ERROR_PARSE, "Expected `(` after if\n", ""); PARSE_TREE_ADD(PARSE_TYPE_IF); break; case TOKEN_ELSE: token = lex_token(file); - while ((token == ' ' || token == '\n') && file->length >= 0) - token = lex_token(file); + //while ((token == ' ' || token == '\n') && file->length >= 0) + // token = lex_token(file); PARSE_TREE_ADD(PARSE_TYPE_ELSE); break; case TOKEN_FOR: token = lex_token(file); - while ((token == ' ' || token == '\n') && file->length >= 0) - token = lex_token(file); + //while ((token == ' ' || token == '\n') && file->length >= 0) + // token = lex_token(file); PARSE_TREE_ADD(PARSE_TYPE_FOR); break; - - case LEX_IDENT: - token = lex_token(file); - break; /* * This is a quick and easy way to do typedefs at parse time @@ -272,6 +271,15 @@ int parse(struct lex_file *file) { token = lex_token(file); break; + case '(': + token = lex_token(file); + PARSE_TREE_ADD(PARSE_TYPE_LPARTH); + break; + case ')': + token = lex_token(file); + PARSE_TREE_ADD(PARSE_TYPE_RPARTH); + break; + case '&': /* & */ token = lex_token(file); if (token == '&') { /* && */ @@ -338,14 +346,6 @@ int parse(struct lex_file *file) { token = lex_token(file); PARSE_TREE_ADD(PARSE_TYPE_ADD); break; - case '(': - token = lex_token(file); - PARSE_TREE_ADD(PARSE_TYPE_LPARTH); - break; - case ')': - token = lex_token(file); - PARSE_TREE_ADD(PARSE_TYPE_RPARTH); - break; case '{': token = lex_token(file); PARSE_TREE_ADD(PARSE_TYPE_LBS); @@ -354,6 +354,17 @@ int parse(struct lex_file *file) { token = lex_token(file); PARSE_TREE_ADD(PARSE_TYPE_RBS); break; + + /* + * TODO: Fix lexer to spit out ( ) as tokens, it seems the + * using '(' or ')' in parser doesn't work properly unless + * there are spaces before them to allow the lexer to properly + * seperate identifiers. -- otherwise it eats all of it. + */ + case LEX_IDENT: + token = lex_token(file); + PARSE_TREE_ADD(PARSE_TYPE_IDENT); + break; } } parse_debug(parseroot); -- 2.39.2