From 8ea397881cf21607835790b1ed38d9f0a02b304c Mon Sep 17 00:00:00 2001 From: ctsk <9384305+ctsk@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:44:37 +0200 Subject: [PATCH] [rlox] Add trace to repl --- rlox/src/main.rs | 3 +++ rlox/src/vm.rs | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/rlox/src/main.rs b/rlox/src/main.rs index bd5feb4..192f4db 100644 --- a/rlox/src/main.rs +++ b/rlox/src/main.rs @@ -11,12 +11,15 @@ use vm::VM; fn repl() { let mut buffer = String::new(); + let do_trace = env::var("LOX_TRACE").is_ok(); + loop { match io::stdin().read_line(&mut buffer) { Ok(_) => { let mut chunk = Chunk::new(); lc::compile(buffer.as_str(), &mut chunk); let mut vm = VM::new(); + vm.set_trace(do_trace); let result = vm.run(&chunk); println!("{:?}", result); buffer.clear(); diff --git a/rlox/src/vm.rs b/rlox/src/vm.rs index 43d86ce..26a5cf3 100644 --- a/rlox/src/vm.rs +++ b/rlox/src/vm.rs @@ -23,6 +23,10 @@ impl VM { } } + pub fn set_trace(&mut self, trace: bool) { + self.trace = trace; + } + fn runtime_err(&self, msg: &'static str) -> VMError { VMError::Runtime(msg.into(), self.pc) }