From 910336c68cc5f49317bb1024b11cfaf70666880c Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 9 Aug 2022 17:23:20 +0300 Subject: feat(tvix/compiler): compile list literals Change-Id: I2c6fedc3dbb7d449d700f3972c3fbd4a7d147f6c Reviewed-on: https://cl.tvl.fyi/c/depot/+/6095 Tested-by: BuildkiteCI Reviewed-by: grfn Autosubmit: tazjin --- tvix/eval/src/compiler.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 { -- cgit 1.4.1