block labels
This commit is contained in:
+136
-32
@@ -67,6 +67,9 @@ Yield_Target :: struct {
|
||||
// True when the loop also yields `none` (a `{T, none}` set → `?T`); set from a
|
||||
// pure-AST scan, used to pick the slot's element type on the first concrete yield.
|
||||
result_optional: bool,
|
||||
// `len(defers)` when this target's body began; a `yield :label` flushes defers down
|
||||
// to here before breaking, so an outer-loop / value-block yield runs inner defers too.
|
||||
defer_floor: int,
|
||||
}
|
||||
|
||||
Build_Ctx :: struct {
|
||||
@@ -92,6 +95,9 @@ Build_Ctx :: struct {
|
||||
// only see loops opened within the defer (those past `loop_floor`).
|
||||
defers: ^[dynamic][]hir.Stmt_Id,
|
||||
loop_defer_starts: ^[dynamic]int,
|
||||
// Parallel to `loop_defer_starts`: the label of each enclosing loop (INVALID when
|
||||
// unlabeled), so a `break :L` / `continue :L` can target an outer labeled loop.
|
||||
loop_labels: ^[dynamic]symbol.Id,
|
||||
defer_depth: int,
|
||||
loop_floor: int,
|
||||
}
|
||||
@@ -4055,7 +4061,7 @@ build_block :: proc(
|
||||
if typed {
|
||||
expected = type_from_syntax(statement.type)
|
||||
}
|
||||
value, value_type := build_value_source(ctx, &body, statement.body, expected, statement.span)
|
||||
value, value_type := build_value_source(ctx, &body, statement.body, expected, statement.span, statement.label)
|
||||
if _, found := find_build_local(ctx.locals^[duplicate_start:], statement.name); found {
|
||||
id := source.addf(
|
||||
checker.diagnostics, statement.span,
|
||||
@@ -4266,7 +4272,7 @@ build_block :: proc(
|
||||
} else if statement.expr == ast.INVALID_EXPR {
|
||||
// `target = { ... yield v }`: build the value block against the
|
||||
// target's type (build_value_block coerces internally).
|
||||
value, _ = build_value_source(ctx, &body, statement.body, target_type, statement.span)
|
||||
value, _ = build_value_source(ctx, &body, statement.body, target_type, statement.span, statement.label)
|
||||
} else {
|
||||
value = build_expr(
|
||||
checker, statement.expr, ctx.locals^[:], ctx.global_reads, ctx.calls,
|
||||
@@ -4286,7 +4292,7 @@ build_block :: proc(
|
||||
if statement.name == checker.sink_symbol {
|
||||
value: hir.Expr_Id
|
||||
if statement.expr == ast.INVALID_EXPR {
|
||||
value, _ = build_value_source(ctx, &body, statement.body, types.INVALID, statement.span)
|
||||
value, _ = build_value_source(ctx, &body, statement.body, types.INVALID, statement.span, statement.label)
|
||||
} else {
|
||||
value = build_expr(checker, statement.expr, ctx.locals^[:], ctx.global_reads, ctx.calls, types.INVALID, ctx.pkg, ctx.file)
|
||||
}
|
||||
@@ -4330,7 +4336,7 @@ build_block :: proc(
|
||||
}
|
||||
value: hir.Expr_Id
|
||||
if statement.expr == ast.INVALID_EXPR {
|
||||
value, _ = build_value_source(ctx, &body, statement.body, local.type, statement.span)
|
||||
value, _ = build_value_source(ctx, &body, statement.body, local.type, statement.span, statement.label)
|
||||
} else {
|
||||
value = build_expr(
|
||||
checker, statement.expr, ctx.locals^[:], ctx.global_reads, ctx.calls,
|
||||
@@ -4596,7 +4602,9 @@ build_block :: proc(
|
||||
ctx.problematic^ = true
|
||||
}
|
||||
append(ctx.loop_defer_starts, len(ctx.defers^))
|
||||
append(ctx.loop_labels, statement.label)
|
||||
loop_body := build_block(ctx, statement.body)
|
||||
pop(ctx.loop_labels)
|
||||
pop(ctx.loop_defer_starts)
|
||||
update := hir.INVALID_STMT
|
||||
if statement.update != ast.INVALID_STMT {
|
||||
@@ -4611,6 +4619,7 @@ build_block :: proc(
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind=.While,
|
||||
span=statement.span,
|
||||
label=statement.label,
|
||||
expr=condition,
|
||||
then_body=loop_body,
|
||||
update=update,
|
||||
@@ -4690,7 +4699,9 @@ build_block :: proc(
|
||||
}
|
||||
}
|
||||
append(ctx.loop_defer_starts, len(ctx.defers^))
|
||||
append(ctx.loop_labels, statement.label)
|
||||
loop_body := build_block(ctx, statement.body, capture_start)
|
||||
pop(ctx.loop_labels)
|
||||
pop(ctx.loop_defer_starts)
|
||||
resize(ctx.locals, capture_start)
|
||||
|
||||
@@ -4699,6 +4710,7 @@ build_block :: proc(
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind=.For,
|
||||
span=statement.span,
|
||||
label=statement.label,
|
||||
local=item_local,
|
||||
index_local=index_local,
|
||||
expr=iterable,
|
||||
@@ -4723,11 +4735,29 @@ build_block :: proc(
|
||||
ctx.problematic^ = true
|
||||
}
|
||||
case .Break, .Continue:
|
||||
// Inside a `defer`, `loop_floor` hides the enclosing loops so only loops
|
||||
// opened within the defer count.
|
||||
if len(ctx.loop_defer_starts^) <= ctx.loop_floor {
|
||||
// `break :L` / `continue :L` targets the innermost enclosing loop labeled `L`;
|
||||
// an unlabeled one targets the innermost loop. Inside a `defer`, `loop_floor`
|
||||
// hides the loops opened outside the defer.
|
||||
target_index := -1
|
||||
if symbol.is_valid(statement.label) {
|
||||
for i := len(ctx.loop_labels^) - 1; i >= ctx.loop_floor; i -= 1 {
|
||||
if ctx.loop_labels^[i] == statement.label {
|
||||
target_index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if len(ctx.loop_defer_starts^) > ctx.loop_floor {
|
||||
target_index = len(ctx.loop_defer_starts^) - 1
|
||||
}
|
||||
if target_index < 0 {
|
||||
keyword := "break" if statement.kind == .Break else "continue"
|
||||
id := source.addf(checker.diagnostics, statement.span, "'%s' outside of a loop", keyword)
|
||||
id: source.Diagnostic_Id
|
||||
if symbol.is_valid(statement.label) {
|
||||
id = source.addf(checker.diagnostics, statement.span,
|
||||
"no enclosing loop is labeled '%s'", symbol_text(checker, statement.label))
|
||||
} else {
|
||||
id = source.addf(checker.diagnostics, statement.span, "'%s' outside of a loop", keyword)
|
||||
}
|
||||
append(&body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind = .Trap, span = statement.span, expr = hir.INVALID_EXPR,
|
||||
@@ -4736,13 +4766,13 @@ build_block :: proc(
|
||||
ctx.problematic^ = true
|
||||
continue
|
||||
}
|
||||
// Exit the loop body and any blocks between here and it: run their
|
||||
// deferred statements down to and including the innermost loop body.
|
||||
flush_defers(ctx, &body, ctx.loop_defer_starts^[len(ctx.loop_defer_starts^) - 1])
|
||||
// Exit the loop body and any blocks between here and the target loop: run their
|
||||
// deferred statements down to and including the target loop body.
|
||||
flush_defers(ctx, &body, ctx.loop_defer_starts^[target_index])
|
||||
append(&body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind = .Break if statement.kind == .Break else .Continue,
|
||||
span = statement.span, expr = hir.INVALID_EXPR,
|
||||
span = statement.span, label = statement.label, expr = hir.INVALID_EXPR,
|
||||
local = hir.INVALID_LOCAL, diagnostic = source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
case .Block:
|
||||
@@ -4767,20 +4797,7 @@ build_block :: proc(
|
||||
}
|
||||
if target_index < 0 {
|
||||
id := source.addf(checker.diagnostics, statement.span,
|
||||
"no enclosing value loop is labeled '%s'", symbol_text(checker, statement.label))
|
||||
append(&body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind = .Trap, span = statement.span, expr = hir.INVALID_EXPR,
|
||||
local = hir.INVALID_LOCAL, diagnostic = id,
|
||||
})
|
||||
ctx.problematic^ = true
|
||||
continue
|
||||
}
|
||||
// ponytail: a `yield :blk` lowers to milestone-18 `break`, which targets the
|
||||
// innermost loop only, so the label must name the innermost value loop.
|
||||
if target_index != len(ctx.yield_targets^) - 1 {
|
||||
id := source.addf(checker.diagnostics, statement.span,
|
||||
"'yield :%s' must target the innermost loop", symbol_text(checker, statement.label))
|
||||
"no enclosing value loop or block is labeled '%s'", symbol_text(checker, statement.label))
|
||||
append(&body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind = .Trap, span = statement.span, expr = hir.INVALID_EXPR,
|
||||
@@ -4805,11 +4822,11 @@ build_block :: proc(
|
||||
}
|
||||
// slot = value (the slot is un-nameable, so no defer can mutate it; no spill).
|
||||
emit_slot_assign(checker, &body, target.slot, yielded, statement.span)
|
||||
// Exit the loop: flush defers down to the loop body, then break.
|
||||
flush_defers(ctx, &body, ctx.loop_defer_starts^[len(ctx.loop_defer_starts^) - 1])
|
||||
// Exit the target: flush defers down to its body, then a labeled break.
|
||||
flush_defers(ctx, &body, target.defer_floor)
|
||||
append(&body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind = .Break, span = statement.span, expr = hir.INVALID_EXPR,
|
||||
kind = .Break, span = statement.span, label = target.label, expr = hir.INVALID_EXPR,
|
||||
local = hir.INVALID_LOCAL, diagnostic = source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
continue
|
||||
@@ -4818,7 +4835,7 @@ build_block :: proc(
|
||||
// is peeled by the value builders (value block / if branch / loop fall-through).
|
||||
id := source.add(
|
||||
checker.diagnostics, statement.span,
|
||||
"'yield' is only valid as the final statement of a value block, or as 'yield :label' inside a labeled value loop",
|
||||
"'yield' is only valid as the final statement of a value block, or as 'yield :label' inside a labeled value loop or block",
|
||||
)
|
||||
append(&body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
@@ -4960,16 +4977,21 @@ build_value_block :: proc(
|
||||
}
|
||||
|
||||
// build_value_source feeds a declaration/assignment RHS into the right value builder:
|
||||
// a `{ ... }` block, an `if` whose branches yield, or a `for`/`while` whose iterations
|
||||
// yield. All three return the produced value and its type for the enclosing binding.
|
||||
// a labeled `blk: { ... }` block, a plain `{ ... }` block, an `if` whose branches yield,
|
||||
// or a `for`/`while` whose iterations yield. All return the produced value and its type
|
||||
// for the enclosing binding. `label` is the labeled-block label (INVALID otherwise).
|
||||
build_value_source :: proc(
|
||||
ctx: ^Build_Ctx,
|
||||
body: ^[dynamic]hir.Stmt_Id,
|
||||
body_stmts: []ast.Stmt_Id,
|
||||
expected: types.Type,
|
||||
span: source.Span,
|
||||
label := symbol.INVALID,
|
||||
) -> (value: hir.Expr_Id, value_type: types.Type) {
|
||||
checker := ctx.checker
|
||||
if symbol.is_valid(label) {
|
||||
return build_value_labeled_block(ctx, body, body_stmts, label, expected, span)
|
||||
}
|
||||
if len(body_stmts) == 1 {
|
||||
#partial switch checker.ast_module.statements[body_stmts[0]].kind {
|
||||
case .If:
|
||||
@@ -5342,6 +5364,83 @@ value_loop_element_type :: proc(ctx: ^Build_Ctx, loop_stmt: ast.Stmt) -> types.T
|
||||
return result
|
||||
}
|
||||
|
||||
// build_value_labeled_block turns `x :: blk: { …; yield :blk v }` into a result slot each
|
||||
// `yield :blk` assigns (via the build_block `.Yield` desugar → `slot = v; break :blk`), then
|
||||
// reads it after the block. Every path must yield (or otherwise exit); HIR holds a `.Block`
|
||||
// that emits the body and the exit label the labeled breaks branch to. No iteration / no
|
||||
// fall-through (unlike a value loop). The type is the annotation when typed, else the first
|
||||
// concrete `yield :blk`'s type (optional when any yield is `none`).
|
||||
build_value_labeled_block :: proc(
|
||||
ctx: ^Build_Ctx,
|
||||
body: ^[dynamic]hir.Stmt_Id,
|
||||
block_stmts: []ast.Stmt_Id,
|
||||
label: symbol.Id,
|
||||
expected: types.Type,
|
||||
span: source.Span,
|
||||
) -> (value: hir.Expr_Id, value_type: types.Type) {
|
||||
checker := ctx.checker
|
||||
result_optional := loop_yields_none(checker, block_stmts)
|
||||
slot := hir.INVALID_LOCAL
|
||||
slot_type := types.INVALID
|
||||
if is_runtime_type(checker, expected) {
|
||||
slot_type = expected
|
||||
slot = new_value_slot(ctx, slot_type)
|
||||
result_optional = types.is_optional(slot_type, &checker.module.types)
|
||||
} else if result_optional {
|
||||
// Untyped block that also yields `none`: pre-type the element from the first
|
||||
// concrete yield (a block has no captures, so a capture-free probe suffices).
|
||||
elem := first_concrete_yield_expr(checker, block_stmts)
|
||||
if elem != ast.INVALID_EXPR {
|
||||
probe := build_expr(checker, elem, ctx.locals^[:], ctx.global_reads, ctx.calls, types.INVALID, ctx.pkg, ctx.file)
|
||||
if checker.module.exprs[probe].kind != .Invalid {
|
||||
slot_type = types.optional(&checker.module.types, checker.module.exprs[probe].type)
|
||||
slot = new_value_slot(ctx, slot_type)
|
||||
}
|
||||
}
|
||||
}
|
||||
append(ctx.yield_targets, Yield_Target{
|
||||
label = label, slot = slot, slot_type = slot_type, result_optional = result_optional,
|
||||
defer_floor = len(ctx.defers^),
|
||||
})
|
||||
built := build_block(ctx, block_stmts)
|
||||
target := pop(ctx.yield_targets)
|
||||
|
||||
if target.slot == hir.INVALID_LOCAL {
|
||||
for s in built {
|
||||
append(body, s)
|
||||
}
|
||||
delete(built, checker.allocator)
|
||||
id := source.add(checker.diagnostics, span,
|
||||
"could not determine the value block's yield type; annotate the binding")
|
||||
ctx.problematic^ = true
|
||||
return invalid_hir_expr(checker, span, id), types.INVALID
|
||||
}
|
||||
// Every path must `yield :blk` (or return/break out); otherwise a path falls through
|
||||
// to the slot read with a poison value.
|
||||
if !all_paths_exit(&checker.module, built) {
|
||||
for s in built {
|
||||
append(body, s)
|
||||
}
|
||||
delete(built, checker.allocator)
|
||||
id := source.add(checker.diagnostics, span, "a labeled value block must 'yield' on every path")
|
||||
ctx.problematic^ = true
|
||||
return invalid_hir_expr(checker, span, id), types.INVALID
|
||||
}
|
||||
|
||||
append(body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind = .Declaration, span = span, local = target.slot, expr = hir.INVALID_EXPR,
|
||||
diagnostic = source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
append(body, hir.stmt_id(len(checker.module.statements)))
|
||||
append(&checker.module.statements, hir.Stmt{
|
||||
kind = .Block, span = span, label = label, then_body = built,
|
||||
local = hir.INVALID_LOCAL, target = hir.INVALID_EXPR, diagnostic = source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
value = slot_read(checker, target.slot, target.slot_type, span)
|
||||
return value, target.slot_type
|
||||
}
|
||||
|
||||
// build_value_loop turns a labeled `for/while ... blk: { … }` whose body ends in a
|
||||
// fall-through `yield` (and may early-exit via `yield :blk x`) into a result slot:
|
||||
// the fall-through value initializes the slot before the loop, each `yield :blk x`
|
||||
@@ -5395,6 +5494,7 @@ build_value_loop :: proc(
|
||||
}
|
||||
append(ctx.yield_targets, Yield_Target{
|
||||
label = loop_stmt.label, slot = slot, slot_type = slot_type, result_optional = result_optional,
|
||||
defer_floor = len(ctx.defers^),
|
||||
})
|
||||
|
||||
// Build the loop with the fall-through peeled off, reusing the normal For/While arm.
|
||||
@@ -5620,6 +5720,8 @@ build_function :: proc(checker: ^Checker, id: Spec_Id) {
|
||||
defers.allocator = checker.allocator
|
||||
loop_defer_starts: [dynamic]int
|
||||
loop_defer_starts.allocator = checker.allocator
|
||||
loop_labels: [dynamic]symbol.Id
|
||||
loop_labels.allocator = checker.allocator
|
||||
yield_targets: [dynamic]Yield_Target
|
||||
yield_targets.allocator = checker.allocator
|
||||
ctx := Build_Ctx{
|
||||
@@ -5635,6 +5737,7 @@ build_function :: proc(checker: ^Checker, id: Spec_Id) {
|
||||
problematic = &problematic,
|
||||
defers = &defers,
|
||||
loop_defer_starts = &loop_defer_starts,
|
||||
loop_labels = &loop_labels,
|
||||
yield_targets = &yield_targets,
|
||||
}
|
||||
block := build_block(&ctx, function.body)
|
||||
@@ -5685,6 +5788,7 @@ build_function :: proc(checker: ^Checker, id: Spec_Id) {
|
||||
}
|
||||
delete(defers)
|
||||
delete(loop_defer_starts)
|
||||
delete(loop_labels)
|
||||
delete(yield_targets)
|
||||
delete(locals)
|
||||
}
|
||||
|
||||
@@ -156,6 +156,9 @@ Stmt_Kind :: enum u8 {
|
||||
For,
|
||||
Break,
|
||||
Continue,
|
||||
// A labeled value block (`blk: { … yield :blk v }`): lowers to its `then_body`
|
||||
// followed by an exit label, so a `yield :blk` (a labeled break) lands after it.
|
||||
Block,
|
||||
}
|
||||
|
||||
Assignment_Op :: enum u8 {
|
||||
@@ -172,6 +175,9 @@ Stmt :: struct {
|
||||
span: source.Span,
|
||||
local: Local_Id,
|
||||
index_local: Local_Id,
|
||||
// `While`/`For`/`Block` may carry a label; a labeled `Break`/`Continue` targets the
|
||||
// matching enclosing loop or value block. `INVALID` when absent.
|
||||
label: symbol.Id,
|
||||
target: Expr_Id,
|
||||
expr: Expr_Id,
|
||||
iterator_type: types.Type,
|
||||
|
||||
@@ -3,6 +3,7 @@ package lower
|
||||
import "../hir"
|
||||
import "../ir"
|
||||
import "../source"
|
||||
import "../symbol"
|
||||
import "../target"
|
||||
import "../types"
|
||||
import "core:fmt"
|
||||
@@ -22,11 +23,15 @@ State :: struct {
|
||||
allocator: mem.Allocator,
|
||||
}
|
||||
|
||||
// `break` branches to `exit_lbl`; `continue` branches to `continue_lbl` (the
|
||||
// loop's update/latch, which runs the update clause then re-tests the condition).
|
||||
// An enclosing exit target. `break` branches to `exit_lbl`; `continue` branches to
|
||||
// `continue_lbl` (the loop's update/latch, which runs the update clause then re-tests
|
||||
// the condition). A labeled `break`/`continue`/`yield` matches `label`; a value block is
|
||||
// `is_loop = false` (it has no `continue` and is skipped by plain `break`/`continue`).
|
||||
Loop_Ctx :: struct {
|
||||
label: symbol.Id,
|
||||
exit_lbl: i64,
|
||||
continue_lbl: i64,
|
||||
is_loop: bool,
|
||||
}
|
||||
|
||||
fresh_label :: proc(state: ^State) -> i64 {
|
||||
@@ -752,10 +757,25 @@ lower_statements :: proc(state: ^State, statements: []hir.Stmt_Id) {
|
||||
})
|
||||
}
|
||||
case .Break, .Continue:
|
||||
// The checker guarantees these only appear inside a loop, so the
|
||||
// stack is non-empty; guard defensively regardless.
|
||||
if len(state.loops) > 0 {
|
||||
target := state.loops[len(state.loops)-1]
|
||||
// Find the target: a labeled `break`/`continue` matches the innermost target
|
||||
// (loop or value block) with that label; an unlabeled one takes the innermost
|
||||
// loop (`continue` and unlabeled targets skip non-loop block targets). The
|
||||
// checker guarantees a match exists; guard defensively regardless.
|
||||
target_index := -1
|
||||
for i := len(state.loops) - 1; i >= 0; i -= 1 {
|
||||
ctx := state.loops[i]
|
||||
if symbol.is_valid(statement.label) {
|
||||
if ctx.label == statement.label && (ctx.is_loop || statement.kind == .Break) {
|
||||
target_index = i
|
||||
break
|
||||
}
|
||||
} else if ctx.is_loop {
|
||||
target_index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if target_index >= 0 {
|
||||
target := state.loops[target_index]
|
||||
label := target.exit_lbl if statement.kind == .Break else target.continue_lbl
|
||||
append_instruction(state, ir.Instruction{
|
||||
op=.Br, span=statement.span, type=types.VOID, integer=label,
|
||||
@@ -763,6 +783,27 @@ lower_statements :: proc(state: ^State, statements: []hir.Stmt_Id) {
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
}
|
||||
case .Block:
|
||||
// A labeled value block: lower its body, then emit the exit label that its
|
||||
// `yield :blk` (a labeled break) branches to. Not a loop, so plain
|
||||
// `break`/`continue` skip it (is_loop=false).
|
||||
exit_lbl := fresh_label(state)
|
||||
append(&state.loops, Loop_Ctx{label=statement.label, exit_lbl=exit_lbl, continue_lbl=exit_lbl, is_loop=false})
|
||||
lower_statements(state, statement.then_body)
|
||||
pop(&state.loops)
|
||||
// Explicit fall-through to the exit label so the preceding block is terminated
|
||||
// (dead code after a terminator gets a fresh recovery block in the emitter),
|
||||
// mirroring the `br` a `while`/`for` emits before its labels.
|
||||
append_instruction(state, ir.Instruction{
|
||||
op=.Br, span=statement.span, type=types.VOID, integer=exit_lbl,
|
||||
target=ir.INVALID_REF, a=ir.INVALID_INSTRUCTION, b=ir.INVALID_INSTRUCTION,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
append_instruction(state, ir.Instruction{
|
||||
op=.Label, span=statement.span, type=types.VOID, integer=exit_lbl,
|
||||
target=ir.INVALID_REF, a=ir.INVALID_INSTRUCTION, b=ir.INVALID_INSTRUCTION,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
case .Expression, .Sink:
|
||||
_ = lower_expr(state, statement.expr)
|
||||
case .Trap:
|
||||
@@ -940,7 +981,7 @@ lower_statements :: proc(state: ^State, statements: []hir.Stmt_Id) {
|
||||
target=ir.INVALID_REF, a=ir.INVALID_INSTRUCTION, b=ir.INVALID_INSTRUCTION,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
append(&state.loops, Loop_Ctx{exit_lbl=exit_lbl, continue_lbl=update_lbl})
|
||||
append(&state.loops, Loop_Ctx{label=statement.label, exit_lbl=exit_lbl, continue_lbl=update_lbl, is_loop=true})
|
||||
lower_statements(state, statement.then_body)
|
||||
pop(&state.loops)
|
||||
append_instruction(state, ir.Instruction{
|
||||
@@ -1072,7 +1113,7 @@ lower_statements :: proc(state: ^State, statements: []hir.Stmt_Id) {
|
||||
// e.g. `for 0..=255 |b: u8| { ... continue }` exits cleanly instead of
|
||||
// overflowing the increment on the final element.
|
||||
continue_lbl := fresh_label(state)
|
||||
append(&state.loops, Loop_Ctx{exit_lbl=exit_lbl, continue_lbl=continue_lbl})
|
||||
append(&state.loops, Loop_Ctx{label=statement.label, exit_lbl=exit_lbl, continue_lbl=continue_lbl, is_loop=true})
|
||||
lower_statements(state, statement.then_body)
|
||||
pop(&state.loops)
|
||||
append_instruction(state, ir.Instruction{
|
||||
@@ -1257,7 +1298,7 @@ lower_statements :: proc(state: ^State, statements: []hir.Stmt_Id) {
|
||||
target=ir.INVALID_REF, a=capture_slot, b=captured,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
append(&state.loops, Loop_Ctx{exit_lbl=exit_lbl, continue_lbl=update_lbl})
|
||||
append(&state.loops, Loop_Ctx{label=statement.label, exit_lbl=exit_lbl, continue_lbl=update_lbl, is_loop=true})
|
||||
lower_statements(state, statement.then_body)
|
||||
pop(&state.loops)
|
||||
append_instruction(state, ir.Instruction{
|
||||
|
||||
@@ -1036,10 +1036,20 @@ parse_yield :: proc(parser: ^Parser) -> ast.Stmt_Id {
|
||||
// checker rejects them outside a loop.
|
||||
parse_loop_control :: proc(parser: ^Parser, kind: ast.Stmt_Kind) -> ast.Stmt_Id {
|
||||
marker := advance(parser) // consume 'break' / 'continue'
|
||||
// `break :outer` / `continue :outer` targets the enclosing loop labeled `outer`.
|
||||
label := symbol.INVALID
|
||||
if _, ok := allow(parser, .Colon); ok {
|
||||
if name, name_ok := allow(parser, .Identifier); name_ok {
|
||||
label = name.symbol
|
||||
} else {
|
||||
source.add(parser.diagnostics, current(parser).span, "expected a loop label after ':'")
|
||||
}
|
||||
}
|
||||
id := ast.stmt_id(len(parser.module.statements))
|
||||
append(&parser.module.statements, ast.Stmt{
|
||||
kind=kind,
|
||||
span=marker.span,
|
||||
label=label,
|
||||
expr=ast.INVALID_EXPR,
|
||||
update=ast.INVALID_STMT,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
@@ -1169,6 +1179,26 @@ parse_statement :: proc(parser: ^Parser) -> ast.Stmt_Id {
|
||||
kind = .Declaration
|
||||
immutable = operator.kind == .Colon_Colon
|
||||
}
|
||||
// A labeled value block (`x :: blk: { … yield :blk v }`): the label lets a
|
||||
// `yield :blk` exit the block past a nested `if`. Block-init body + label.
|
||||
if current(parser).kind == .Identifier && peek(parser).kind == .Colon {
|
||||
label := parse_optional_loop_label(parser)
|
||||
body := parse_block(parser)
|
||||
id := ast.stmt_id(len(parser.module.statements))
|
||||
append(&parser.module.statements, ast.Stmt{
|
||||
kind=kind,
|
||||
span=span_from(name.span, previous(parser).span),
|
||||
name=name.symbol,
|
||||
type=type_syntax,
|
||||
immutable=immutable,
|
||||
label=label,
|
||||
target=ast.INVALID_EXPR,
|
||||
expr=ast.INVALID_EXPR,
|
||||
body=body,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
return id
|
||||
}
|
||||
// A `{` on the right is a value block: parse its statements now; the
|
||||
// checker turns its final `yield` into the declared/assigned value.
|
||||
if current(parser).kind == .Left_Brace {
|
||||
@@ -1227,6 +1257,22 @@ parse_statement :: proc(parser: ^Parser) -> ast.Stmt_Id {
|
||||
expr := parse_expression(parser)
|
||||
if _, ok := allow(parser, .Equal); ok {
|
||||
skip_newlines(parser)
|
||||
// A labeled value block assigned to a complex target (`a[i] = blk: { … }`).
|
||||
if current(parser).kind == .Identifier && peek(parser).kind == .Colon {
|
||||
label := parse_optional_loop_label(parser)
|
||||
body := parse_block(parser)
|
||||
id := ast.stmt_id(len(parser.module.statements))
|
||||
append(&parser.module.statements, ast.Stmt{
|
||||
kind=.Assignment,
|
||||
span=span_from(parser.module.exprs[expr].span, previous(parser).span),
|
||||
target=expr,
|
||||
label=label,
|
||||
expr=ast.INVALID_EXPR,
|
||||
body=body,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
return id
|
||||
}
|
||||
// A value block assigned to a complex target (`a[i] = { ... }`, `p.f = { ... }`).
|
||||
if current(parser).kind == .Left_Brace {
|
||||
brace := current(parser)
|
||||
|
||||
Reference in New Issue
Block a user