summaryrefslogtreecommitdiffstats
path: root/crates/rebel-lang/src/scope.rs
blob: b8a66ce68328166bf90b24abb0f68a66554bf9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use std::collections::HashMap;

use rebel_parse::ast;

#[derive(Debug)]
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)]
pub enum ModuleEntry<T> {
	Module(Module<T>),
	Def(T),
}