frontend: repl: add show-lex and show-ast command options

This commit is contained in:
2024-12-06 20:24:50 +00:00
parent 175e191aa6
commit bd377b00f4

View File

@@ -9,7 +9,8 @@
#include <stdbool.h> #include <stdbool.h>
enum { enum {
OPT_LEX_ONLY = 0x1000u, OPT_SHOW_LEX_TOKENS = 100,
OPT_SHOW_AST_NODES,
}; };
static void skip_line(struct ivy_lexer *lex) static void skip_line(struct ivy_lexer *lex)
@@ -28,8 +29,10 @@ static void skip_line(struct ivy_lexer *lex)
int repl(const b_command *cmd, const b_arglist *args, const b_array *_) int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
{ {
bool lex_only bool show_lex
= b_arglist_get_count(args, OPT_LEX_ONLY, B_COMMAND_INVALID_ID) > 0; = 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 line_ed *ed = line_ed_create(); struct line_ed *ed = line_ed_create();
line_ed_set_flags(ed, LINE_ED_REMOVE_CONTINUATIONS); line_ed_set_flags(ed, LINE_ED_REMOVE_CONTINUATIONS);
@@ -66,11 +69,8 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
continue; continue;
} }
if (show_lex) {
print_lex_token(tok); print_lex_token(tok);
if (lex_only) {
ivy_token_destroy(tok);
continue;
} }
status = ivy_parser_push_token(parser, tok); status = ivy_parser_push_token(parser, tok);
@@ -87,7 +87,11 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
} }
struct ivy_ast_node *root = ivy_parser_root_node(parser); struct ivy_ast_node *root = ivy_parser_root_node(parser);
ivy_ast_node_print(root);
if (show_ast) {
struct ivy_ast_node_iterator it = {0};
ivy_ast_node_iterate(root, &it, print_ast_node);
}
} }
ivy_parser_destroy(parser); ivy_parser_destroy(parser);
@@ -104,12 +108,20 @@ B_COMMAND(CMD_REPL, CMD_ROOT)
B_COMMAND_HELP_OPTION(); B_COMMAND_HELP_OPTION();
B_COMMAND_FUNCTION(repl); B_COMMAND_FUNCTION(repl);
B_COMMAND_OPTION(OPT_LEX_ONLY) B_COMMAND_OPTION(OPT_SHOW_LEX_TOKENS)
{ {
B_OPTION_LONG_NAME("lex-only"); B_OPTION_LONG_NAME("show-lex");
B_OPTION_SHORT_NAME('L'); B_OPTION_SHORT_NAME('l');
B_OPTION_DESC( B_OPTION_DESC(
"print the lexical tokens generated by the user " "print the lexical tokens generated from the input.");
}
B_COMMAND_OPTION(OPT_SHOW_AST_NODES)
{
B_OPTION_LONG_NAME("show-ast");
B_OPTION_SHORT_NAME('a');
B_OPTION_DESC(
"print the abstract syntax tree generated from the "
"input."); "input.");
} }
} }