about summary refs log tree commit diff
path: root/tvix/eval/src/chunk.rs
diff options
context:
space:
mode:
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]
+    }
+}