Reading a global def

This commit is contained in:
Wolfgang Bumiller 2012-07-16 14:52:52 +02:00
parent 4f611475de
commit d581fdc048
2 changed files with 132 additions and 9 deletions

View file

@ -579,7 +579,9 @@ int lex_do(lex_file *lex)
lex->tok->constval.t = TYPE_VECTOR; lex->tok->constval.t = TYPE_VECTOR;
} else if (!strcmp(v, "for") || } else if (!strcmp(v, "for") ||
!strcmp(v, "while") || !strcmp(v, "while") ||
!strcmp(v, "do")) !strcmp(v, "do") ||
!strcmp(v, "var") ||
!strcmp(v, "const"))
lex->tok->ttype = TOKEN_KEYWORD; lex->tok->ttype = TOKEN_KEYWORD;
return lex->tok->ttype; return lex->tok->ttype;

137
parser.c
View file

@ -1,24 +1,136 @@
#include <stdio.h>
#include <stdarg.h>
#include "gmqcc.h" #include "gmqcc.h"
#include "lexer.h" #include "lexer.h"
typedef struct { typedef struct {
lex_file *lex; lex_file *lex;
int tok; int tok;
MEM_VECTOR_MAKE(ast_value*, globals);
} parser_t; } parser_t;
MEM_VEC_FUNCTIONS(parser_t, ast_value*, globals)
void parseerror(parser_t *parser, const char *fmt, ...)
{
va_list ap;
if (parser)
printf("error %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line);
else
printf("error: ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
bool parser_next(parser_t *parser)
{
/* lex_do kills the previous token */
parser->tok = lex_do(parser->lex);
if (parser->tok == TOKEN_EOF || parser->tok >= TOKEN_ERROR)
return false;
return true;
}
/* lift a token out of the parser so it's not destroyed by parser_next */
token *parser_lift(parser_t *parser)
{
token *tok = parser->lex->tok;
parser->lex->tok = NULL;
return tok;
}
#define parser_tokval(p) (p->lex->tok->value)
#define parser_token(p) (p->lex->tok)
#define parser_ctx(p) (p->lex->tok->ctx)
ast_value* parser_find_global(parser_t *parser, const char *name)
{
size_t i;
for (i = 0; i < parser->globals_count; ++i) {
if (!strcmp(parser->globals[i]->name, name))
return parser->globals[i];
}
return NULL;
}
bool parser_do(parser_t *parser) bool parser_do(parser_t *parser)
{ {
if (parser->tok == TOKEN_TYPENAME)
{
ast_value *var;
int vtype = parser->lex->tok->constval.t;
/* Declaring a variable */
if (!parser_next(parser))
return false;
if (parser->tok != TOKEN_IDENT) {
parseerror(parser, "expected variable name\n");
return false;
}
var = parser_find_global(parser, parser_tokval(parser));
if (var) {
parseerror(parser, "global already exists: %s\n", parser_tokval(parser));
return false;
}
var = ast_value_new(parser_ctx(parser), parser_tokval(parser), vtype);
if (!parser_t_globals_add(parser, var))
return false;
/* Constant assignment */
if (!parser_next(parser))
return false;
if (parser->tok == ';')
return parser_next(parser);
if (parser->tok != '=') {
parseerror(parser, "expected '=' or ';'");
return false;
}
/* '=' found, assign... */
parseerror(parser, "TODO, const assignment");
return false;
}
else if (parser->tok == TOKEN_KEYWORD)
{
/* handle 'var' and 'const' */
return false;
}
else if (parser->tok == '.')
{
/* entity-member declaration */
return false;
}
else
{
parseerror(parser, "unexpected token: %s", parser->lex->tok->value);
return false;
}
return true; return true;
} }
bool parser_compile(const char *filename) bool parser_compile(const char *filename)
{ {
size_t i;
parser_t *parser; parser_t *parser;
parser = (parser_t*)mem_a(sizeof(parser_t)); parser = (parser_t*)mem_a(sizeof(parser_t));
if (!parser) if (!parser)
return false; return false;
MEM_VECTOR_INIT(parser, globals);
parser->lex = lex_open(filename); parser->lex = lex_open(filename);
if (!parser->lex) { if (!parser->lex) {
@ -26,19 +138,28 @@ bool parser_compile(const char *filename)
return false; return false;
} }
for (parser->tok = lex_do(parser->lex); /* initial lexer/parser state */
parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR; parser->lex->flags.noops = true;
parser->tok = lex_do(parser->lex))
if (parser_next(parser))
{ {
if (!parser_do(parser)) { while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
printf("parse error\n"); {
lex_close(parser->lex); if (!parser_do(parser)) {
mem_d(parser); printf("parse error\n");
return false; lex_close(parser->lex);
mem_d(parser);
return false;
}
} }
} }
lex_close(parser->lex); lex_close(parser->lex);
for (i = 0; i < parser->globals_count; ++i) {
ast_value_delete(parser->globals[i]);
}
MEM_VECTOR_CLEAR(parser, globals);
mem_d(parser); mem_d(parser);
return true; return true;
} }