Skip to content

Add Go-style switch-case statement#478

Closed
ganehag wants to merge 2 commits into
d5:masterfrom
ganehag:master
Closed

Add Go-style switch-case statement#478
ganehag wants to merge 2 commits into
d5:masterfrom
ganehag:master

Conversation

@ganehag

@ganehag ganehag commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Implements switch/case/default/fallthrough/break with multi-value cases, tagless switch (acts as if/else chain), and optional init statement. This solves #57 from way back in 2019.

ganehag added 2 commits April 28, 2026 19:33
Implements switch/case/default/fallthrough/break with multi-value cases,
tagless switch (acts as if/else chain), and optional init statement.

- token: add Switch, Case, Default, Fallthrough keywords
- parser: add SwitchStmt and CaseClause AST nodes, parseSwitchStmt/parseCaseClause
- parser: fix expectSemi to accept case/default as implicit terminators
- parser: add OpDup opcode (duplicates stack top for tag comparisons)
- compiler: implement compileSwitchStmt with two-entry-point body pattern
  for correct stack discipline on fallthrough
- compiler: update loop struct and branch handling so break/continue/fallthrough
  correctly target switch vs loop contexts
- vm: add OpDup handler
freeze(x) recursively converts Arrays to ImmutableArrays and Maps to
ImmutableMaps. Already-immutable values with no mutable children are
returned as the same pointer (zero allocation). A memo table prevents
infinite recursion on self-referential structures.

Primitives, strings, bytes, functions and errors pass through unchanged.

Closes the planned item from docs/freeze-builtin.md.
@ganehag ganehag closed this Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant