refactor(tazjin/rlox): Let scanner tokens own their lexeme

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
This commit is contained in:
Vincent Ambo 2021-01-14 18:36:06 +03:00 committed by tazjin
parent 1d8e3f4f8b
commit 20a6cfeee2
5 changed files with 150 additions and 158 deletions

View file

@ -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) {