Skip to content

feat(expr): add a deterministic expression language for content - #2709

Merged
houko merged 1 commit into
mainfrom
feat/expr-package-1364
Dec 1, 2026
Merged

houko merged 1 commit into
mainfrom
feat/expr-package-1364

Conversation

@houko

@houko houko commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

The only expression evaluator was private to dialogue (pkg/dialogue/expr.go): Yarn <<if>> knew only visited and visited_count, and a game could not add has_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

  • Language: numbers (float64), bools and strings; ?:, ||, && (short-circuiting), ==/!=, numeric comparisons, + - * / %, unary - and !, grouping, string literals with escapes, true/false.
  • Inputs: variables are named at parse time and are numbers; functions are typed (Params []Kind, Result Kind) and come from a parse-time map or Env.Funcs. Builtins floor, round (half away from zero), abs, min, max and clamp cannot be replaced.
  • Type checking at parse: each node carries its kind while the tree is built, so 1 + true, !1, mixed ?: branches, string comparison of different kinds and a wrong arity are *expr.ParseError values with a column, never a runtime failure.
  • Determinism: one IEEE-754 operation per node in source order, no fused multiply-add or reassociation; rounding cases come from math.Mod, math.Floor and math.Round; division and remainder by zero are ErrDivisionByZero rather than infinities, and clamp with a low bound above its high bound fails.
  • Limits: MaxNodes (4096) and MaxDepth (128) at parse time; no loops, assignment or I/O, so the node count bounds evaluation too.
  • API: Parse(src, names, funcs), Expr.Eval(env), Expr.Number/Bool/String, Expr.NodeCount, Env.Vars/Env.Funcs, and ErrUnknownVariable, 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 a Parse fuzzer that requires a *ParseError or ErrCostLimit and re-evaluates whatever parses. go test ./pkg/expr passes; go tool nsgo check, ./internal/repocheck, bun run verify-notes and go run ./tools/changelog check pass.

Docs, changelog, notes

docs/en/expr.md and docs/zh/expr.md document 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.md and .agents/notes/implemented/feature/2026-11-30-expr-1364.md record the decision and the alternatives.

No visual change: no screenshot.

What remains of #1364 is switching dialogue's evaluator to pkg/expr with visited/visited_count and DialogueOptions.Funcs, debug.Console's eval, and the datatable expression column; this PR is the package they depend on.

Part of #1364.

@github-actions github-actions Bot added enhancement New feature or request documentation Improvements or additions to documentation area/ci area/agents area/skill size/XL labels Nov 30, 2026
@houko
houko force-pushed the feat/expr-package-1364 branch from fcddc2b to a986683 Compare December 1, 2026 01:20
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
houko force-pushed the feat/expr-package-1364 branch from a986683 to c79aa9e Compare December 1, 2026 03:25
@houko
houko merged commit adbe2f5 into main Dec 1, 2026
140 of 142 checks passed
@houko
houko deleted the feat/expr-package-1364 branch December 1, 2026 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agents area/ci area/skill documentation Improvements or additions to documentation enhancement New feature or request size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant