feat(tvix/eval): implement list concatenation
Change-Id: Icdf715d116371a9f139bdf95266410bf967bef25 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6144 Reviewed-by: sterni <sternenseemann@systemli.org> Tested-by: BuildkiteCI
This commit is contained in:
parent
4eafaae9e6
commit
75a22321ce
7 changed files with 30 additions and 0 deletions
|
@ -141,6 +141,7 @@ impl Compiler {
|
|||
BinOpKind::LessOrEq => self.chunk.add_op(OpCode::OpLessOrEq),
|
||||
BinOpKind::More => self.chunk.add_op(OpCode::OpMore),
|
||||
BinOpKind::MoreOrEq => self.chunk.add_op(OpCode::OpMoreOrEq),
|
||||
BinOpKind::Concat => self.chunk.add_op(OpCode::OpConcat),
|
||||
|
||||
BinOpKind::NotEqual => {
|
||||
self.chunk.add_op(OpCode::OpEqual);
|
||||
|
|
|
@ -41,6 +41,7 @@ pub enum OpCode {
|
|||
|
||||
// Lists
|
||||
OpList(usize),
|
||||
OpConcat,
|
||||
|
||||
// Strings
|
||||
OpInterpolate(usize),
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
[ 1 2 3 4 ]
|
|
@ -0,0 +1 @@
|
|||
[ 1 2 ] ++ [ 3 4 ]
|
|
@ -18,3 +18,12 @@ impl Display for NixList {
|
|||
f.write_str("]")
|
||||
}
|
||||
}
|
||||
|
||||
impl NixList {
|
||||
pub fn concat(&self, other: &Self) -> Self {
|
||||
let mut lhs = self.clone();
|
||||
let mut rhs = other.clone();
|
||||
lhs.0.append(&mut rhs.0);
|
||||
lhs
|
||||
}
|
||||
}
|
||||
|
|
|
@ -81,6 +81,16 @@ impl Value {
|
|||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_list(self) -> EvalResult<NixList> {
|
||||
match self {
|
||||
Value::List(l) => Ok(l),
|
||||
other => Err(Error::TypeError {
|
||||
expected: "list",
|
||||
actual: other.type_of(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Value {
|
||||
|
|
|
@ -151,6 +151,13 @@ impl VM {
|
|||
}
|
||||
|
||||
OpCode::OpList(count) => self.run_list(count)?,
|
||||
|
||||
OpCode::OpConcat => {
|
||||
let rhs = self.pop().as_list()?;
|
||||
let lhs = self.pop().as_list()?;
|
||||
self.push(Value::List(lhs.concat(&rhs)))
|
||||
}
|
||||
|
||||
OpCode::OpInterpolate(count) => self.run_interpolate(count)?,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue