about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/main.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-14T15·36+0300
committertazjin <mail@tazj.in>2021-01-14T16·53+0000
commit20a6cfeee233bde9ba1f482fa4545f5e395d6235 (patch)
tree95c0f22e1fc187135fa66010e22e3ae13d2e5b3c /users/tazjin/rlox/src/main.rs
parent1d8e3f4f8b4479a47e744b3d153fe0e624958817 (diff)
refactor(tazjin/rlox): Let scanner tokens own their lexeme r/2107
This removes the runtime dependency on a borrow into the program
source code.

It's not yet ideal because there are a lot of tokens where we really
don't care about the lexeme, but this is what the book does and I
am not going to change that.

Change-Id: I888e18f98597766d6f725cbf9241e8eb2bd839e2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2394
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.rs9
1 files changed, 5 insertions, 4 deletions
diff --git a/users/tazjin/rlox/src/main.rs b/users/tazjin/rlox/src/main.rs
index 222eb4cd534f..8970349bfa69 100644
--- a/users/tazjin/rlox/src/main.rs
+++ b/users/tazjin/rlox/src/main.rs
@@ -25,12 +25,14 @@ 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");
-    run(&contents);
+    let mut lox = interpreter::Interpreter::create();
+    run(&mut lox, &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!("> ");
@@ -38,14 +40,13 @@ fn run_prompt() {
         io::stdin()
             .read_line(&mut line)
             .expect("failed to read user input");
-        run(&line);
+        run(&mut lox, &line);
         line.clear();
     }
 }
 
-fn run(code: &str) {
+fn run(lox: &mut interpreter::Interpreter, 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) {