This repository was archived by the owner on Jun 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Use a linked list for the Env #25
Merged
+41
−49
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,52 @@ | ||
| use bumpalo::{collections::Vec as BumpVec, Bump}; | ||
| use bumpalo::Bump; | ||
|
|
||
| use crate::binder::Eval; | ||
|
|
||
| use super::value::Value; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Env<'a, V>(BumpVec<'a, &'a Value<'a, V>>) | ||
| pub enum Env<'a, V> | ||
| where | ||
| V: Eval<'a>; | ||
| V: Eval<'a>, | ||
| { | ||
| Empty, | ||
| Cons { | ||
| data: &'a Value<'a, V>, | ||
| next: &'a Env<'a, V>, | ||
| }, | ||
| } | ||
|
|
||
| impl<'a, V> Env<'a, V> | ||
| where | ||
| V: Eval<'a>, | ||
| { | ||
| pub fn new_in(arena: &'a Bump) -> &'a Self { | ||
| arena.alloc(Self(BumpVec::new_in(arena))) | ||
| arena.alloc(Self::Empty) | ||
| } | ||
|
|
||
| pub fn push(&'a self, arena: &'a Bump, argument: &'a Value<'a, V>) -> &'a Self { | ||
| let mut new_env = self.0.clone(); | ||
|
|
||
| new_env.push(argument); | ||
|
|
||
| arena.alloc(Self(new_env)) | ||
| pub fn push(&'a self, arena: &'a Bump, arg: &'a Value<'a, V>) -> &'a Self { | ||
| arena.alloc(Self::Cons { | ||
| data: arg, | ||
| next: self, | ||
| }) | ||
| } | ||
|
|
||
| pub fn lookup(&'a self, name: usize) -> Option<&'a Value<'a, V>> { | ||
| self.0.get(self.0.len() - name).copied() | ||
| // De Bruijn indices are 1-based | ||
| // So the data at the env[i] is at De Bruijn index i-1 | ||
| pub fn lookup(&self, index: usize) -> Option<&'a Value<'a, V>> { | ||
| if index == 0 { | ||
| return None; | ||
| } | ||
|
|
||
| match self { | ||
| Env::Empty => None, | ||
| Env::Cons { data, next: parent } => { | ||
| if index == 1 { | ||
| return Some(data); | ||
| } | ||
|
|
||
| parent.lookup(index - 1) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.