2020-11-23 00:47:27 +01:00
|
|
|
use std::env;
|
2020-11-23 00:59:03 +01:00
|
|
|
use std::fs;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process;
|
2020-11-23 00:47:27 +01:00
|
|
|
|
2021-01-17 18:31:03 +01:00
|
|
|
mod bytecode;
|
2021-01-18 18:27:14 +01:00
|
|
|
mod scanner;
|
2021-01-17 10:13:52 +01:00
|
|
|
mod treewalk;
|
2020-11-23 00:47:27 +01:00
|
|
|
|
2021-01-18 01:21:52 +01:00
|
|
|
/// Trait for making the different interpreters callable in the same
|
|
|
|
/// way.
|
|
|
|
pub trait Lox {
|
2021-02-28 00:13:52 +01:00
|
|
|
type Value: std::fmt::Debug;
|
2021-01-18 01:21:52 +01:00
|
|
|
type Error: std::fmt::Display;
|
|
|
|
|
|
|
|
fn create() -> Self;
|
2021-02-27 10:27:52 +01:00
|
|
|
fn interpret(
|
|
|
|
&mut self,
|
|
|
|
source: String,
|
|
|
|
) -> Result<Self::Value, Vec<Self::Error>>;
|
2021-01-18 01:21:52 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 00:47:27 +01:00
|
|
|
fn main() {
|
2021-01-18 01:21:52 +01:00
|
|
|
let mut args = env::args();
|
|
|
|
if args.len() > 2 {
|
|
|
|
println!("Usage: rlox [script]");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:31:03 +01:00
|
|
|
match env::var("LOX_INTERPRETER").as_ref().map(String::as_str) {
|
2021-02-27 10:27:52 +01:00
|
|
|
Ok("treewalk") => {
|
|
|
|
pick::<treewalk::interpreter::Interpreter>(args.nth(1))
|
|
|
|
}
|
2021-01-18 01:21:52 +01:00
|
|
|
_ => pick::<bytecode::Interpreter>(args.nth(1)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pick<I: Lox>(file_arg: Option<String>) {
|
|
|
|
if let Some(file) = file_arg {
|
|
|
|
run_file::<I>(&file);
|
|
|
|
} else {
|
|
|
|
run_prompt::<I>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run Lox code from a file and print results to stdout
|
|
|
|
fn run_file<I: Lox>(file: &str) {
|
2021-02-27 10:27:52 +01:00
|
|
|
let contents =
|
|
|
|
fs::read_to_string(file).expect("failed to read the input file");
|
2021-01-18 01:21:52 +01:00
|
|
|
let mut lox = I::create();
|
|
|
|
run(&mut lox, contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate Lox code interactively in a shitty REPL.
|
|
|
|
fn run_prompt<I: Lox>() {
|
|
|
|
let mut line = String::new();
|
|
|
|
let mut lox = I::create();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
print!("> ");
|
|
|
|
io::stdout().flush().unwrap();
|
|
|
|
io::stdin()
|
|
|
|
.read_line(&mut line)
|
|
|
|
.expect("failed to read user input");
|
|
|
|
run(&mut lox, std::mem::take(&mut line));
|
|
|
|
line.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run<I: Lox>(lox: &mut I, code: String) {
|
2021-02-28 00:13:52 +01:00
|
|
|
match lox.interpret(code) {
|
|
|
|
Ok(result) => println!("=> {:?}", result),
|
|
|
|
Err(errors) => {
|
|
|
|
for error in errors {
|
|
|
|
eprintln!("{}", error);
|
|
|
|
}
|
2021-01-18 01:21:52 +01:00
|
|
|
}
|
2020-12-31 11:27:10 +01:00
|
|
|
}
|
|
|
|
}
|