From 3495c3390361a9952de7ace2838b23b04bde7514 Mon Sep 17 00:00:00 2001 From: s-over-4 Date: Sun, 9 Jul 2023 23:36:39 -0400 Subject: hi Ī£airu --- src/include/parser.h | 22 +++--------- src/include/tree.h | 6 ++-- src/parser.c | 100 ++++++++++++++++++++++----------------------------- 3 files changed, 51 insertions(+), 77 deletions(-) diff --git a/src/include/parser.h b/src/include/parser.h index 694bd5e..0ba2038 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -13,37 +13,25 @@ parser_t* parser_init(lexer_t* lexer); void parser_destroy(parser_t* parser); // expect token, or die -void parser_token_expect(parser_t* parser, token_t* token); +void parser_token_expect(parser_t* parser, int token); void parser_token_expectf(parser_t* parser, int (*expected_token)(token_t*)); // do the parse tree_t* parser_parse(parser_t* parser); -// parse statements -// - end in semicolon or ) or } -// - can contain one expression -// - return what they evaluate to -tree_t* parser_parse_statements(parser_t* parser); -tree_t* parser_parse_statement(parser_t* parser); - // parse expressions -// - in parenthetical -// - contain only one statement -// - return what that statement evaluates to -// - adds layer of scope) -// basically a statement, but with a layer of scope +// - 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 -// - many statements -// - return what last statement evaluates to +// - contains many exprs +// - returns last expr tree_t* parser_parse_blocks(parser_t* parser); tree_t* parser_parse_block(parser_t* parser); -// blocks contain many statements, any of which may contain any number of expressions - tree_t* parser_parse_def(parser_t* parser); tree_t* parser_parse_call(parser_t* parser); diff --git a/src/include/tree.h b/src/include/tree.h index 6374062..fde66ea 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -19,9 +19,11 @@ typedef struct TREE_STRUC { } comp; struct { // === DEFINITIONS === - char* name; // name of definition - int mutability; // mutability of definition + char* type; // the definition type + char** tags; // the definition tags + char* name; // the definition name struct TREE_STRUC* value; // value of definition + // :D lines line up :D } def; struct { // === CALLS === diff --git a/src/parser.c b/src/parser.c index ae6f615..019051b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -20,52 +20,67 @@ parser_t* parser_init(lexer_t* lexer) { void parser_destroy(parser_t* parser) { free(parser); } -void parser_token_expect(parser_t* parser, token_t* token) { - if (token == parser->token) { +void parser_token_expect(parser_t* parser, int token) { + token == parser->token->type? + parser->token = lexer_get_next_token(parser->lexer): die( - "unexpected token!\n\ttype: [%s]\n\tvalue: [%s]", + "unexpected token\n\ttype: [%s]\n\tvalue: [%s]", token_get_type(parser->token->type), parser->token->value ); - } } void parser_token_expectf(parser_t* parser, int (*expected_token)(token_t*)) { expected_token(parser->token)? parser->token = lexer_get_next_token(parser->lexer): die( - "unexpected token!\n\ttype: [%s]\n\tvalue: [%s]", + "unexpected token\n\ttype: [%s]\n\tvalue: [%s]", token_get_type(parser->token->type), parser->token->value ); } -tree_t* parser_parse(parser_t* parser) { return parser_parse_statements(parser); } +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"); + } + + return NULL; /* unreachable */ +} -tree_t* parser_parse_statements(parser_t* parser) { +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_statement(parser); - + statement = parser_parse_expr(parser); comp->data.comp.value[0] = statement; - comp->data.comp.size += 1; - while (parser->token->type == TOKEN_STMNT_END) { - statement = parser_parse_statement(parser); + tree_t* statement; - comp->data.comp.value[0] = statement; - comp->data.comp.size += 1; - } - - return comp; -} + parser_token_expect(parser, TOKEN_STMNT_END); -tree_t* parser_parse_statement(parser_t* parser) { + statement = parser_parse_expr(parser); + comp->data.comp.size += 1; + 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) { @@ -77,52 +92,21 @@ tree_t* parser_parse_block(parser_t* parser) { } tree_t* parser_parse_def(parser_t* parser) { - tree_t* tree; + tree_t* def; - tree->type = TREE_DEF; - tree->data.def.name = parser->token->value; + def->type = TREE_DEF; - lexer_next(parser->lexer); + def->data.def.name = parser->token->value; - tree->data.def.mutability = strcmp(parser->token->value, "mut"); - - // collect other tags - // check for arguments - // wait for DEF_SET - // return value as parse_statement (could be just one or block) + def->data.def. + + while (parser->token->type == TOKEN_DEF_TAG) { + + } - return tree; } tree_t* parser_parse_call(parser_t* parser) { - tree_t* tree; - - tree->type = TREE_CALL; - tree->data.call.target = parser->token->value; - - lexer_next(parser->lexer); - - if (parser->token->type == TOKEN_FN_APPLY) { - - lexer_next(parser->lexer); - - tree->data.call.args = realloc(tree->data.call.args, sizeof(tree->data.call.args) + sizeof(parser->token->value)); - tree->data.call.args_size += sizeof(parser->token->value); - - - lexer_next(parser->lexer); - - while (parser->token->type == TOKEN_LIST_DELIM) { - lexer_next(parser->lexer); - tree->data.call.args = realloc(tree->data.call.args, sizeof(tree->data.call.args) + sizeof(parser->token->value)); - tree->data.call.args_size += sizeof(parser->token->value); - lexer_next(parser->lexer); - } - - return tree; - } else { - return tree; - } } tree_t* parser_parse_type_str(parser_t* parser) { -- cgit v1.2.3