a23860be3e
ci / build (ubuntu) (push) Has been cancelled
ci / unit + fmt (push) Has been cancelled
ci / examples (push) Has been cancelled
ci / goldens + tools (push) Has been cancelled
ci / apps (push) Has been cancelled
ci / selfhost smoke (push) Has been cancelled
ci / macos smoke (push) Has been cancelled
ci / windows smoke (push) Has been cancelled
ci / CI gate (push) Has been cancelled
selfhost-loop / bootstrap determinism (push) Has been cancelled
Bump compiler banners and package version to 1.0.0, activate SEMVER policy, and add RELEASE_v1.0.0 notes. Fix closure auto-Drop leaking outer Array drops into nested capture bodies (iter_hof). Fmt-clean examples/src for CI.
79 lines
1.9 KiB
Plaintext
79 lines
1.9 KiB
Plaintext
// Session 63 — multi-rep patterns, compound/zipped rep, nested template $(…)*
|
|
import Std::Io::{PrintLine};
|
|
import Std::String::{String_FromInt};
|
|
|
|
// Compound rep: $( $a:expr , $b:expr ),* → parallel lists, zip in template
|
|
macro! add_pairs {
|
|
( $($a:expr, $b:expr),* ) => {
|
|
var __acc: int = 0;
|
|
$( __acc = __acc + ($a + $b); )*
|
|
__acc
|
|
}
|
|
}
|
|
|
|
// Multi-rep groups separated by `;` in the call
|
|
// sum_n!(1,2; 10,20,30) → sum first group + sum second group
|
|
macro! sum_groups {
|
|
( $($x:expr),* ; $($y:expr),* ) => {
|
|
var __s: int = 0;
|
|
$( __s = __s + $x; )*
|
|
$( __s = __s + $y; )*
|
|
__s
|
|
}
|
|
}
|
|
|
|
// Nested template: outer over $x, inner body uses current $x once
|
|
// (same-list nested MacroRep expands once when bound as single)
|
|
macro! double_each_sum {
|
|
( $($x:expr),* ) => {
|
|
var __t: int = 0;
|
|
$(
|
|
$( __t = __t + $x; )*
|
|
$( __t = __t + $x; )*
|
|
)*
|
|
__t
|
|
}
|
|
}
|
|
|
|
// Prefix fixed + trailing rep still works
|
|
macro! named_sum {
|
|
( $label:ident, $($n:expr),* ) => {
|
|
var __u: int = 0;
|
|
$( __u = __u + $n; )*
|
|
__u
|
|
}
|
|
}
|
|
|
|
func Main() -> int {
|
|
let p: int = add_pairs!(1, 10, 2, 20);
|
|
// (1+10)+(2+20) = 33
|
|
let g: int = sum_groups!(1, 2; 10, 20, 30);
|
|
// 1+2+10+20+30 = 63
|
|
let d: int = double_each_sum!(3, 4);
|
|
// (3+3)+(4+4) = 14
|
|
let n: int = named_sum!(ignored, 5, 6, 7);
|
|
// 18
|
|
PrintLine(String_FromInt(p));
|
|
PrintLine(String_FromInt(g));
|
|
PrintLine(String_FromInt(d));
|
|
PrintLine(String_FromInt(n));
|
|
if p != 33 {
|
|
PrintLine("FAIL add_pairs");
|
|
return 1;
|
|
}
|
|
if g != 63 {
|
|
PrintLine("FAIL sum_groups");
|
|
return 1;
|
|
}
|
|
if d != 14 {
|
|
PrintLine("FAIL double_each_sum");
|
|
return 1;
|
|
}
|
|
if n != 18 {
|
|
PrintLine("FAIL named_sum");
|
|
return 1;
|
|
}
|
|
PrintLine("PASS macro_nested");
|
|
return 0;
|
|
}
|