2012-07-16 12:52:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-07-16 11:59:10 +00:00
|
|
|
#include "gmqcc.h"
|
|
|
|
#include "lexer.h"
|
|
|
|
|
2012-07-16 12:14:37 +00:00
|
|
|
typedef struct {
|
|
|
|
lex_file *lex;
|
|
|
|
int tok;
|
2012-07-16 12:52:52 +00:00
|
|
|
|
|
|
|
MEM_VECTOR_MAKE(ast_value*, globals);
|
2012-07-16 12:14:37 +00:00
|
|
|
} parser_t;
|
|
|
|
|
2012-07-16 12:52:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-16 12:14:37 +00:00
|
|
|
bool parser_do(parser_t *parser)
|
|
|
|
{
|
2012-07-16 12:52:52 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-07-16 12:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-16 11:59:10 +00:00
|
|
|
bool parser_compile(const char *filename)
|
|
|
|
{
|
2012-07-16 12:52:52 +00:00
|
|
|
size_t i;
|
2012-07-16 12:14:37 +00:00
|
|
|
parser_t *parser;
|
|
|
|
|
|
|
|
parser = (parser_t*)mem_a(sizeof(parser_t));
|
|
|
|
if (!parser)
|
|
|
|
return false;
|
|
|
|
|
2012-07-16 12:52:52 +00:00
|
|
|
MEM_VECTOR_INIT(parser, globals);
|
2012-07-16 12:14:37 +00:00
|
|
|
parser->lex = lex_open(filename);
|
|
|
|
|
|
|
|
if (!parser->lex) {
|
|
|
|
printf("failed to open file \"%s\"\n", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-16 12:52:52 +00:00
|
|
|
/* initial lexer/parser state */
|
|
|
|
parser->lex->flags.noops = true;
|
|
|
|
|
|
|
|
if (parser_next(parser))
|
2012-07-16 12:14:37 +00:00
|
|
|
{
|
2012-07-16 12:52:52 +00:00
|
|
|
while (parser->tok != TOKEN_EOF && parser->tok < TOKEN_ERROR)
|
|
|
|
{
|
|
|
|
if (!parser_do(parser)) {
|
2012-07-19 09:22:09 +00:00
|
|
|
if (parser->tok == TOKEN_EOF)
|
|
|
|
break;
|
2012-07-16 12:52:52 +00:00
|
|
|
printf("parse error\n");
|
|
|
|
lex_close(parser->lex);
|
|
|
|
mem_d(parser);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-16 12:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lex_close(parser->lex);
|
2012-07-16 12:52:52 +00:00
|
|
|
for (i = 0; i < parser->globals_count; ++i) {
|
|
|
|
ast_value_delete(parser->globals[i]);
|
|
|
|
}
|
|
|
|
MEM_VECTOR_CLEAR(parser, globals);
|
|
|
|
|
2012-07-16 12:14:37 +00:00
|
|
|
mem_d(parser);
|
|
|
|
return true;
|
2012-07-16 11:59:10 +00:00
|
|
|
}
|