#include "../debug.h" #include "../line-ed/line-ed.h" #include "cmd.h" #include #include #include #include #include enum { OPT_SHOW_LEX_TOKENS = 100, 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)) { struct ivy_token *tok = ivy_lexer_read(lex); bool line_end = (tok->t_type == IVY_TOK_LINEFEED); ivy_token_destroy(tok); if (line_end) { break; } } } 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_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]...[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"); 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); struct ivy_lexer *lex; enum ivy_status status = ivy_lexer_create(&lex); if (status != IVY_OK) { line_ed_destroy(ed); return -1; } ivy_lexer_set_source(lex, &ed->l_line_source); struct ivy_parser *parser; status = ivy_parser_create(&parser); if (status != IVY_OK) { ivy_lexer_destroy(lex); line_ed_destroy(ed); return -1; } while (true) { struct ivy_token *tok = ivy_lexer_read(lex); status = ivy_lexer_get_status(lex); if (status == IVY_ERR_EOF) { break; } if (status != IVY_OK) { b_err("lex error (%s)", ivy_status_to_string(ivy_lexer_get_status(lex))); continue; } if (repl.r_show_lex) { print_lex_token(tok); } status = ivy_parser_push_token(parser, tok); if (status != IVY_OK) { b_err("parse error (%s)", ivy_status_to_string(status)); ivy_token_destroy(tok); skip_line(lex); continue; } if (ivy_lexer_tokens_available(lex)) { continue; } struct ivy_ast_node *child = NULL; while ((child = ivy_parser_dequeue_node(parser))) { repl_eval_node(&repl, child); ivy_ast_node_destroy(child); } } ivy_parser_destroy(parser); ivy_lexer_destroy(lex); line_ed_destroy(ed); return 0; } B_COMMAND(CMD_REPL, CMD_ROOT) { B_COMMAND_NAME("shell"); B_COMMAND_SHORT_NAME('S'); B_COMMAND_DESC("start an interactive Ivy shell."); B_COMMAND_HELP_OPTION(); B_COMMAND_FUNCTION(repl); B_COMMAND_OPTION(OPT_SHOW_LEX_TOKENS) { B_OPTION_LONG_NAME("show-lex"); B_OPTION_SHORT_NAME('l'); B_OPTION_DESC( "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."); } }