Skip to content

Allow any expression as if/else branch - #119

Merged
vito merged 4 commits into
mainfrom
if-else-term-operands
Jun 12, 2026
Merged

vito merged 4 commits into
mainfrom
if-else-term-operands

Conversation

@vito

@vito vito commented Jun 12, 2026 •

Copy link
Copy Markdown
Owner

What

Generalizes if/else so each branch is any expression, not a mandatory Block:

let status = if (active) "on" else "off"
let bigger = if (x > y) x + 1 else y + 1
let grade  = if (x > 100) "high" else if (x > 5) "medium" else "low"

Why

Braces were never really part of the if syntax — they're how Dang groups multiple expressions into one. This change makes the grammar say what was always true under the hood: if (x) { ... } else { ... } is just if/else applied to two block expressions. Everything already an expression in Dang composes the same way; conditionals now do too.

It also deletes special cases rather than adding one: else if used to be hand-wrapped in a synthetic single-form Block by the grammar. Now an else-if chain is literally an else branch whose expression is another if — the grammar, the AST, the formatter, and divergence analysis all got simpler.

How

  • Grammar (dang.peg): the branch production is just Form. No special brace handling is needed: map literals are [k: v] and object literals are {{ }}, so a single { in expression position can only open a block — every existing program parses identically, and braces in a branch mean exactly what they mean in any other expression position (including {{a: 1}} as a record literal branch).
  • AST: Conditional.Then/.Else are plain Nodes (previously *Block / any with *Block assertions sprinkled through infer/eval/walk/divergence).
  • Formatter: prints non-block branches inline; still collapses else { if ... } into an else if chain.
  • Tree-sitter: the grammar is derived from the PEG so it picked the change up automatically, modulo the classic dangling-else GLR ambiguity — resolved with prec.right on conditional, so else binds to the nearest if, matching the PEG's greedy parse.
  • Docs: control-flow and blocks pages now tell this story.

Notes

  • Dangling else: if (a) if (b) x else y binds the else to the inner if (PEG ordered choice and tree-sitter prec.right agree). Covered by a corpus test.
  • Since whitespace spans newlines, a brace-less body may sit on the following line (if (cond)\n foo()). Same newline-greediness the infix operator rules already have.
  • New tests/test_conditional_expr_operands.dang covers bare operands, operator expressions, brace-less else-if chains, mixed block/non-block branches, record-literal branches, nested conditionals, and guard-clause narrowing through a bare diverging branch; formatter round-trip is exercised by the existing TestFormatLanguage suite.

🤖 Generated with Claude Code

vito added 2 commits June 12, 2026 16:44
Generalize the Conditional grammar so each branch is any Form rather
than requiring a Block. Braces stop being part of the if/else syntax:
`if (x) { a } else { b }` is now just if/else applied to two block
expressions, and `if (x) "yes" else "no"` works directly. Block is
still tried first when parsing a branch so brace-delimited bodies keep
parsing as grouping blocks rather than map/object literals.

Conditional.Then/Else become plain Nodes, which also removes the
synthetic single-form Block that `else if` used to be wrapped in —
an else-if chain is now literally an else branch whose expression is
another if. Inference, eval, divergence analysis, and the formatter
operate on the nodes directly; the formatter prints non-block branches
inline and still collapses `else { if ... }` into a chain.

The tree-sitter grammar is derived from the PEG, so it picks the new
shape up automatically, but the generalized form introduces the
classic dangling-else ambiguity in GLR; resolve it by patching the
conditional rule with right associativity so `else` binds to the
nearest `if`, matching the PEG's greedy parse.

Signed-off-by: Alex Suraci <[email protected]>
Update the control-flow and blocks pages (and the dang-language skill
reference) to reflect the generalized conditional grammar: branches
are arbitrary expressions, a braced branch is just a block grouping
several expressions into one, and `else if` is nothing more than an
else branch whose expression is another if.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0f319dd
Status: ✅  Deploy successful!
Preview URL: https://9bec52d3.dang-3kk.pages.dev
Branch Preview URL: https://if-else-term-operands.dang-3kk.pages.dev

View logs

vito added 2 commits June 12, 2026 17:08
The Block-first ordering was defending against a brace ambiguity that
does not exist: map literals are square-bracketed ([k: v]) and object
literals are double-braced ({{ }}), so a single brace in expression
position can only ever open a block. Worse, preferring Block made an
if branch the one place where {{}} parsed as a nested empty block
instead of an empty object literal. A branch is now literally a Form,
so braces mean exactly what they mean in any other expression
position.

Signed-off-by: Alex Suraci <[email protected]>
hack/generate also compiles the grammar to wasm for the docs
playground; regenerate it (via dagger generate) so it tracks the
generalized if/else branch productions.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 598ba62 into main Jun 12, 2026
8 checks passed
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