diff options
author | Vincent Ambo <mail@tazj.in> | 2021-01-14T14·44+0300 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-01-14T15·19+0000 |
commit | fe97398fd9d1e20ad4a953e27d080721b949865a (patch) | |
tree | 9212b05097922a9e63ef16fea9be8ebaa18c952b /users/tazjin/rlox/src/main.rs | |
parent | 1ed34443d832fcfd7b683ecdcb58b0c445443def (diff) |
refactor(tazjin/rlox): Thread lifetimes through interpreter r/2104
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
Diffstat (limited to 'users/tazjin/rlox/src/main.rs')
-rw-r--r-- | users/tazjin/rlox/src/main.rs | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/users/tazjin/rlox/src/main.rs b/users/tazjin/rlox/src/main.rs index 8970349bfa69..222eb4cd534f 100644 --- a/users/tazjin/rlox/src/main.rs +++ b/users/tazjin/rlox/src/main.rs @@ -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) { |