ilo 26.5.0

ilo - the token-minimal programming language AI agents write
Documentation
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Cross-engine regression tests for `dur-parse` and `dur-fmt` — the duration
// parse/format builtins added in 0.12.x.
//
// `dur-parse s > R n t` — lenient parser for human duration strings.
//   Accepts: abbreviations (s/m/h/d/w), full names (singular + plural),
//   decimal quantities, mixed sequences.
// `dur-fmt n > t` — format seconds as human-readable; drops zero parts.
//
// Both are tree-bridge eligible: VM and Cranelift dispatch through the tree
// interpreter, so all engines share the same semantics. Tests fan across
// available engines to catch any future divergence.

use std::process::Command;

fn ilo() -> Command {
    Command::new(env!("CARGO_BIN_EXE_ilo"))
}

#[cfg(feature = "cranelift")]
const ENGINES: &[&str] = &["--vm", "--jit"];
#[cfg(not(feature = "cranelift"))]
const ENGINES: &[&str] = &["--vm"];

/// Run source with the VM engine and return trimmed stdout.
fn run_ok(engine: &str, src: &str, entry: &str) -> String {
    let out = ilo()
        .args([src, engine, entry])
        .output()
        .expect("failed to run ilo");
    assert!(
        out.status.success(),
        "ilo {engine} {src:?} {entry:?} failed: stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8_lossy(&out.stdout).trim().to_string()
}

fn run_err(engine: &str, src: &str, entry: &str) -> String {
    let out = ilo()
        .args([src, engine, entry])
        .output()
        .expect("failed to run ilo");
    assert!(
        !out.status.success(),
        "ilo {engine} {src:?} {entry:?} unexpectedly succeeded: stdout={}",
        String::from_utf8_lossy(&out.stdout)
    );
    String::from_utf8_lossy(&out.stderr).to_string()
}

fn parse_num(s: &str) -> f64 {
    s.lines()
        .next()
        .unwrap_or("")
        .trim()
        .parse::<f64>()
        .unwrap_or_else(|_| panic!("expected number, got: {s:?}"))
}

// ── dur-parse: happy-path parsing ────────────────────────────────────────────

#[test]
fn dur_parse_hours_minutes() {
    let src = "f>R n t;dur-parse \"3h 30m\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 12600.0, "engine={e}");
    }
}

#[test]
fn dur_parse_week_and_days() {
    let src = "f>R n t;dur-parse \"1 week 2 days\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 777600.0, "engine={e}");
    }
}

#[test]
fn dur_parse_decimal_hours() {
    let src = "f>R n t;dur-parse \"1.5 hours\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 5400.0, "engine={e}");
    }
}

#[test]
fn dur_parse_bare_seconds_abbrev() {
    let src = "f>R n t;dur-parse \"90s\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 90.0, "engine={e}");
    }
}

#[test]
fn dur_parse_no_space_between_number_and_unit() {
    let src = "f>R n t;dur-parse \"4h32m\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 16320.0, "engine={e}");
    }
}

#[test]
fn dur_parse_full_unit_names() {
    // "3 weeks 2 days 5 hours" — the exact example from the P3 spec
    let src = "f>R n t;dur-parse \"3 weeks 2 days 5 hours\"";
    for e in ENGINES {
        let secs = parse_num(&run_ok(e, src, "f"));
        // 3w = 1814400; 2d = 172800; 5h = 18000 => 2005200
        assert_eq!(secs, 2005200.0, "engine={e}");
    }
}

#[test]
fn dur_parse_singular_forms() {
    let src = "f>R n t;dur-parse \"1 week 1 day 1 hour 1 minute 1 second\"";
    for e in ENGINES {
        let secs = parse_num(&run_ok(e, src, "f"));
        // 604800 + 86400 + 3600 + 60 + 1 = 694861
        assert_eq!(secs, 694861.0, "engine={e}");
    }
}

#[test]
fn dur_parse_abbreviation_d() {
    let src = "f>R n t;dur-parse \"1d\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 86400.0, "engine={e}");
    }
}

#[test]
fn dur_parse_abbreviation_w() {
    let src = "f>R n t;dur-parse \"2w\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 1209600.0, "engine={e}");
    }
}

#[test]
fn dur_parse_hr_hrs_aliases() {
    let src = "f>R n t;dur-parse \"4 hrs\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 14400.0, "engine={e}");
    }
}

#[test]
fn dur_parse_min_alias() {
    let src = "f>R n t;dur-parse \"30 mins\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 1800.0, "engine={e}");
    }
}

#[test]
fn dur_parse_sec_alias() {
    let src = "f>R n t;dur-parse \"45 sec\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 45.0, "engine={e}");
    }
}

#[test]
fn dur_parse_zero() {
    let src = "f>R n t;dur-parse \"0h\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 0.0, "engine={e}");
    }
}

// ── dur-parse: error cases ────────────────────────────────────────────────────

#[test]
fn dur_parse_empty_string_errors() {
    let src = "f>R n t;dur-parse \"\"";
    for e in ENGINES {
        // Should succeed at runtime (returns Ok/Err Result), but result is Err.
        // We run as `dur-parse! ""` to force error propagation.
        let out = ilo().args([src, e, "f"]).output().expect("ilo");
        // The program itself succeeds (no crash), output should start with "^"
        // (Err indicator) when printed by the engine.
        let stdout = String::from_utf8_lossy(&out.stdout).to_string();
        // The result is Err; printed as "^..." without auto-unwrap.
        // We check that it's not an Ok number.
        assert!(
            !stdout
                .trim()
                .chars()
                .next()
                .map(|c| c.is_ascii_digit())
                .unwrap_or(false),
            "engine={e}: expected Err result, got numeric stdout: {stdout}"
        );
    }
}

#[test]
fn dur_parse_no_unit_errors() {
    // Input has a number but no recognisable unit — should Err.
    let src = "f>t;r=dur-parse \"42\";?r{~_:\"ok\";^_:\"err\"}";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "err", "engine={e}");
    }
}

#[test]
fn dur_parse_bad_unit_errors() {
    let src = "f>t;r=dur-parse \"3 parsecs\";?r{~_:\"ok\";^_:\"err\"}";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "err", "engine={e}");
    }
}

// ── dur-fmt: happy-path formatting ───────────────────────────────────────────

#[test]
fn dur_fmt_hours_and_minutes() {
    let src = "f>t;dur-fmt 9720";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "2h 42m", "engine={e}");
    }
}

#[test]
fn dur_fmt_exactly_one_day() {
    let src = "f>t;dur-fmt 86400";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "1 day", "engine={e}");
    }
}

#[test]
fn dur_fmt_exactly_one_week() {
    let src = "f>t;dur-fmt 604800";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "1 week", "engine={e}");
    }
}

#[test]
fn dur_fmt_minutes_and_seconds() {
    let src = "f>t;dur-fmt 90";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "1m 30s", "engine={e}");
    }
}

#[test]
fn dur_fmt_bare_seconds() {
    let src = "f>t;dur-fmt 45";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "45s", "engine={e}");
    }
}

#[test]
fn dur_fmt_zero() {
    let src = "f>t;dur-fmt 0";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "0s", "engine={e}");
    }
}

#[test]
fn dur_fmt_week_and_days() {
    // 1 week + 2 days = 604800 + 172800 = 777600
    let src = "f>t;dur-fmt 777600";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "1 week 2 days", "engine={e}");
    }
}

#[test]
fn dur_fmt_complex() {
    // 2 days + 3 hours = 172800 + 10800 = 183600
    let src = "f>t;dur-fmt 183600";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "2 days 3h", "engine={e}");
    }
}

#[test]
fn dur_fmt_hours_only() {
    let src = "f>t;dur-fmt 3600";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "1h", "engine={e}");
    }
}

#[test]
fn dur_fmt_plural_hours() {
    let src = "f>t;dur-fmt 7200";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "2h", "engine={e}");
    }
}

#[test]
fn dur_fmt_plural_weeks() {
    // 3 weeks
    let src = "f>t;dur-fmt 1814400";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "3 weeks", "engine={e}");
    }
}

// ── round-trip: parse then format ────────────────────────────────────────────

#[test]
fn dur_round_trip_3h_30m() {
    // "3h 30m" -> 12600s -> "3h 30m"
    let src = "f>R t t;n=dur-parse! \"3h 30m\";~dur-fmt n";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "3h 30m", "engine={e}");
    }
}

#[test]
fn dur_round_trip_days_and_hours() {
    // "2 days 3 hours" -> 183600s -> "2 days 3h"
    // Note: full name "hours" normalises to abbreviated "h" in output.
    let src = "f>R t t;n=dur-parse! \"2 days 3 hours\";~dur-fmt n";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "2 days 3h", "engine={e}");
    }
}

#[test]
fn dur_round_trip_1_week() {
    let src = "f>R t t;n=dur-parse! \"1 week\";~dur-fmt n";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "1 week", "engine={e}");
    }
}

// ── bad-type errors ───────────────────────────────────────────────────────────
//
// Note: the JIT engine silently returns nil for tree-bridge builtins that
// receive wrong-type args (same pattern as `dirname 42` on JIT). This is a
// known JIT limitation for tree-bridge builtins; only the VM engine is tested
// for wrong-type error propagation here. The VM gets the runtime error through
// the tree interpreter dispatch path. This matches the test pattern used by
// the dirname/basename/pathjoin tests.

#[test]
fn dur_parse_wrong_type_errors() {
    // dur-parse expects text; passing a number should error on VM.
    let src = "f>R n t;dur-parse 42";
    let stderr = run_err("--vm", src, "f");
    assert!(
        stderr.contains("dur-parse"),
        "VM: expected dur-parse error, got: {stderr}"
    );
}

#[test]
fn dur_fmt_wrong_type_errors() {
    // dur-fmt expects a number; passing text should error on VM.
    let src = "f>t;dur-fmt \"hello\"";
    let stderr = run_err("--vm", src, "f");
    assert!(
        stderr.contains("dur-fmt"),
        "VM: expected dur-fmt error, got: {stderr}"
    );
}

// ── Negative + fractional round-trip (cross-engine) ─────────────────────────

#[test]
fn dur_fmt_negative_emits_single_leading_minus() {
    let src = "f>t;dur-fmt -90";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "-1m 30s", "engine={e}");
    }
}

#[test]
fn dur_parse_negative_is_sticky_across_tokens() {
    // Sticky sign: leading `-` applies to every following token until an
    // explicit `+` resets it. So `-1m 30s` = -90 (not -30), which is what
    // makes the dur-fmt -> dur-parse round-trip symmetric for negatives.
    let src = "f>R n t;dur-parse \"-1m 30s\"";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), -90.0, "engine={e}");
    }
}

#[test]
fn dur_round_trip_negative_multi_part() {
    // End-to-end: fmt(-5400) -> "-1h 30m" -> parse -> -5400.
    let src = "f>R n t;dur-parse (dur-fmt -5400)";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), -5400.0, "engine={e}");
    }
}

#[test]
fn dur_fmt_preserves_fractional_seconds() {
    // 90.5 -> "1m 30.5s" — the previous implementation truncated to "1m 30s".
    let src = "f>t;dur-fmt 90.5";
    for e in ENGINES {
        assert_eq!(run_ok(e, src, "f"), "1m 30.5s", "engine={e}");
    }
}

#[test]
fn dur_round_trip_fractional_seconds() {
    // End-to-end: fmt(90.5) -> "1m 30.5s" -> parse -> 90.5.
    let src = "f>R n t;dur-parse (dur-fmt 90.5)";
    for e in ENGINES {
        assert_eq!(parse_num(&run_ok(e, src, "f")), 90.5, "engine={e}");
    }
}

#[test]
fn dur_parse_months_rejected() {
    // Months are deliberately unsupported (variable length). All these
    // forms should error rather than silently parse.
    for src in [
        "f>R n t;dur-parse \"3mo\"",
        "f>R n t;dur-parse \"3 months\"",
        "f>R n t;dur-parse \"3 month\"",
    ] {
        for e in ENGINES {
            let stderr = run_err(e, src, "f");
            assert!(
                stderr.contains("dur-parse") || stderr.contains("no recognised unit"),
                "engine={e} src={src}: expected dur-parse error, got: {stderr}"
            );
        }
    }
}