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:
@@ -1798,7 +1798,13 @@ impl Interpreter {
|
||||
if args.len() != 1 {
|
||||
return Err(err("toString requires 1 argument"));
|
||||
}
|
||||
Ok(EvalResult::Value(Value::String(format!("{}", args[0]))))
|
||||
// For strings, return the string itself (no quotes)
|
||||
// For other values, use Display formatting
|
||||
let result = match &args[0] {
|
||||
Value::String(s) => s.clone(),
|
||||
v => format!("{}", v),
|
||||
};
|
||||
Ok(EvalResult::Value(Value::String(result)))
|
||||
}
|
||||
|
||||
BuiltinFn::TypeOf => {
|
||||
|
||||
Reference in New Issue
Block a user