feat: add managed types, source classes and scoped native calls

This commit is contained in:
2026-10-21 01:47:08 +08:00
parent 7c842789c0
commit a970fd7707
48 changed files with 2069 additions and 134 deletions
+66 -17
View File
@@ -39,7 +39,7 @@ pub struct Scope {
#[derive(Debug, Clone)]
pub struct SymbolTable {
pub root: Scope,
/// Top-level function scopes in declaration order. Nested declarations
/// Function and class-method scopes in declaration order. Nested declarations
/// are rejected until source closure semantics are implemented.
pub functions: Vec<Scope>,
}
@@ -48,9 +48,11 @@ fn error(span: Span, message: impl Into<String>) -> Diagnostic {
diagnostic(DiagnosticKind::Semantic, span, message)
}
fn definition(declaration: &Decl) -> &Function {
let DeclKind::Function(function) = &declaration.kind;
function
fn declaration_name(declaration: &Decl) -> &frontend::ast::Name {
match &declaration.kind {
DeclKind::Function(function) => &function.name,
DeclKind::Class(class) => &class.name,
}
}
/// Resolve a closed source unit plus explicitly declared host globals.
@@ -69,21 +71,25 @@ pub fn analyze(root: &AstRoot, options: &CompileOptions) -> Result<SymbolTable,
if globals.contains("") {
return Err(error(root.span(), "host global names must not be empty"));
}
let functions: Vec<&Function> = match root {
AstRoot::Module(program) => program.declarations.iter().map(definition).collect(),
let declarations: Vec<&Decl> = match root {
AstRoot::Module(program) => program.declarations.iter().collect(),
AstRoot::Interactive(input) => input
.statements
.iter()
.filter_map(|statement| {
if let StmtKind::Declaration(declaration) = &statement.kind {
Some(definition(declaration))
} else {
None
}
.filter_map(|statement| match &statement.kind {
StmtKind::Declaration(declaration) => Some(declaration),
_ => None,
})
.collect(),
AstRoot::Expression(_) => Vec::new(),
};
let functions: Vec<&Function> = declarations
.iter()
.flat_map(|declaration| match &declaration.kind {
DeclKind::Function(function) => vec![function],
DeclKind::Class(class) => class.methods.iter().collect(),
})
.collect();
let (kind, name) = match root {
AstRoot::Module(_) => (ScopeKind::Module, "<module>"),
AstRoot::Expression(_) => (ScopeKind::Expression, "<expression>"),
@@ -91,10 +97,43 @@ pub fn analyze(root: &AstRoot, options: &CompileOptions) -> Result<SymbolTable,
};
let mut root_scope = Builder::new(kind, name, root.span());
let mut declared = BTreeSet::new();
for function in &functions {
declare(&mut declared, &function.name.text, function.name.span)?;
globals.insert(function.name.text.clone());
root_scope.intern(&function.name.text);
for declaration in &declarations {
let name = declaration_name(declaration);
declare(&mut declared, &name.text, name.span)?;
globals.insert(name.text.clone());
root_scope.intern(&name.text);
}
for declaration in &declarations {
if let DeclKind::Class(class) = &declaration.kind {
let base_name = class
.base
.as_ref()
.map_or("Object", |base| base.text.as_str());
if !globals.contains(base_name) {
return Err(error(
class.span,
format!("class base is not supplied: {base_name}"),
));
}
root_scope.intern(base_name);
let mut members = BTreeSet::new();
for field in &class.fields {
declare(&mut members, &field.name.text, field.span)?;
}
for method in &class.methods {
declare(&mut members, &method.name.text, method.span)?;
if method
.parameters
.first()
.is_none_or(|parameter| parameter.name.text != "self")
{
return Err(error(
method.span,
"class method requires an explicit first self parameter",
));
}
}
}
}
// Analyze all bodies after registering all functions, enabling forward
// references and recursion without instantiating function objects here.
@@ -119,6 +158,11 @@ pub fn analyze(root: &AstRoot, options: &CompileOptions) -> Result<SymbolTable,
globals.insert(assignment.name.text.clone());
root_scope.intern(&assignment.name.text);
}
StmtKind::MemberAssignment(assignment) => {
root_scope.expression(&assignment.object, &globals)?;
root_scope.expression(&assignment.value, &globals)?;
root_scope.intern(&assignment.name.text);
}
StmtKind::Declaration(_) => {}
StmtKind::Expression(expression) => {
root_scope.expression(expression, &globals)?
@@ -181,7 +225,7 @@ fn function_scope(function: &Function, globals: &BTreeSet<String>) -> Result<Sco
StmtKind::Declaration(_) => {
return Err(error(
statement.span,
"nested function declarations are not compiled yet",
"nested declarations are not compiled yet",
));
}
_ => {}
@@ -210,6 +254,11 @@ fn function_scope(function: &Function, globals: &BTreeSet<String>) -> Result<Sco
}
returned = true;
}
StmtKind::MemberAssignment(assignment) => {
builder.expression(&assignment.object, globals)?;
builder.expression(&assignment.value, globals)?;
builder.intern(&assignment.name.text);
}
StmtKind::Expression(expression) => builder.expression(expression, globals)?,
StmtKind::Declaration(_) => unreachable!("nested declaration rejected above"),
}