about summary refs log tree commit diff
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
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>
-rw-r--r--tvix/eval/src/chunk.rs26
-rw-r--r--tvix/eval/src/main.rs1
2 files changed, 27 insertions, 0 deletions
diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs
new file mode 100644
index 0000000000..3475e58f18
--- /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]
+    }
+}
diff --git a/tvix/eval/src/main.rs b/tvix/eval/src/main.rs
index fc366961b5..c55f48a543 100644
--- a/tvix/eval/src/main.rs
+++ b/tvix/eval/src/main.rs
@@ -4,6 +4,7 @@ use std::{
     mem, process,
 };
 
+mod chunk;
 mod errors;
 mod eval;
 mod opcode;