Skip to content

Support ALT replicators #45

@associate-1

Description

@associate-1

Description

The parser does not support replicated ALT blocks (ALT i = 0 FOR n), analogous to the already-supported replicated SEQ, PAR, and IF constructs.

Syntax

ALT i = 0 FOR n.inputs
  BYTE ch:
  in[i] ? ch
    SEQ
      out ! ch

This creates n.inputs alternatives, each guarded by in[i] ? ch, and selects whichever channel is ready first.

Affected files

  • kroc/modules/course/examples/sort_pump.occ (line 184)
  • kroc/modules/course/examples/sort_pump_2.occ (line 184)
  • kroc/modules/course/examples/sort_inside.occ (line 213)
  • kroc/modules/course/examples/sort_inside_2.occ (line 224, 294)
  • kroc/modules/course/examples/bar.occ (lines 327, 338, 352)

This is the single most impactful missing feature — it blocks 5 out of 16 example files.

Current error

sort_pump.occ:184: expected indented block after ALT
sort_pump.occ:184: unexpected token: =

The parser sees ALT and expects an indented block of alternatives, but instead finds the replicator syntax i = 0 FOR n.

Proposed Go mapping

// ALT i = 0 FOR n  →  select over dynamically built cases
// Requires reflect.Select for runtime-variable number of cases

Since Go's select requires statically known cases, the replicated ALT will need reflect.Select to build the case list dynamically at runtime:

cases := make([]reflect.SelectCase, n)
for i := 0; i < n; i++ {
    cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(in[i])}
}
chosen, value, _ := reflect.Select(cases)
ch := byte(value.Int())
// ... body using chosen as i

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions