The readable way to build strings, as you noted `{a} {b}` beats `a + " " + b`.
A backtick string embeds any expression in `{…}` and desugars to the Phase-7c
`+` chain, wrapping each hole in `str(...)`:
`hello {name}, n={count + 1}`
== "hello " + name + ", n=" + str(count + 1)
- Lexer: a backtick captures its content raw as TK_INTERP.
- Parser: parse_interp splits literal runs from `{…}` holes (brace-depth aware,
`{{`/`}}` escape to literal braces), re-lexes each hole as a full expression
(save/restore toks/pi like an import), and folds it all into E_BIN(+) nodes —
so no new AST or runtime beyond the existing concat.
- str(x): a string passes through; int/bool/fixed convert via a small emitted
@fn_int_str prelude (digits from the end of a buffer, '-' for negatives),
emitted once into any program that uses it.
examples/strings.ludic gains interpolation cases (now prints 1..7); the smoke
covers it. Grammar + ludic_syntax.h tokenize backtick strings (holes highlighted
as embedded code). LANGUAGE.md documents it as the preferred form.
Reseeded (22530 lines); C-free fixpoint holds; goldens identical; 18/18; vocab +
doc-fences clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
26 lines
910 B
Text
26 lines
910 B
Text
# strings.ludic — strings are values: compare with ==/!=, join with +, or (best)
|
|
# interpolate with `backticks {expr}`. Running it prints: 1 2 3 4 5 6 7
|
|
program Strings {
|
|
entry {
|
|
let greeting = "hello"
|
|
if greeting == "hello" { print_int(1) } # content comparison
|
|
if greeting != "world" { print_int(2) }
|
|
|
|
let who = "Ludic"
|
|
let msg = greeting + ", " + who + "!" # concatenation chains
|
|
if msg == "hello, Ludic!" { print_int(3) }
|
|
|
|
# build up a string in a loop
|
|
var line = ""
|
|
var i = 0
|
|
while i < 3 { line = line + "ab"; i = i + 1 }
|
|
if line == "ababab" { print_int(4) }
|
|
|
|
if (greeting + who) != greeting { print_int(5) }
|
|
|
|
# interpolation: `{expr}` embeds any expression (numbers become text)
|
|
let n = 42
|
|
if `{greeting}, {who}!` == "hello, Ludic!" { print_int(6) }
|
|
if `n={n}, next={n + 1}` == "n=42, next=43" { print_int(7) }
|
|
}
|
|
}
|