diff --git a/frontend/cmd/compile.c b/frontend/cmd/compile.c index 16ddb49..66f7d14 100644 --- a/frontend/cmd/compile.c +++ b/frontend/cmd/compile.c @@ -17,8 +17,12 @@ enum { static int compile_file(const char *path, const b_arglist *args) { - bool show_lex = b_arglist_get_count(args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID) > 0; - bool show_ast = b_arglist_get_count(args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID) > 0; + bool show_lex = b_arglist_get_count( + args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID) + > 0; + bool show_ast = b_arglist_get_count( + args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID) + > 0; FILE *fp = fopen(path, "r"); if (!fp) { @@ -83,7 +87,8 @@ static int compile_file(const char *path, const b_arglist *args) if (r == 0 && show_ast) { struct ivy_ast_node *root = ivy_parser_root_node(parser); struct ivy_ast_node_iterator it = {0}; - ivy_ast_node_iterate(root, &it, print_ast_node); + ivy_ast_node_iterate( + root, &it, IVY_AST_ITERATE_REGULAR, print_ast_node); } if (lex) { diff --git a/frontend/cmd/repl.c b/frontend/cmd/repl.c index c1ffe73..2175098 100644 --- a/frontend/cmd/repl.c +++ b/frontend/cmd/repl.c @@ -13,6 +13,10 @@ enum { OPT_SHOW_AST_NODES, }; +struct repl { + bool r_show_lex, r_show_ast; +}; + static void skip_line(struct ivy_lexer *lex) { while (ivy_lexer_tokens_available(lex)) { @@ -27,24 +31,49 @@ static void skip_line(struct ivy_lexer *lex) } } +static int repl_eval_node(struct repl *repl, struct ivy_ast_node *node) +{ + struct ivy_ast_node_iterator it = {0}; + + if (repl->r_show_ast) { + ivy_ast_node_iterate( + node, &it, IVY_AST_ITERATE_REGULAR, print_ast_node); + } + + ivy_ast_node_iterate(node, &it, IVY_AST_ITERATE_POSTORDER, print_ast_node); + + return 0; +} + int repl(const b_command *cmd, const b_arglist *args, const b_array *_) { - b_printf("[bold,bright_red]error[[E0384][reset,bold,white]: cannot assign twice to immutable variable `i`[reset]\n"); + b_printf( + "[bold,bright_red]error[[E0384][reset,bold,white]: cannot " + "assign twice to immutable variable `i`[reset]\n"); b_printf("[bold,bright_blue] -->[reset] src/main.rs:7:3\n"); b_printf("[bold,bright_blue] |[reset]\n"); b_printf("[bold,bright_blue]4 |[reset] let i = 0;\n"); b_printf("[bold,bright_blue] | -[reset]\n"); b_printf("[bold,bright_blue] | |[reset]\n"); - b_printf("[bold,bright_blue] | first assignment to `i`[reset]\n"); - b_printf("[bold,bright_blue] | help: make this binding mutable: `mut i`[reset]\n"); + b_printf( + "[bold,bright_blue] | first assignment to " + "`i`[reset]\n"); + b_printf( + "[bold,bright_blue] | help: make this binding " + "mutable: `mut i`[reset]\n"); b_printf("[bold,bright_blue]...[reset]\n"); b_printf("[bold,bright_blue]7 |[reset] i += 1;\n"); - b_printf("[bold,bright_blue] |[bold,bright_red] ^^^^^^ cannot assign twice to immutable variable[reset]\n"); + b_printf( + "[bold,bright_blue] |[bold,bright_red] ^^^^^^ cannot " + "assign twice to immutable variable[reset]\n"); - bool show_lex - = b_arglist_get_count(args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID) > 0; - bool show_ast - = b_arglist_get_count(args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID) > 0; + struct repl repl = {}; + repl.r_show_lex = b_arglist_get_count( + args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID) + > 0; + repl.r_show_ast = b_arglist_get_count( + args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID) + > 0; struct line_ed *ed = line_ed_create(); line_ed_set_flags(ed, LINE_ED_REMOVE_CONTINUATIONS); @@ -81,7 +110,7 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_) continue; } - if (show_lex) { + if (repl.r_show_lex) { print_lex_token(tok); } @@ -98,11 +127,11 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_) continue; } - struct ivy_ast_node *root = ivy_parser_root_node(parser); + struct ivy_ast_node *child = NULL; - if (show_ast) { - struct ivy_ast_node_iterator it = {0}; - ivy_ast_node_iterate(root, &it, print_ast_node); + while ((child = ivy_parser_dequeue_node(parser))) { + repl_eval_node(&repl, child); + ivy_ast_node_destroy(child); } } diff --git a/frontend/line-ed/line-ed.c b/frontend/line-ed/line-ed.c index 5f442d7..a57a737 100644 --- a/frontend/line-ed/line-ed.c +++ b/frontend/line-ed/line-ed.c @@ -1,16 +1,17 @@ #include "line-ed.h" + #include "history.h" #include "hook.h" #include "input.h" #include "prompt.h" +#include #include #include #include #include #include #include -#include #define LINE_ED_FROM_LEX_SOURCE(p) \ ((struct line_ed *)((char *)p - offsetof(struct line_ed, l_line_source))) @@ -22,7 +23,7 @@ static enum ivy_status readline( struct line_ed *ed = LINE_ED_FROM_LEX_SOURCE(src); line_ed_set_scope_type(ed, scope_type); - size_t r = line_ed_readline(ed, out, max); + long r = line_ed_readline(ed, out, max); line_ed_set_scope_type(ed, NULL); if (r < 0) {