feat(tvix/eval): implement if/else expressions
These expressions use simple jumps to skip the correct expression conditionally in the bytecode by advancing the instruction pointer. Note that these expressions are already covered by a test behind the `nix_tests` feature flag, but adding more is probably sensible. Change-Id: Ibe0eba95d216321c883d3b6b5816e2ab6fe7eef1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6148 Tested-by: BuildkiteCI Reviewed-by: grfn <grfn@gws.fyi> Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
parent
2422f2f224
commit
d9d94eb27f
4 changed files with 84 additions and 5 deletions
|
|
@ -83,6 +83,10 @@ impl VM {
|
|||
self.stack.push(value)
|
||||
}
|
||||
|
||||
fn peek(&self, offset: usize) -> &Value {
|
||||
&self.stack[self.stack.len() - 1 - offset]
|
||||
}
|
||||
|
||||
fn run(&mut self) -> EvalResult<Value> {
|
||||
loop {
|
||||
match self.inc_ip() {
|
||||
|
|
@ -91,6 +95,10 @@ impl VM {
|
|||
self.push(c);
|
||||
}
|
||||
|
||||
OpCode::OpPop => {
|
||||
self.pop();
|
||||
}
|
||||
|
||||
OpCode::OpAdd => {
|
||||
let b = self.pop();
|
||||
let a = self.pop();
|
||||
|
|
@ -163,6 +171,16 @@ impl VM {
|
|||
}
|
||||
|
||||
OpCode::OpInterpolate(count) => self.run_interpolate(count)?,
|
||||
|
||||
OpCode::OpJump(offset) => {
|
||||
self.ip += offset;
|
||||
}
|
||||
|
||||
OpCode::OpJumpIfFalse(offset) => {
|
||||
if !self.peek(0).as_bool()? {
|
||||
self.ip += offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if self.ip == self.chunk.code.len() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue