diff options
author | Irene Knapp <ireneista@irenes.space> | 2024-04-07 00:39:49 -0700 |
---|---|---|
committer | Irene Knapp <ireneista@irenes.space> | 2024-04-07 00:39:49 -0700 |
commit | 846255c2c3eb9c4be2e4d6a2d41026cd54003bca (patch) | |
tree | b133bb093807edae803e9859c245ea52f659bccd | |
parent | 2ecd2af4745e165f0fa9c37e4a6a30f4a0a01496 (diff) |
do command strings with a readtable, too
Change-Id: Ife32049006a9bf4cadcb3100300f487331dd8c09
-rw-r--r-- | src/main.rs | 22 |
1 files 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( |