From f1e455d2fa84067edda695cbba5ddb9e5b77235e Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Wed, 4 Jan 2023 13:41:19 -0800 Subject: functions parse now i think --- src/main.c | 46 ++++++++++++++++++++++++++++------------------ src/parser.c | 34 ++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/src/main.c b/src/main.c index d281524..3a4fe33 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ #include int main(int argc, char **argv) { - /* TEST LEXER */ + /* DONE: TEST LEXER */ /* lexer_t *lexer = init_lexer("'(fasd asdf)"); */ /* token_t *t = lexer_collect_next(lexer); */ /* while (t != NULL) { */ @@ -21,7 +21,7 @@ int main(int argc, char **argv) { /* t = lexer_collect_next(lexer); */ /* } */ - /* TEST REPL POSSIBILITY */ + /* TODO: TEST REPL POSSIBILITY */ /* printf("Welcome to the NXS REPL.\n"); */ /* char *buf = malloc(2); */ @@ -43,30 +43,40 @@ int main(int argc, char **argv) { /* } */ /* } */ - /* TEST PARSER, VISITOR, PRINTER (self evaluating types) */ + /* DONE: TEST PARSER, VISITOR, PRINTER (self evaluating types) */ /* lexer_t *lexer = init_lexer("\"hello world\""); */ /* parser_t *parser = init_parser(lexer); */ /* visitor_t *visitor = init_visitor(parser); */ /* ast_t *root = eval(visitor); */ /* print_root(root); */ - /* TEST PARSING LISTS */ - lexer_t *lexer = init_lexer("(hello world)"); + /* DONE: TEST PARSING LISTS */ + /* lexer_t *lexer = init_lexer("(hello (world #f))"); */ + /* parser_t *parser = init_parser(lexer); */ + /* ast_t *root = parse_all(parser); */ + /* ast_t *list = root->subnodes[0]; */ + /* ast_t *hello = list->car; */ + /* ast_t *inner = list->cdr->car; */ + /* ast_t *world = inner->car; */ + /* ast_t *fal = inner->cdr->car; */ + /* printf("%s\n", hello->string_value); */ + /* printf("%s\n", world->string_value); */ + /* printf("%d\n", fal->bool_value); */ + + /* TODO: TEST PARSING FUNCTIONS */ + lexer_t *lexer = init_lexer("(lambda (x) (y))"); + printf("why is code not working, part 1\n"); parser_t *parser = init_parser(lexer); + printf("why is code not working, part 2\n"); ast_t *root = parse_all(parser); - ast_t *list = root->subnodes[0]; - ast_t *hello = list->car; - ast_t *world = list->cdr->car; - printf("%s\n", hello->string_value); - printf("%s\n", world->string_value); - /* ast_t *e2 = list->cdr->car; */ - /* if (e2 == NULL) { */ - /* printf("something is wrong here...\n"); */ - /* exit(0); */ - /* } */ - - /* printf("%s\n", e2->string_value); */ - /* printf("number 3\n"); */ + ast_t *func = root->subnodes[0]; + printf("%d\n", func->type); + /* DONE: TEST PARSING QUOTE */ + /* lexer_t *lexer = init_lexer("'(hello)"); */ + /* parser_t *parser = init_parser(lexer); */ + /* ast_t *root = parse_all(parser); */ + /* ast_t *quote = root->subnodes[0]; */ + /* printf("%s\n", quote->cdr->car->car->string_value); */ return 0; } diff --git a/src/parser.c b/src/parser.c index 2f592b9..62e1c5f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -45,6 +45,8 @@ void parser_move(parser_t *parser) { void parser_eat(parser_t *parser, int type) { parser_move(parser); + if (parser->tokens[parser->i] == NULL) + printf("weird.\n"); if (parser->tokens[parser->i]->type != type) parser_error(parser); } @@ -92,7 +94,7 @@ ast_t *parse_function_args(parser_t *parser) { token_t *current_token = parser->tokens[parser->i]; while (current_token->type != TOKEN_RPAREN) { - if (parser->tokens[parser->i]->type != TOKEN_ID) + if (current_token->type != TOKEN_ID) parser_error(parser); car = parse_symbol(parser); @@ -101,7 +103,6 @@ ast_t *parse_function_args(parser_t *parser) { cur->cdr = init_ast_pair(NULL, NULL); cur = cur->cdr; - parser_move(parser); current_token = parser->tokens[parser->i]; } @@ -118,7 +119,9 @@ ast_t *parse_function(parser_t *parser) { parse_expr(parser); /* a function can contain a single expression */ if (cdr == NULL) parser_error(parser); - parser_eat(parser, TOKEN_RPAREN); + if (parser->tokens[parser->i]->type != TOKEN_RPAREN) + parser_error(parser); + parser_move(parser); return init_ast_function(car, cdr); } @@ -144,13 +147,10 @@ ast_t *parse_list(parser_t *parser) { bool first_entry = true; token_t *current_token = parser->tokens[parser->i]; while (current_token->type != TOKEN_RPAREN) { - if (parser->tokens[parser->i]->type == TOKEN_ID) { - if (strcmp(parser->tokens[parser->i]->value, "lambda") == 0 && - first_entry) { - car = parse_function(parser); - first_entry = false; - } else if (strcmp(parser->tokens[parser->i]->value, "bind") == 0 && - first_entry) { + if (current_token->type == TOKEN_ID) { + if (strcmp(current_token->value, "lambda") == 0 && first_entry) { + return parse_function(parser); + } else if (strcmp(current_token->value, "bind") == 0 && first_entry) { parse_bind(parser); return NULL; } else { @@ -175,6 +175,8 @@ ast_t *parse_quote(parser_t *parser) { parser_move(parser); ast_t *car = init_ast_symbol("quote"); ast_t *expr = parse_expr(parser); + if (expr == NULL) + parser_error(parser); ast_t *ret = init_ast_pair( car, init_ast_pair( expr, init_ast_pair(NULL, NULL))); /* Converts ' to `quote` */ @@ -199,9 +201,8 @@ ast_t *parse_expr(parser_t *parser) { return parse_symbol(parser); else if (t->type == TOKEN_LPAREN) return parse_list(parser); - else { + else parser_error(parser); - } return NULL; } @@ -212,8 +213,10 @@ ast_t *parse_all(parser_t *parser) { int i = 0; while (t != NULL) { cur = parse_expr(parser); - if (cur == NULL) + if (cur == NULL) { + t = parser->tokens[parser->i]; continue; + } i++; asts = realloc(asts, i * sizeof(ast_t *)); asts[i - 1] = cur; @@ -222,4 +225,7 @@ ast_t *parse_all(parser_t *parser) { return init_ast_root(asts, i); } -void parser_error(parser_t *parser) { exit(1); } +void parser_error(parser_t *parser) { + printf("PARSER ERROR: something went wrong.\n"); + exit(1); +} -- cgit