From f03f4dfc06ef0a2022b8cb35eced694ca07a063d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 29 Apr 2024 19:13:59 +0200 Subject: 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. --- crates/rebel-lang/src/scope.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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, } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct Context { pub vars: Module, + pub types: Module, pub methods: HashMap>, } +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 { match stmt { @@ -90,6 +110,10 @@ impl Context { pub struct Module(pub HashMap>); impl Module { + 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()?; -- cgit v1.2.3