More parse tree stuff

This commit is contained in:
Dale Weiler 2012-04-09 22:44:06 -04:00
parent a6d9357ef9
commit a5e44c6631
3 changed files with 36 additions and 28 deletions

3
lex.c
View file

@ -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;

6
main.c
View file

@ -25,13 +25,10 @@
#include <limits.h>
#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);

55
parse.c
View file

@ -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);