feat(tvix/vm): implement list construction

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>
This commit is contained in:
Vincent Ambo 2022-08-09 17:11:02 +03:00 committed by clbot
parent c67747cbe1
commit 5763b62172
2 changed files with 20 additions and 1 deletions

View file

@ -32,4 +32,7 @@ pub enum OpCode {
// Attribute sets
OpAttrs(usize),
// Lists
OpList(usize),
}

View file

@ -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)]