diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-22 20:42:46 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-22 20:42:46 +0300 |
commit | 55ff37ab09d4a5374ed02089a0d3fd6b8300c20e (patch) | |
tree | 0039ea161850c46e015092287f97a202d6ae23c8 | |
parent | c88bb2e5c14e93a39d7e09651323e64ff644634d (diff) | |
download | myslip-55ff37ab09d4a5374ed02089a0d3fd6b8300c20e.tar.gz myslip-55ff37ab09d4a5374ed02089a0d3fd6b8300c20e.zip |
feat: --babysteps for REPL for easier debugging
-rw-r--r-- | src/main.rs | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index d7a2196..e48917d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,7 @@ fn main() { let mut next_is_load = false; let mut no_stdlib = false; + let mut babysteps = false; let mut help = false; for arg in env::args() { @@ -62,6 +63,7 @@ fn main() { s if s.ends_with("myslip") => continue, "-l" | "--load" => next_is_load = true, "--no-stdlib" => no_stdlib = true, + "--babysteps" => babysteps = true, "--help" | "-h" | "-?" => help = true, s => match file_to_execute { Some(n) => { @@ -88,6 +90,11 @@ fn main() { " --no-stdlib stops myslip from loading the\n" + " standard library at startup\n" + "\n" + + " --babysteps makes the repl evaluate\n" + + " s-expressions one step at a\n" + + " time, displaying the result of\n" + + " each one on a separate line\n" + + "\n" + " --help | -h | -? displays this help message\n" ); @@ -123,10 +130,10 @@ fn main() { return; } }; - execute_expression(&binds, exp); + execute_expression(&binds, exp, false); }, None => { - match repl(binds) { + match repl(binds, babysteps) { Ok(()) => println!("bye :)"), Err(e) => println!("Error: {}", e), } @@ -149,7 +156,7 @@ fn prompt_line(stdin: &io::Stdin, stdout: &mut io::Stdout) -> Result<(bool, Stri Ok((code == 0, line)) } -fn execute_expression(binds: &Vec<SExp>, mut exp: SExp) { +fn execute_expression(binds: &Vec<SExp>, mut exp: SExp, babysteps: bool) { for bind in binds.into_iter().rev() { exp = scons(bind.clone(), exp); @@ -165,19 +172,36 @@ fn execute_expression(binds: &Vec<SExp>, mut exp: SExp) { }, }; - let result = match exp.multistep() { - Ok(t) => t, - Err(e) => { - println!("Runtime error: {}", e); - return; - }, - }; + if babysteps { + if exp.is_value() { + println!("{} : {}", exp, ty); + } + while !exp.is_value() { + exp = match exp.step() { + Ok(t) => t, + Err(e) => { + println!("Runtime error: {}", e); + return; + }, + }; + + println!("{} : {}", exp, ty); + } + } else { + let result = match exp.multistep() { + Ok(t) => t, + Err(e) => { + println!("Runtime error: {}", e); + return; + }, + }; - println!("{} : {}", result, ty); + println!("{} : {}", result, ty); + } } -fn repl(mut binds: Vec<SExp>) -> Result<(), io::Error> { +fn repl(mut binds: Vec<SExp>, babysteps: bool) -> Result<(), io::Error> { let stdin = io::stdin(); let mut stdout = io::stdout(); @@ -219,7 +243,7 @@ fn repl(mut binds: Vec<SExp>) -> Result<(), io::Error> { } } - execute_expression(&binds, orig_expression); + execute_expression(&binds, orig_expression, babysteps); } Ok(()) |