about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
Diffstat (limited to 'users')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs16
-rw-r--r--users/tazjin/rlox/src/bytecode/value.rs1
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs24
3 files changed, 40 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index 8d3f716d1647..39bb2f4907a7 100644
--- a/users/tazjin/rlox/src/bytecode/compiler.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler.rs
@@ -144,6 +144,10 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
             ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
         }
 
+        TokenKind::String(_) => {
+            ParseRule::new(Some(Compiler::string), None, Precedence::None)
+        },
+
         _ => ParseRule::new(None, None, Precedence::None),
     }
 }
@@ -255,6 +259,18 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         Ok(())
     }
 
+    fn string(&mut self) -> LoxResult<()> {
+        match &self.previous().kind {
+            TokenKind::String(s) => {
+                let s = s.clone();
+                self.emit_constant(Value::String(s));
+            }
+            _ => unreachable!("only called for strings"),
+        }
+
+        Ok(())
+    }
+
     fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> {
         self.advance();
         let rule: ParseRule<T> = rule_for(&self.previous().kind);
diff --git a/users/tazjin/rlox/src/bytecode/value.rs b/users/tazjin/rlox/src/bytecode/value.rs
index dcd238a7cd39..c6667a698ec9 100644
--- a/users/tazjin/rlox/src/bytecode/value.rs
+++ b/users/tazjin/rlox/src/bytecode/value.rs
@@ -3,6 +3,7 @@ pub enum Value {
     Nil,
     Bool(bool),
     Number(f64),
+    String(String),
 }
 
 impl Value {
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")]