diff options
author | Vincent Ambo <mail@tazj.in> | 2022-08-09T14·11+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-08-13T15·35+0000 |
commit | 5763b62172e81122e88560f93f3d1d8755d9cbfb (patch) | |
tree | d06e1f40c4d8befbcd438ec3b19d47e44dcc2ed5 | |
parent | c67747cbe126649057e47f2f4756645a553e682e (diff) |
feat(tvix/vm): implement list construction r/4430
Change-Id: Iec2b4910800ab29daae6d71b58a8acd14ccb1cc1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6094 Tested-by: BuildkiteCI Reviewed-by: grfn <grfn@gws.fyi> Autosubmit: tazjin <tazjin@tvl.su>
-rw-r--r-- | tvix/eval/src/opcode.rs | 3 | ||||
-rw-r--r-- | tvix/eval/src/vm.rs | 18 |
2 files changed, 20 insertions, 1 deletions
diff --git a/tvix/eval/src/opcode.rs b/tvix/eval/src/opcode.rs index 58372f61cb17..adfa2433fcc3 100644 --- a/tvix/eval/src/opcode.rs +++ b/tvix/eval/src/opcode.rs @@ -32,4 +32,7 @@ pub enum OpCode { // Attribute sets OpAttrs(usize), + + // Lists + OpList(usize), } diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index e8c0551caf67..d328900a34c5 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -7,7 +7,7 @@ use crate::{ chunk::Chunk, errors::{Error, EvalResult}, opcode::OpCode, - value::{NixAttrs, NixString, Value}, + value::{NixAttrs, NixList, NixString, Value}, }; pub struct VM { @@ -117,6 +117,7 @@ impl VM { OpCode::OpTrue => self.push(Value::Bool(true)), OpCode::OpFalse => self.push(Value::Bool(false)), OpCode::OpAttrs(count) => self.run_attrset(count)?, + OpCode::OpList(count) => self.run_list(count)?, } if self.ip == self.chunk.code.len() { @@ -138,6 +139,21 @@ impl VM { self.push(Value::Attrs(Rc::new(NixAttrs::Map(attrs)))); Ok(()) } + + // Construct runtime representation of a list. Because the list + // items are on the stack in reverse order, the vector is created + // initialised and elements are directly assigned to their + // respective indices. + fn run_list(&mut self, count: usize) -> EvalResult<()> { + let mut list = vec![Value::Null; count]; + + for idx in 0..count { + list[count - idx - 1] = self.pop(); + } + + self.push(Value::List(NixList(list))); + Ok(()) + } } #[derive(Clone, Copy, Debug, PartialEq)] |