about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-09T14·23+0300
committerclbot <clbot@tvl.fyi>2022-08-13T15·39+0000
commit910336c68cc5f49317bb1024b11cfaf70666880c (patch)
treed7603394041cf0e6fa310030e9f93642bf878c3b
parent5763b62172e81122e88560f93f3d1d8755d9cbfb (diff)
feat(tvix/compiler): compile list literals r/4431
Change-Id: I2c6fedc3dbb7d449d700f3972c3fbd4a7d147f6c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6095
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Autosubmit: tazjin <tazjin@tvl.su>
-rw-r--r--tvix/eval/src/compiler.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs
index d7d6bb38a7..ce3642b585 100644
--- a/tvix/eval/src/compiler.rs
+++ b/tvix/eval/src/compiler.rs
@@ -52,6 +52,11 @@ impl Compiler {
                 self.compile_attr_set(node)
             }
 
+            rnix::SyntaxKind::NODE_LIST => {
+                let node = rnix::types::List::cast(node).unwrap();
+                self.compile_list(node)
+            }
+
             kind => {
                 println!("visiting unsupported node: {:?}", kind);
                 Ok(())
@@ -190,6 +195,24 @@ impl Compiler {
         self.chunk.add_op(OpCode::OpAttrs(count));
         Ok(())
     }
+
+    // Compile list literals into equivalent bytecode. List
+    // construction is fairly simple, composing of pushing code for
+    // each literal element and an instruction with the element count.
+    //
+    // The VM, after evaluating the code for each element, simply
+    // constructs the list from the given number of elements.
+    fn compile_list(&mut self, node: rnix::types::List) -> EvalResult<()> {
+        let mut count = 0;
+
+        for item in node.items() {
+            count += 1;
+            self.compile(item)?;
+        }
+
+        self.chunk.add_op(OpCode::OpList(count));
+        Ok(())
+    }
 }
 
 pub fn compile(ast: rnix::AST) -> EvalResult<Chunk> {