From 4f611475de9b371b1a02aa6eff215c7d6c87085d Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 16 Jul 2012 14:14:37 +0200 Subject: [PATCH] Starting some parsing --- lexer.c | 31 ++++++++++++++++++------------- parser.c | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/lexer.c b/lexer.c index 867fb94..2c6bcb0 100644 --- a/lexer.c +++ b/lexer.c @@ -559,20 +559,25 @@ int lex_do(lex_file *lex) lex->tok->ttype = TOKEN_IDENT; v = lex->tok->value; - if (!strcmp(v, "void") || - !strcmp(v, "int") || - !strcmp(v, "float") || - !strcmp(v, "vector") ) - { + if (!strcmp(v, "void")) { lex->tok->ttype = TOKEN_TYPENAME; - switch (v[1]) { - case 'o': lex->tok->constval.t = TYPE_VOID; break; - case 'n': lex->tok->constval.t = TYPE_INTEGER; break; - case 'l': lex->tok->constval.t = TYPE_FLOAT; break; - case 'e': lex->tok->constval.t = TYPE_VECTOR; break; - } - } - else if (!strcmp(v, "for") || + lex->tok->constval.t = TYPE_VOID; + } else if (!strcmp(v, "int")) { + lex->tok->ttype = TOKEN_TYPENAME; + lex->tok->constval.t = TYPE_INTEGER; + } else if (!strcmp(v, "float")) { + lex->tok->ttype = TOKEN_TYPENAME; + lex->tok->constval.t = TYPE_FLOAT; + } else if (!strcmp(v, "string")) { + lex->tok->ttype = TOKEN_TYPENAME; + lex->tok->constval.t = TYPE_STRING; + } else if (!strcmp(v, "entity")) { + lex->tok->ttype = TOKEN_TYPENAME; + lex->tok->constval.t = TYPE_ENTITY; + } else if (!strcmp(v, "vector")) { + lex->tok->ttype = TOKEN_TYPENAME; + lex->tok->constval.t = TYPE_VECTOR; + } else if (!strcmp(v, "for") || !strcmp(v, "while") || !strcmp(v, "do")) lex->tok->ttype = TOKEN_KEYWORD; diff --git a/parser.c b/parser.c index 0dd972e..c753c59 100644 --- a/parser.c +++ b/parser.c @@ -1,7 +1,44 @@ #include "gmqcc.h" #include "lexer.h" +typedef struct { + lex_file *lex; + int tok; +} parser_t; + +bool parser_do(parser_t *parser) +{ + return true; +} + bool parser_compile(const char *filename) { - return false; + parser_t *parser; + + parser = (parser_t*)mem_a(sizeof(parser_t)); + if (!parser) + return false; + + parser->lex = lex_open(filename); + + if (!parser->lex) { + printf("failed to open file \"%s\"\n", filename); + return false; + } + + for (parser->tok = lex_do(parser->lex); + parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR; + parser->tok = lex_do(parser->lex)) + { + if (!parser_do(parser)) { + printf("parse error\n"); + lex_close(parser->lex); + mem_d(parser); + return false; + } + } + + lex_close(parser->lex); + mem_d(parser); + return true; } -- 2.39.2