diff options
Diffstat (limited to 'src/parser/parser.c')
-rw-r--r-- | src/parser/parser.c | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/parser/parser.c b/src/parser/parser.c index 065bb32..041422e 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -32,6 +32,7 @@ #include "lexer.h" #include "log.h" #include "parser.h" +#include "parser/types.h" static void tok_error(struct token *tok, const char *fmt, ...) @@ -47,21 +48,28 @@ tok_error(struct token *tok, const char *fmt, ...) static bool parse_declaration(struct lexer *lexer, struct token *tok) { + struct type *typ; + /* - * TODO: Support more types. + * TODO: Support custom types. */ - if (tok->kind != TK_VOID && tok->kind != TK_INT) { - tok_error(tok, "expected \"void\" or \"int\"\n"); + if (!(tok->flags & TF_BUILTIN_TYPE)) { + tok_error(tok, "expected built-in type\n"); + return false; + } + + typ = types_find_builtin(tok->kind); + if (typ == NULL) { return false; } lexer_next(lexer, tok); - if (tok->kind != TK_IDENTIFIER) { + if (tok->kind != TOK_IDENTIFIER) { tok_error(tok, "expected identifier\n"); return false; } - log_debug("Parsed declaration of \"%.*s\"\n", tok->len, tok->pos); + log_debug("name: \"%.*s\", type: { name: \"%s\", size: %lu }\n", tok->len, tok->pos, typ->name, typ->size); lexer_next(lexer, tok); return true; @@ -80,13 +88,13 @@ parser_parse(struct lexer *lexer) return false; } - while (tok.kind != TK_EOF) { - if (tok.kind == TK_UNKNOWN) { + while (tok.kind != TOK_EOF) { + if (tok.kind == TOK_UNKNOWN) { tok_error(&tok, "unrecognized token\n"); return false; } - if ((tok.flags & TF_BUILTIN_TYPE) || tok.kind == TK_IDENTIFIER) { + if ((tok.flags & TF_BUILTIN_TYPE) || tok.kind == TOK_IDENTIFIER) { success = parse_declaration(lexer, &tok); } |