refactor(tazjin/rlox): Thread lifetimes through interpreter

In order to store a function in the interpreter's representation of a
callable, the lifetimes used throughout rlox need to be threaded
through properly.

This is currently not optimal, for two reasons:

* following the design of the book's scanner, the source code slice
  needs to still be available at runtime. Rust makes this explicit,
  but it seems unnecessary.

* the interpreter's lifetime is now bounded to be smaller than the
  source's, which means that the REPL no longer persists state between
  evaluations

Both of these can be fixed eventually by diverging the scanner from
the book slightly, but right now that's not my priority.

Change-Id: Id0bf694541ff59795cfdea3c64a965384a49bfe2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2391
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2021-01-14 17:44:53 +03:00 committed by tazjin
parent 1ed34443d8
commit fe97398fd9
3 changed files with 59 additions and 48 deletions

View file

@ -25,14 +25,12 @@ fn main() {
// Run Lox code from a file and print results to stdout
fn run_file(file: &str) {
let contents = fs::read_to_string(file).expect("failed to read the input file");
let mut lox = interpreter::Interpreter::create();
run(&mut lox, &contents);
run(&contents);
}
// Evaluate Lox code interactively in a shitty REPL.
fn run_prompt() {
let mut line = String::new();
let mut lox = interpreter::Interpreter::create();
loop {
print!("> ");
@ -40,13 +38,14 @@ fn run_prompt() {
io::stdin()
.read_line(&mut line)
.expect("failed to read user input");
run(&mut lox, &line);
run(&line);
line.clear();
}
}
fn run(lox: &mut interpreter::Interpreter, code: &str) {
fn run(code: &str) {
let chars: Vec<char> = code.chars().collect();
let mut lox = interpreter::Interpreter::create();
match scanner::scan(&chars) {
Ok(tokens) => match parser::parse(tokens) {