diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-28 16:20:17 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-28 16:20:17 +0200 |
commit | 476e0b02e7570d5f04f7b4fa8614b3cb46de919b (patch) | |
tree | cbdea60584f8d30f99749abdccf8083c22850362 /crates | |
parent | 853b44ba5b1d64070017336c1a480256afbfd0b2 (diff) | |
download | rebel-476e0b02e7570d5f04f7b4fa8614b3cb46de919b.tar rebel-476e0b02e7570d5f04f7b4fa8614b3cb46de919b.zip |
rebel-lang: scope: make Module generic again
Even with the merged Contexts, we will need separate value and type
namespaces.
Diffstat (limited to 'crates')
-rw-r--r-- | crates/rebel-lang/src/scope.rs | 26 | ||||
-rw-r--r-- | crates/rebel-lang/src/value.rs | 2 |
2 files changed, 17 insertions, 11 deletions
diff --git a/crates/rebel-lang/src/scope.rs b/crates/rebel-lang/src/scope.rs index 420c88f..3fd7c99 100644 --- a/crates/rebel-lang/src/scope.rs +++ b/crates/rebel-lang/src/scope.rs @@ -10,7 +10,7 @@ use crate::{ #[derive(Debug, Default)] pub struct Context { - pub values: Module, + pub values: Module<(Type, Option<Value>)>, pub methods: HashMap<TypeFamily, HashMap<&'static str, Func>>, } @@ -50,18 +50,18 @@ impl Context { } } -#[derive(Debug, Default)] -pub struct Module(pub HashMap<String, ModuleEntry>); +#[derive(Debug)] +pub struct Module<T>(pub HashMap<String, ModuleEntry<T>>); -impl Module { - pub fn lookup(&self, path: &[ast::Ident<'_>]) -> Option<(&Type, Option<&Value>)> { +impl<T> Module<T> { + pub fn lookup(&self, path: &[ast::Ident<'_>]) -> Option<&T> { let (ident, rest) = path.split_first()?; match self.0.get(ident.name)? { ModuleEntry::Module(module) => module.lookup(rest), - ModuleEntry::Def((typ, val)) => { + ModuleEntry::Def(def) => { if rest.is_empty() { - Some((&typ, val.as_ref())) + Some(def) } else { None } @@ -70,8 +70,14 @@ impl Module { } } +impl<T> Default for Module<T> { + fn default() -> Self { + Self(HashMap::new()) + } +} + #[derive(Debug)] -pub enum ModuleEntry { - Module(Module), - Def((Type, Option<Value>)), +pub enum ModuleEntry<T> { + Module(Module<T>), + Def(T), } diff --git a/crates/rebel-lang/src/value.rs b/crates/rebel-lang/src/value.rs index 4f30dfd..4c5dc25 100644 --- a/crates/rebel-lang/src/value.rs +++ b/crates/rebel-lang/src/value.rs @@ -244,7 +244,7 @@ impl Value { ctx.values .lookup(&path.components) - .and_then(|(_, val)| val) + .and_then(|(_, val)| val.as_ref()) .ok_or(EvalError) .cloned() } |