diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-29 19:13:59 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-29 19:17:25 +0200 |
commit | f03f4dfc06ef0a2022b8cb35eced694ca07a063d (patch) | |
tree | a8a2f1f643ed5a20f538f4f6a0a4a8b165ae079e | |
parent | 684a6e62b40389dad5875c86c8d50658d676128d (diff) | |
download | rebel-f03f4dfc06ef0a2022b8cb35eced694ca07a063d.tar rebel-f03f4dfc06ef0a2022b8cb35eced694ca07a063d.zip |
rebel-lang: scope: add type namespace
Create a type namespace that is separate from the value namespace, and
initialize it with the basic types that should be in scope by default.
-rw-r--r-- | crates/rebel-lang/src/scope.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/crates/rebel-lang/src/scope.rs b/crates/rebel-lang/src/scope.rs index e935a07..559dc28 100644 --- a/crates/rebel-lang/src/scope.rs +++ b/crates/rebel-lang/src/scope.rs @@ -15,12 +15,32 @@ pub struct Var { pub value: Option<Value>, } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct Context { pub vars: Module<Var>, + pub types: Module<Type>, pub methods: HashMap<TypeFamily, HashMap<&'static str, Func>>, } +impl Default for Context { + fn default() -> Self { + let vars = Module::default(); + let mut types = Module::default(); + let methods = HashMap::default(); + + // Type "prelude" + types.insert("bool", Type::Bool); + types.insert("int", Type::Int); + types.insert("str", Type::Str); + + Self { + vars, + types, + methods, + } + } +} + impl Context { pub fn record_type(&mut self, stmt: &ast::BodyStmt) -> typing::Result<Type> { match stmt { @@ -90,6 +110,10 @@ impl Context { pub struct Module<T>(pub HashMap<String, ModuleEntry<T>>); impl<T> Module<T> { + pub fn insert(&mut self, ident: &str, value: T) { + self.0.insert(ident.to_owned(), ModuleEntry::Def(value)); + } + pub fn lookup(&self, path: &[ast::Ident<'_>]) -> Option<&T> { let (ident, rest) = path.split_first()?; |