feat: add managed types, source classes and scoped native calls
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
| 文件 | 职责 |
|
||||
| --- | --- |
|
||||
| [lib.rs](lib.rs) | CompileOptions、公开分析/编译入口、AST 原文范围检查与带来源诊断 |
|
||||
| [analysis.rs](analysis.rs) | 模块/函数/表达式/单次交互作用域、局部槽、全局名称、逐引用绑定和初始化检查 |
|
||||
| [codegen.rs](codegen.rs) | AST 到已有指令、嵌套代码模板、EndorCode 验证、显式 VM 装载与临时根释放 |
|
||||
| [analysis.rs](analysis.rs) | 模块/函数/类方法/表达式/单次交互作用域、局部槽、全局名称、逐引用绑定和初始化检查 |
|
||||
| [codegen.rs](codegen.rs) | AST 到函数与类创建指令、嵌套代码模板、EndorCode 验证、显式 VM 装载与临时根释放 |
|
||||
|
||||
节点定义在 frontend/Endor.asdl;这里匹配生成的 AST,不复制节点类型。源码和调用方提供的 AST 都先分析后生成,不执行 AST。修改名称语义、代码布局或所有权时同步 [编译契约](../../../docs/runtime/compiler.md)、[检查](../tests/README.md) 与 [示例](../examples/README.md)。返回 [compiler](../README.md)。
|
||||
|
||||
@@ -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"),
|
||||
}
|
||||
|
||||
@@ -150,7 +150,39 @@ impl<'a> Emitter<'a> {
|
||||
}
|
||||
|
||||
fn declaration(&mut self, declaration: &Decl, symbols: &SymbolTable) -> Result<(), Diagnostic> {
|
||||
let DeclKind::Function(function) = &declaration.kind;
|
||||
match &declaration.kind {
|
||||
DeclKind::Function(function) => {
|
||||
self.function_value(function, symbols)?;
|
||||
self.emit(Opcode::StoreGlobal, self.name_index(&function.name.text))
|
||||
}
|
||||
DeclKind::Class(class) => {
|
||||
self.constant(Value::Text(class.name.text.clone()))?;
|
||||
let base = class
|
||||
.base
|
||||
.as_ref()
|
||||
.map_or("Object", |base| base.text.as_str());
|
||||
self.emit(Opcode::LoadGlobal, self.name_index(base))?;
|
||||
for field in &class.fields {
|
||||
self.constant(Value::Text(field.name.text.clone()))?;
|
||||
self.constant(Value::Text(field.type_name.text.clone()))?;
|
||||
}
|
||||
self.emit(Opcode::BuildMap, class.fields.len())?;
|
||||
for method in &class.methods {
|
||||
self.constant(Value::Text(method.name.text.clone()))?;
|
||||
self.function_value(method, symbols)?;
|
||||
}
|
||||
self.emit(Opcode::BuildMap, class.methods.len())?;
|
||||
self.simple(Opcode::BuildClass);
|
||||
self.emit(Opcode::StoreGlobal, self.name_index(&class.name.text))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn function_value(
|
||||
&mut self,
|
||||
function: &frontend::ast::Function,
|
||||
symbols: &SymbolTable,
|
||||
) -> Result<(), Diagnostic> {
|
||||
let scope = symbols
|
||||
.functions
|
||||
.iter()
|
||||
@@ -176,7 +208,7 @@ impl<'a> Emitter<'a> {
|
||||
self.children.push((index, child));
|
||||
self.constant(Value::Nil)?; // Replaced by a managed code object during load().
|
||||
self.instructions.push(Instruction::make_function(0, 0));
|
||||
self.emit(Opcode::StoreGlobal, self.name_index(&function.name.text))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store_binding(&mut self, name: &str, value: &Expr) -> Result<(), Diagnostic> {
|
||||
@@ -202,6 +234,11 @@ impl<'a> Emitter<'a> {
|
||||
StmtKind::Assignment(assignment) => {
|
||||
self.store_binding(&assignment.name.text, &assignment.value)?
|
||||
}
|
||||
StmtKind::MemberAssignment(assignment) => {
|
||||
self.expression(&assignment.value)?;
|
||||
self.expression(&assignment.object)?;
|
||||
self.emit(Opcode::StoreAttr, self.name_index(&assignment.name.text))?;
|
||||
}
|
||||
StmtKind::Return(statement) => {
|
||||
if let Some(value) = &statement.value {
|
||||
self.expression(value)?;
|
||||
|
||||
Reference in New Issue
Block a user