diff options
author | Vincent Ambo <mail@tazj.in> | 2022-08-16T12·53+0300 |
---|---|---|
committer | tazjin <tazjin@tvl.su> | 2022-09-01T17·41+0000 |
commit | a00e4730a5ec824a8452152de72458230f72e61a (patch) | |
tree | 24ebc2c3839bb707abbb19f1efb17f8d59dae847 /tvix/eval | |
parent | 8fa3bc71374eba92c79346c1d0e92d9445b87a71 (diff) |
feat(tvix/eval): implement `assert` operator r/4566
This implements `assert`, which evaluates an expression and aborts evaluation if the value is not `true`. At this point we should introduce eval-failed-* tests; probably asserting against some representation of the error enum? Change-Id: If54c8f616d89b829c1860a4835dde60a2cd70d7a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6230 Reviewed-by: grfn <grfn@gws.fyi> Reviewed-by: sterni <sternenseemann@systemli.org> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval')
-rw-r--r-- | tvix/eval/src/compiler.rs | 16 | ||||
-rw-r--r-- | tvix/eval/src/errors.rs | 2 | ||||
-rw-r--r-- | tvix/eval/src/opcode.rs | 3 | ||||
-rw-r--r-- | tvix/eval/src/vm.rs | 6 |
4 files changed, 27 insertions, 0 deletions
diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs index 193b4a0f33ed..27e917c54569 100644 --- a/tvix/eval/src/compiler.rs +++ b/tvix/eval/src/compiler.rs @@ -170,6 +170,11 @@ impl Compiler { self.compile_with(node) } + rnix::SyntaxKind::NODE_ASSERT => { + let node = rnix::types::Assert::cast(node).unwrap(); + self.compile_assert(node) + } + kind => panic!("visiting unsupported node: {:?}", kind), } } @@ -795,6 +800,17 @@ impl Compiler { self.compile(node.body().unwrap()) } + fn compile_assert(&mut self, node: rnix::types::Assert) -> EvalResult<()> { + // Compile the assertion condition to leave its value on the stack. + self.compile(node.condition().unwrap())?; + self.chunk.push_op(OpCode::OpAssert); + + // The runtime will abort evaluation at this point if the + // assertion failed, if not the body simply continues on like + // normal. + self.compile(node.body().unwrap()) + } + // Emit the literal string value of an identifier. Required for // several operations related to attribute sets, where identifiers // are used as string keys. diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs index 07ee8a4c7dae..03f22306ec35 100644 --- a/tvix/eval/src/errors.rs +++ b/tvix/eval/src/errors.rs @@ -33,6 +33,8 @@ pub enum Error { UnknownDynamicVariable(String), ParseErrors(Vec<rnix::parser::ParseError>), + + AssertionFailed, } impl Display for Error { diff --git a/tvix/eval/src/opcode.rs b/tvix/eval/src/opcode.rs index 7dcfea93aaf7..cda3fdf26be5 100644 --- a/tvix/eval/src/opcode.rs +++ b/tvix/eval/src/opcode.rs @@ -72,4 +72,7 @@ pub enum OpCode { // Close scopes while leaving their expression value around. OpCloseScope(usize), // number of locals to pop + + // Asserts stack top is a boolean, and true. + OpAssert, } diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index 09e3aa247555..e088d22776db 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -304,6 +304,12 @@ impl VM { return Err(Error::UnknownDynamicVariable(ident.to_string())); } + + OpCode::OpAssert => { + if !self.pop().as_bool()? { + return Err(Error::AssertionFailed); + } + } } #[cfg(feature = "disassembler")] |