Repository navigation
Allow any expression as if/else branch - #119
Merged
Merged
Conversation
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]>
Deploying dang with
|
| 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 |
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Generalizes
if/elseso each branch is any expression, not a mandatoryBlock:Why
Braces were never really part of the
ifsyntax — 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 justif/elseapplied 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 ifused to be hand-wrapped in a synthetic single-formBlockby the grammar. Now an else-if chain is literally anelsebranch whose expression is anotherif— the grammar, the AST, the formatter, and divergence analysis all got simpler.How
dang.peg): the branch production is justForm. 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).Conditional.Then/.Elseare plainNodes (previously*Block/anywith*Blockassertions sprinkled through infer/eval/walk/divergence).else { if ... }into anelse ifchain.prec.rightonconditional, soelsebinds to the nearestif, matching the PEG's greedy parse.Notes
if (a) if (b) x else ybinds theelseto the innerif(PEG ordered choice and tree-sitterprec.rightagree). Covered by a corpus test.if (cond)\n foo()). Same newline-greediness the infix operator rules already have.tests/test_conditional_expr_operands.dangcovers 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 existingTestFormatLanguagesuite.🤖 Generated with Claude Code