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).
153 lines
5.0 KiB
Plaintext
Executable File
153 lines
5.0 KiB
Plaintext
Executable File
#import "modules/std.sx";
|
|
#import "modules/math";
|
|
#import "modules/ui/types.sx";
|
|
#import "modules/ui/view.sx";
|
|
|
|
// VStack layout: measure all children, stack vertically
|
|
// Width is constrained from parent; height is unspecified (children choose)
|
|
layout_vstack :: (children: *List(ViewChild), bounds: Frame, spacing: f32, alignment: HAlignment) {
|
|
n := children.len;
|
|
if n == 0 { return; }
|
|
|
|
content_width := bounds.size.width;
|
|
|
|
y := bounds.origin.y;
|
|
i := 0;
|
|
while i < n {
|
|
child := @children.items[i];
|
|
child_size := child.view.size_that_fits(ProposedSize.{
|
|
width = content_width,
|
|
height = null
|
|
});
|
|
x_offset := align_h(alignment, child_size.width, content_width);
|
|
child.computed_frame = Frame.{
|
|
origin = Point.{ x = bounds.origin.x + x_offset, y = y },
|
|
size = child_size
|
|
};
|
|
child.view.layout(child.computed_frame);
|
|
y = y + child_size.height + spacing;
|
|
i += 1;
|
|
}
|
|
}
|
|
|
|
// HStack layout: measure all children, stack horizontally
|
|
// Height is constrained from parent; width is unspecified (children choose)
|
|
layout_hstack :: (children: *List(ViewChild), bounds: Frame, spacing: f32, alignment: VAlignment) {
|
|
n := children.len;
|
|
if n == 0 { return; }
|
|
|
|
content_height := bounds.size.height;
|
|
|
|
x := bounds.origin.x;
|
|
i := 0;
|
|
while i < n {
|
|
child := @children.items[i];
|
|
child_size := child.view.size_that_fits(ProposedSize.{
|
|
width = null,
|
|
height = content_height
|
|
});
|
|
y_offset := align_v(alignment, child_size.height, content_height);
|
|
child.computed_frame = Frame.{
|
|
origin = Point.{ x = x, y = bounds.origin.y + y_offset },
|
|
size = child_size
|
|
};
|
|
child.view.layout(child.computed_frame);
|
|
x = x + child_size.width + spacing;
|
|
i += 1;
|
|
}
|
|
}
|
|
|
|
// ZStack layout: all children get same bounds, aligned
|
|
layout_zstack :: (children: *List(ViewChild), bounds: Frame, alignment: Alignment) {
|
|
n := children.len;
|
|
if n == 0 { return; }
|
|
|
|
proposal := ProposedSize.{
|
|
width = bounds.size.width,
|
|
height = bounds.size.height
|
|
};
|
|
|
|
i := 0;
|
|
while i < n {
|
|
child := @children.items[i];
|
|
child_size := child.view.size_that_fits(proposal);
|
|
x_offset := align_h(alignment.h, child_size.width, bounds.size.width);
|
|
y_offset := align_v(alignment.v, child_size.height, bounds.size.height);
|
|
child.computed_frame = Frame.{
|
|
origin = Point.{ x = bounds.origin.x + x_offset, y = bounds.origin.y + y_offset },
|
|
size = child_size
|
|
};
|
|
child.view.layout(child.computed_frame);
|
|
i += 1;
|
|
}
|
|
}
|
|
|
|
// Measure helpers — compute stack size from children
|
|
|
|
measure_vstack :: (children: *List(ViewChild), proposal: ProposedSize, spacing: f32) -> Size {
|
|
n := children.len;
|
|
if n == 0 { return Size.zero(); }
|
|
|
|
max_width : f32 = 0.0;
|
|
total_height : f32 = 0.0;
|
|
|
|
// Measure children: constrain width, leave height unspecified
|
|
child_proposal := ProposedSize.{ width = proposal.width, height = null };
|
|
i := 0;
|
|
while i < n {
|
|
child_size := children.items[i].view.size_that_fits(child_proposal);
|
|
children.items[i].computed_frame.size = child_size;
|
|
if child_size.width > max_width { max_width = child_size.width; }
|
|
total_height = total_height + child_size.height;
|
|
i += 1;
|
|
}
|
|
|
|
total_height = total_height + spacing * xx (n - 1);
|
|
|
|
result_width := min(proposal.width ?? max_width, max_width);
|
|
Size.{ width = result_width, height = total_height }
|
|
}
|
|
|
|
measure_hstack :: (children: *List(ViewChild), proposal: ProposedSize, spacing: f32) -> Size {
|
|
n := children.len;
|
|
if n == 0 { return Size.zero(); }
|
|
|
|
total_width : f32 = 0.0;
|
|
max_height : f32 = 0.0;
|
|
|
|
// Measure children: constrain height, leave width unspecified
|
|
child_proposal := ProposedSize.{ width = null, height = proposal.height };
|
|
i := 0;
|
|
while i < n {
|
|
child_size := children.items[i].view.size_that_fits(child_proposal);
|
|
children.items[i].computed_frame.size = child_size;
|
|
total_width = total_width + child_size.width;
|
|
if child_size.height > max_height { max_height = child_size.height; }
|
|
i += 1;
|
|
}
|
|
|
|
total_width = total_width + spacing * xx (n - 1);
|
|
|
|
result_height := min(proposal.height ?? max_height, max_height);
|
|
Size.{ width = total_width, height = result_height }
|
|
}
|
|
|
|
measure_zstack :: (children: *List(ViewChild), proposal: ProposedSize) -> Size {
|
|
n := children.len;
|
|
if n == 0 { return Size.zero(); }
|
|
|
|
max_width : f32 = 0.0;
|
|
max_height : f32 = 0.0;
|
|
|
|
i := 0;
|
|
while i < n {
|
|
child_size := children.items[i].view.size_that_fits(proposal);
|
|
children.items[i].computed_frame.size = child_size;
|
|
if child_size.width > max_width { max_width = child_size.width; }
|
|
if child_size.height > max_height { max_height = child_size.height; }
|
|
i += 1;
|
|
}
|
|
|
|
Size.{ width = max_width, height = max_height }
|
|
}
|