From ef3734a3e9381b2e91552089774126f58f560bc3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 21:21:32 +0000 Subject: Improve handling of builtin types in Nir --- dhall/src/semantics/builtins.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'dhall/src/semantics/builtins.rs') diff --git a/dhall/src/semantics/builtins.rs b/dhall/src/semantics/builtins.rs index eb50612..7fbb933 100644 --- a/dhall/src/semantics/builtins.rs +++ b/dhall/src/semantics/builtins.rs @@ -14,22 +14,17 @@ use std::convert::TryInto; /// A partially applied builtin. /// Invariant: the evaluation of the given args must not be able to progress further #[derive(Debug, Clone)] -pub(crate) struct BuiltinClosure { - pub env: NzEnv, - pub b: Builtin, +pub(crate) struct BuiltinClosure { + env: NzEnv, + b: Builtin, /// Arguments applied to the closure so far. - pub args: Vec, + args: Vec, } -impl BuiltinClosure { - pub fn new(b: Builtin, env: NzEnv) -> Self { - BuiltinClosure { - env, - b, - args: Vec::new(), - } +impl BuiltinClosure { + pub fn new(b: Builtin, env: NzEnv) -> NirKind { + apply_builtin(b, Vec::new(), env) } - pub fn apply(&self, a: Nir) -> NirKind { use std::iter::once; let args = self.args.iter().cloned().chain(once(a)).collect(); @@ -272,6 +267,14 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { }; let ret = match (b, args.as_slice()) { + (Builtin::Bool, []) + | (Builtin::Natural, []) + | (Builtin::Integer, []) + | (Builtin::Double, []) + | (Builtin::Text, []) => Ret::NirKind(BuiltinType(b)), + (Builtin::Optional, [t]) => Ret::NirKind(OptionalType(t.clone())), + (Builtin::List, [t]) => Ret::NirKind(ListType(t.clone())), + (Builtin::OptionalNone, [t]) => { Ret::NirKind(EmptyOptionalLit(t.clone())) } @@ -489,9 +492,9 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { } } -impl std::cmp::PartialEq for BuiltinClosure { +impl std::cmp::PartialEq for BuiltinClosure { fn eq(&self, other: &Self) -> bool { self.b == other.b && self.args == other.args } } -impl std::cmp::Eq for BuiltinClosure {} +impl std::cmp::Eq for BuiltinClosure {} -- cgit v1.2.3