From c6e575eaa6e9271f35137e71d9fff54fbd9cf5ca Mon Sep 17 00:00:00 2001 From: c+1 Date: Mon, 15 May 2023 17:25:01 -0400 Subject: my favorieveh part of parsisud is when dr parser persius says "its parsn time!" and just abifhfuckidngly PARSES all over those guys --- src/parser.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/parser.c b/src/parser.c index 2cc0d39..4da693b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,12 +1,13 @@ -#include "include/parser.h" -#include "include/token.h" - - #include #include #include +#include "include/log.h" +#include "include/parser.h" +#include "include/token.h" + + // initialize a parser parser_t* parser_init(lexer_t* lexer) { parser_t* parser = calloc(1, sizeof(struct PARSER_STRUC)); @@ -22,9 +23,7 @@ void parser_check_expect(parser_t* parser, int token_type) { if (parser->token->type == token_type) { parser->token = lexer_get_next_token(parser->lexer); } else { - fprintf(stderr, "[ERRR] [%d]\n\tUnexpected token '%s' with type '%d'.", - parser->lexer->i, parser->token->value, parser->token->type); - + log_err("Unexpected token"); exit(1); } } @@ -56,6 +55,7 @@ tree_t* parser_parse_chunk(parser_t* parser) { } } +// parse a list of chunks tree_t* parser_parse_chunks(parser_t* parser) { tree_t* subtree = tree_init(TREE_SUBTREE); subtree->data.subtree.val = calloc(1, sizeof(struct TREE_STRUC)); @@ -89,10 +89,23 @@ tree_t* parser_parse_fn_call(parser_t* parser) {}; tree_t* parser_parse_fn_def(parser_t* parser) {}; -tree_t* parser_parse_var(parser_t* parser) {}; +tree_t* parser_parse_var(parser_t* parser) { + char* token_val = parser->token->value; + + parser_check_expect(parser, TOKEN_KEYWORD); // var name or fn name + + // check if function + if (parser->token->type == TOKEN_FN_APPLY) { + return parser_parse_fn_call(parser); + } + + tree_t* var = tree_init(TREE_VAR); + var->data.var.name = token_val; + return var; +}; tree_t* parser_parse_var_def(parser_t* parser) { - int var_is_const = 0; + int var_is_const; parser_check_expect(parser, TOKEN_KEYWORD); // let char* var_name = parser->token->value; // set variable name parser_check_expect(parser, TOKEN_KEYWORD); // expect variable name & advance @@ -101,13 +114,14 @@ tree_t* parser_parse_var_def(parser_t* parser) { var_is_const = 1; parser_check_expect(parser, TOKEN_DEFINE_CONST); } else { + var_is_const = 0; parser_check_expect(parser, TOKEN_DEFINE_MUT); } tree_t* var_val = parser_parse_expr(parser); // set the value - tree_t* var_def = tree_init(TREE_VAR_DEF); // create the var + tree_t* var_def = tree_init(TREE_VAR_DEF); // create the var, as a subtree - var_def->data.var_def.name = var_name; + var_def->data.var_def.name = var_name; var_def->data.var_def.val = var_val; var_def->data.var_def.is_const = &var_is_const; @@ -122,7 +136,12 @@ tree_t* parser_parse_keyword(parser_t* parser) { } else if (strcmp(parser->token->value, "fn")) { return parser_parse_fn_def(parser); } else { - return parser_parse_var(parser); + return parser_parse_var(parser); // assume attempting to call + // variable/function; + // TODO: differentiate b/w the + // two on call; may be a syntax + // change :P just to make the + // interpreter easier } } -- cgit v1.2.3