From 89f566ef5782c967e14eee5727dce2740a3c24b4 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 15 Aug 2022 01:31:02 +0300 Subject: feat(tvix/eval): emit instructions for dynamic var resolution If an unknown variable is encountered and the with stack is not empty, emit instructions for resolving the variable at runtime. Change-Id: I752f4bd0025335744e4747364abd1bd34130374e Reviewed-on: https://cl.tvl.fyi/c/depot/+/6223 Tested-by: BuildkiteCI Reviewed-by: sterni Reviewed-by: grfn --- tvix/eval/src/compiler.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'tvix/eval/src/compiler.rs') diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs index cb568bfc6f..970ad7a1b7 100644 --- a/tvix/eval/src/compiler.rs +++ b/tvix/eval/src/compiler.rs @@ -352,7 +352,17 @@ impl Compiler { // features are not yet implemented. match self.resolve_local(name) { Some(idx) => self.chunk.push_op(OpCode::OpGetLocal(idx)), - None => return Err(Error::UnknownStaticVariable(node)), + None => { + if self.scope.with_stack.is_empty() { + return Err(Error::UnknownStaticVariable(node)); + } + + // Variable needs to be dynamically resolved + // at runtime. + let idx = self.chunk.push_constant(Value::String(name.into())); + self.chunk.push_op(OpCode::OpConstant(idx)); + self.chunk.push_op(OpCode::OpResolveWith) + } } } }; -- cgit 1.4.1