From 6f600c8300c028beb07bf224baf7dfdaa6490fd3 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 28 Feb 2021 22:35:26 +0200 Subject: feat(tazjin/rlox): Add initial support for strings ... including concatenation. This diverges significantly from the book, as I'm using std::String instead of implementing the book's whole heap object management system. It's possible that Lox in Rust actually doesn't need a GC and the ownership model works just fine. Change-Id: I374a0461d627cfafc26b2b54bfefac8b7c574d00 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2577 Tested-by: BuildkiteCI Reviewed-by: tazjin --- users/tazjin/rlox/src/bytecode/vm.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'users/tazjin/rlox/src/bytecode/vm.rs') diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs index 634d4eced146..730eee321206 100644 --- a/users/tazjin/rlox/src/bytecode/vm.rs +++ b/users/tazjin/rlox/src/bytecode/vm.rs @@ -104,10 +104,32 @@ impl VM { ); } - OpCode::OpAdd => binary_op!(self, Number, +), OpCode::OpSubtract => binary_op!(self, Number, -), OpCode::OpMultiply => binary_op!(self, Number, *), OpCode::OpDivide => binary_op!(self, Number, /), + + OpCode::OpAdd => { + let b = self.pop(); + let a = self.pop(); + + match (a, b) { + (Value::String(s_a), Value::String(s_b)) => { + let mut new_s = s_a.clone(); + new_s.push_str(&s_b); + self.push(Value::String(new_s)); + } + + (Value::Number(n_a), Value::Number(n_b)) => + self.push(Value::Number(n_a + n_b)), + + _ => return Err(Error { + line: self.chunk.get_line(self.ip - 1), + kind: ErrorKind::TypeError( + "'+' operator only works on strings and numbers".into() + ), + }) + } + } } #[cfg(feature = "disassemble")] -- cgit 1.4.1