diff options
-rw-r--r-- | src/include/parser.h | 50 | ||||
-rw-r--r-- | src/parser.c | 122 |
2 files changed, 17 insertions, 155 deletions
diff --git a/src/include/parser.h b/src/include/parser.h index 7541303..b101485 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -1,48 +1,20 @@ #ifndef PARSER_H #define PARSER_H -#include "lexer.h" +#include "util.h" #include "tree.h" - -#include <stdarg.h> - -typedef struct PARSER_STRUC { - lexer_t* lexer; // lexer used by the parser - token_t* token; // current token - - enum { - DEF, - CAL - } state; - +#include "token.h" + +typedef struct PARSER { + /* the token list being parsed */ + token_t* token; + + /* the abstract syntax tree being generated */ + tree_t* tree; } parser_t; -parser_t* parser_init(lexer_t* lexer); +parser_t* parser_init(token_t* token); +/* free parser struct, **but not þe token list ∨ ast** */ void parser_destroy(parser_t* parser); -// expect tokens, or die -void parser_token_expect(parser_t* parser, int token_type, ...); - -// do the parse -tree_t* parser_parse(parser_t* parser); - -// parse expressions -// - returns what it evaluates to -// - can contain other exprs -tree_t* parser_parse_exprs(parser_t* parser); -tree_t* parser_parse_expr(parser_t* parser); - -// parse blocks -// - in curly brackets -// - contains many exprs -// - returns last expr -tree_t* parser_parse_blocks(parser_t* parser); -tree_t* parser_parse_block(parser_t* parser); - -tree_t* parser_parse_def(parser_t* parser); -tree_t* parser_parse_call(parser_t* parser); - -tree_t* parser_parse_type_str(parser_t* parser); -tree_t* parser_parse_type_int(parser_t* parser); - #endif diff --git a/src/parser.c b/src/parser.c index c32202e..300f3de 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,125 +1,15 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "include/token.h" -#include "include/util.h" -#include "include/tree.h" -#include "include/lexer.h" #include "include/parser.h" -parser_t* parser_init(lexer_t* lexer) { +parser_t* parser_init(token_t* token) { parser_t* parser; - parser = calloc(1, sizeof(struct PARSER_STRUC)); - parser->lexer = lexer; - parser->token = lexer_get_next_token(lexer); + parser = emalloc(sizeof(struct PARSER)); + parser->token = token; + parser->tree = NULL; return parser; } -void parser_destroy(parser_t* parser) { free(parser); } - -void parser_token_expect(parser_t* parser, int tokens, ...) { - va_list ap; - va_start(ap, tokens); - for (int i = 0; i < tokens; ++i) { - va_arg(ap, int) == parser->token->type? - parser->token = lexer_get_next_token(parser->lexer): - die( - "unexpected token\n\ttype: [%s]\n\tvalue: [%s]", - token_get_type(parser->token->type), - parser->token->value - ); - } - va_end(ap); -} - -tree_t* parser_parse(parser_t* parser) { return parser_parse_exprs(parser); } - -tree_t* parser_parse_expr(parser_t* parser) { - switch (parser->token->type) { - case TOKEN_DEF_TAG: - return parser_parse_def(parser); - break; - case TOKEN_KEYWORD: - return parser_parse_call(parser); - break; - default: - die("BAD TOKEN"); /* TODO: make more informative error msgs */ - } - - return NULL; /* unreachable */ - /* hopefully */ -} - -tree_t* parser_parse_exprs(parser_t* parser) { - tree_t* comp; - tree_t* statement; - - comp = tree_init(TREE_COMP); - comp->data.comp.value = calloc(1, sizeof(struct TREE_STRUC*)); - - statement = parser_parse_expr(parser); - comp->data.comp.value[0] = statement; - while (parser->token->type == TOKEN_STMNT_END) { - tree_t* statement; - - parser_token_expect(parser, TOKEN_STMNT_END); - - statement = parser_parse_expr(parser); - comp->data.comp.size ++; - comp->data.comp.value = realloc( - comp->data.comp.value, - comp->data.comp.size * sizeof(struct TREE_STRUC) - ); - comp->data.comp.value[comp->data.comp.size - 1] = statement; - } - - return comp; -} - -tree_t* parser_parse_blocks(parser_t* parser) { - -} - -tree_t* parser_parse_block(parser_t* parser) { - -} - -tree_t* parser_parse_def(parser_t* parser) { - tree_t* def; - - def->type = TREE_DEF; - - while (parser->token->type == TOKEN_DEF_TAG) { - def->data.def.tags_size ++; - def->data.def.tags = realloc( - def->data.def.args, - def->data.def.args_size + sizeof(parser->token->value) - ); - } - -} - -tree_t* parser_parse_call(parser_t* parser) { +void parser_destroy(parser_t* parser) { + free(parser); } - -tree_t* parser_parse_type_str(parser_t* parser) { - tree_t* tree; - - tree->type = TREE_TYPE_STR; - tree->data.type_str.value = parser->token->value; - - return tree; -} - -tree_t* parser_parse_type_int(parser_t* parser) { - tree_t* tree; - - tree->type = TREE_TYPE_INT; - tree->data.type_int.value = strtol(parser->token->value, NULL, 10); - - return tree; -} - |