summaryrefslogtreecommitdiff
path: root/dhall/src
diff options
context:
space:
mode:
authorNadrieril2020-11-01 15:17:42 +0000
committerNadrieril2020-11-01 15:17:42 +0000
commit2c245e7f5ce4019381e0fa47a1e2caf6276106be (patch)
tree26189d7ae5287f406862ef81f001f2fef5a1222a /dhall/src
parent8a2de7537986b1d60b9e8ce3bc4c08e9ec6c489a (diff)
Store file cache in ImportEnv
Diffstat (limited to 'dhall/src')
-rw-r--r--dhall/src/error/mod.rs4
-rw-r--r--dhall/src/semantics/resolve/cache.rs2
-rw-r--r--dhall/src/semantics/resolve/env.rs23
-rw-r--r--dhall/src/semantics/resolve/resolve.rs14
4 files changed, 22 insertions, 21 deletions
diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs
index d533264..800b8c1 100644
--- a/dhall/src/error/mod.rs
+++ b/dhall/src/error/mod.rs
@@ -1,6 +1,6 @@
use std::io::Error as IOError;
-use crate::semantics::resolve::{ImportLocation, ImportStack};
+use crate::semantics::resolve::{CyclesStack, ImportLocation};
use crate::syntax::{Import, ParseError};
mod builder;
@@ -31,7 +31,7 @@ pub enum ImportError {
MissingEnvVar,
SanityCheck,
UnexpectedImport(Import<()>),
- ImportCycle(ImportStack, ImportLocation),
+ ImportCycle(CyclesStack, ImportLocation),
Url(url::ParseError),
}
diff --git a/dhall/src/semantics/resolve/cache.rs b/dhall/src/semantics/resolve/cache.rs
index 164baea..dd686ea 100644
--- a/dhall/src/semantics/resolve/cache.rs
+++ b/dhall/src/semantics/resolve/cache.rs
@@ -40,7 +40,7 @@ fn load_cache_dir(
Err(CacheError::MissingConfiguration)
}
-#[derive(Debug, PartialEq)]
+#[derive(Debug, Clone, PartialEq)]
pub struct Cache {
cache_dir: Option<PathBuf>,
}
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<Label>,
}
-pub type ImportCache = HashMap<ImportLocation, TypedHir>;
-pub type ImportStack = Vec<ImportLocation>;
+pub type MemCache = HashMap<ImportLocation, TypedHir>;
+pub type CyclesStack = Vec<ImportLocation>;
/// Environment for resolving imports
-#[derive(Debug, Clone, Default)]
+#[derive(Debug)]
pub struct ImportEnv {
- cache: ImportCache,
- stack: ImportStack,
+ pub file_cache: Cache,
+ mem_cache: MemCache,
+ stack: CyclesStack,
}
impl NameEnv {
@@ -66,18 +67,22 @@ impl NameEnv {
impl ImportEnv {
pub fn new() -> Self {
- ImportEnv::default()
+ ImportEnv {
+ file_cache: Cache::new(),
+ mem_cache: Default::default(),
+ stack: Default::default(),
+ }
}
pub fn get_from_cache<'a>(
&'a self,
location: &ImportLocation,
) -> Option<&'a TypedHir> {
- self.cache.get(location)
+ self.mem_cache.get(location)
}
pub fn set_cache(&mut self, location: ImportLocation, expr: TypedHir) {
- self.cache.insert(location, expr);
+ self.mem_cache.insert(location, expr);
}
pub fn with_cycle_detection(
diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs
index f6d664e..8f8a9fe 100644
--- a/dhall/src/semantics/resolve/resolve.rs
+++ b/dhall/src/semantics/resolve/resolve.rs
@@ -10,7 +10,7 @@ use crate::builtins::Builtin;
use crate::error::ErrorBuilder;
use crate::error::{Error, ImportError};
use crate::operations::{BinOp, OpKind};
-use crate::semantics::{mkerr, Cache, Hir, HirKind, ImportEnv, NameEnv, Type};
+use crate::semantics::{mkerr, Hir, HirKind, ImportEnv, NameEnv, Type};
use crate::syntax;
use crate::syntax::{
Expr, ExprKind, FilePath, FilePrefix, Hash, ImportMode, ImportTarget,
@@ -222,19 +222,17 @@ fn make_aslocation_uniontype() -> Expr {
fn resolve_one_import(
env: &mut ImportEnv,
- cache: &Cache,
import: &Import,
location: ImportLocation,
span: Span,
) -> Result<TypedHir, Error> {
match import.mode {
ImportMode::Code => {
- let (hir, ty) = cache.caching_import(
+ let (hir, ty) = env.file_cache.clone().caching_import(
import,
|| location.fetch_dhall(),
|parsed| {
- let typed =
- resolve_with_env(env, cache, parsed)?.typecheck()?;
+ let typed = resolve_with_env(env, parsed)?.typecheck()?;
let hir = typed.normalize().to_hir();
Ok((hir, typed.ty))
},
@@ -391,7 +389,6 @@ fn traverse_resolve_expr(
fn resolve_with_env(
env: &mut ImportEnv,
- cache: &Cache,
parsed: Parsed,
) -> Result<Resolved, Error> {
let Parsed(expr, base_location) = parsed;
@@ -406,7 +403,7 @@ fn resolve_with_env(
return Ok(expr.clone());
}
let expr = env.with_cycle_detection(location.clone(), |env| {
- resolve_one_import(env, cache, &import, location.clone(), span)
+ resolve_one_import(env, &import, location.clone(), span)
})?;
// Add the resolved import to the cache
env.set_cache(location, expr.clone());
@@ -417,8 +414,7 @@ fn resolve_with_env(
}
pub fn resolve(parsed: Parsed) -> Result<Resolved, Error> {
- let cache = Cache::new();
- resolve_with_env(&mut ImportEnv::new(), &cache, parsed)
+ resolve_with_env(&mut ImportEnv::new(), parsed)
}
pub fn skip_resolve_expr(expr: &Expr) -> Result<Hir, Error> {