From 8a2de7537986b1d60b9e8ce3bc4c08e9ec6c489a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Nov 2020 14:53:38 +0000 Subject: Untangle ImportEnv::handle_import --- dhall/src/semantics/resolve/env.rs | 43 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'dhall/src/semantics/resolve/env.rs') diff --git a/dhall/src/semantics/resolve/env.rs b/dhall/src/semantics/resolve/env.rs index 6346a6d..25d2277 100644 --- a/dhall/src/semantics/resolve/env.rs +++ b/dhall/src/semantics/resolve/env.rs @@ -69,9 +69,20 @@ impl ImportEnv { ImportEnv::default() } - pub fn handle_import( + pub fn get_from_cache<'a>( + &'a self, + location: &ImportLocation, + ) -> Option<&'a TypedHir> { + self.cache.get(location) + } + + pub fn set_cache(&mut self, location: ImportLocation, expr: TypedHir) { + self.cache.insert(location, expr); + } + + pub fn with_cycle_detection( &mut self, - mut location: ImportLocation, + location: ImportLocation, do_resolve: impl FnOnce(&mut Self) -> Result, ) -> Result { if self.stack.contains(&location) { @@ -79,25 +90,13 @@ impl ImportEnv { ImportError::ImportCycle(self.stack.clone(), location).into() ); } - Ok(match self.cache.get(&location) { - Some(expr) => expr.clone(), - None => { - let expr = { - // Push the current location on the stack - self.stack.push(location); - // Resolve the import recursively - // WARNING: do not propagate errors here or the stack will get messed up. - let result = do_resolve(self); - // Remove location from the stack. - location = self.stack.pop().unwrap(); - result - }?; - - // Add the resolved import to the cache - self.cache.insert(location, expr.clone()); - - expr - } - }) + // Push the current location on the stack + self.stack.push(location); + // Resolve the import recursively + // WARNING: do not propagate errors here or the stack will get messed up. + let result = do_resolve(self); + // Remove location from the stack. + self.stack.pop().unwrap(); + result } } -- cgit v1.2.3 From 2c245e7f5ce4019381e0fa47a1e2caf6276106be Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Nov 2020 15:17:42 +0000 Subject: Store file cache in ImportEnv --- dhall/src/semantics/resolve/env.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'dhall/src/semantics/resolve/env.rs') diff --git a/dhall/src/semantics/resolve/env.rs b/dhall/src/semantics/resolve/env.rs index 25d2277..4d502ce 100644 --- a/dhall/src/semantics/resolve/env.rs +++ b/dhall/src/semantics/resolve/env.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::error::{Error, ImportError}; -use crate::semantics::{AlphaVar, ImportLocation, TypedHir, VarEnv}; +use crate::semantics::{AlphaVar, Cache, ImportLocation, TypedHir, VarEnv}; use crate::syntax::{Label, V}; /// Environment for resolving names. @@ -10,14 +10,15 @@ pub struct NameEnv { names: Vec