summaryrefslogtreecommitdiffstats
path: root/crates/rebel-lang/src/scope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rebel-lang/src/scope.rs')
-rw-r--r--crates/rebel-lang/src/scope.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/crates/rebel-lang/src/scope.rs b/crates/rebel-lang/src/scope.rs
new file mode 100644
index 0000000..6dcc7ee
--- /dev/null
+++ b/crates/rebel-lang/src/scope.rs
@@ -0,0 +1,35 @@
+use std::collections::HashMap;
+
+use rebel_parse::ast;
+
+#[derive(Debug, Clone)]
+pub struct Module<T>(HashMap<String, ModuleEntry<T>>);
+
+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) => {
+ if rest.is_empty() {
+ Some(typ)
+ } else {
+ None
+ }
+ }
+ }
+ }
+}
+
+impl<T> Default for Module<T> {
+ fn default() -> Self {
+ Self(Default::default())
+ }
+}
+
+#[derive(Debug, Clone)]
+pub enum ModuleEntry<T> {
+ Module(Module<T>),
+ Def(T),
+}