Skip to content

Refactor built-in function dispatch to enum switch#506

Open
bertysentry wants to merge 1 commit into
mainfrom
505-refactor-builtin-function-dispatch
Open

Refactor built-in function dispatch to enum switch#506
bertysentry wants to merge 1 commit into
mainfrom
505-refactor-builtin-function-dispatch

Conversation

@bertysentry

Copy link
Copy Markdown
Contributor

Fixes #505

What

Replaces the ~21-branch if/else-if chain in BuiltinFunctionCallAst.populateTuples() (AwkParser.java) — which compared a stored int index against repeated BUILTIN_FUNC_NAMES.get(...) map lookups by string literal — with a table-driven, enum-based dispatch:

  • New BuiltinFunction enum with one constant per AWK built-in (ATAN2(atan2) ... TOUPPER(toupper)), each carrying its AWK source-level name, plus a BY_NAME map and BuiltinFunction.of(String) resolver. It replaces the Map<String, Integer> of synthetic token values and the fIdx = 257 counter; those integer values were never used as values, only for name membership (lexer) and identity (dispatch).
  • Lexer now checks BuiltinFunction.of(text) != null instead of probing the map.
  • BuiltinFunctionCallAst resolves its enum constant once in the constructor; populateTuples() is a flat switch (builtin). Duplicated arity validation is factored into two shared helpers (populateArgumentsTuples for the fixed-arity math/string functions, populateOneArgumentTuples for close/tolower/toupper/system), and the structurally unique builtins (sprintf, length, srand, sub/gsub, split, substr) each get their own named, Javadoc''d private method. The unreachable NotImplementedError fallback is preserved as the default case.

Behavior

No functional change intended. All error messages, argument-count checks, and tuple-emission order are preserved exactly, including existing quirks (e.g. sub''s error messages being reused for gsub).

Verification

  • mvn formatter:format: no changes needed.
  • mvn verify run on the pristine tree (baseline) and with this change: both pass, and a per-test comparison of target/failsafe-reports shows zero status changes across all 939 gawk/BWK/POSIX compatibility tests (453 passed / 253 skipped / 111 failed / 122 errors in both runs — the failures are pre-existing compatibility gaps, unchanged test-by-test).
  • checkstyle, pmd, and spotbugs reports: 0 findings.

🤖 Generated with Claude Code

Replace the ~21-branch if/else-if chain in
BuiltinFunctionCallAst.populateTuples(), which compared a stored int
against repeated BUILTIN_FUNC_NAMES map lookups, with a BuiltinFunction
enum resolved once in the constructor and dispatched via switch.

The BUILTIN_FUNC_NAMES map of synthetic integer token values is removed:
its values were never used, only name membership (lexer) and identity
(dispatch), both now covered by the enum. Duplicated arity validation is
factored into shared helpers; structurally unique builtins (sprintf,
length, srand, sub/gsub, split, substr) get their own named methods.

Behavior and error messages are preserved exactly: mvn verify shows zero
per-test status changes across the 939 gawk/BWK/POSIX compatibility
tests, and checkstyle/pmd/spotbugs report no findings.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

Refactor built-in function dispatch in AwkParser to be table-driven

1 participant