plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
fc5d059
— Peter John
2026-11-11T01:17:52+05:30
checker: look globals up by hash in TypeEnv
- compiler/checker/src/env.rs +35 -6
compiler/checker/src/env.rs
CHANGED
|
@@ -6,13 +6,40 @@
|
|
|
6
6
|
|
|
7
7
|
use crate::types::TypeScheme;
|
|
8
8
|
use std::collections::btree_map;
|
|
9
|
-
use std::collections::BTreeMap;
|
|
9
|
+
use std::collections::{BTreeMap, HashMap};
|
|
10
10
|
use std::iter::Peekable;
|
|
11
11
|
use std::rc::Rc;
|
|
12
12
|
|
|
13
|
+
/// The frozen globals: sorted for the range and in-order reads, hashed for the lookups by
|
|
14
|
+
/// name, which are most of an env's use (a sorted map of every global compares a name with a
|
|
15
|
+
/// dozen others per lookup).
|
|
16
|
+
#[derive(Debug, Clone, Default)]
|
|
17
|
+
struct Base {
|
|
18
|
+
sorted: BTreeMap<String, TypeScheme>,
|
|
19
|
+
index: HashMap<String, TypeScheme>,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl Base {
|
|
23
|
+
fn get(&self, k: &str) -> Option<&TypeScheme> {
|
|
24
|
+
self.index.get(k)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn contains_key(&self, k: &str) -> bool {
|
|
28
|
+
self.index.contains_key(k)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fn range<'a>(&'a self, from: (std::ops::Bound<&'a str>, std::ops::Bound<&'a str>)) -> btree_map::Range<'a, String, TypeScheme> {
|
|
32
|
+
self.sorted.range::<str, _>(from)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn iter(&self) -> btree_map::Iter<'_, String, TypeScheme> {
|
|
36
|
+
self.sorted.iter()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
13
40
|
#[derive(Debug, Clone, Default)]
|
|
14
41
|
pub struct TypeEnv {
|
|
15
|
-
base: Rc<
|
|
42
|
+
base: Rc<Base>,
|
|
16
43
|
/// Names bound since the base was frozen; `None` hides a base name (`remove`).
|
|
17
44
|
own: BTreeMap<String, Option<TypeScheme>>,
|
|
18
45
|
}
|
|
@@ -31,10 +58,12 @@ impl TypeEnv {
|
|
|
31
58
|
for (k, v) in std::mem::take(&mut self.own) {
|
|
32
59
|
match v {
|
|
33
60
|
Some(v) => {
|
|
61
|
+
base.index.insert(k.clone(), v.clone());
|
|
34
|
-
base.insert(k, v);
|
|
62
|
+
base.sorted.insert(k, v);
|
|
35
63
|
}
|
|
36
64
|
None => {
|
|
37
|
-
base.remove(&k);
|
|
65
|
+
base.index.remove(&k);
|
|
66
|
+
base.sorted.remove(&k);
|
|
38
67
|
}
|
|
39
68
|
}
|
|
40
69
|
}
|
|
@@ -81,7 +110,7 @@ impl TypeEnv {
|
|
|
81
110
|
return true;
|
|
82
111
|
}
|
|
83
112
|
self.base
|
|
84
|
-
.range
|
|
113
|
+
.range((std::ops::Bound::Included(prefix), std::ops::Bound::Unbounded))
|
|
85
114
|
.take_while(|(k, _)| k.starts_with(prefix))
|
|
86
115
|
.any(|(k, _)| !matches!(self.own.get(k), Some(None)))
|
|
87
116
|
}
|
|
@@ -97,7 +126,7 @@ impl TypeEnv {
|
|
|
97
126
|
.filter_map(|(k, v)| v.as_ref().map(|v| (k, v)));
|
|
98
127
|
let base = self
|
|
99
128
|
.base
|
|
100
|
-
.range
|
|
129
|
+
.range(from())
|
|
101
130
|
.take_while(move |(k, _)| k.starts_with(prefix))
|
|
102
131
|
.filter(move |(k, _)| !self.own.contains_key(k.as_str()));
|
|
103
132
|
own.chain(base)
|