Skip to content

Commit 21af1e7

Browse files
committed
Make local and item environment lazy
1 parent ffd0976 commit 21af1e7

File tree

5 files changed

+131
-96
lines changed

5 files changed

+131
-96
lines changed

fathom/src/core/binary.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt::Debug;
77
use std::slice::SliceIndex;
88
use std::sync::Arc;
99

10+
use super::semantics::LocalExprs;
1011
use crate::core::semantics::{self, ArcValue, Elim, Head, LazyValue, Value};
1112
use crate::core::{Const, Item, Module, Prim, Term, UIntStyle};
1213
use crate::env::{EnvLen, SharedEnv, UniqueEnv};
@@ -254,8 +255,8 @@ impl fmt::Display for BufferError {
254255
impl std::error::Error for BufferError {}
255256

256257
pub struct Context<'arena, 'data> {
257-
item_exprs: UniqueEnv<ArcValue<'arena>>,
258-
local_exprs: SharedEnv<ArcValue<'arena>>,
258+
item_exprs: UniqueEnv<LazyValue<'arena>>,
259+
local_exprs: LocalExprs<'arena>,
259260
initial_buffer: Buffer<'data>,
260261
pending_formats: Vec<(usize, ArcValue<'arena>)>,
261262
cached_refs: HashMap<usize, Vec<ParsedRef<'arena>>>,
@@ -295,7 +296,7 @@ impl<'arena, 'data> Context<'arena, 'data> {
295296
for item in module.items {
296297
match item {
297298
Item::Def { expr, .. } => {
298-
let expr = self.eval_env().eval(expr);
299+
let expr = self.eval_env().delay(expr);
299300
self.item_exprs.push(expr);
300301
}
301302
}
@@ -332,8 +333,8 @@ impl<'arena, 'data> Context<'arena, 'data> {
332333
let mut exprs = Vec::with_capacity(formats.len());
333334

334335
while let Some((format, next_formats)) = self.elim_env().split_telescope(formats) {
335-
let expr = self.read_format(reader, &format)?;
336-
exprs.push(LazyValue::eager(expr.clone()));
336+
let expr = LazyValue::eager(self.read_format(reader, &format)?);
337+
exprs.push(expr.clone());
337338
formats = next_formats(expr);
338339
}
339340

@@ -344,7 +345,9 @@ impl<'arena, 'data> Context<'arena, 'data> {
344345
}
345346
Value::FormatCond(_label, format, cond) => {
346347
let value = self.read_format(reader, &self.elim_env().force_lazy(format))?;
347-
let cond_res = self.elim_env().apply_closure(cond, value.clone());
348+
let cond_res = self
349+
.elim_env()
350+
.apply_closure(cond, LazyValue::eager(value.clone()));
348351

349352
match cond_res.as_ref() {
350353
Value::ConstLit(Const::Bool(true)) => Ok(value),
@@ -366,8 +369,8 @@ impl<'arena, 'data> Context<'arena, 'data> {
366369
while let Some((format, next_formats)) = self.elim_env().split_telescope(formats) {
367370
let mut reader = reader.clone();
368371

369-
let expr = self.read_format(&mut reader, &format)?;
370-
exprs.push(LazyValue::eager(expr.clone()));
372+
let expr = LazyValue::eager(self.read_format(&mut reader, &format)?);
373+
exprs.push(expr.clone());
371374
formats = next_formats(expr);
372375

373376
max_relative_offset =

fathom/src/core/prim.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44
use fxhash::FxHashMap;
55
use scoped_arena::Scope;
66

7+
use super::semantics::{LazyValue, LocalExprs};
78
use crate::core::semantics::{ArcValue, Elim, ElimEnv, Head, Value};
89
use crate::core::{self, Const, Plicity, Prim, UIntStyle};
910
use crate::env::{self, SharedEnv, UniqueEnv};
@@ -512,8 +513,8 @@ struct EnvBuilder<'interner, 'arena> {
512513
interner: &'interner RefCell<StringInterner>,
513514
scope: &'arena Scope<'arena>,
514515
meta_exprs: UniqueEnv<Option<ArcValue<'arena>>>,
515-
item_exprs: UniqueEnv<ArcValue<'arena>>,
516-
local_exprs: SharedEnv<ArcValue<'arena>>,
516+
item_exprs: UniqueEnv<LazyValue<'arena>>,
517+
local_exprs: LocalExprs<'arena>,
517518
}
518519

519520
impl<'interner, 'arena> EnvBuilder<'interner, 'arena> {

0 commit comments

Comments
 (0)