about summary refs log tree commit diff
path: root/tvix/eval/src/chunk.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-07T20·41+0300
committertazjin <tazjin@tvl.su>2022-08-12T13·05+0000
commit3aaefc3000dd80b88d30aefd9e58ceee5a1eddee (patch)
tree7eea6ae2df40781cf7850ac18c0fa38ada1be97a /tvix/eval/src/chunk.rs
parent48a7449834246ad967301f2dc011b019171a5bbc (diff)
feat(tvix/eval): add initial chunk representation r/4405
Change-Id: I53202e93938bede421c8f1c98901e4c67544e257
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6069
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix/eval/src/chunk.rs')
-rw-r--r--tvix/eval/src/chunk.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs
new file mode 100644
index 000000000000..3475e58f1870
--- /dev/null
+++ b/tvix/eval/src/chunk.rs
@@ -0,0 +1,26 @@
+use crate::opcode::{CodeIdx, ConstantIdx, OpCode};
+use crate::value::Value;
+
+#[derive(Debug, Default)]
+pub struct Chunk {
+    pub code: Vec<OpCode>,
+    constants: Vec<Value>,
+}
+
+impl Chunk {
+    pub fn add_op(&mut self, data: OpCode) -> CodeIdx {
+        let idx = self.code.len();
+        self.code.push(data);
+        CodeIdx(idx)
+    }
+
+    pub fn add_constant(&mut self, data: Value) -> ConstantIdx {
+        let idx = self.constants.len();
+        self.constants.push(data);
+        ConstantIdx(idx)
+    }
+
+    pub fn constant(&self, idx: ConstantIdx) -> &Value {
+        &self.constants[idx.0]
+    }
+}