examples/interpret: Misc cleanup

This commit is contained in:
John 2023-10-26 22:13:48 -05:00
parent aead97e357
commit e22b1b7bfd

View File

@ -12,7 +12,7 @@ fn main() -> Result<(), Box<dyn Error>> {
take_stdin()?; take_stdin()?;
} else { } else {
for path in conf.paths.iter().map(PathBuf::as_path) { for path in conf.paths.iter().map(PathBuf::as_path) {
parse(&std::fs::read_to_string(path)?, Some(path))?; run_file(&std::fs::read_to_string(path)?, Some(path))?;
} }
} }
Ok(()) Ok(())
@ -32,24 +32,31 @@ fn take_stdin() -> Result<(), Box<dyn Error>> {
const PROMPT: &str = "> "; const PROMPT: &str = "> ";
if stdin().is_terminal() { if stdin().is_terminal() {
let mut interpreter = Interpreter::new(); let mut interpreter = Interpreter::new();
let mut buf = String::new();
print!("{PROMPT}"); print!("{PROMPT}");
stdout().flush()?; stdout().flush()?;
for line in stdin().lines() { while let Ok(len) = stdin().read_line(&mut buf) {
let line = line?; match len {
if !line.is_empty() { 0 => {
let _ = run(&line, &mut interpreter).map_err(|e| eprintln!("{e}"));
println!(); println!();
break;
}
1 => {
let _ = run(&buf, &mut interpreter).map_err(|e| eprintln!("{e}"));
buf.clear();
}
_ => (),
} }
print!("{PROMPT}"); print!("{PROMPT}");
stdout().flush()?; stdout().flush()?;
} }
} else { } else {
parse(&std::io::read_to_string(stdin())?, None)? run_file(&std::io::read_to_string(stdin())?, None)?
} }
Ok(()) Ok(())
} }
fn parse(file: &str, path: Option<&Path>) -> Result<(), Box<dyn Error>> { fn run_file(file: &str, path: Option<&Path>) -> Result<(), Box<dyn Error>> {
match Parser::from(Lexer::new(file)).parse() { match Parser::from(Lexer::new(file)).parse() {
Ok(ast) => Interpreter::new().interpret(&ast)?, Ok(ast) => Interpreter::new().interpret(&ast)?,
Err(e) if e.start().is_some() => print!("{:?}:{}", path.unwrap_or(Path::new("-")), e), Err(e) if e.start().is_some() => print!("{:?}:{}", path.unwrap_or(Path::new("-")), e),
@ -59,19 +66,16 @@ fn parse(file: &str, path: Option<&Path>) -> Result<(), Box<dyn Error>> {
Ok(()) Ok(())
} }
fn run(file: &str, interpreter: &mut Interpreter) -> Result<(), Box<dyn Error>> { fn run(input: &str, interpreter: &mut Interpreter) -> Result<(), Box<dyn Error>> {
// If it parses successfully as a program, run the program // If it parses successfully as a program, run the program
match Parser::from(Lexer::new(file)).parse() { let ast = Parser::from(Lexer::new(input)).parse();
Ok(ast) => interpreter.interpret(&ast)?, if let Ok(ast) = ast {
Err(e) => { interpreter.interpret(&ast)?;
// If not, re-parse as an expression, and print the stack return Ok(());
let Ok(expr) = Parser::from(Lexer::new(file)).parse_expr() else { }
Err(e)? let ast = Parser::from(Lexer::new(input)).parse_expr()?;
}; for value in interpreter.eval(&ast)? {
for value in interpreter.eval(&expr)? {
println!("{value}"); println!("{value}");
} }
}
}
Ok(()) Ok(())
} }