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

v0.5.8

Choose a tag to compare

@HelloAIXIAOJI HelloAIXIAOJI released this 31 Jul 05:27
· 732 commits to master since this release

[v0.5.8] - 2025-07-31

🚀 Parser Syntax Enhancement

🎯 Major Improvements

  • Lexer Enhancement

    • ✅ Added lexical support for arrow operator (->)
    • ✅ Improved multi-character operator recognition
  • Expression Parser Completion

    • ✅ Implemented pointer member access syntax (ptr->member)
    • ✅ Fixed postfix operator handling in parenthesized expressions
    • ✅ Supported member access chains in complex expressions ((*ptr).method())
    • ✅ Enhanced postfix operator processing loop
  • Type Parser Extension

    • ✅ Added fixed-size array pointer type (*[size]Type)
    • ✅ Added fixed-size pointer array type ([size]*Type)
    • ✅ Improved combined array and pointer type parsing

🔧 Technical Implementation

Lexer Fix

// Added arrow operator to multi-character operators
if ["==", "!=", ">=", "<=", "&&", "||", "::", "..", "++", "--",
    "+=", "-=", "*=", "/=", "%=", "=>", "->"].contains(&two_char_op.as_str())

Expression Parser Enhancement

// Support arrow operator after variables
} else if self.peek() == Some(&"->".to_string()) {
    self.consume(); // Consume "->"
    let member_name = self.consume().ok_or_else(|| "Expected member name".to_string())?;
    let pointer_expr = Expression::Variable(name);
    Ok(Expression::PointerMemberAccess(Box::new(pointer_expr), member_name))
}

// Support postfix operators after parentheses
loop {
    if self.peek() == Some(&".".to_string()) {
        // Method call or field access
    } else if self.peek() == Some(&"->".to_string()) {
        // Pointer member access
    } else if self.peek() == Some(&"[".to_string()) {
        // Array access
    } else {
        break;
    }
}

Type Parser Extension

// Array pointer type: *[5]int
if self.peek() == Some(&"[".to_string()) {
    self.consume(); // Consume "["
    let size_token = self.consume().ok_or_else(|| "Expected array size".to_string())?;
    let size = size_token.parse::<usize>()?;
    self.expect("]")?;
    let element_type = Box::new(self.parse_expression_type()?);
    Ok(Type::ArrayPointer(element_type, size))
}

// Pointer array type: [5]*int
if let Some(size) = array_size {
    Ok(Type::PointerArray(target_type, size))
}

🧪 Test Validation

  • Advanced pointer syntax test (advanced_pointer_syntax_test.cn) - Fully passed
  • Pointer member access test (working_pointer_member_test.cn) - Fully passed
  • Complex expression parsing - (*ptr).method() syntax works
  • Postfix operator chains - Support multi-level member access

📊 Parsing Capability Enhancement

Feature v0.5.7 Status v0.5.8 Status Improvement
Arrow operator -> ❌ Not supported ✅ Fully supported Complete lexing/parsing
Parenthesized expressions ❌ Partial support ✅ Full support Fixed postfix processing
Array pointer types ❌ Not supported ✅ Fully supported *[size]Type parsing
Pointer array types ❌ Not supported ✅ Fully supported [size]*Type parsing
Complex member access ❌ Parse error ✅ Fully supported (*ptr).method() works

🔧 Compile-time Type Checker

New Static Analysis Module

  • ✅ Complete compile-time type checking (src/analyzer/type_checker.rs)
  • ✅ Variable declaration type validation
  • ✅ Function parameter/return type checking
  • ✅ Expression type inference/validation
  • ✅ Binary operator compatibility checking

Type Checking Coverage

// Supported checks
- Variable declaration: x : int = "string" ❌
- Function arguments: add("hello", 42) ❌
- Return types: return "string" when expecting int ❌
- Expression inference: Auto type deduction
- Arithmetic operations: int + string ❌
- Comparison operations: Operand compatibility
- Logical operations: Bool operand required

Error Detection/Reporting

  • ✅ Detailed type error messages
  • ✅ Multiple error collection
  • ✅ Error recovery mechanism
  • ✅ Prevents type-unsafe code execution

Type Compatibility System

  • ✅ Auto type compatible with all types
  • ✅ Numeric implicit conversions (int → float, int → long)
  • ✅ Pointer type compatibility
  • ✅ Array element type consistency

🧪 Type Checking Validation

Success Cases

// type_check_test.cn - All passed
x : int = 42;           ✅ Type match
result : int = add(x, 10); ✅ Correct function call
length : int = name.length(); ✅ Correct method call

Error Detection

// type_error_test.cn - Detected 4 errors
x : int = "string";     ❌ Type mismatch
add("hello", 42);       ❌ Argument type error
return "string";        ❌ Return type error (expected int)

🎉 Real-world Impact

  • Parsing success rate: Full support for advanced pointer syntax
  • Type safety: Compile-time type checking catches errors early
  • Developer experience: Detailed error messages help debugging
  • Code quality: Prevents type-related runtime errors
  • Syntax coverage: All v0.5.6 pointer syntax now correctly parsed

🔄 Backward Compatibility

  • ✅ Existing code works unchanged
  • ✅ Type checking is optional
  • ✅ New syntax features are optional
  • ✅ Excellent parser/checker performance
  • ✅ More accurate error messages

[v0.5.8] - 2025-07-31

🚀 解析器语法支持完善

🎯 主要改进

  • 词法分析器增强

    • ✅ 添加箭头操作符(->)识别支持
    • ✅ 完善多字符操作符识别
  • 表达式解析器完善

    • ✅ 实现指针成员访问语法 (ptr->member)
    • ✅ 修复括号表达式后缀操作符处理
    • ✅ 支持复杂表达式成员访问链 ((*ptr).method())
    • ✅ 增强后缀操作符处理循环
  • 类型解析器扩展

    • ✅ 支持固定大小数组指针 (*[size]Type)
    • ✅ 支持固定大小指针数组 ([size]*Type)
    • ✅ 完善数组/指针组合类型解析

🔧 技术实现

词法分析器修复

// 添加箭头操作符到多字符操作符
if ["==", "!=", ">=", "<=", "&&", "||", "::", "..", "++", "--",
    "+=", "-=", "*=", "/=", "%=", "=>", "->"].contains(&two_char_op.as_str())

表达式解析器增强

// 支持变量后箭头操作符
} else if self.peek() == Some(&"->".to_string()) {
    self.consume(); // 消费 "->"
    let member_name = self.consume().ok_or_else(|| "期望成员名".to_string())?;
    let pointer_expr = Expression::Variable(name);
    Ok(Expression::PointerMemberAccess(Box::new(pointer_expr), member_name))
}

// 支持括号后后缀操作符
loop {
    if self.peek() == Some(&".".to_string()) {
        // 方法调用或字段访问
    } else if self.peek() == Some(&"->".to_string()) {
        // 指针成员访问
    } else if self.peek() == Some(&"[".to_string()) {
        // 数组访问
    } else {
        break;
    }
}

类型解析器扩展

// 数组指针类型: *[5]int
if self.peek() == Some(&"[".to_string()) {
    self.consume(); // 消费 "["
    let size_token = self.consume().ok_or_else(|| "期望数组大小".to_string())?;
    let size = size_token.parse::<usize>()?;
    self.expect("]")?;
    let element_type = Box::new(self.parse_expression_type()?);
    Ok(Type::ArrayPointer(element_type, size))
}

// 指针数组类型: [5]*int
if let Some(size) = array_size {
    Ok(Type::PointerArray(target_type, size))
}

🧪 测试验证

  • 高级指针语法测试 (advanced_pointer_syntax_test.cn) - 完全通过
  • 指针成员访问测试 (working_pointer_member_test.cn) - 完全通过
  • 复杂表达式解析 - (*ptr).method() 语法正常工作
  • 后缀操作符链 - 支持多级成员访问

📊 解析能力提升

语法特性 v0.5.7状态 v0.5.8状态 改进说明
箭头操作符 -> ❌ 不支持 ✅ 完全支持 完整词法/语法支持
括号表达式 ❌ 部分支持 ✅ 完全支持 修复后缀处理循环
数组指针类型 ❌ 不支持 ✅ 完全支持 *[size]Type 解析
指针数组类型 ❌ 不支持 ✅ 完全支持 [size]*Type 解析
复杂成员访问 ❌ 解析错误 ✅ 完全支持 (*ptr).method() 正常工作

🔧 编译时类型检查器

新增静态分析模块

  • ✅ 完整编译时类型检查 (src/analyzer/type_checker.rs)
  • ✅ 变量声明类型验证
  • ✅ 函数参数/返回值类型检查
  • ✅ 表达式类型推断/验证
  • ✅ 二元操作符兼容性检查

类型检查覆盖

// 支持检查
- 变量声明: x : int = "string" ❌
- 函数参数: add("hello", 42) ❌
- 返回值: return "string" 但期望 int ❌
- 表达式推断: 自动推断复杂类型
- 算术操作: int + string ❌
- 比较操作: 操作数兼容性
- 逻辑操作: 要求bool操作数

错误检测/报告

  • ✅ 详细类型错误信息
  • ✅ 多错误收集
  • ✅ 错误恢复机制
  • ✅ 阻止类型不安全代码执行

类型兼容系统

  • ✅ Auto类型兼容所有类型
  • ✅ 数值隐式转换 (int → float, int → long)
  • ✅ 指针类型兼容性
  • ✅ 数组元素类型一致性

🧪 类型检查验证

成功案例

// type_check_test.cn - 全部通过
x : int = 42;           ✅ 类型匹配
result : int = add(x, 10); ✅ 函数调用正确
length : int = name.length(); ✅ 方法调用正确

错误检测

// type_error_test.cn - 检测到4个错误
x : int = "string";     ❌ 类型不匹配
add("hello", 42);       ❌ 参数类型错误
return "string";        ❌ 返回类型错误 (期望int)

🎉 实际效果

  • 解析成功率: 完全支持高级指针语法
  • 类型安全性: 编译时类型检查提前发现错误
  • 开发体验: 详细错误信息帮助调试
  • 代码质量: 防止类型相关运行时错误
  • 语法覆盖: 所有v0.5.6指针语法正确解析

🔄 向后兼容

  • ✅ 现有代码无需修改
  • ✅ 类型检查为可选功能
  • ✅ 新语法特性可选使用
  • ✅ 解析器/检查器性能优秀
  • ✅ 错误信息更准确有用

Full Changelog: CodeNothingCommunity/CodeNothing@v0.5.7...v0.5.8