use rustyline::error::ReadlineError; use talc_lang::{Parser, Vm, Symbol, value::{Value, Function}, compiler::repl}; use std::{rc::Rc, io::Write}; fn main() -> Result<(), Box> { let parser = Parser::new(); let mut vm = Vm::new(256); let mut globals = Vec::new(); let prev1_sym = Symbol::get("_"); let prev2_sym = Symbol::get("__"); let prev3_sym = Symbol::get("___"); vm.set_global(prev1_sym, Value::Nil); vm.set_global(prev2_sym, Value::Nil); vm.set_global(prev3_sym, Value::Nil); let mut rl = rustyline::DefaultEditor::new()?; loop { let line = rl.readline(">> "); let line = match line { Ok(line) => line, Err(ReadlineError::Eof) => break, Err(ReadlineError::Interrupted) => continue, Err(e) => { eprintln!("Error: {e}"); continue }, }; let ex = match parser.parse(&line) { Ok(ex) => ex, Err(e) => { eprintln!("Error: {e}"); continue }, }; let (f, g) = repl(&ex, &globals); globals = g; let func = Rc::new(f); Function::disasm_recursive(func.clone(), &mut std::io::stdout())?; std::io::stdout().flush()?; match vm.run(func) { Ok(v) => { vm.set_global(prev3_sym, vm.get_global(prev2_sym).unwrap().clone()); vm.set_global(prev2_sym, vm.get_global(prev1_sym).unwrap().clone()); vm.set_global(prev1_sym, v.clone()); if v != Value::Nil { println!("{v:#}"); } } Err(e) => eprintln!("{e}"), } } Ok(()) }