feat(tvix/eval): implement equality operator

Change-Id: I9dd54aed72cd7e67593dc76f5a046ebbda40c26f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6078
Tested-by: BuildkiteCI
Autosubmit: tazjin <tazjin@tvl.su>
Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2022-08-08 02:51:28 +03:00 committed by tazjin
parent 28f57abac1
commit ded0fb9e21
3 changed files with 25 additions and 8 deletions

View file

@ -78,7 +78,7 @@ impl Compiler {
BinOpKind::Sub => OpCode::OpSub,
BinOpKind::Mul => OpCode::OpMul,
BinOpKind::Div => OpCode::OpDiv,
BinOpKind::Equal => OpCode::OpEqual,
_ => todo!(),
};

View file

@ -17,13 +17,16 @@ pub enum OpCode {
OpTrue,
OpFalse,
// Unary operators
OpInvert,
OpNegate,
// Arithmetic binary operators
OpAdd,
OpSub,
OpMul,
OpDiv,
// Unary operators
OpInvert,
OpNegate,
// Logical binary operators
OpEqual,
}

View file

@ -92,10 +92,24 @@ impl VM {
}
},
OpCode::OpInvert => todo!(),
OpCode::OpNull => todo!(),
OpCode::OpTrue => todo!(),
OpCode::OpFalse => todo!(),
OpCode::OpEqual => {
let v2 = self.pop();
let v1 = self.pop();
let eq = match (v1, v2) {
(Value::Float(f), Value::Integer(i))
| (Value::Integer(i), Value::Float(f)) => f == (i as f64),
_ => v2 == v2,
};
self.push(Value::Bool(eq))
}
OpCode::OpInvert => todo!("invert"),
OpCode::OpNull => todo!("null"),
OpCode::OpTrue => todo!("true"),
OpCode::OpFalse => todo!("false"),
}
if self.ip == self.chunk.code.len() {