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
+2
View File
@@ -14,4 +14,6 @@
| [parser.rs](parser.rs) | 统一根/ParseMode 与具体解析 API、错误映射、AST 辅助函数,以及独立语法文件的编译期引入 |
| [grammar.peg](grammar.peg) | token 输入的 PEG 规则、运算符优先级与 AST 构造动作;保留 peg::parser! 外层声明 |
函数体允许独立方法调用等表达式语句;末尾 return 可省略,已有编译器隐式返回 Nil。
改节点先编辑 ../Endor.asdl,再用 scripts/generate_ast.py 更新 Rust/编辑器产物,契约见 [AST 模型](../../../docs/runtime/ast.md)。改输入适配时同步 [阅读器契约](../../../docs/runtime/reader.md) 与读取错误检查;改词法从 lexer.peg 进入,并同步 token、诊断和错误检查;改语法从 grammar.peg 进入,并同步 AST、[测试](../tests/README.md)、样例与 [前端契约](../../../docs/runtime/frontend.md)。词法与语法两份 PEG 动作均不做 VM 分配、I/O 或修改外部状态,避免回溯造成副作用。返回 [frontend](../README.md)。
+6 -4
View File
@@ -26,9 +26,11 @@ pub trait Spanned {
impl Program {
/// Iterate the currently supported top-level function declarations.
pub fn functions(&self) -> impl Iterator<Item = &Function> {
self.declarations.iter().map(|declaration| {
let DeclKind::Function(function) = &declaration.kind;
function
})
self.declarations
.iter()
.filter_map(|declaration| match &declaration.kind {
DeclKind::Function(function) => Some(function),
DeclKind::Class(_) => None,
})
}
}
+32 -1
View File
@@ -3,7 +3,7 @@
use super::Spanned;
use crate::Span;
pub const AST_SCHEMA_ID: &str =
"urn:endor:ast:sha256:8c89fde6c029357a0809751c41cb46d59972b987bbe1b25c0cced4bfcdbe2a7b";
"urn:endor:ast:sha256:ad2ac52943dd26e08395c0d2698861da98bb89c360241c4712c769de86709fd0";
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AstDocument {
@@ -63,16 +63,32 @@ impl Spanned for Decl {
#[serde(tag = "kind", content = "data", deny_unknown_fields)]
pub enum DeclKind {
Function(Function),
Class(Class),
}
impl Spanned for DeclKind {
fn span(&self) -> Span {
match self {
Self::Function(node) => node.span(),
Self::Class(node) => node.span(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Class {
pub name: Name,
pub base: Option<Name>,
pub fields: Vec<Parameter>,
pub methods: Vec<Function>,
pub span: Span,
}
impl Spanned for Class {
fn span(&self) -> Span {
self.span
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Function {
pub name: Name,
pub parameters: Vec<Parameter>,
@@ -124,6 +140,7 @@ impl Spanned for Stmt {
pub enum StmtKind {
Binding(Binding),
Assignment(Assignment),
MemberAssignment(MemberAssignment),
Return(ReturnStatement),
Expression(Expr),
Declaration(Decl),
@@ -133,6 +150,7 @@ impl Spanned for StmtKind {
match self {
Self::Binding(node) => node.span(),
Self::Assignment(node) => node.span(),
Self::MemberAssignment(node) => node.span(),
Self::Return(node) => node.span(),
Self::Expression(node) => node.span(),
Self::Declaration(node) => node.span(),
@@ -165,6 +183,19 @@ impl Spanned for Assignment {
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MemberAssignment {
pub object: Expr,
pub name: Name,
pub value: Expr,
pub span: Span,
}
impl Spanned for MemberAssignment {
fn span(&self) -> Span {
self.span
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReturnStatement {
pub value: Option<Expr>,
pub span: Span,
+45 -3
View File
@@ -1,9 +1,9 @@
// Generated by scripts/generate_ast.py from Endor.asdl. Do not edit.
use super::{
Assignment, AstDocument, AstRoot, BinaryOp, Binding, Block, Decl, DeclKind, Expr, ExprKind,
Function, InteractiveInput, Name, Parameter, Program, ReturnStatement, Spanned, Stmt, StmtKind,
UnaryOp,
Assignment, AstDocument, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr,
ExprKind, Function, InteractiveInput, MemberAssignment, Name, Parameter, Program,
ReturnStatement, Spanned, Stmt, StmtKind, UnaryOp,
};
use crate::Span;
pub trait Visitor {
@@ -48,6 +48,12 @@ pub trait Visitor {
self.leave_node();
result
}
fn visit_class(&mut self, node: &Class) -> Result<(), Self::Error> {
self.enter_node(node.span())?;
let result = walk_class(self, node);
self.leave_node();
result
}
fn visit_function(&mut self, node: &Function) -> Result<(), Self::Error> {
self.enter_node(node.span())?;
let result = walk_function(self, node);
@@ -90,6 +96,12 @@ pub trait Visitor {
self.leave_node();
result
}
fn visit_member_assignment(&mut self, node: &MemberAssignment) -> Result<(), Self::Error> {
self.enter_node(node.span())?;
let result = walk_member_assignment(self, node);
self.leave_node();
result
}
fn visit_return_statement(&mut self, node: &ReturnStatement) -> Result<(), Self::Error> {
self.enter_node(node.span())?;
let result = walk_return_statement(self, node);
@@ -163,9 +175,26 @@ pub fn walk_decl_kind<V: Visitor + ?Sized>(
DeclKind::Function(value) => {
visitor.visit_function(value)?;
}
DeclKind::Class(value) => {
visitor.visit_class(value)?;
}
}
Ok(())
}
pub fn walk_class<V: Visitor + ?Sized>(visitor: &mut V, node: &Class) -> Result<(), V::Error> {
visitor.visit_name(&node.name)?;
if let Some(value) = &node.base {
visitor.visit_name(value)?;
}
for value in &node.fields {
visitor.visit_parameter(value)?;
}
for value in &node.methods {
visitor.visit_function(value)?;
}
visitor.visit_span(&node.span)?;
Ok(())
}
pub fn walk_function<V: Visitor + ?Sized>(
visitor: &mut V,
node: &Function,
@@ -211,6 +240,9 @@ pub fn walk_stmt_kind<V: Visitor + ?Sized>(
StmtKind::Assignment(value) => {
visitor.visit_assignment(value)?;
}
StmtKind::MemberAssignment(value) => {
visitor.visit_member_assignment(value)?;
}
StmtKind::Return(value) => {
visitor.visit_return_statement(value)?;
}
@@ -238,6 +270,16 @@ pub fn walk_assignment<V: Visitor + ?Sized>(
visitor.visit_span(&node.span)?;
Ok(())
}
pub fn walk_member_assignment<V: Visitor + ?Sized>(
visitor: &mut V,
node: &MemberAssignment,
) -> Result<(), V::Error> {
visitor.visit_expr(&node.object)?;
visitor.visit_name(&node.name)?;
visitor.visit_expr(&node.value)?;
visitor.visit_span(&node.span)?;
Ok(())
}
pub fn walk_return_statement<V: Visitor + ?Sized>(
visitor: &mut V,
node: &ReturnStatement,
+35 -2
View File
@@ -51,6 +51,23 @@ peg::parser! {
= function:function() {
Decl { span: function.span, kind: DeclKind::Function(function) }
}
/ start:keyword("class") name:name() base:(colon() base:name() { base })?
lbrace() members:class_member()* end:rbrace() {
let mut fields = Vec::new();
let mut methods = Vec::new();
for member in members {
match member {
ClassMember::Field(field) => fields.push(field),
ClassMember::Method(method) => methods.push(method),
}
}
let span = start.through(end);
Decl { span, kind: DeclKind::Class(Class { name, base, fields, methods, span }) }
}
rule class_member() -> ClassMember
= function:function() { ClassMember::Method(function) }
/ field:parameter() { ClassMember::Field(field) }
pub rule interactive() -> InteractiveInput
= statement:interactive_statement() {
@@ -62,6 +79,7 @@ peg::parser! {
Stmt { span: declaration.span, kind: StmtKind::Declaration(declaration) }
}
/ binding:binding() { binding_statement(binding) }
/ assignment:member_assignment() { assignment }
/ assignment:assignment() { assignment }
/ expression:expression() {
Stmt { span: expression.span, kind: StmtKind::Expression(expression) }
@@ -79,15 +97,19 @@ peg::parser! {
}
rule block() -> Block
= start:lbrace() locals:local_statement()* returned:return_statement() end:rbrace() {
= start:lbrace() locals:local_statement()* returned:return_statement()? end:rbrace() {
let mut statements = locals;
statements.push(returned);
if let Some(returned) = returned { statements.push(returned); }
Block { statements, span: start.through(end) }
}
rule local_statement() -> Stmt
= binding:binding() { binding_statement(binding) }
/ assignment:member_assignment() { assignment }
/ assignment:assignment() { assignment }
/ !return_keyword() expression:expression() {
Stmt { span: expression.span, kind: StmtKind::Expression(expression) }
}
rule assignment() -> Stmt
= name:name() equal() value:expression() {
@@ -95,6 +117,17 @@ peg::parser! {
Stmt { span, kind: StmtKind::Assignment(Assignment { name, value, span }) }
}
rule member_assignment() -> Stmt
= target:postfix_expression() equal() value:expression() {?
let span = target.span.through(value.span);
match target.kind {
ExprKind::Member { object, name } => Ok(Stmt {
span, kind: StmtKind::MemberAssignment(MemberAssignment { object: *object, name, value, span }),
}),
_ => Err("member assignment target"),
}
}
rule binding() -> Binding
= start:let_keyword() name:name() equal() value:expression() {
Binding { span: start.through(value.span), name, value }
+9 -3
View File
@@ -1,6 +1,7 @@
use crate::ast::{
Assignment, AstRoot, BinaryOp, Binding, Block, Decl, DeclKind, Expr, ExprKind, Function,
InteractiveInput, Name, Parameter, Program, ReturnStatement, Stmt, StmtKind, UnaryOp,
Assignment, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr, ExprKind, Function,
InteractiveInput, MemberAssignment, Name, Parameter, Program, ReturnStatement, Stmt, StmtKind,
UnaryOp,
};
use crate::lexer::decode_string;
use crate::{Diagnostic, DiagnosticKind, Span, Token, TokenKind, lex};
@@ -44,7 +45,7 @@ pub fn parse_source_expression(
parse_expression(source.text()).map_err(|error| source.annotate(error))
}
/// Parse one or more basic typed function declarations. This is a syntax
/// Parse one or more typed function or class declarations. This is a syntax
/// operation: type names and name references are retained without resolution.
pub fn parse(source: &str) -> Result<Program, Diagnostic> {
let tokens = lex(source)?;
@@ -93,6 +94,11 @@ enum Suffix {
Call(Vec<Expr>, Span),
}
enum ClassMember {
Field(Parameter),
Method(Function),
}
fn postfix(expression: Expr, suffix: Suffix) -> Expr {
match suffix {
Suffix::Member(name) => Expr {