Add eager if(cond, t, f) builtin#20
Merged
Merged
Conversation
What: - Register `if(cond, t, f)` in Builtins(). Truthy condition picks t; falsey picks f. Both branches always evaluate. - Rewrite the Go `if` keyword token to a sentinel identifier in preprocessSource so the parser accepts the call. The rewrite uses the same scheme already in place for `map`; the rewrite list is factored out so future additions stay symmetric. - Translate the sentinel back via displayIdent in evalIdent, resolveCallable, and prewalk so user-visible env entries, registered shadows, error messages, and the prepared-func cache all key on "if". - Update templates.md and higher-order-patterns.md to use `if(cond, t, f)` instead of the brittle `cond && t || f` idiom. Update spec.md and llms.txt to document the new builtin. Why: - `if(cond, t, f)` reads cleanly even when both branches are non-nil values, where `cond && t || f` silently picks `f` whenever `t` happens to be falsey. - Consolidating the keyword-rewrite plumbing keeps the next addition (`?.` / `?[` in step 7) easy and consistent.
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
if(cond, t, f)as an eager builtin. Truthy condition returnst, falsey returnsf. Both branches always evaluate.ifkeyword token to a sentinel before parsing, mirroring the existingmaprewrite. The keyword-rewrite list is factored out so future additions stay symmetric.displayIdentinevalIdent,resolveCallable, andprewalkso env entries, registered shadows, error messages, and the prepared-func cache all key on the user-visible name"if".templates.mdandhigher-order-patterns.mdto useif(cond, t, f)instead of the brittlecond && t || fidiom. Updatespec.mdandllms.txtto document the new builtin.Why
if(cond, t, f)reads cleanly even when both branches are non-nil values, wherecond && t || fsilently picksfwheneverthappens to be falsey. Consolidating the keyword-rewrite plumbing also keeps the next addition (?./?[in step 7) easy and consistent.Test plan
go test ./...go test -race ./...go test -run=^$ -fuzz=FuzzCompile -fuzztime=20s .go test -run=^$ -fuzz=FuzzEval -fuzztime=20s .