about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/value.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-03-02T11·11+0200
committertazjin <mail@tazj.in>2021-03-02T19·48+0000
commit432e7a7dddf56224297285c9f47f0aa3963eb5b5 (patch)
treede3ee9a64b116e43f4ea4911e27d9a58603de9b7 /users/tazjin/rlox/src/bytecode/value.rs
parentbcea8e0d169974ba57a54ca098f480d6294a7fb1 (diff)
feat(tazjin/rlox): Intern all string constants r/2263
This is again a step closer to the book, but there are some notable
differences:

* Only constants encountered by the compiler are interned, all other
  string operations (well, concatenation) happen with heap objects.

* OpReturn will always ensure that a returned string value is newly
  heap allocated and does not reference the interner.

Change-Id: If4f04309446e01b8ff2db51094e9710d465dbc50
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2582
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/bytecode/value.rs')
-rw-r--r--users/tazjin/rlox/src/bytecode/value.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/bytecode/value.rs b/users/tazjin/rlox/src/bytecode/value.rs
index c6667a698ec9..19a4dcc92eac 100644
--- a/users/tazjin/rlox/src/bytecode/value.rs
+++ b/users/tazjin/rlox/src/bytecode/value.rs
@@ -1,9 +1,29 @@
+use super::interner::InternedStr;
+
 #[derive(Clone, Debug, PartialEq)]
 pub enum Value {
     Nil,
     Bool(bool),
     Number(f64),
-    String(String),
+    String(LoxString),
+}
+
+#[derive(Clone, Debug, PartialEq)]
+pub enum LoxString {
+    Heap(String),
+    Interned(InternedStr),
+}
+
+impl From<String> for LoxString {
+    fn from(s: String) -> Self {
+        LoxString::Heap(s)
+    }
+}
+
+impl From<InternedStr> for LoxString {
+    fn from(s: InternedStr) -> Self {
+        LoxString::Interned(s)
+    }
 }
 
 impl Value {