Skip to content

Add optional gawk-style arrays of arrays (a[i][j])#439

Merged
bertysentry merged 6 commits intomainfrom
438-add-optional-gawk-style-arrays-of-arrays
Apr 18, 2026
Merged

Add optional gawk-style arrays of arrays (a[i][j])#439
bertysentry merged 6 commits intomainfrom
438-add-optional-gawk-style-arrays-of-arrays

Conversation

@bertysentry
Copy link
Copy Markdown
Contributor

Closes #438

Summary

This PR adds support for optional gawk-style arrays of arrays (a[i][j]) as described in issue #438.

Changes

AwkSettings

  • Added allowArraysOfArrays field (default: true) to enable/disable chained-bracket syntax at compile time.

Awk

  • Thread allowArraysOfArrays setting into AwkParser via compile() and compileExpression().

AwkParser

  • When allowArraysOfArrays is enabled, parse chained-bracket expressions (a[i][j], a[i][j][k], mixed forms like a[i][j,k]) instead of throwing an error.
  • Comma subscripts inside a single bracket pair still use SUBSEP (unchanged behavior).
  • When disabled, the existing parser error is preserved.

Opcode / AwkTuples

  • Introduced new opcodes for nested array lvalue operations to support reads, writes, in, for-in, delete, ++/--, and compound assignment on nested arrays.

AVM

  • Extended the runtime to execute nested-array tuples, building on the existing generic Map-based infrastructure.

Behavior

Expression Behavior
a[1][2] = 42; print a[1][2] Prints 42
a[1][2,3] = 42; print a[1][2,3] Inner comma uses SUBSEP
a[1,2] = 42; print a[1,2] Unchanged
2 in a[1] Membership test on subarray
for (k in a[1]) Iterates subarray
delete a[1][2] Removes nested element
allowArraysOfArrays = false + a[1][2] Parser error

Tests

  • Added regression tests in AwkTest covering the acceptance criteria from the issue.
  • Added parser-rejection tests in AwkParserTest for when the flag is disabled.

Documentation

  • Updated README.md, compatibility.md, java.md, and java-compile.md.

- Add allowArraysOfArrays to AwkSettings (default: true)
- Thread the flag into Awk.compile() and Awk.compileExpression()
- Update AwkParser to parse chained-bracket syntax a[i][j] when enabled
- Introduce new opcodes for nested array lvalue operations
- Extend AVM to execute nested-array tuples at runtime
- Cover reads, writes, in, for-in, delete, ++/--, compound assignment
- Add regression tests in AwkTest and AwkParserTest
- Update documentation (README, compatibility.md, java.md, java-compile.md)

Closes #438
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds optional (default-enabled) support for gawk-style “arrays of arrays” syntax (a[i][j]) across the Jawk compiler and runtime, including parser acceptance, new tuple opcodes, and AVM execution, with accompanying regression and documentation updates.

Changes:

  • Introduces AwkSettings.allowArraysOfArrays and threads it into compilation so the parser can accept/reject chained-bracket syntax.
  • Extends AwkParser, tuples/opcodes, and AVM to support nested-array reads/writes and array-valued operations on subarrays.
  • Expands test coverage (including parser-rejection when disabled) and updates site docs; enables additional POSIX conformance tests by implementing length(array).

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/main/java/io/jawk/util/AwkSettings.java Adds allowArraysOfArrays compile-time flag and exposes it via API/diagnostics.
src/main/java/io/jawk/Awk.java Passes allowArraysOfArrays into AwkParser during compilation/expression compilation.
src/main/java/io/jawk/frontend/AwkParser.java Parses chained brackets when enabled; updates tuple generation to support subarrays in in, for-in, delete, assignment, built-ins, and function params.
src/main/java/io/jawk/intermediate/Opcode.java Adds new opcodes to operate on stack-provided map elements and nested-array creation.
src/main/java/io/jawk/intermediate/AwkTuples.java Adds tuple emission helpers for the new map-element opcodes.
src/main/java/io/jawk/backend/AVM.java Implements execution for new opcodes; adds length(array) behavior and scalar/array context checks.
src/test/java/io/jawk/AwkTest.java Adds behavior-based regression tests covering nested array semantics and key runtime paths.
src/test/java/io/jawk/AwkParserTest.java Adds test ensuring chained-bracket syntax is rejected when allowArraysOfArrays is disabled.
src/test/java/io/jawk/PosixConformanceTest.java Enables POSIX tests that rely on length(array) by removing prior assumptions.
src/site/markdown/java.md Documents the new AwkSettings.setAllowArraysOfArrays(...) option and default behavior.
src/site/markdown/java-compile.md Documents that compilation settings affect accepted syntax; shows disabling arrays-of-arrays before compiling.
src/site/markdown/compatibility.md Notes support for arrays-of-arrays alongside classic SUBSEP multidimensional arrays.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/main/java/io/jawk/intermediate/Opcode.java Outdated
Comment thread src/main/java/io/jawk/frontend/AwkParser.java Outdated
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

getAst1().populateTuples(tuples);
// get the index
getAst2().populateTuples(tuples);
tuples.dereferenceArray();

P2 Badge Autovivify missing parent arrays on nested reads

This rvalue path dereferences nested arrays via getAst1().populateTuples()/dereferenceArray() without creating an intermediate subarray, so print a[1][2] on an unset a[1] ends up dereferencing a blank scalar as an array at runtime. That is inconsistent with the new gawk-style arrays-of-arrays behavior (and with this patch’s write-side autovivification), causing nested reads on previously unset parents to fail unexpectedly.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/main/java/io/jawk/frontend/AwkParser.java
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/test/java/io/jawk/AwkTest.java Outdated
Comment thread src/main/java/io/jawk/backend/AVM.java
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/test/java/io/jawk/AwkTest.java Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/main/java/io/jawk/frontend/AwkParser.java Outdated
Comment thread src/main/java/io/jawk/frontend/AwkParser.java Outdated
Comment thread src/main/java/io/jawk/frontend/AwkParser.java Outdated
Comment thread src/main/java/io/jawk/frontend/AwkParser.java Outdated
Comment thread src/main/java/io/jawk/intermediate/Opcode.java Outdated
Comment thread src/main/java/io/jawk/frontend/AwkParser.java
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/main/java/io/jawk/intermediate/Opcode.java
@bertysentry bertysentry merged commit 66be7a5 into main Apr 18, 2026
9 checks passed
@bertysentry bertysentry deleted the 438-add-optional-gawk-style-arrays-of-arrays branch April 18, 2026 19:18
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.

Add optional gawk-style arrays of arrays (a[i][j])

2 participants