279 lines
5.8 KiB
Rust
279 lines
5.8 KiB
Rust
// Charter AST - 抽象语法树定义
|
||
// 基于NAC UDM类型系统
|
||
|
||
use nac_udm::prelude::*;
|
||
use nac_udm::primitives::SovereigntyType;
|
||
use serde::{Deserialize, Serialize};
|
||
|
||
/// 程序(顶层)
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct Program {
|
||
pub items: Vec<TopLevelItem>,
|
||
}
|
||
|
||
/// 顶层项
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub enum TopLevelItem {
|
||
Module(ModuleDeclaration),
|
||
Import(ImportStatement),
|
||
Asset(AssetDefinition),
|
||
Contract(ContractDefinition),
|
||
Function(FunctionDeclaration),
|
||
}
|
||
|
||
/// 函数声明(顶层函数,用于标准库)
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct FunctionDeclaration {
|
||
pub modifiers: Vec<MethodModifier>,
|
||
pub name: String,
|
||
pub parameters: Vec<Parameter>,
|
||
pub return_type: Option<TypeAnnotation>,
|
||
pub body: Block,
|
||
}
|
||
|
||
/// 模块声明
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ModuleDeclaration {
|
||
pub name: String,
|
||
}
|
||
|
||
/// 导入语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ImportStatement {
|
||
pub path: String,
|
||
}
|
||
|
||
/// 资产定义
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct AssetDefinition {
|
||
pub name: String,
|
||
pub gnacs_code: GNACSCode, // 使用NAC UDM的GNACSCode类型
|
||
pub sovereignty: Option<SovereigntyType>, // 使用NAC UDM的SovereigntyType
|
||
pub fields: Vec<FieldDeclaration>,
|
||
pub methods: Vec<MethodDeclaration>,
|
||
}
|
||
|
||
// SovereigntyType已由NAC UDM定义,直接使用
|
||
|
||
/// 合约定义
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ContractDefinition {
|
||
pub name: String,
|
||
pub fields: Vec<FieldDeclaration>,
|
||
pub methods: Vec<MethodDeclaration>,
|
||
}
|
||
|
||
/// 字段声明
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct FieldDeclaration {
|
||
pub name: String,
|
||
pub type_annotation: TypeAnnotation,
|
||
}
|
||
|
||
/// 方法声明
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct MethodDeclaration {
|
||
pub modifiers: Vec<MethodModifier>,
|
||
pub name: String,
|
||
pub parameters: Vec<Parameter>,
|
||
pub return_type: Option<TypeAnnotation>,
|
||
pub requires: Vec<Expression>, // 前置条件
|
||
pub ensures: Vec<Expression>, // 后置条件
|
||
pub body: Block,
|
||
}
|
||
|
||
/// 方法修饰符
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub enum MethodModifier {
|
||
Public,
|
||
Private,
|
||
Internal,
|
||
Payable,
|
||
View,
|
||
Pure,
|
||
}
|
||
|
||
/// 参数
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct Parameter {
|
||
pub name: String,
|
||
pub type_annotation: TypeAnnotation,
|
||
}
|
||
|
||
/// 类型注解
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub enum TypeAnnotation {
|
||
// 基础类型
|
||
Uint8,
|
||
Uint16,
|
||
Uint32,
|
||
Uint64,
|
||
Uint128,
|
||
Uint256,
|
||
Int8,
|
||
Int16,
|
||
Int32,
|
||
Int64,
|
||
Int128,
|
||
Int256,
|
||
Bool,
|
||
String,
|
||
Bytes,
|
||
Address,
|
||
Hash,
|
||
Timestamp,
|
||
|
||
// NAC类型
|
||
DID,
|
||
GNACSCode,
|
||
ConstitutionalReceipt,
|
||
AssetInstance,
|
||
ACC20,
|
||
ACC721,
|
||
ACC1155,
|
||
ACCRWA,
|
||
|
||
// 数组类型
|
||
Array(Box<TypeAnnotation>, Option<usize>),
|
||
|
||
// Vec类型(动态数组)
|
||
Vec(Box<TypeAnnotation>),
|
||
|
||
// 引用类型
|
||
Reference(Box<TypeAnnotation>),
|
||
}
|
||
|
||
/// 代码块
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct Block {
|
||
pub statements: Vec<Statement>,
|
||
}
|
||
|
||
/// 语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub enum Statement {
|
||
Let(LetStatement),
|
||
Assign(AssignStatement),
|
||
If(IfStatement),
|
||
For(ForStatement),
|
||
While(WhileStatement),
|
||
Return(ReturnStatement),
|
||
Emit(EmitStatement),
|
||
RequireCR(Expression),
|
||
VerifyCR(Expression),
|
||
Expression(Expression),
|
||
}
|
||
|
||
/// Let语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct LetStatement {
|
||
pub mutable: bool,
|
||
pub name: String,
|
||
pub type_annotation: Option<TypeAnnotation>,
|
||
pub value: Expression,
|
||
}
|
||
|
||
/// 赋值语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct AssignStatement {
|
||
pub target: String,
|
||
pub value: Expression,
|
||
}
|
||
|
||
/// If语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct IfStatement {
|
||
pub condition: Expression,
|
||
pub then_block: Block,
|
||
pub else_block: Option<Block>,
|
||
}
|
||
|
||
/// For语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ForStatement {
|
||
pub variable: String,
|
||
pub iterable: Expression,
|
||
pub body: Block,
|
||
}
|
||
|
||
/// While语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct WhileStatement {
|
||
pub condition: Expression,
|
||
pub body: Block,
|
||
}
|
||
|
||
/// Return语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ReturnStatement {
|
||
pub value: Option<Expression>,
|
||
}
|
||
|
||
/// Emit语句
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct EmitStatement {
|
||
pub event_name: String,
|
||
pub arguments: Vec<Expression>,
|
||
}
|
||
|
||
/// 表达式
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub enum Expression {
|
||
// 字面量
|
||
Integer(u64),
|
||
HexNumber(String),
|
||
String(String),
|
||
Boolean(bool),
|
||
GNACSCode(String),
|
||
DID(String),
|
||
|
||
// 标识符
|
||
Identifier(String),
|
||
|
||
// 二元运算
|
||
Binary(BinaryOp, Box<Expression>, Box<Expression>),
|
||
|
||
// 一元运算
|
||
Unary(UnaryOp, Box<Expression>),
|
||
|
||
// 函数调用
|
||
FunctionCall(String, Vec<Expression>),
|
||
|
||
// 成员访问
|
||
MemberAccess(Box<Expression>, String),
|
||
|
||
// 数组访问
|
||
ArrayAccess(Box<Expression>, Box<Expression>),
|
||
|
||
// If表达式(三元表达式)
|
||
If(Box<Expression>, Box<Expression>, Box<Expression>),
|
||
|
||
// 类型转换
|
||
Cast(Box<Expression>, TypeAnnotation),
|
||
}
|
||
|
||
/// 二元运算符
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub enum BinaryOp {
|
||
Add,
|
||
Sub,
|
||
Mul,
|
||
Div,
|
||
Mod,
|
||
Equal,
|
||
NotEqual,
|
||
Less,
|
||
Greater,
|
||
LessEqual,
|
||
GreaterEqual,
|
||
And,
|
||
Or,
|
||
}
|
||
|
||
/// 一元运算符
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub enum UnaryOp {
|
||
Not,
|
||
Neg,
|
||
}
|