diff options
author | Vincent Ambo <mail@tazj.in> | 2021-03-02T20·15+0200 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-03-03T10·50+0000 |
commit | 2cd77ea26d76b20ff820f1ebe5e77f1360f5d1f5 (patch) | |
tree | aada926332ef86b2252f1b3ebcfc6d2c768ce40d /users/tazjin/rlox/src/bytecode/vm.rs | |
parent | 432e7a7dddf56224297285c9f47f0aa3963eb5b5 (diff) |
feat(tazjin/rlox): Add support for print statement r/2264
Change-Id: Ic3e7e722325c8784b848c0bcd573c2e51e123c40 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2583 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/bytecode/vm.rs')
-rw-r--r-- | users/tazjin/rlox/src/bytecode/vm.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs index 02db30de58a9..c43b197279be 100644 --- a/users/tazjin/rlox/src/bytecode/vm.rs +++ b/users/tazjin/rlox/src/bytecode/vm.rs @@ -72,6 +72,10 @@ impl VM { match op { OpCode::OpReturn => { + if self.stack.is_empty() { + return Ok(Value::Nil); + } + let val = self.pop(); return Ok(self.return_value(val)); } @@ -135,6 +139,11 @@ impl VM { }) } } + + OpCode::OpPrint => { + let val = self.pop(); + println!("{}", self.print_value(val)); + } } #[cfg(feature = "disassemble")] @@ -159,6 +168,16 @@ impl VM { LoxString::Interned(id) => self.strings.lookup(*id), } } + + fn print_value(&self, val: Value) -> String { + match val { + Value::String(LoxString::Heap(s)) => s, + Value::String(LoxString::Interned(id)) => { + self.strings.lookup(id).into() + } + _ => format!("{:?}", val), + } + } } pub fn interpret(strings: Interner, chunk: chunk::Chunk) -> LoxResult<Value> { |