Files
sx/library/modules/ui/render.sx
agra bdd0e96d78 feat(lang): block value requires no trailing ; (Rust-style)
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).
2026-06-02 09:23:50 +03:00

180 lines
4.2 KiB
Plaintext
Executable File

#import "modules/std.sx";
#import "modules/ui/types.sx";
RenderNodeType :: enum {
rect;
rounded_rect;
text;
image;
clip_push;
clip_pop;
opacity_push;
opacity_pop;
}
RenderNode :: struct {
type: RenderNodeType;
frame: Frame;
// Rect / rounded_rect
fill_color: Color;
stroke_color: Color;
stroke_width: f32;
corner_radius: f32;
// Text
text: string;
font_size: f32;
text_color: Color;
// Image
texture_id: u32;
uv_min: Point;
uv_max: Point;
// Opacity
opacity: f32;
depth: s64;
}
RenderTree :: struct {
nodes: List(RenderNode);
generation: s64;
init :: () -> RenderTree {
RenderTree.{ generation = 0 }
}
clear :: (self: *RenderTree) {
self.nodes.len = 0;
self.generation += 1;
}
add :: (self: *RenderTree, node: RenderNode) -> s64 {
idx := self.nodes.len;
self.nodes.append(node);
idx
}
}
// Stateful builder — views use this to emit render nodes
RenderContext :: struct {
tree: *RenderTree;
clip_depth: s64;
opacity: f32;
depth: s64;
init :: (tree: *RenderTree) -> RenderContext {
RenderContext.{
tree = tree,
clip_depth = 0,
opacity = 1.0,
depth = 0
}
}
add_rect :: (self: *RenderContext, frame: Frame, fill: Color) {
self.tree.add(.{
type = .rect,
frame = frame,
fill_color = fill,
opacity = self.opacity,
depth = self.depth,
});
self.depth += 1;
}
add_rounded_rect :: (self: *RenderContext, frame: Frame, fill: Color, radius: f32) {
self.tree.add(.{
type = .rounded_rect,
frame = frame,
fill_color = fill,
corner_radius = radius,
opacity = self.opacity,
depth = self.depth,
});
self.depth += 1;
}
add_stroked_rect :: (self: *RenderContext, frame: Frame, fill: Color, stroke: Color, stroke_w: f32, radius: f32) {
self.tree.add(.{
type = .rounded_rect,
frame = frame,
fill_color = fill,
stroke_color = stroke,
stroke_width = stroke_w,
corner_radius = radius,
opacity = self.opacity,
depth = self.depth,
});
self.depth += 1;
}
add_text :: (self: *RenderContext, frame: Frame, text: string, font_size: f32, color: Color) {
self.tree.add(.{
type = .text,
frame = frame,
text = text,
font_size = font_size,
text_color = color,
opacity = self.opacity,
depth = self.depth,
});
self.depth += 1;
}
add_image :: (self: *RenderContext, frame: Frame, texture_id: u32) {
self.add_image_uv(frame, texture_id, Point.zero(), Point.{ x = 1.0, y = 1.0 });
}
add_image_uv :: (self: *RenderContext, frame: Frame, texture_id: u32, uv_min: Point, uv_max: Point) {
self.tree.add(.{
type = .image,
frame = frame,
texture_id = texture_id,
uv_min = uv_min,
uv_max = uv_max,
opacity = self.opacity,
depth = self.depth,
});
self.depth += 1;
}
push_clip :: (self: *RenderContext, frame: Frame) {
self.tree.add(.{
type = .clip_push,
frame = frame,
depth = self.depth,
});
self.clip_depth += 1;
}
pop_clip :: (self: *RenderContext) {
self.tree.add(.{
type = .clip_pop,
depth = self.depth,
});
self.clip_depth -= 1;
}
push_opacity :: (self: *RenderContext, alpha: f32) {
prev := self.opacity;
self.opacity = prev * alpha;
self.tree.add(.{
type = .opacity_push,
opacity = self.opacity,
depth = self.depth,
});
}
pop_opacity :: (self: *RenderContext, prev_opacity: f32) {
self.tree.add(.{
type = .opacity_pop,
depth = self.depth,
});
self.opacity = prev_opacity;
}
}