feat(tvix/eval): implement boolean inversion operator
Change-Id: Icb1d449fdee4d67b5f1eefdbc01baa1584ea0a67 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6079 Tested-by: BuildkiteCI Autosubmit: tazjin <tazjin@tvl.su> Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
parent
ded0fb9e21
commit
d431f43f5f
2 changed files with 17 additions and 1 deletions
|
@ -1,6 +1,8 @@
|
||||||
//! This module implements the backing representation of runtime
|
//! This module implements the backing representation of runtime
|
||||||
//! values in the Nix language.
|
//! values in the Nix language.
|
||||||
|
|
||||||
|
use crate::errors::{Error, EvalResult};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
Null,
|
Null,
|
||||||
|
@ -26,6 +28,16 @@ impl Value {
|
||||||
Value::Float(_) => "float",
|
Value::Float(_) => "float",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn as_bool(self) -> EvalResult<bool> {
|
||||||
|
match self {
|
||||||
|
Value::Bool(b) => Ok(b),
|
||||||
|
other => Err(Error::TypeError {
|
||||||
|
expected: "bool",
|
||||||
|
actual: other.type_of(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
|
|
@ -81,6 +81,11 @@ impl VM {
|
||||||
NumberPair::Integer(i1, i2) => self.push(Value::Integer(i1 / i2)),
|
NumberPair::Integer(i1, i2) => self.push(Value::Integer(i1 / i2)),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
OpCode::OpInvert => {
|
||||||
|
let v = self.pop().as_bool()?;
|
||||||
|
self.push(Value::Bool(!v));
|
||||||
|
}
|
||||||
|
|
||||||
OpCode::OpNegate => match self.pop() {
|
OpCode::OpNegate => match self.pop() {
|
||||||
Value::Integer(i) => self.push(Value::Integer(-i)),
|
Value::Integer(i) => self.push(Value::Integer(-i)),
|
||||||
Value::Float(f) => self.push(Value::Float(-f)),
|
Value::Float(f) => self.push(Value::Float(-f)),
|
||||||
|
@ -106,7 +111,6 @@ impl VM {
|
||||||
self.push(Value::Bool(eq))
|
self.push(Value::Bool(eq))
|
||||||
}
|
}
|
||||||
|
|
||||||
OpCode::OpInvert => todo!("invert"),
|
|
||||||
OpCode::OpNull => todo!("null"),
|
OpCode::OpNull => todo!("null"),
|
||||||
OpCode::OpTrue => todo!("true"),
|
OpCode::OpTrue => todo!("true"),
|
||||||
OpCode::OpFalse => todo!("false"),
|
OpCode::OpFalse => todo!("false"),
|
||||||
|
|
Loading…
Reference in a new issue