feat(tvix/eval): implement unary negation operator
Change-Id: I5d012cc073e55d79d7b34b88283aab3164864293 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6075 Tested-by: BuildkiteCI Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
parent
d35ecc0caf
commit
72be759e1e
3 changed files with 35 additions and 1 deletions
|
|
@ -31,6 +31,11 @@ impl Compiler {
|
|||
self.compile_binop(op)
|
||||
}
|
||||
|
||||
rnix::SyntaxKind::NODE_UNARY_OP => {
|
||||
let op = rnix::types::UnaryOp::cast(node).expect("TODO: (should not be possible)");
|
||||
self.compile_unary_op(op)
|
||||
}
|
||||
|
||||
kind => {
|
||||
println!("visiting unsupported node: {:?}", kind);
|
||||
Ok(())
|
||||
|
|
@ -77,6 +82,19 @@ impl Compiler {
|
|||
self.chunk.add_op(opcode);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compile_unary_op(&mut self, op: rnix::types::UnaryOp) -> EvalResult<()> {
|
||||
self.compile(op.value().unwrap())?;
|
||||
|
||||
use rnix::types::UnaryOpKind;
|
||||
let opcode = match op.operator() {
|
||||
UnaryOpKind::Invert => OpCode::OpInvert,
|
||||
UnaryOpKind::Negate => OpCode::OpNegate,
|
||||
};
|
||||
|
||||
self.chunk.add_op(opcode);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile(ast: rnix::AST) -> EvalResult<Chunk> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue