From 846255c2c3eb9c4be2e4d6a2d41026cd54003bca Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sun, 7 Apr 2024 00:39:49 -0700 Subject: do command strings with a readtable, too Change-Id: Ife32049006a9bf4cadcb3100300f487331dd8c09 --- src/main.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3866316..9055d5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,6 +117,7 @@ fn main() { (Mode::Integer, make_integer_readtable()), (Mode::Decimal, make_decimal_readtable()), (Mode::Str, make_string_readtable()), + (Mode::CommandNamed, make_command_string_readtable()), ]), wip_str: String::from(""), // TODO: I don't think we need a return stack @@ -219,10 +220,14 @@ struct RPState { // Err(err_msg("(shuf) not implemented yet!".into())) // } -fn command_str(c: char, state: &mut RPState) -> Result<(), Exit> { - if c != ')' { +fn make_command_string_readtable() -> ReadTable { + let mut result = ReadTable::new(move |c, state| { state.wip_str.push(c); - } else if c == ')' { + + Ok(()) + }); + + result.set_action(')', move |_, state| { match state.wip_str.as_str() { // TODO: // (times) @@ -238,22 +243,22 @@ fn command_str(c: char, state: &mut RPState) -> Result<(), Exit> { Value::Str(eval_body) => { state.wip_str.clear(); state.mode = Mode::CommandChar; - return eval(&eval_body, state); + eval(&eval_body, state) } _ => { - return Err(err_msg(format!( + Err(err_msg(format!( "Unable to execute ({}) as a named word", word))) } } } else { - return Err(err_msg(format!( + Err(err_msg(format!( "Unable to execute ({}) as a named word", word))) } } } - } + }); - Ok(()) + result } @@ -586,7 +591,6 @@ fn eval(input: &str, state: &mut RPState) -> Result<(), Exit> { } else { match state.mode { Mode::CommandChar => command_char(c, state), - Mode::CommandNamed => command_str(c, state), Mode::RegisterChar => register_char(c, state), Mode::RegisterStr => register_str(c, state), _ => return Err(err_msg( -- cgit 1.4.1