#Package: ast

    MoonBit AST 节点定义(零依赖) | MoonBit AST node definitions (zero dependencies)

    #概述 | Overview

    定义 MoonBit 源码的抽象语法树(AST)节点类型,包括类型、表达式、模式、声明等。作为整个 transpiler 的基础数据模型,所有其他包均依赖此包。

    Defines abstract syntax tree (AST) node types for MoonBit source code, including types, expressions, patterns, and declarations. Serves as the foundational data model for the entire transpiler.

    #核心类型 | Core Types

    TypeDescription
    MoonType[CN] MoonBit 类型节点(23 变体:基础类型 + 容器 + 泛型) / [EN] MoonBit type node (23 variants: primitives + containers + generics)
    MoonExpr[CN] MoonBit 表达式节点(9 变体:字面量/变量/调用/二元/条件/匹配/await/lambda) / [EN] MoonBit expression node (9 variants)
    MoonPattern[CN] MoonBit 模式匹配节点(5 变体:变量/整数/字符串/通配/构造器) / [EN] MoonBit pattern matching node (5 variants)
    MoonDecl[CN] MoonBit 声明节点(7 变体:fn/async fn/struct/enum/trait/impl/extern fn) / [EN] MoonBit declaration node (7 variants)
    TraitMethod[CN] trait 方法签名 / [EN] Trait method signature
    ImplMethod[CN] impl 方法实现(含方法体) / [EN] Impl method definition (with body)
    Moon2DartError[CN] 统一错误类型(8 子错误:Parse/Transpile/ReverseTranspile/Json/Schema/FileIO/Project/Type) / [EN] Unified error type (8 sub-errors)
    TypeDisplay[CN] 类型显示 trait / [EN] Type display trait

    #核心函数 | Core Functions

    FunctionSignatureDescription
    MoonType::to_moon_stringMoonType -> String[CN] 类型转 MoonBit 字符串 / [EN] Type to MoonBit string
    MoonType::contains_resultMoonType -> Bool[CN] 递归检测是否包含 Result / [EN] Recursively check for Result type
    MoonType::is_pureMoonType -> Bool[CN] 判断是否可纯转换 / [EN] Check if type is purely mappable
    MoonType::needs_result_typeMoonType -> Bool[CN] 是否需要自定义 Result 类型 / [EN] Whether custom Result type is needed
    is_nested_optionMoonType -> Bool[CN] 检测嵌套 Option / [EN] Detect nested Option
    has_nested_option_in_declsArray[MoonDecl] -> Bool[CN] 声明列表中是否有嵌套 Option / [EN] Check for nested Option in declarations
    collect_types_from_declMoonDecl -> Array[MoonType][CN] 收集声明中使用的所有类型 / [EN] Collect all types used in declaration

    #依赖 | Dependencies

    • (零依赖 / Zero dependencies)

    #测试 | Tests

    • 白盒测试 / Whitebox: 11
    • 黑盒测试 / Blackbox: 0

    TypeDisplay

    pub(open) trait TypeDisplay {
    fn to_display_string(Self) -> String
    }

    类型显示 trait — 统一类型到字符串的转换接口 | Type display trait — unified type-to-string conversion interface

    Moon2DartError

    pub(all) suberror Moon2DartError {
    ParseError(String)
    TranspileError(String)
    ReverseTranspileError(String)
    JsonParseError(String)
    SchemaError(String)
    FileIOError(String)
    ProjectError(String)
    TypeError(String)
    }

    moon2dart 统一错误类型 — 覆盖所有阶段的错误场景 | moon2dart unified error type — covers all error scenarios 各包通过 raise Moon2DartError::XXX(...) 抛出,调用方通过 try...catch 捕获 | Packages raise via raise Moon2DartError::XXX(...), callers catch via try...catch

    ImplMethod

    pub(all) struct ImplMethod {
    name : String
    params : Array[(String, MoonType)]
    ret_type : MoonType
    body : MoonExpr
    } derive(Compare, Eq,
    Debug
    )

    impl 方法实现 — 方法名 + 参数列表 + 返回类型 + 方法体 | Impl method — name + params + return type + body

    ImplMethod::compare

    fn ImplMethod::compare(ImplMethod, ImplMethod) -> Int

    ImplMethod::equal

    fn ImplMethod::equal(ImplMethod, ImplMethod) -> Bool

    ImplMethod::not_equal

    fn ImplMethod::not_equal(x : ImplMethod, y : ImplMethod) -> Bool

    ImplMethod::op_ge

    fn ImplMethod::op_ge(x : ImplMethod, y : ImplMethod) -> Bool

    ImplMethod::op_gt

    fn ImplMethod::op_gt(x : ImplMethod, y : ImplMethod) -> Bool

    ImplMethod::op_le

    fn ImplMethod::op_le(x : ImplMethod, y : ImplMethod) -> Bool

    ImplMethod::op_lt

    fn ImplMethod::op_lt(x : ImplMethod, y : ImplMethod) -> Bool

    MoonDecl

    pub(all) enum MoonDecl {
    DFn(String, Array[String], Array[(String, MoonType)], MoonType, MoonExpr)
    DAsyncFn(String, Array[String], Array[(String, MoonType)], MoonType, MoonExpr)
    DStruct(String, Array[String], Array[(String, MoonType)], Array[String])
    DEnum(String, Array[String])
    DTrait(String, Array[TraitMethod])
    DImpl(String, String, Array[ImplMethod])
    DExternFn(String, Array[(String, MoonType)], MoonType, String)
    } derive(Compare, Eq,
    Debug
    )

    MoonBit 声明节点 | MoonBit declaration node

    MoonDecl::compare

    fn MoonDecl::compare(MoonDecl, MoonDecl) -> Int

    MoonDecl::equal

    fn MoonDecl::equal(MoonDecl, MoonDecl) -> Bool

    MoonDecl::not_equal

    fn MoonDecl::not_equal(x : MoonDecl, y : MoonDecl) -> Bool

    MoonDecl::op_ge

    fn MoonDecl::op_ge(x : MoonDecl, y : MoonDecl) -> Bool

    MoonDecl::op_gt

    fn MoonDecl::op_gt(x : MoonDecl, y : MoonDecl) -> Bool

    MoonDecl::op_le

    fn MoonDecl::op_le(x : MoonDecl, y : MoonDecl) -> Bool

    MoonDecl::op_lt

    fn MoonDecl::op_lt(x : MoonDecl, y : MoonDecl) -> Bool

    MoonDecl::to_repr

    MoonExpr

    pub(all) enum MoonExpr {
    EInt(Int)
    EDouble(Double)
    EBool(Bool)
    EString(String)
    EVar(String)
    ECall(String, Array[MoonExpr])
    EBinOp(String, MoonExpr, MoonExpr)
    EIf(MoonExpr, MoonExpr, MoonExpr)
    EMatch(MoonExpr, Array[(MoonPattern, MoonExpr)])
    EAwait(MoonExpr)
    ELambda(Array[(String, MoonType)], MoonExpr)
    } derive(Compare, Eq,
    Debug
    )

    MoonBit 表达式节点(v0.1.0 子集)| MoonBit expression node (v0.1.0 subset)

    MoonExpr::compare

    fn MoonExpr::compare(MoonExpr, MoonExpr) -> Int

    MoonExpr::equal

    fn MoonExpr::equal(MoonExpr, MoonExpr) -> Bool

    MoonExpr::not_equal

    fn MoonExpr::not_equal(x : MoonExpr, y : MoonExpr) -> Bool

    MoonExpr::op_ge

    fn MoonExpr::op_ge(x : MoonExpr, y : MoonExpr) -> Bool

    MoonExpr::op_gt

    fn MoonExpr::op_gt(x : MoonExpr, y : MoonExpr) -> Bool

    MoonExpr::op_le

    fn MoonExpr::op_le(x : MoonExpr, y : MoonExpr) -> Bool

    MoonExpr::op_lt

    fn MoonExpr::op_lt(x : MoonExpr, y : MoonExpr) -> Bool

    MoonExpr::to_repr

    MoonPattern

    pub(all) enum MoonPattern {
    PVar(String)
    PInt(Int)
    PString(String)
    PWild
    PCtor(String, Array[MoonPattern])
    } derive(Compare, Eq,
    Debug
    )

    MoonBit 模式匹配节点 | MoonBit pattern matching node

    MoonPattern::compare

    fn MoonPattern::compare(MoonPattern, MoonPattern) -> Int

    MoonPattern::equal

    fn MoonPattern::equal(MoonPattern, MoonPattern) -> Bool

    MoonPattern::not_equal

    fn MoonPattern::not_equal(x : MoonPattern, y : MoonPattern) -> Bool

    MoonPattern::op_ge

    fn MoonPattern::op_ge(x : MoonPattern, y : MoonPattern) -> Bool

    MoonPattern::op_gt

    fn MoonPattern::op_gt(x : MoonPattern, y : MoonPattern) -> Bool

    MoonPattern::op_le

    fn MoonPattern::op_le(x : MoonPattern, y : MoonPattern) -> Bool

    MoonPattern::op_lt

    fn MoonPattern::op_lt(x : MoonPattern, y : MoonPattern) -> Bool

    MoonType

    pub(all) enum MoonType {
    MInt
    MUInt
    MInt64
    MUInt64
    MDouble
    MFloat
    MBool
    MString
    MChar
    MUnit
    MArray(MoonType)
    MMap(MoonType, MoonType)
    MSet(MoonType)
    MOption(MoonType)
    MResult(MoonType, MoonType)
    MFuture(MoonType)
    MTypeVar(String)
    MCustom(String)
    } derive(Compare, Eq,
    Debug
    )

    MoonBit 类型节点 — 表示 MoonBit 源码中的类型 | MoonBit type node — represents types in MoonBit source

    MoonType::compare

    fn MoonType::compare(MoonType, MoonType) -> Int

    MoonType::contains_result

    fn MoonType::contains_result(self : MoonType) -> Bool

    递归检查类型中是否包含 Result | Recursively check if type contains Result

    MoonType::equal

    fn MoonType::equal(MoonType, MoonType) -> Bool

    MoonType::is_pure

    fn MoonType::is_pure(self : MoonType) -> Bool

    判断类型是否可纯转换(不含 Result 和 Custom) | Check if type is purely transpilable (no Result or Custom)

    MoonType::needs_result_type

    fn MoonType::needs_result_type(self : MoonType) -> Bool

    判断类型是否需要自定义 Result 类型定义 | Check if type requires a custom Result type definition

    MoonType::not_equal

    fn MoonType::not_equal(x : MoonType, y : MoonType) -> Bool

    MoonType::op_ge

    fn MoonType::op_ge(x : MoonType, y : MoonType) -> Bool

    MoonType::op_gt

    fn MoonType::op_gt(x : MoonType, y : MoonType) -> Bool

    MoonType::op_le

    fn MoonType::op_le(x : MoonType, y : MoonType) -> Bool

    MoonType::op_lt

    fn MoonType::op_lt(x : MoonType, y : MoonType) -> Bool

    MoonType::to_display_string

    fn MoonType::to_display_string(self : MoonType) -> String

    MoonType::to_moon_string

    fn MoonType::to_moon_string(self : MoonType) -> String

    MoonType → MoonBit 类型字符串表示 | MoonType to MoonBit type string representation

    MoonType::to_repr

    TraitMethod

    pub(all) struct TraitMethod {
    name : String
    params : Array[(String, MoonType)]
    ret_type : MoonType
    } derive(Compare, Eq,
    Debug
    )

    Trait 方法签名 — 方法名 + 参数列表 + 返回类型 | Trait method signature — name + params + return type

    TraitMethod::compare

    fn TraitMethod::compare(TraitMethod, TraitMethod) -> Int

    TraitMethod::equal

    fn TraitMethod::equal(TraitMethod, TraitMethod) -> Bool

    TraitMethod::not_equal

    fn TraitMethod::not_equal(x : TraitMethod, y : TraitMethod) -> Bool

    TraitMethod::op_ge

    fn TraitMethod::op_ge(x : TraitMethod, y : TraitMethod) -> Bool

    TraitMethod::op_gt

    fn TraitMethod::op_gt(x : TraitMethod, y : TraitMethod) -> Bool

    TraitMethod::op_le

    fn TraitMethod::op_le(x : TraitMethod, y : TraitMethod) -> Bool

    TraitMethod::op_lt

    fn TraitMethod::op_lt(x : TraitMethod, y : TraitMethod) -> Bool

    collect_types_from_decl

    fn collect_types_from_decl(d : MoonDecl) -> Array[MoonType]

    收集声明中使用的所有 MoonType | Collect all MoonType used in a declaration

    has_nested_option_in_decls

    fn has_nested_option_in_decls(decls : Array[MoonDecl]) -> Bool

    遍历声明列表,检测是否使用了嵌套 Option 类型 | Scan declaration list for nested Option type usage

    is_nested_option

    fn is_nested_option(t : MoonType) -> Bool

    检测类型是否为嵌套 Option(MOption(MOption()))| Check if type is a nested Option (MOption(MOption()))

    Source Files