diff options
-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()?; |