[obsidian] vault backup: 2023-09-01 18:50:32[
This commit is contained in:
parent
1f02303cb4
commit
56b35ed5ee
@ -20,14 +20,28 @@ fn eval<'a>(expr:Expr,env:&'a mut Vec<(String,Val)>)->Result<Val,Error>{
|
|||||||
|
|
||||||
Environmentは評価全体で見ればLetやLambdaごとに分岐していく構造だけれど、局所的には1列のベクタで表現できる
|
Environmentは評価全体で見ればLetやLambdaごとに分岐していく構造だけれど、局所的には1列のベクタで表現できる
|
||||||
|
|
||||||
|
こんな感じすかねえ
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
struct<'a,T> Environment(&'a mut Vec<T>)
|
struct EnvironmentT<'a, T>(&'a mut Vec<(String, T)>, usize);
|
||||||
|
|
||||||
impl<'a,T> Environment{
|
impl<'a, T> EnvironmentT<'a, T> {
|
||||||
pub fn new()->Self{
|
pub fn new(vec: &'a mut Vec<(String, T)>, mut names: Vec<(String, T)>) -> Self {
|
||||||
Self(&mut Vec::new())
|
let len = vec.len();
|
||||||
|
vec.append(&mut names);
|
||||||
|
Self(vec, len)
|
||||||
|
}
|
||||||
|
pub fn drop(&mut self) {
|
||||||
|
self.0.truncate(self.1);
|
||||||
|
}
|
||||||
|
pub fn lookup(&self, name: &String) -> Option<&'a T> {
|
||||||
|
let res = self
|
||||||
|
.0
|
||||||
|
.iter()
|
||||||
|
.rev()
|
||||||
|
.filter(|(n, _v)| name == n)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
res.get(0).map(|(_, v)| v)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
Loading…
Reference in New Issue
Block a user