about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/compiler.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-02-28T20·35+0200
committertazjin <mail@tazj.in>2021-03-01T21·09+0000
commit6f600c8300c028beb07bf224baf7dfdaa6490fd3 (patch)
tree01d50ec6a56be6db105c386b83664915224de757 /users/tazjin/rlox/src/bytecode/compiler.rs
parent3b33c1bd7627c9427a410276c2a49c2b04f70edc (diff)
feat(tazjin/rlox): Add initial support for strings r/2258
... 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 <mail@tazj.in>
Diffstat (limited to 'users/tazjin/rlox/src/bytecode/compiler.rs')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index 8d3f716d16..39bb2f4907 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);