Repository navigation
feat(expr): add a deterministic expression language for content - #2709
Merged
Merged
Conversation
houko
force-pushed
the
feat/expr-package-1364
branch
from
December 1, 2026 01:20
fcddc2b to
a986683
Compare
The only evaluator was private to dialogue and knew two functions, and everywhere else conditions were names or fixed numbers, so a computed formula or balance rule meant Go code that iOS and the web cannot load at run time #1364. pkg/expr parses and type-checks numbers, bools, strings, named numeric variables, typed game functions and the builtins floor, round, abs, min, max and clamp; ?:, ||, &&, comparisons and arithmetic; with short-circuit evaluation, columns on every parse error, MaxNodes/MaxDepth cost limits and a determinism contract. Tests cover the operators, variables, functions, parse and eval errors, the limits, concurrent evaluation and a Parse fuzzer.
houko
force-pushed
the
feat/expr-package-1364
branch
from
December 1, 2026 03:25
a986683 to
c79aa9e
Compare
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.
The only expression evaluator was private to dialogue (
pkg/dialogue/expr.go): Yarn<<if>>knew onlyvisitedandvisited_count, and a game could not addhas_item("key"). Elsewhere a condition was a name with an optional!, or a fixed number in a data table row, so a computed damage formula or balance rule meant Go code — which iOS and the web cannot load at run time.This adds
pkg/expr, the shared evaluator the rest of #1364 will build on.What changed
float64), bools and strings;?:,||,&&(short-circuiting),==/!=, numeric comparisons,+ - * / %, unary-and!, grouping, string literals with escapes,true/false.Params []Kind,Result Kind) and come from a parse-time map orEnv.Funcs. Builtinsfloor,round(half away from zero),abs,min,maxandclampcannot be replaced.1 + true,!1, mixed?:branches, string comparison of different kinds and a wrong arity are*expr.ParseErrorvalues with a column, never a runtime failure.math.Mod,math.Floorandmath.Round; division and remainder by zero areErrDivisionByZerorather than infinities, andclampwith a low bound above its high bound fails.MaxNodes(4096) andMaxDepth(128) at parse time; no loops, assignment or I/O, so the node count bounds evaluation too.Parse(src, names, funcs),Expr.Eval(env),Expr.Number/Bool/String,Expr.NodeCount,Env.Vars/Env.Funcs, andErrUnknownVariable,ErrUnknownFunction,ErrDivisionByZero,ErrCostLimit.Tests
Arithmetic and precedence including unary and ternary, variables and custom functions, bool and string results, ten parse-error cases each asserting a column and message, evaluation errors (missing variable, missing function, a failing function's error, division by zero, an inverted clamp), the node and depth limits, deterministic results from eight concurrent evaluations of one
Expr, and aParsefuzzer that requires a*ParseErrororErrCostLimitand re-evaluates whatever parses.go test ./pkg/exprpasses;go tool nsgo check,./internal/repocheck,bun run verify-notesandgo run ./tools/changelog checkpass.Docs, changelog, notes
docs/en/expr.mdanddocs/zh/expr.mddocument the language, the operator table, the determinism contract and the limits; the package is registered in the README table, both docs indexes, both stability tables, the agent skill's package list and.github/labeler.yml.changelog.d/1364-expr.mdand.agents/notes/implemented/feature/2026-11-30-expr-1364.mdrecord the decision and the alternatives.No visual change: no screenshot.
What remains of #1364 is switching dialogue's evaluator to
pkg/exprwithvisited/visited_countandDialogueOptions.Funcs,debug.Console'seval, and thedatatableexpression column; this PR is the package they depend on.Part of #1364.