frontend: repl: combine repl_full and repl_lex_only
This commit is contained in:
@@ -4,69 +4,36 @@
|
|||||||
|
|
||||||
#include <blue/cmd.h>
|
#include <blue/cmd.h>
|
||||||
#include <blue/term.h>
|
#include <blue/term.h>
|
||||||
#include <ivy/lang/lex.h>
|
|
||||||
#include <ivy/lang/ast.h>
|
#include <ivy/lang/ast.h>
|
||||||
|
#include <ivy/lang/lex.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
OPT_LEX_ONLY = 0x1000u,
|
OPT_LEX_ONLY = 0x1000u,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int repl_lex_only()
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
|
|
||||||
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)));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
print_lex_token(tok);
|
|
||||||
}
|
|
||||||
|
|
||||||
ivy_lexer_destroy(lex);
|
|
||||||
line_ed_destroy(ed);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void skip_line(struct ivy_lexer *lex)
|
static void skip_line(struct ivy_lexer *lex)
|
||||||
{
|
{
|
||||||
while (ivy_lexer_tokens_available(lex)) {
|
while (ivy_lexer_tokens_available(lex)) {
|
||||||
struct ivy_token *tok = ivy_lexer_read(lex);
|
struct ivy_token *tok = ivy_lexer_read(lex);
|
||||||
bool line_end = (tok->t_type == IVY_TOK_LINEFEED);
|
bool line_end = (tok->t_type == IVY_TOK_LINEFEED);
|
||||||
|
|
||||||
ivy_token_destroy(tok);
|
ivy_token_destroy(tok);
|
||||||
|
|
||||||
if (line_end) {
|
if (line_end) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int repl_full()
|
int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
|
||||||
{
|
{
|
||||||
|
bool lex_only
|
||||||
|
= b_arglist_get_count(args, OPT_LEX_ONLY, 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);
|
||||||
|
|
||||||
struct ivy_lexer *lex;
|
struct ivy_lexer *lex;
|
||||||
enum ivy_status status = ivy_lexer_create(&lex);
|
enum ivy_status status = ivy_lexer_create(&lex);
|
||||||
|
|
||||||
@@ -99,11 +66,16 @@ static int repl_full()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (lex_only) {
|
||||||
|
print_lex_token(tok);
|
||||||
|
ivy_token_destroy(tok);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
status = ivy_parser_push_token(parser, tok);
|
status = ivy_parser_push_token(parser, tok);
|
||||||
|
|
||||||
if (status != IVY_OK) {
|
if (status != IVY_OK) {
|
||||||
b_err("parse error (%s)",
|
b_err("parse error (%s)", ivy_status_to_string(status));
|
||||||
ivy_status_to_string(status));
|
|
||||||
ivy_token_destroy(tok);
|
ivy_token_destroy(tok);
|
||||||
skip_line(lex);
|
skip_line(lex);
|
||||||
continue;
|
continue;
|
||||||
@@ -115,15 +87,6 @@ static int repl_full()
|
|||||||
|
|
||||||
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);
|
ivy_ast_node_print(root);
|
||||||
#if 0
|
|
||||||
struct ivy_ast_node *node = ivy_parser_dequeue_node(parser);
|
|
||||||
if (!node) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ivy_ast_node_print(node);
|
|
||||||
ivy_ast_node_destroy(node);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ivy_parser_destroy(parser);
|
ivy_parser_destroy(parser);
|
||||||
@@ -132,15 +95,6 @@ static int repl_full()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
|
|
||||||
{
|
|
||||||
if (b_arglist_get_count(args, OPT_LEX_ONLY, B_COMMAND_INVALID_ID) > 0) {
|
|
||||||
return repl_lex_only();
|
|
||||||
}
|
|
||||||
|
|
||||||
return repl_full();
|
|
||||||
}
|
|
||||||
|
|
||||||
B_COMMAND(CMD_REPL, CMD_ROOT)
|
B_COMMAND(CMD_REPL, CMD_ROOT)
|
||||||
{
|
{
|
||||||
B_COMMAND_NAME("shell");
|
B_COMMAND_NAME("shell");
|
||||||
@@ -149,9 +103,12 @@ 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_LEX_ONLY)
|
||||||
|
{
|
||||||
B_OPTION_LONG_NAME("lex-only");
|
B_OPTION_LONG_NAME("lex-only");
|
||||||
B_OPTION_SHORT_NAME('L');
|
B_OPTION_SHORT_NAME('L');
|
||||||
B_OPTION_DESC("print the lexical tokens generated by the user input.");
|
B_OPTION_DESC(
|
||||||
|
"print the lexical tokens generated by the user "
|
||||||
|
"input.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user