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

v0.4.6

Choose a tag to compare

@HelloAIXIAOJI HelloAIXIAOJI released this 25 Jul 21:08
· 867 commits to master since this release

[v0.4.6] - 2025-07-26

🚀 Major New Features

1. Default Parameter Values

Default parameter values allow function parameters to specify default values that are used when arguments are omitted during function calls.

Key Features:

  • Optional parameters: Makes function parameters optional
  • Expression support: Default values can be constants, variables, or complex expressions
  • Improved readability: Makes APIs clearer and reduces boilerplate code
  • Mixed usage: Can be used alongside required parameters

Syntax:

fn function_name(param : type = default_value, ...) : return_type {
    // Function body
}

Examples:

// Simple default parameters
fn greet(name : string = "World", greeting : string = "Hello") : string {
    return `${greeting}, ${name}!`;
};

// Using default values
greet();               // Returns "Hello, World!"
greet("Alice");        // Returns "Hello, Alice!"
greet("Bob", "Hi");    // Returns "Hi, Bob!"

// Expression as default value
fn multiply(a : int, b : int = a * 2) : int {
    return a * b;
};

multiply(5);    // Returns 50 (5 * 10)
multiply(3, 4); // Returns 12 (3 * 4)

2. Raw String Literals

Raw string literals preserve all characters exactly as written, without processing escape sequences.

Key Features:

  • No escaping needed: Special characters like \ remain literal
  • Simplifies complex strings: Ideal for regex, file paths, etc.
  • Preserves special characters: Sequences like \n remain as text
  • Improved readability: Reduces escape clutter in code

Syntax:

r"raw string content"

Examples:

// Regular string (requires escaping)
path1 : string = "C:\\Users\\Documents\\file.txt";

// Raw string (no escaping needed)
path2 : string = r"C:\Users\Documents\file.txt";

// Special characters comparison
normal : string = "Line1\nLine2\tTab";  // Special characters processed
raw : string = r"Line1\nLine2\tTab";    // Special characters preserved

// Regex without double escaping
regex : string = r"\d+\.\d+";

🔧 Technical Implementation

  • Default Parameters:

    • Added default_value field to AST Parameter struct
    • Enhanced function parser to handle = syntax
    • Implemented default value calculation during function calls
  • Raw Strings:

    • Added RawStringLiteral expression type to AST
    • Enhanced lexer to recognize r"..." tokens
    • Modified parser to handle raw string expressions
    • Updated evaluator to preserve raw string contents

🔄 Compatibility

  • Full backward compatibility: Existing code works unchanged
  • Optional usage: Features available but not required
  • String interoperability: Raw and regular strings work together seamlessly
  • Mixed parameters: Default parameters can be combined with required parameters

🔮 Future Plans

  1. Named Parameters:

    createPerson(age: 30, name: "Alice");
    
  2. Rest Parameters:

    fn sum(first: int, ...rest: array<int>) : int { ... }
    
  3. Multiline Raw Strings:

    r"""
    Multiline
    raw string
    """
    
  4. Raw String Interpolation:

    r`Regex: \d+ matches ${number}`
    

[v0.4.6] - 2025-07-26

🚀 重大新功能

1. 默认参数值 (Default Parameter Values)

允许函数参数指定默认值,调用时省略参数将使用默认值。

主要特性:

  • 参数可选性:使函数参数变为可选
  • 表达式支持:默认值可以是常量、变量或复杂表达式
  • 提高可读性:使API更清晰,减少样板代码
  • 混合使用:可与必需参数一起使用

语法:

fn 函数名(参数名 : 类型 = 默认值, ...) : 返回类型 {
    // 函数体
}

示例:

// 简单默认参数
fn greet(name : string = "世界", greeting : string = "你好") : string {
    return `${greeting}, ${name}!`;
};

// 使用默认值调用
greet();               // 返回 "你好, 世界!"
greet("张三");         // 返回 "你好, 张三!"
greet("李四", "您好"); // 返回 "您好, 李四!"

// 表达式作为默认值
fn multiply(a : int, b : int = a * 2) : int {
    return a * b;
};

multiply(5);    // 返回 50 (5 * 10)
multiply(3, 4); // 返回 12 (3 * 4)

2. 原始字符串字面量 (Raw String Literals)

原始字符串保留所有字符原样,不处理转义序列。

主要特性:

  • 无需转义:特殊字符(如\)保持原义
  • 简化复杂字符串:适合正则表达式、文件路径等场景
  • 保留特殊字符\n等序列保持文本形式
  • 提高可读性:减少代码中的转义字符

语法:

r"原始字符串内容"

示例:

// 常规字符串(需要转义)
path1 : string = "C:\\Users\\Documents\\file.txt";

// 原始字符串(无需转义)
path2 : string = r"C:\Users\Documents\file.txt";

// 特殊字符比较
normal : string = "第一行\n第二行\t制表符";  // 特殊字符被处理
raw : string = r"第一行\n第二行\t制表符";    // 特殊字符保持原样

// 正则表达式(无需双重转义)
regex : string = r"\d+\.\d+";

🔧 技术实现

  • 默认参数:

    • AST参数结构体新增default_value字段
    • 增强函数解析器处理=语法
    • 实现函数调用时的默认值计算
  • 原始字符串:

    • AST新增RawStringLiteral表达式类型
    • 增强词法分析器识别r"..."标记
    • 修改解析器处理原始字符串表达式
    • 更新求值器保留原始字符串内容

🔄 兼容性

  • 完全向后兼容:现有代码无需修改
  • 可选使用:功能可用但不强制使用
  • 字符串互操作:原始字符串与常规字符串可互操作
  • 参数混合使用:默认参数可与必需参数混合使用

🔮 未来计划

  1. 命名参数:

    createPerson(age: 30, name: "张三");
    
  2. 剩余参数:

    fn sum(first: int, ...rest: array<int>) : int { ... }
    
  3. 多行原始字符串:

    r"""
    多行
    原始字符串
    """
    
  4. 原始字符串插值:

    r`正则表达式: \d+ 匹配 ${number}`
    

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