summaryrefslogtreecommitdiffstats
path: root/crates/rebel-lang/src/scope.rs
blob: bae9678392396e514bcc0595ff9a74252db04b38 (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
36
37
38
39
40
41
use std::collections::HashMap;

use rebel_parse::ast;

use crate::{
	func::Func,
	typing::{Type, TypeFamily},
	value::Value,
};

#[derive(Debug, Default)]
pub struct Context {
	pub values: Module,
	pub methods: HashMap<TypeFamily, HashMap<&'static str, Func>>,
}

#[derive(Debug, Default)]
pub struct Module(HashMap<String, ModuleEntry>);

impl Module {
	pub fn lookup(&self, path: &[ast::Ident<'_>]) -> Option<(&Type, Option<&Value>)> {
		let (ident, rest) = path.split_first()?;

		match self.0.get(ident.name)? {
			ModuleEntry::Module(module) => module.lookup(rest),
			ModuleEntry::Def((typ, val)) => {
				if rest.is_empty() {
					Some((&typ, val.as_ref()))
				} else {
					None
				}
			}
		}
	}
}

#[derive(Debug)]
pub enum ModuleEntry {
	Module(Module),
	Def((Type, Option<Value>)),
}