From 20f5ccefeb88ce4b79369085977b674c612e8fed Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 11 Aug 2022 15:29:11 +0300 Subject: feat(tvix/eval): implement attribute set access operator Fairly straightforward, handling the optimised representations manually and otherwise delegating to BTreeMap. Note that parsing of raw identifiers is not yet implemented. Encountering an identifier node usually means that there is locals access going on, so we need a special case for compiling a node in such a way that an identifier's literal value ends up on the stack. Change-Id: I13fbab7ac657b17ef3f4c5859fe737c321890c8a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6158 Tested-by: BuildkiteCI Reviewed-by: sterni Reviewed-by: grfn --- tvix/eval/src/value/attrs.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tvix/eval/src/value') diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs index 9204a5bb955a..ecb819fad77c 100644 --- a/tvix/eval/src/value/attrs.rs +++ b/tvix/eval/src/value/attrs.rs @@ -45,6 +45,26 @@ impl AttrsRep { } } } + + fn select(&self, key: &str) -> Option<&Value> { + match self { + AttrsRep::Empty => None, + + AttrsRep::KV { name, value } => { + if key == "name" { + return Some(&name); + } + + if key == "value" { + return Some(&value); + } + + None + } + + AttrsRep::Map(map) => map.get(&key.to_string().into()), + } + } } #[derive(Clone, Debug)] @@ -133,6 +153,11 @@ impl NixAttrs { } } + // Select a value from an attribute set by key. + pub fn select(&self, key: &str) -> Option<&Value> { + self.0.select(key) + } + /// Implement construction logic of an attribute set, to encapsulate /// logic about attribute set optimisations inside of this module. pub fn construct(count: usize, mut stack_slice: Vec) -> EvalResult { -- cgit 1.4.1