feat: implement string interpolation

Add support for string interpolation with {expr} syntax:
  "Hello, {name}!" becomes "Hello, " + toString(name) + "!"

Lexer changes:
- Add StringPart enum (Literal/Expr) and InterpolatedString token
- Detect {expr} in strings and capture expression text
- Support escaped braces with \{ and \}

Parser changes:
- Add desugar_interpolated_string() to convert to concatenation
- Automatically wrap expressions in toString() calls

Interpreter changes:
- Fix toString() to not add quotes around strings

Tests added:
- 4 lexer tests for interpolation tokenization
- 4 integration tests for full interpolation pipeline

Add examples/interpolation.lux demonstrating:
- Variable interpolation
- Number interpolation (auto toString)
- Expression interpolation ({a + b})
- Escaped braces

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 09:30:37 -05:00
parent 3734a17e5c
commit f670bd2659
5 changed files with 305 additions and 19 deletions

View File

@@ -904,6 +904,41 @@ c")"#;
assert_eq!(eval(source).unwrap(), r#"["a", "b", "c"]"#);
}
// String interpolation tests
#[test]
fn test_string_interpolation_simple() {
let source = r#"
let name = "World"
let x = "Hello, {name}!"
"#;
assert_eq!(eval(source).unwrap(), r#""Hello, World!""#);
}
#[test]
fn test_string_interpolation_numbers() {
let source = r#"
let n = 42
let x = "The answer is {n}"
"#;
assert_eq!(eval(source).unwrap(), r#""The answer is 42""#);
}
#[test]
fn test_string_interpolation_expressions() {
let source = r#"
let a = 2
let b = 3
let x = "{a} + {b} = {a + b}"
"#;
assert_eq!(eval(source).unwrap(), r#""2 + 3 = 5""#);
}
#[test]
fn test_string_interpolation_escaped_braces() {
let source = r#"let x = "literal \{braces\}""#;
assert_eq!(eval(source).unwrap(), r#""literal {braces}""#);
}
// Option tests
#[test]
fn test_option_constructors() {