A block's value is now its last statement ONLY when that statement is a trailing expression with no `;`. A trailing `;` discards the value, leaving the block void. This makes value-vs-statement explicit and lets the compiler reject "this block was supposed to produce a value". Compiler: - Parser records `Block.produces_value` (last stmt is a no-`;` trailing expression) + `Block.discarded_semi` (the `;` that discarded a value), via `expectSemicolonAfter`. A trailing expression before `}` may now omit its `;` (previously a parse error). Match-arm and else-arm bodies are built value-producing regardless of the arm `;` (arms are exempt — the `;` is an arm terminator). - Lowering: `lowerBlockValue` / the block-expr path / `inferExprType` respect `produces_value`. A value-position block that discards its value is a hard error (`lowerValueBody` for function bodies; the value-context `.block` path for if/else branches, `catch` bodies, value bindings, match arms). Pure-failable `-> !` bodies (value rides the error channel) and a value-if whose branches are void are handled without false errors. - `defer`/`onfail` cleanup bodies lower as statements (void), so a trailing `;` there is fine. Migration (behavior-preserving — output unchanged): - stdlib + ~210 examples: dropped the trailing `;` on value-position last expressions. `format` now ends with an explicit `#insert "return result;"` (it relied on `#insert`-as-block-value, which `;` discards). - Two `main :: () -> s32` examples that relied on the old silent default-return got an explicit trailing `0`. - Rejection snapshots 0412 / 1013 regenerated (their quoted source lines lost a `;`); the diagnostics themselves are unchanged. Docs/tests: specs.md "Block values" section; examples 0040 (rules) + 0041 (rejection); 3 parser unit tests. Filed issue 0066 (pre-existing match-arm negated-literal phi-width quirk, surfaced not caused here). Gates: zig build, zig build test, run_examples.sh -> 343 passed, cross_compile.sh -> 7 passed (also refreshed its stale example names).
167 lines
3.9 KiB
Plaintext
167 lines
3.9 KiB
Plaintext
#import "modules/std.sx";
|
|
#import "modules/math/math.sx";
|
|
#import "modules/compiler.sx";
|
|
#import "modules/test.sx";
|
|
pkg :: #import "modules/testpkg";
|
|
|
|
add :: (a: s32, b: s32) -> s32 { a + b }
|
|
|
|
mul :: (a: s32, b: s32) -> s32 { a * b }
|
|
|
|
// P4 edge: Chained default→default calls
|
|
Chained :: protocol {
|
|
base :: (msg: string) -> s32;
|
|
wrap :: (msg: string) -> s32 {
|
|
self.base(msg) + 1
|
|
}
|
|
double_wrap :: (msg: string) -> s32 {
|
|
self.wrap(msg) + self.wrap(msg)
|
|
}
|
|
}
|
|
|
|
main :: () {
|
|
|
|
// ========================================================
|
|
// 2. OPERATORS & PRECEDENCE
|
|
// ========================================================
|
|
print("=== 2. Operators ===\n");
|
|
|
|
// Arithmetic
|
|
print("add: {}\n", 3 + 4);
|
|
print("sub: {}\n", 10 - 3);
|
|
print("mul: {}\n", 6 * 7);
|
|
print("div: {}\n", 20 / 4);
|
|
print("mod: {}\n", 17 % 5);
|
|
print("neg: {}\n", -(5));
|
|
|
|
// Comparisons
|
|
print("eq: {}\n", 5 == 5);
|
|
print("neq: {}\n", 5 != 3);
|
|
print("lt: {}\n", 3 < 5);
|
|
print("gt: {}\n", 5 > 3);
|
|
print("le: {}\n", 5 <= 5);
|
|
print("ge: {}\n", 5 >= 3);
|
|
|
|
// Chained comparisons
|
|
v := 50;
|
|
print("chain: {}\n", 0 <= v <= 100);
|
|
print("chain-gt: {}\n", 100 > v > 0);
|
|
print("chain-mixed: {}\n", 100 > v >= 0);
|
|
|
|
// Equality chains
|
|
print("eq-chain: {}\n", 5 == 5 == 5);
|
|
print("eq-chain-f: {}\n", 5 == 5 == 6);
|
|
|
|
// Bitwise
|
|
print("band: {}\n", 0xFF & 0x0F);
|
|
print("bor: {}\n", 1 | 2 | 4);
|
|
|
|
// Bitwise XOR
|
|
print("bxor: {}\n", 0xFF ^ 0x0F);
|
|
print("bxor2: {}\n", 6 ^ 3);
|
|
|
|
// Bitwise NOT
|
|
print("bnot: {}\n", ~0);
|
|
print("bnot2: {}\n", ~1);
|
|
|
|
// Shifts
|
|
print("shl: {}\n", 1 << 4);
|
|
print("shr: {}\n", 256 >> 4);
|
|
print("shl2: {}\n", 3 << 3);
|
|
print("shr2: {}\n", 255 >> 1);
|
|
|
|
// Bitwise on variables
|
|
bv1 := 0xFF;
|
|
bv2 := 0x0F;
|
|
print("band-var: {}\n", bv1 & bv2);
|
|
bv3 := 1;
|
|
bv4 := 6;
|
|
print("bor-var: {}\n", bv3 | bv4);
|
|
print("bxor-var: {}\n", bv1 ^ bv2);
|
|
print("shl-var: {}\n", bv3 << 4);
|
|
print("shr-var: {}\n", bv1 >> 4);
|
|
print("bnot-var: {}\n", ~bv2);
|
|
|
|
// Bitwise compound assignment
|
|
bca := 0xFF;
|
|
bca &= 0x0F;
|
|
print("and-assign: {}\n", bca);
|
|
bco := 0x0F;
|
|
bco |= 0xF0;
|
|
print("or-assign: {}\n", bco);
|
|
bcx := 0xFF;
|
|
bcx ^= 0x0F;
|
|
print("xor-assign: {}\n", bcx);
|
|
bcs := 1;
|
|
bcs <<= 8;
|
|
print("shl-assign: {}\n", bcs);
|
|
bcr := 256;
|
|
bcr >>= 4;
|
|
print("shr-assign: {}\n", bcr);
|
|
|
|
// Modulo on variables
|
|
mv1 := 17;
|
|
mv2 := 5;
|
|
print("mod-var: {}\n", mv1 % mv2);
|
|
|
|
// Logical (short-circuit)
|
|
print("and: {}\n", true and true);
|
|
print("and-false: {}\n", true and false);
|
|
print("or: {}\n", false or true);
|
|
print("or-false: {}\n", false or false);
|
|
|
|
// Short-circuit verification
|
|
print("short-and: {}\n", false and true);
|
|
print("short-or: {}\n", true or false);
|
|
|
|
// Compound assignment
|
|
ca := 10;
|
|
ca += 5;
|
|
print("ca+=: {}\n", ca);
|
|
ca -= 3;
|
|
print("ca-=: {}\n", ca);
|
|
ca *= 2;
|
|
print("ca*=: {}\n", ca);
|
|
ca /= 6;
|
|
print("ca/=: {}\n", ca);
|
|
|
|
// Precedence
|
|
print("prec1: {}\n", 2 + 3 * 4);
|
|
print("prec2: {}\n", (2 + 3) * 4);
|
|
|
|
// xx explicit cast
|
|
big2 : f64 = 200.7;
|
|
small : u8 = xx big2;
|
|
print("xx-cast: {}\n", small);
|
|
|
|
// Implicit widening conversions
|
|
wu : u8 = 200;
|
|
ws : s64 = wu;
|
|
print("widen-u8-s64: {}\n", ws);
|
|
|
|
wi3 : s32 = 42;
|
|
wf : f64 = wi3;
|
|
print("widen-s32-f64: {}\n", wf);
|
|
|
|
wf32 : f32 = 1.5;
|
|
wf64 : f64 = wf32;
|
|
print("widen-f32-f64: {}\n", wf64);
|
|
|
|
wu2 : u8 = 100;
|
|
ws2 : s16 = wu2;
|
|
print("widen-u8-s16: {}\n", ws2);
|
|
|
|
// More xx narrowing
|
|
xl : s64 = 12345;
|
|
xs : s32 = xx xl;
|
|
print("xx-s64-s32: {}\n", xs);
|
|
|
|
xd : f64 = 1.5;
|
|
xf : f32 = xx xd;
|
|
print("xx-f64-f32: {}\n", xf);
|
|
|
|
xdf : f64 = 7.9;
|
|
xdi : s32 = xx xdf;
|
|
print("xx-f64-s32: {}\n", xdi);
|
|
}
|