From 7f1d507f4f5dfe59b77a527eec5f5aa70d73812e Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Sat, 21 Mar 2026 10:55:47 -0500 Subject: [PATCH] Fixing more bugs. WIP. --- dmc-lib/src/ast/class.rs | 20 +++++++++++++------- dmc-lib/src/ast/type_use.rs | 9 ++++++--- dmc-lib/src/scope/block_scope.rs | 7 +++++++ dmc-lib/src/scope/class_body_scope.rs | 7 +++++++ dmc-lib/src/scope/class_scope.rs | 7 +++++++ dmc-lib/src/scope/function_scope.rs | 7 +++++++ dmc-lib/src/scope/mod.rs | 23 +++++++++++++++++++++++ dmc-lib/src/scope/module_scope.rs | 7 +++++++ dmc-lib/src/symbol_table/mod.rs | 5 ++++- 9 files changed, 81 insertions(+), 11 deletions(-) diff --git a/dmc-lib/src/ast/class.rs b/dmc-lib/src/ast/class.rs index b2381d1..02e6995 100644 --- a/dmc-lib/src/ast/class.rs +++ b/dmc-lib/src/ast/class.rs @@ -32,6 +32,8 @@ pub struct Class { fields: Vec, functions: Vec, scope_id: Option, + self_class_scope_id: Option, + self_class_body_scope_id: Option, } impl Class { @@ -51,31 +53,35 @@ impl Class { fields, functions, scope_id: None, + self_class_scope_id: None, + self_class_body_scope_id: None, } } pub fn init_scopes(&mut self, symbol_table: &mut SymbolTable, container_scope: usize) { self.scope_id = Some(container_scope); - let class_scope = + let class_scope_id = symbol_table.push_class_scope(&format!("class_scope({})", self.declared_name)); + self.self_class_scope_id = Some(class_scope_id); for generic_parameter in &mut self.generic_parameters { - generic_parameter.init_scopes(symbol_table, class_scope); + generic_parameter.init_scopes(symbol_table, class_scope_id); } - let class_body_scope = symbol_table + let class_body_scope_id = symbol_table .push_class_body_scope(&format!("class_body_scope({})", self.declared_name)); + self.self_class_body_scope_id = Some(class_body_scope_id); for field in &mut self.fields { - field.init_scopes(symbol_table, class_body_scope); + field.init_scopes(symbol_table, class_body_scope_id); } if let Some(constructor) = &mut self.constructor { - constructor.init_scopes(symbol_table, class_body_scope); + constructor.init_scopes(symbol_table, class_body_scope_id); } for function in &mut self.functions { - function.init_scopes(symbol_table, class_body_scope); + function.init_scopes(symbol_table, class_body_scope_id); } symbol_table.pop_scope(); @@ -111,7 +117,7 @@ impl Class { resolve_ctor_name(fqn_context), false, true, - self.scope_id.unwrap(), + self.self_class_body_scope_id.unwrap(), vec![], )) }; diff --git a/dmc-lib/src/ast/type_use.rs b/dmc-lib/src/ast/type_use.rs index 91d6454..75be815 100644 --- a/dmc-lib/src/ast/type_use.rs +++ b/dmc-lib/src/ast/type_use.rs @@ -90,9 +90,10 @@ impl TypeUse { .class_instance_types() .get(&class_symbol) .unwrap(), - TypeSymbol::GenericParameter(generic_parameter_symbol) => { - todo!(); - } + TypeSymbol::GenericParameter(generic_parameter_symbol) => types_table + .generic_parameter_types() + .get(&generic_parameter_symbol) + .unwrap(), } } @@ -172,6 +173,8 @@ mod tests { fn type_check_generics() -> Result<(), Vec> { let mut compilation_unit = parse_compilation_unit( " + class String end + class Foo ctor(t: T) end end diff --git a/dmc-lib/src/scope/block_scope.rs b/dmc-lib/src/scope/block_scope.rs index c2fce2c..409cd24 100644 --- a/dmc-lib/src/scope/block_scope.rs +++ b/dmc-lib/src/scope/block_scope.rs @@ -1,5 +1,6 @@ use crate::symbol::variable_symbol::VariableSymbol; use std::collections::HashMap; +use std::fmt::{Debug, Formatter}; use std::rc::Rc; pub struct BlockScope { @@ -29,3 +30,9 @@ impl BlockScope { self.parent_id } } + +impl Debug for BlockScope { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "BlockScope({}, {})", self.debug_name, self.parent_id) + } +} diff --git a/dmc-lib/src/scope/class_body_scope.rs b/dmc-lib/src/scope/class_body_scope.rs index 8a0d509..f1e39c1 100644 --- a/dmc-lib/src/scope/class_body_scope.rs +++ b/dmc-lib/src/scope/class_body_scope.rs @@ -3,6 +3,7 @@ use crate::symbol::constructor_symbol::ConstructorSymbol; use crate::symbol::field_symbol::FieldSymbol; use crate::symbol::function_symbol::FunctionSymbol; use std::collections::HashMap; +use std::fmt::{Debug, Formatter}; use std::rc::Rc; pub struct ClassBodyScope { @@ -62,3 +63,9 @@ impl ClassBodyScope { self.parent_id } } + +impl Debug for ClassBodyScope { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "ClassBodyScope({}, {})", self.debug_name, self.parent_id) + } +} diff --git a/dmc-lib/src/scope/class_scope.rs b/dmc-lib/src/scope/class_scope.rs index 75ae003..16c75f3 100644 --- a/dmc-lib/src/scope/class_scope.rs +++ b/dmc-lib/src/scope/class_scope.rs @@ -1,5 +1,6 @@ use crate::symbol::generic_parameter_symbol::GenericParameterSymbol; use std::collections::HashMap; +use std::fmt::{Debug, Formatter}; use std::rc::Rc; pub struct ClassScope { @@ -31,3 +32,9 @@ impl ClassScope { &mut self.generic_parameter_symbols } } + +impl Debug for ClassScope { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "ClassScope({}, {})", self.debug_name, self.parent_id) + } +} diff --git a/dmc-lib/src/scope/function_scope.rs b/dmc-lib/src/scope/function_scope.rs index 1ea73b1..34de02f 100644 --- a/dmc-lib/src/scope/function_scope.rs +++ b/dmc-lib/src/scope/function_scope.rs @@ -1,5 +1,6 @@ use crate::symbol::parameter_symbol::ParameterSymbol; use std::collections::HashMap; +use std::fmt::{Debug, Formatter}; use std::rc::Rc; pub struct FunctionScope { @@ -29,3 +30,9 @@ impl FunctionScope { self.parent_id } } + +impl Debug for FunctionScope { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "FunctionScope({}, {})", self.debug_name, self.parent_id) + } +} diff --git a/dmc-lib/src/scope/mod.rs b/dmc-lib/src/scope/mod.rs index 390c98a..574577f 100644 --- a/dmc-lib/src/scope/mod.rs +++ b/dmc-lib/src/scope/mod.rs @@ -3,6 +3,7 @@ use crate::scope::class_body_scope::ClassBodyScope; use crate::scope::class_scope::ClassScope; use crate::scope::function_scope::FunctionScope; use crate::scope::module_scope::ModuleScope; +use std::fmt::{Debug, Formatter}; pub mod block_scope; pub mod class_body_scope; @@ -29,3 +30,25 @@ impl Scope { } } } + +impl Debug for Scope { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Scope::Module(module_scope) => { + write!(f, "{:?}", module_scope) + } + Scope::Class(class_scope) => { + write!(f, "{:?}", class_scope) + } + Scope::ClassBody(class_body_scope) => { + write!(f, "{:?}", class_body_scope) + } + Scope::Function(function_scope) => { + write!(f, "{:?}", function_scope) + } + Scope::Block(block_scope) => { + write!(f, "{:?}", block_scope) + } + } + } +} diff --git a/dmc-lib/src/scope/module_scope.rs b/dmc-lib/src/scope/module_scope.rs index 72df133..3856f5a 100644 --- a/dmc-lib/src/scope/module_scope.rs +++ b/dmc-lib/src/scope/module_scope.rs @@ -1,6 +1,7 @@ use crate::symbol::class_symbol::ClassSymbol; use crate::symbol::function_symbol::FunctionSymbol; use std::collections::HashMap; +use std::fmt::{Debug, Formatter}; use std::rc::Rc; pub struct ModuleScope { @@ -40,3 +41,9 @@ impl ModuleScope { self.parent_id } } + +impl Debug for ModuleScope { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "ModuleScope({}, {:?})", self.debug_name, self.parent_id) + } +} diff --git a/dmc-lib/src/symbol_table/mod.rs b/dmc-lib/src/symbol_table/mod.rs index 711ff48..59f9894 100644 --- a/dmc-lib/src/symbol_table/mod.rs +++ b/dmc-lib/src/symbol_table/mod.rs @@ -170,7 +170,10 @@ impl SymbolTable { .constructor_symbol_mut() .replace(constructor_symbol); } - _ => panic!("Attempt to insert ConstructorSymbol in incompatible scope"), + _ => panic!( + "Attempt to insert ConstructorSymbol in incompatible scope: {:?}", + self.scope(constructor_symbol.scope_id()) + ), } }