Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

v0.4.7

Choose a tag to compare

@HelloAIXIAOJI HelloAIXIAOJI released this 26 Jul 21:22
· 844 commits to master since this release

[v0.4.7] - 2025-07-27

🎯 Major New Feature: Enum Types

Core Features

  • Full enum syntax support

    • Basic enums: enum Color { Red, Green, Blue };
    • Parameterized enums: enum Result { Success(string, int), Error(string) };
    • Complex enums: Supports up to 10 parameters of different types per variant
    • Mixed enums: Different variants can have different parameter structures
  • Type safety guarantees

    • Strict runtime type checking and parameter validation
    • Smart type inference for automatic enum recognition
    • Function parameter and return value enum validation
    • Type safety checks for enum variable assignments
  • Method call support

    • toString(): Get string representation of enum
    • getEnumName(): Get enum type name
    • getVariantName(): Get enum variant name
    • length(): Get number of fields in enum variant
  • String operation integration

    • Support for enum-string concatenation (+ operator)
    • New string methods: startsWith(), endsWith(), contains()
    • Full string comparison and processing support

Syntax Examples

// Basic enum
enum Status { Active, Inactive, Pending };

// Parameterized enum
enum ApiResponse {
    Ok(int, string),
    Error(string),
    NotFound(string)
};

// Enum usage
status : Status = Status::Active;
response : ApiResponse = ApiResponse::Ok(200, "Success");

// Enum methods
statusName : string = status.getVariantName();  // "Active"
responseStr : string = response.toString();     // "ApiResponse::Ok(200, Success)"

Technical Implementation

  • AST Extension: Added Enum, EnumVariant, EnumField structs and expressions
  • Parser Enhancement: New enum_parser.rs module for full enum syntax parsing
  • Interpreter Improvement: Added EnumInstance value type with creation/access logic
  • Type System Integration: Extended type checking for smart enum matching

Test Verification

  • ✅ Basic: Simple enum definition and usage
  • ✅ Complex: JSON processing, HTTP states, game state machines
  • ✅ Performance: 1000+ enum instance creation
  • ✅ Edge Cases: Null values, special chars, long strings
  • ✅ Business Logic: Auth systems, API responses, state transitions

Backward Compatibility

  • ✅ Fully backward compatible with existing code
  • ✅ Seamless integration with classes, interfaces, namespaces
  • ✅ No breaking changes to existing APIs

Known Limitations

  • Negative number literals not supported as enum parameters
  • Pattern matching syntax not yet implemented (planned)
  • Only positional parameters supported (no named fields)

Example Files

  • examples/enum_test.cn - Basic functionality demo
  • examples/enum_complex_test.cn - Complex scenario applications
  • examples/enum_final_test.cn - Comprehensive functionality tests

[v0.4.7] - 2025-07-27

🎯 重大新功能:枚举类型 (Enum Types)

核心特性

  • 完整的枚举语法支持

    • 基础枚举:enum Color { Red, Green, Blue };
    • 带参数枚举:enum Result { Success(string, int), Error(string) };
    • 复杂枚举:支持每个变体最多10个不同类型参数
    • 混合枚举:不同变体可拥有不同的参数结构
  • 类型安全保障

    • 严格的运行时类型检查和参数验证
    • 自动识别枚举类型的智能类型推断
    • 函数参数和返回值的枚举类型验证
    • 枚举变量赋值的类型安全检查
  • 方法调用支持

    • toString():获取枚举的字符串表示
    • getEnumName():获取枚举类型名称
    • getVariantName():获取枚举变体名称
    • length():获取枚举变体字段数量
  • 字符串操作集成

    • 支持枚举与字符串的连接操作(+ 运算符)
    • 新增字符串方法:startsWith(), endsWith(), contains()
    • 完整的字符串比较和处理支持

语法示例

// 基础枚举
enum Status { Active, Inactive, Pending };

// 带参数枚举
enum ApiResponse {
    Ok(int, string),
    Error(string),
    NotFound(string)
};

// 枚举使用
status : Status = Status::Active;
response : ApiResponse = ApiResponse::Ok(200, "成功");

// 枚举方法
statusName : string = status.getVariantName();  // "Active"
responseStr : string = response.toString();     // "ApiResponse::Ok(200, 成功)"

技术实现

  • AST扩展:新增EnumEnumVariantEnumField结构体和相关表达式
  • 解析器增强:新增enum_parser.rs模块支持完整枚举语法
  • 解释器改进:新增EnumInstance值类型实现创建/访问逻辑
  • 类型系统集成:扩展类型检查机制支持智能枚举匹配

测试验证

  • ✅ 基础测试:简单枚举定义和使用
  • ✅ 复杂场景:JSON处理、HTTP状态、游戏状态机
  • ✅ 性能测试:1000+枚举实例创建
  • ✅ 边界情况:空值、特殊字符、长字符串
  • ✅ 业务逻辑:认证系统、API响应、状态转换

向后兼容性

  • ✅ 完全向后兼容现有代码
  • ✅ 与类、接口、命名空间无缝集成
  • ✅ 不破坏现有API和语法结构

已知限制

  • 不支持负数字面量作为枚举参数
  • 暂未实现模式匹配语法(计划中)
  • 仅支持位置参数(不支持命名字段)

示例文件

  • examples/enum_test.cn - 基础功能演示
  • examples/enum_complex_test.cn - 复杂场景应用
  • examples/enum_final_test.cn - 综合功能测试

Full Changelog: CodeNothingCommunity/CodeNothing@v0.4.6...v0.4.7