Fixing more bugs. WIP.

This commit is contained in:
Jesse Brault 2026-03-21 10:55:47 -05:00
parent 912f208705
commit 7f1d507f4f
9 changed files with 81 additions and 11 deletions

View File

@ -32,6 +32,8 @@ pub struct Class {
fields: Vec<Field>, fields: Vec<Field>,
functions: Vec<Function>, functions: Vec<Function>,
scope_id: Option<usize>, scope_id: Option<usize>,
self_class_scope_id: Option<usize>,
self_class_body_scope_id: Option<usize>,
} }
impl Class { impl Class {
@ -51,31 +53,35 @@ impl Class {
fields, fields,
functions, functions,
scope_id: None, 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) { pub fn init_scopes(&mut self, symbol_table: &mut SymbolTable, container_scope: usize) {
self.scope_id = Some(container_scope); self.scope_id = Some(container_scope);
let class_scope = let class_scope_id =
symbol_table.push_class_scope(&format!("class_scope({})", self.declared_name)); 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 { 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)); .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 { 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 { 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 { 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(); symbol_table.pop_scope();
@ -111,7 +117,7 @@ impl Class {
resolve_ctor_name(fqn_context), resolve_ctor_name(fqn_context),
false, false,
true, true,
self.scope_id.unwrap(), self.self_class_body_scope_id.unwrap(),
vec![], vec![],
)) ))
}; };

View File

@ -90,9 +90,10 @@ impl TypeUse {
.class_instance_types() .class_instance_types()
.get(&class_symbol) .get(&class_symbol)
.unwrap(), .unwrap(),
TypeSymbol::GenericParameter(generic_parameter_symbol) => { TypeSymbol::GenericParameter(generic_parameter_symbol) => types_table
todo!(); .generic_parameter_types()
} .get(&generic_parameter_symbol)
.unwrap(),
} }
} }
@ -172,6 +173,8 @@ mod tests {
fn type_check_generics() -> Result<(), Vec<Diagnostic>> { fn type_check_generics() -> Result<(), Vec<Diagnostic>> {
let mut compilation_unit = parse_compilation_unit( let mut compilation_unit = parse_compilation_unit(
" "
class String end
class Foo<T> class Foo<T>
ctor(t: T) end ctor(t: T) end
end end

View File

@ -1,5 +1,6 @@
use crate::symbol::variable_symbol::VariableSymbol; use crate::symbol::variable_symbol::VariableSymbol;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::rc::Rc; use std::rc::Rc;
pub struct BlockScope { pub struct BlockScope {
@ -29,3 +30,9 @@ impl BlockScope {
self.parent_id 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)
}
}

View File

@ -3,6 +3,7 @@ use crate::symbol::constructor_symbol::ConstructorSymbol;
use crate::symbol::field_symbol::FieldSymbol; use crate::symbol::field_symbol::FieldSymbol;
use crate::symbol::function_symbol::FunctionSymbol; use crate::symbol::function_symbol::FunctionSymbol;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::rc::Rc; use std::rc::Rc;
pub struct ClassBodyScope { pub struct ClassBodyScope {
@ -62,3 +63,9 @@ impl ClassBodyScope {
self.parent_id 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)
}
}

View File

@ -1,5 +1,6 @@
use crate::symbol::generic_parameter_symbol::GenericParameterSymbol; use crate::symbol::generic_parameter_symbol::GenericParameterSymbol;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::rc::Rc; use std::rc::Rc;
pub struct ClassScope { pub struct ClassScope {
@ -31,3 +32,9 @@ impl ClassScope {
&mut self.generic_parameter_symbols &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)
}
}

View File

@ -1,5 +1,6 @@
use crate::symbol::parameter_symbol::ParameterSymbol; use crate::symbol::parameter_symbol::ParameterSymbol;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::rc::Rc; use std::rc::Rc;
pub struct FunctionScope { pub struct FunctionScope {
@ -29,3 +30,9 @@ impl FunctionScope {
self.parent_id 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)
}
}

View File

@ -3,6 +3,7 @@ use crate::scope::class_body_scope::ClassBodyScope;
use crate::scope::class_scope::ClassScope; use crate::scope::class_scope::ClassScope;
use crate::scope::function_scope::FunctionScope; use crate::scope::function_scope::FunctionScope;
use crate::scope::module_scope::ModuleScope; use crate::scope::module_scope::ModuleScope;
use std::fmt::{Debug, Formatter};
pub mod block_scope; pub mod block_scope;
pub mod class_body_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)
}
}
}
}

View File

@ -1,6 +1,7 @@
use crate::symbol::class_symbol::ClassSymbol; use crate::symbol::class_symbol::ClassSymbol;
use crate::symbol::function_symbol::FunctionSymbol; use crate::symbol::function_symbol::FunctionSymbol;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::rc::Rc; use std::rc::Rc;
pub struct ModuleScope { pub struct ModuleScope {
@ -40,3 +41,9 @@ impl ModuleScope {
self.parent_id 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)
}
}

View File

@ -170,7 +170,10 @@ impl SymbolTable {
.constructor_symbol_mut() .constructor_symbol_mut()
.replace(constructor_symbol); .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())
),
} }
} }