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

v0.4.5

Choose a tag to compare

@HelloAIXIAOJI HelloAIXIAOJI released this 25 Jul 19:57
· 881 commits to master since this release

CodeNothing String Interpolation Feature - v0.4.5 (2025-07-26)

🚀 Major New Feature: String Interpolation

String interpolation is a powerful feature that allows developers to embed expressions within strings, making string construction more concise, intuitive, and readable.

✨ New Features

1. Template String Syntax

  • Use backticks (`) to define template strings
  • Expression embedding: Use ${...} syntax to embed expressions within strings
  • Multiline strings: Support line breaks and preserve formatting

2. Interpolation Expression Support

  • Variable interpolation: ${name}
  • Expression interpolation: ${a + b}
  • Function call interpolation: ${getTime()}
  • Nested string interpolation: ${inner ${value}}
  • Ternary operator interpolation: ${condition ? "true" : "false"}

🔧 Technical Implementation

AST Extension

// New expression type
Expression::StringInterpolation(Vec<StringInterpolationSegment>)

// String interpolation segment enum
enum StringInterpolationSegment {
    Text(String),                 // Regular text segment
    Expression(Box<Expression>),  // Expression segment
}

Lexer Enhancements

  • Recognize backtick-delimited strings
  • Separate text segments and expression segments
  • Special token processing (INTERP_START, INTERP_TEXT, INTERP_EXPR, INTERP_END)

Parser Enhancements

  • Parse string interpolation expressions
  • Handle nested expressions
  • Expression evaluation

Executor Enhancements

  • Concatenate text segments
  • Evaluate expressions and convert values to strings
  • Handle string conversion for different value types

📝 Syntax Examples

// 1. Simple variable interpolation
name : string = "World";
greeting : string = `Hello, ${name}!`;  // Result: "Hello, World!"

// 2. Expression interpolation
a : int = 5;
b : int = 3;
result : string = `${a} + ${b} = ${a + b}`;  // Result: "5 + 3 = 8"

// 3. Function call interpolation
message : string = `Current time: ${getTimeString()}`;

// 4. Nested interpolation
c : int = 10;
nested : string = `Nested: ${`Inner value: ${c * 2}`}`;  // Result: "Nested: Inner value: 20"

// 5. Conditional interpolation
score : int = 85;
grade : string = `Grade: ${score >= 90 ? "A" : score >= 80 ? "B" : score >= 60 ? "C" : "F"}`;
// Result: "Grade: B"

// 6. Multiline interpolation
multiline : string = `Multiline string
Line 1: ${a}
Line 2: ${b}`;

🔄 Compatibility

  • Fully backward compatible
  • Regular double-quoted strings still available
  • String concatenation (+) operator remains valid

📚 Use Cases

  • Dynamic message construction
  • Template generation
  • Complex string formatting
  • Multi-language support
  • HTML/XML generation

🔮 Future Expansion Plans

1. Formatting Control

Consider adding extended support for string formatting, e.g.:

`${value:fmt}` // Where fmt is a format specifier
`${number:.2f}` // Preserve 2 decimal places

2. Template Engine

Build a more complete template engine based on string interpolation, supporting control structures like conditionals and loops.

3. Raw Strings

Add support for raw strings without special character escaping:

r`Raw string, no escaping of \n etc.`

4. Tagged Templates

Support JavaScript-like tagged template functionality:

html`<div>${content}</div>`

This version brings modern string processing capabilities to CodeNothing, making string operations more concise and expressive while maintaining the language's simplicity and ease of use.

CodeNothing 字符串插值功能 - v0.4.5(2025-07-26)

🚀 重大新功能:字符串插值

字符串插值是一种强大的特性,允许开发者在字符串中嵌入表达式,使字符串构建更加简洁、直观和可读。

✨ 新增功能

1. 模板字符串语法

  • 使用反引号 (`) 定义模板字符串
  • 表达式嵌入:使用 ${...} 语法在字符串中嵌入表达式
  • 多行字符串:支持换行,保留格式

2. 插值表达式支持

  • 变量插值${name}
  • 表达式插值${a + b}
  • 函数调用插值${getTime()}
  • 嵌套字符串插值${内部${value}}
  • 三元运算符插值${condition ? "真" : "假"}

🔧 技术实现

AST扩展

// 新增表达式类型
Expression::StringInterpolation(Vec<StringInterpolationSegment>)

// 字符串插值片段枚举
enum StringInterpolationSegment {
    Text(String),                 // 普通文本片段
    Expression(Box<Expression>),  // 表达式片段
}

词法分析器增强

  • 识别反引号字符串
  • 分离文本片段和表达式片段
  • 特殊标记处理 (INTERP_START, INTERP_TEXT, INTERP_EXPR, INTERP_END)

解析器增强

  • 解析字符串插值表达式
  • 处理嵌套表达式
  • 表达式求值

执行器增强

  • 连接文本片段
  • 计算表达式值并转换为字符串
  • 处理不同类型值的字符串转换

📝 语法示例

// 1. 简单变量插值
name : string = "世界";
greeting : string = `你好,${name}!`;  // 结果: "你好,世界!"

// 2. 表达式插值
a : int = 5;
b : int = 3;
result : string = `${a} + ${b} = ${a + b}`;  // 结果: "5 + 3 = 8"

// 3. 函数调用插值
message : string = `当前时间是:${getTimeString()}`;

// 4. 嵌套插值
c : int = 10;
nested : string = `嵌套插值:${`内部值: ${c * 2}`}`;  // 结果: "嵌套插值:内部值: 20"

// 5. 条件插值
score : int = 85;
grade : string = `等级: ${score >= 90 ? "优秀" : score >= 80 ? "良好" : score >= 60 ? "及格" : "不及格"}`;
// 结果: "等级: 良好"

// 6. 多行插值
multiline : string = `多行字符串
第一行:${a}
第二行:${b}`;

🔄 兼容性

  • 完全向后兼容
  • 常规双引号字符串仍然可用
  • 字符串连接 (+) 操作符仍然有效

📚 使用场景

  • 动态消息构造
  • 模板生成
  • 复杂字符串格式化
  • 多语言支持
  • HTML/XML生成

🔮 未来扩展计划

1. 格式化控制

考虑添加对字符串格式化的扩展支持,例如:

`${value:fmt}` // 其中fmt是格式说明符
`${number:.2f}` // 保留2位小数

2. 模板引擎

基于字符串插值构建更完整的模板引擎,支持条件、循环等控制结构。

3. 原始字符串

添加对不转义特殊字符的原始字符串 (raw string) 支持:

r`原始字符串,不对\n等特殊字符进行转义`

4. 标记模板

支持类似JavaScript标记模板的功能:

html`<div>${content}</div>`

这个版本为CodeNothing带来了现代字符串处理的强大功能,使字符串操作更加简洁、表达力更强,同时保持了语言的简单性和易用性。

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