strykelang 0.9.1

A highly parallel Perl 5 interpreter written in Rust
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use crate::common::*;
use stryke::error::ErrorKind;

#[test]
fn parallel_map() {
    let result = eval("my @a = pmap { $_ * 2 } (1,2,3,4,5); scalar @a");
    assert_eq!(result.to_int(), 5);
}

#[test]
fn parallel_map_preserves_input_order_in_results() {
    assert_eq!(
        eval_string(r#"(1,2,3,4) |> pmap { $_ * 2 } |> join ','"#),
        "2,4,6,8"
    );
}

#[test]
fn parallel_map_progress_flag_runs() {
    assert_eq!(
        eval_string(r#"(1, 2, 3, 4) |> pmap { $_ * 2 }, progress => 0 |> join ','"#),
        "2,4,6,8"
    );
}

/// Failed `pmap` elements stringify as empty between commas; order matches the input list.
#[test]
fn parallel_map_mixed_undef_slots_preserve_positions() {
    assert_eq!(
        eval_string(r#"(1, 2, 3) |> pmap { $_ == 1 ? 1/0 : $_ * 2 } |> join ','"#),
        ",4,6",
    );
}

#[test]
fn pmap_chunked_preserves_input_order() {
    assert_eq!(
        eval_string(r#"(1, 2, 3, 4) |> pmap_chunked 2 { $_ * 2 } |> join ','"#),
        "2,4,6,8",
    );
}

/// Chunk size larger than the list still processes every element in order.
#[test]
fn pmap_chunked_large_chunk_small_list() {
    assert_eq!(
        eval_string(r#"(1, 2) |> pmap_chunked 99 { $_ * 2 } |> join ','"#),
        "2,4",
    );
}

/// Matched elements stay in original list order (not sorted by value).
#[test]
fn parallel_grep_preserves_input_order() {
    assert_eq!(
        eval_string(r#"(3, 1, 4) |> pgrep { $_ > 1 } |> join ','"#),
        "3,4",
    );
}

#[test]
fn parallel_grep() {
    let result = eval("my @a = pgrep { $_ % 2 == 0 } (1,2,3,4,5,6); scalar @a");
    assert_eq!(result.to_int(), 3);
}

#[test]
fn parallel_grep_progress_flag_runs() {
    assert_eq!(
        eval_string(r#"(1, 2, 3, 4) |> pgrep { $_ % 2 == 0 }, progress => 0 |> join ','"#),
        "2,4",
    );
}

#[test]
fn parallel_map_empty_list() {
    assert_eq!(eval_int(r#"scalar pmap { $_ } ()"#), 0);
}

#[test]
fn parallel_grep_empty_list() {
    assert_eq!(eval_int(r#"scalar pgrep { 1 } ()"#), 0);
}

#[test]
fn parallel_sort_empty_list() {
    assert_eq!(eval_int(r#"scalar psort ()"#), 0);
}

#[test]
fn pmap_chunked_empty_list() {
    assert_eq!(eval_int(r#"scalar pmap_chunked 4 { $_ } ()"#), 0);
}

/// `pmap` keeps result length; block failures become `undef` per element (VM `Err` → `UNDEF`).
#[test]
fn parallel_map_keeps_length_when_block_errors() {
    assert_eq!(eval_int(r#"scalar pmap { 1/0 } (1, 2, 3)"#), 3);
}

/// `pgrep` treats block failure as false — the input item is not kept.
#[test]
fn parallel_grep_drops_items_when_block_errors() {
    assert_eq!(eval_int(r#"scalar pgrep { 1/0 } (1, 2, 3)"#), 0);
}

/// Only the item whose predicate errors is excluded; others still match.
#[test]
fn parallel_grep_mixed_errors_and_successes() {
    assert_eq!(
        eval_int(r#"my @a = pgrep { $_ == 2 ? 1/0 : $_ > 0 } (1, 2, 3); scalar @a"#),
        2,
    );
}

#[test]
fn parallel_map_single_element() {
    assert_eq!(eval_string(r#"(21) |> pmap { $_ * 2 } |> join ','"#), "42");
}

/// Captured non-`mysync` lexicals cannot be assigned from parallel workers (`Scope::parallel_guard`).
/// Use `pfor` here: `pmap` / `pgrep` swallow per-element block failures (`undef` / drop), so guard
/// violations would not surface as the program result.
#[test]
fn parallel_block_rejects_captured_lexical_assignment() {
    assert_eq!(
        eval_err_kind(r#"my $x = 0; pfor { $x = 1 } (1); 1"#),
        ErrorKind::Runtime,
    );
}

#[test]
fn parallel_block_allows_mysync_scalar_mutation() {
    assert_eq!(eval_int(r#"mysync $c = 0; pmap { $c++ } (1, 2, 3); $c"#), 3);
}

/// `mysync` compound assignment in `pmap` — full RMW under the atomic lock, no parallel-guard error.
#[test]
fn parallel_mysync_compound_assign_in_pmap() {
    assert_eq!(
        eval_int(r#"mysync $c = 0; pmap { $c += $_ } (1, 2, 3); $c"#),
        6
    );
}

/// `mysync` scalar may be updated in `pgrep` workers (predicate runs once per element).
#[test]
fn parallel_mysync_mutation_in_pgrep() {
    assert_eq!(
        eval_int(r#"mysync $n = 0; pgrep { $n++; $_ > 1 } (1, 2, 3); $n"#),
        3
    );
}

#[test]
fn parallel_mysync_increment_in_pmap_chunked() {
    assert_eq!(
        eval_int(r#"mysync $c = 0; pmap_chunked 2 { $c++ } (1, 2, 3); $c"#),
        3
    );
}

/// Shared `mysync` array — `push` from `pmap` workers is serialized via the atomic array path.
#[test]
fn parallel_mysync_array_push_in_pmap() {
    assert_eq!(
        eval_int(r#"mysync @a; pmap { push @a, $_ } (1, 2, 3); scalar @a"#),
        3
    );
}

/// Shared `mysync` hash — element updates from `pfor` workers.
#[test]
fn parallel_mysync_hash_buckets_in_pfor() {
    assert_eq!(
        eval_int(
            r#"mysync %bucket; pfor { $bucket{$_ % 2} += 1 } (0..9); $bucket{0} + $bucket{1}"#
        ),
        10
    );
}

/// `pfor` with `mysync` mutation succeeds (same guard rules as `pmap`, but `pfor` propagates errors).
#[test]
fn parallel_pfor_mysync_increment_no_error() {
    assert_eq!(eval_int(r#"mysync $c = 0; pfor { $c++ } (1, 2, 3); $c"#), 3);
}

#[test]
fn parallel_grep_single_element() {
    assert_eq!(eval_int(r#"scalar pgrep { $_ > 0 } (7)"#), 1);
}

#[test]
fn parallel_sort_single_element_unchanged() {
    assert_eq!(
        eval_string(r#"(99) |> psort { $a <=> $b } |> join ','"#),
        "99"
    );
}

#[test]
fn parallel_sort() {
    assert_eq!(
        eval_string(r#"(5,3,1,4,2) |> psort { $a <=> $b } |> join ','"#),
        "1,2,3,4,5"
    );
}

#[test]
fn parallel_sort_default_string_order() {
    assert_eq!(
        eval_string(r#"("c","a","b") |> psort |> join ','"#),
        "a,b,c"
    );
}

#[test]
fn parallel_for_runs() {
    assert_eq!(eval_int("pfor { $_ } (1,2,3); 99"), 99);
}

#[test]
fn par_lines_invokes_block_per_line_with_mysync_count() {
    let dir = std::env::temp_dir().join(format!("stryke_par_lines_{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let p = dir.join("lines.txt");
    std::fs::write(&p, "a,b\n2,3").unwrap();
    let path = p.to_str().unwrap();
    let code = format!(r#"mysync $n = 0; par_lines "{path}", fn {{ $n++ }}; $n"#);
    assert_eq!(eval_int(&code), 2);
    std::fs::remove_dir_all(&dir).ok();
}

/// Bare block + `if /re/` must not trip the parallel guard on regex capture scalars (`$&`, …).
#[test]
fn par_lines_bare_block_say_if_regex() {
    let dir = std::env::temp_dir().join(format!("stryke_par_lines_re_{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let p = dir.join("big.log");
    std::fs::write(&p, "ok\nERR x\n").unwrap();
    let path = p.to_str().unwrap();
    let code = format!(r#"mysync $o = ""; par_lines "{path}", {{ $o .= $_ if /ERR/ }}; $o"#);
    assert_eq!(eval_string(&code), "ERR x");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn par_walk_visits_files_and_dirs_with_mysync_count() {
    let dir = std::env::temp_dir().join(format!("stryke_par_walk_{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("a")).unwrap();
    std::fs::write(dir.join("a/x.txt"), "1").unwrap();
    std::fs::write(dir.join("root.txt"), "2").unwrap();
    let path = dir.to_str().unwrap();
    let code = format!(r#"mysync $n = 0; par_walk "{path}", fn {{ $n++ }}; $n"#);
    // root dir, root.txt, subdir a, a/x.txt = 4 paths
    assert_eq!(eval_int(&code), 4);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn par_sed_rewrites_multiple_files_in_parallel() {
    let dir = std::env::temp_dir().join(format!("stryke_par_sed_{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let f1 = dir.join("one.txt");
    let f2 = dir.join("two.txt");
    std::fs::write(&f1, "foo bar").unwrap();
    std::fs::write(&f2, "foo baz").unwrap();
    let p1 = f1.to_str().unwrap();
    let p2 = f2.to_str().unwrap();
    let code = format!(
        r#"my $k = par_sed("foo", "ZZ", "{p1}", "{p2}"); $k . ":" . slurp("{p1}") . slurp("{p2}")"#
    );
    let out = eval_string(&code);
    assert_eq!(out, "2:ZZ barZZ baz");
    std::fs::remove_dir_all(&dir).ok();
}

/// `fan { }` iterates `$_` from `0` to `rayon::current_num_threads() - 1` (same pool as `stryke -j`).
#[test]
fn fan_default_count_matches_rayon_thread_pool() {
    let n = rayon::current_num_threads();
    let expected = (n * n.saturating_sub(1) / 2) as i64;
    assert_eq!(
        eval_int(r#"fn prto { $s += $_ } mysync $s = 0; fan { prto }; $s"#),
        expected,
    );
}

#[test]
fn fan_zero_iterations_skips_block() {
    assert_eq!(eval_int(r#"fan 0 { die "should not run" }; 1"#), 1);
}

#[test]
fn fan_cap_collects_return_values_in_index_order() {
    assert_eq!(eval_string(r#"join ",", fan_cap 4 { $_ * 2 }"#), "0,2,4,6");
}

#[test]
fn fan_cap_zero_iterations_yields_empty_list() {
    assert_eq!(eval_int(r#"my @a = fan_cap 0 { die "no" }; scalar @a"#), 0);
}

#[test]
fn pfor_empty_list_skips_block() {
    assert_eq!(eval_int(r#"pfor { die "should not run" } (); 1"#), 1);
}

#[test]
fn pfor_undefined_bareword_sub_is_runtime_error() {
    assert_eq!(
        eval_err_kind(r#"pfor { nosuchsub } (1); 1"#),
        ErrorKind::Runtime,
    );
}

#[test]
fn fan_undefined_bareword_sub_is_runtime_error() {
    assert_eq!(
        eval_err_kind(r#"fan 1 { nosuchsub }; 1"#),
        ErrorKind::Runtime,
    );
}

#[test]
fn pfor_die_in_worker_is_die_kind() {
    assert_eq!(
        eval_err_kind(r#"pfor { die "worker" } (1); 1"#),
        ErrorKind::Die,
    );
}

#[test]
fn fan_die_in_worker_is_die_kind() {
    assert_eq!(
        eval_err_kind(r#"fan 1 { die "worker" }; 1"#),
        ErrorKind::Die,
    );
}

/// Bareword `{ processme }` is a zero-arg sub call; `@_` is `($_)` (fan worker index 0..N-1).
#[test]
fn fan_bareword_sub_passes_worker_index_as_topic() {
    assert_eq!(
        eval_int(r#"fn processme { $s += $_ } mysync $s = 0; fan 50 { processme }; $s"#),
        1225,
    );
}

/// Bareword `{ processme }` in `pfor` — zero-arg call; `@_` is `($_)` for each list element.
#[test]
fn pfor_bareword_sub_passes_list_item_as_topic() {
    assert_eq!(
        eval_int(r#"fn processme { $s += $_ } mysync $s = 0; pfor { processme } (0..49); $s"#),
        1225,
    );
}

#[test]
fn parallel_reduce_sum() {
    assert_eq!(eval_int("(1,2,3,4,5) |> preduce { $a + $b }"), 15);
}

#[test]
fn parallel_reduce_product() {
    assert_eq!(eval_int("(1,2,3,4,5) |> preduce { $a * $b }"), 120);
}

#[test]
fn parallel_reduce_max() {
    assert_eq!(eval_int("(3,7,1,9,2) |> preduce { $a > $b ? $a : $b }"), 9);
}

#[test]
fn parallel_reduce_single_element() {
    assert_eq!(eval_int("(42) |> preduce { $a + $b }"), 42);
}

#[test]
fn parallel_reduce_empty_list_returns_undef() {
    assert_eq!(eval_int("defined(() |> preduce { $a + $b }) ? 1 : 0"), 0);
}

#[test]
fn parallel_reduce_string_concat() {
    assert_eq!(
        eval_string(r#"("a","b","c") |> preduce { $a . $b }"#),
        "abc"
    );
}

#[test]
fn parallel_reduce_with_array_variable() {
    assert_eq!(
        eval_int("my @nums = (10, 20, 30); @nums |> preduce { $a + $b }"),
        60
    );
}

#[test]
fn preduce_init_empty_returns_identity() {
    assert_eq!(eval_int("() |> preduce_init 0, { $a + $b }"), 0);
}

#[test]
fn preduce_init_single_element_folds_from_identity() {
    assert_eq!(eval_int("(9) |> preduce_init 0, { $a + $b }"), 9);
}

#[test]
fn preduce_init_histogram_merges_partials() {
    assert_eq!(
        eval_int(r#"my $h = ("a","b","a") |> preduce_init {}, { $a->{$b}++; $a }; $h->{a}"#),
        2
    );
    assert_eq!(
        eval_int(r#"my $h = ("a","b","a") |> preduce_init {}, { $a->{$b}++; $a }; $h->{b}"#),
        1
    );
    assert_eq!(
        eval_int(
            r#"my $h = ("x","y","x") |> preduce_init {}, { my ($acc, $item) = @_; $acc->{$item}++; $acc }; $h->{"x"}"#
        ),
        2
    );
}

#[test]
fn barrier_builtin_returns_barrier_value() {
    assert_eq!(eval("barrier(2)").type_name(), "Barrier");
}

#[test]
fn barrier_wait_returns_truthy_scalar() {
    assert_eq!(eval_int(r#"my $b = barrier(1); $b->wait"#), 1);
}

#[test]
fn preduce_two_elements_folds_pair() {
    assert_eq!(eval_int(r#"(3, 5) |> preduce { $a + $b }"#), 8);
}

#[test]
fn async_await_returns_block_value() {
    assert_eq!(eval_int(r#"my $t = async { 40 + 2 }; await($t)"#), 42,);
}

#[test]
fn timer_reports_elapsed_time() {
    assert_eq!(eval_int(r#"0+((timer { 1 }) > 0)"#), 1);
}

#[test]
fn trace_returns_block_value() {
    assert_eq!(eval_int(r#"trace { 7 + 1 }"#), 8);
}