Commit 54c3714
f-strings: f\"x={n}\" interpolation as parser sugar over concat_many
Second piece of the Python-ergonomics catch-up. Adds the f-string
syntax that Python users reach for first when formatting output.
Lexer:
- FStringPart enum with Literal(String) / Expr(String) variants
- Token::FString(Vec<FStringPart>) carries the lexed template
- read_fstring() handles brace-balanced expression segments + the
Python-compatible `{{` / `}}` escapes for literal braces
- Lexer dispatch triggers on `f\"` or `f'` (also `F\"`/`F'`); bare `f`
identifiers parse normally
Parser:
- Token::FString in primary-expression position re-parses each Expr
segment via a sub-Parser (Parser::new + parse_expression) and
emits Expression::Call { name: \"concat_many\", args }
- Literal segments become Expression::String; expression segments
become whatever sub-parsing produces (Number, Call, Add, etc.)
- concat_many already handles mixed int/float/string args via
to_string internally — f\"x={n}\" works for any value type
without an explicit to_string call
Tests (examples/tests/test_fstrings.omc — 10 tests, all pass):
- Simple int / float / string interpolation
- Multiple {} segments in one string
- Arithmetic inside braces
- Function call inside braces (abs(), nth_fibonacci())
- {{ and }} escapes for literal braces
- No-interp f-string equivalent to regular string
- Empty f-string returns empty string
What f-strings DON'T yet support (Python parity work):
- Format-spec syntax: f\"{x:.2f}\", f\"{n:>10}\". OMC has explicit
formatters (str_pad_left, str_pad_right, etc.) that callers can use
inside braces — f\"{str_pad_left(to_string(n), 10, \\\"0\\\")}\" works
today and is composable. Format-spec sugar can come later as
another parser-side translation.
- Multi-line f-strings via \"\"\"...\"\"\". Triple-quoted strings exist;
f-prefix on them isn't wired through the lexer yet.
- Self-documenting expressions: f\"{n=}\" (Python 3.8+). Easy
parser-level rewrite if/when users ask for it.
Regression: 8 exception tests + 57 substrate + 70 builtins + 18
harmonic libs + 16 heal + 10 new f-string = 179 OMC tests, all pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent 85e2888 commit 54c3714
2 files changed
Lines changed: 192 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
6 | 16 | | |
7 | 17 | | |
8 | 18 | | |
| |||
31 | 41 | | |
32 | 42 | | |
33 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
34 | 48 | | |
35 | 49 | | |
36 | 50 | | |
| |||
220 | 234 | | |
221 | 235 | | |
222 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
223 | 318 | | |
224 | 319 | | |
225 | 320 | | |
| |||
342 | 437 | | |
343 | 438 | | |
344 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
345 | 449 | | |
346 | 450 | | |
347 | 451 | | |
| |||
1536 | 1640 | | |
1537 | 1641 | | |
1538 | 1642 | | |
| 1643 | + | |
| 1644 | + | |
| 1645 | + | |
| 1646 | + | |
| 1647 | + | |
| 1648 | + | |
| 1649 | + | |
| 1650 | + | |
| 1651 | + | |
| 1652 | + | |
| 1653 | + | |
| 1654 | + | |
| 1655 | + | |
| 1656 | + | |
| 1657 | + | |
| 1658 | + | |
| 1659 | + | |
| 1660 | + | |
| 1661 | + | |
| 1662 | + | |
| 1663 | + | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
1539 | 1672 | | |
1540 | 1673 | | |
1541 | 1674 | | |
| |||
0 commit comments