talc/talc-bin/src/main.rs

59 lines
1.4 KiB
Rust
Raw Normal View History

2024-02-22 19:27:35 +00:00
use rustyline::error::ReadlineError;
use talc_lang::{Parser, Vm, Symbol, value::{Value, Function}, compiler::repl};
use std::{rc::Rc, io::Write};
2024-02-21 16:04:18 +00:00
2024-02-22 19:27:35 +00:00
fn main() -> Result<(), Box<dyn std::error::Error>> {
2024-02-21 16:04:18 +00:00
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);
2024-02-22 19:27:35 +00:00
let mut rl = rustyline::DefaultEditor::new()?;
2024-02-21 16:04:18 +00:00
loop {
2024-02-22 19:27:35 +00:00
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
},
};
2024-02-21 16:04:18 +00:00
let ex = match parser.parse(&line) {
Ok(ex) => ex,
2024-02-22 19:27:35 +00:00
Err(e) => { eprintln!("Error: {e}"); continue },
2024-02-21 16:04:18 +00:00
};
2024-02-22 19:27:35 +00:00
let (f, g) = repl(&ex, &globals);
globals = g;
let func = Rc::new(f);
2024-02-21 16:04:18 +00:00
2024-02-22 19:27:35 +00:00
Function::disasm_recursive(func.clone(), &mut std::io::stdout())?;
std::io::stdout().flush()?;
match vm.run(func) {
2024-02-21 16:04:18 +00:00
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 {
2024-02-22 19:27:35 +00:00
println!("{v:#}");
2024-02-21 16:04:18 +00:00
}
}
2024-02-22 19:27:35 +00:00
Err(e) => eprintln!("{e}"),
2024-02-21 16:04:18 +00:00
}
}
2024-02-22 19:27:35 +00:00
Ok(())
2024-02-21 16:04:18 +00:00
}