// 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, } /// 顶层项 #[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, pub name: String, pub parameters: Vec, pub return_type: Option, 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, // 使用NAC UDM的SovereigntyType pub fields: Vec, pub methods: Vec, } // SovereigntyType已由NAC UDM定义,直接使用 /// 合约定义 #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ContractDefinition { pub name: String, pub fields: Vec, pub methods: Vec, } /// 字段声明 #[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, pub name: String, pub parameters: Vec, pub return_type: Option, pub requires: Vec, // 前置条件 pub ensures: Vec, // 后置条件 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, Option), // Vec类型(动态数组) Vec(Box), // 引用类型 Reference(Box), } /// 代码块 #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Block { pub statements: Vec, } /// 语句 #[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, 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, } /// 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, } /// Emit语句 #[derive(Debug, Clone, Serialize, Deserialize)] pub struct EmitStatement { pub event_name: String, pub arguments: Vec, } /// 表达式 #[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, Box), // 一元运算 Unary(UnaryOp, Box), // 函数调用 FunctionCall(String, Vec), // 成员访问 MemberAccess(Box, String), // 数组访问 ArrayAccess(Box, Box), // If表达式(三元表达式) If(Box, Box, Box), // 类型转换 Cast(Box, 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, }