Skip to content

Add support for static inner classes - #107

Merged
martin-henz merged 75 commits into
mainfrom
inner-classes
Oct 27, 2026
Merged

martin-henz merged 75 commits into
mainfrom
inner-classes

Conversation

@kjw142857

Copy link
Copy Markdown
Contributor

This PR adds support for static inner classes within java-slang.

Note: support for non-static inner classes is not supported yet due to a framework being needed to resolve name collisions. This can be potentially developed in the future.

kjw142857 and others added 30 commits June 10, 2026 09:55
…ntain permissions'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
- Updated grammar.pegjs and grammar.ts to add EnumDeclaration parsing
- Added TopLevelClassOrInterfaceDeclaration and ClassMemberDeclaration alternatives for EnumDeclaration
- Added EnumDeclaration, EnumBody, EnumConstantList, and EnumConstant parsing rules
- Created src/compiler/__tests__/tests/enum.test.ts with 3 enum test cases
- Updated src/compiler/__tests__/index.ts to import and run enum tests

Remaining work:
- Run enum compiler tests to verify parsing works
- Implement enum code generation in compiler.ts (enum initialization, synthetic methods)
- Run full test suite to validate no regressions
- Verify enum runtime behavior (ordinal(), name(), values(), valueOf())

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Updated grammar (grammar.pegjs and grammar.ts) to parse enum declarations
  - EnumDeclaration, EnumBody, EnumConstantList, EnumConstant rules
  - Support for optional semicolon after constants and enum body members

- Extended AST types (src/ast/types/classes.ts)
  - Added EnumDeclaration, EnumBody, EnumConstant interfaces
  - Updated ClassDeclaration union to include EnumDeclaration
  - Updated ClassBodyDeclaration to include EnumDeclaration

- Added EnumDeclaration to NodeMap (src/ast/types/ast.ts)

- Updated compiler to handle enum declarations
  - Added compileEnum() method in src/compiler/compiler.ts
  - Updated compile() to route EnumDeclaration through compileEnum()
  - Fixed type signatures to handle both ClassDeclaration and EnumDeclaration
  - Set enum parent to java/lang/Enum and ACC_ENUM flag

- Updated ast-extractor.ts and ec-evaluator/utils.ts to accept ClassDeclaration[]
  - Updated searchMainMtdClass() to filter out enums

- Created src/compiler/__tests__/tests/enum.test.ts with 3 test cases
  - enum switch and synthetic methods
  - enum values returns cloned array
  - enum constructors and instance fields

Status: Enums parse and compile, but synthetic methods not yet implemented.
Tests failing because ordinal(), name(), values(), valueOf() missing.

Next: Implement synthetic enum method generation in compiler.ts

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Added enumOrdinals Map to track enum constant ordinals
- Registered ordinal(), name(), toString(), values(), valueOf() in symbol table
- Fixed FieldInfo insertion to remove invalid 'ordinal' property
- Fixed generateSimpleEnumMethod to use indexFieldrefInfo()

Status: Compiler builds but enum compiler tests fail with:
  1. Switch statement codegen doesn't recognize enum types
  2. Bytecode generation may have structural issues

Next: Fix enum type detection in switch codegen, then debug bytecode generation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* jvm changes

* include try/catch/finally support in code generator

* fix try statement logic

* add parser and type checker integration

* fix finally bug

* add tests and fix syntax error

* Patch grammar logic for throws keyword

* Revert "Patch grammar logic for throws keyword"

This reverts commit 8e933b6.

* Patch grammar logic for throws keyword

* Add fix for execption table finally logic

* Add more tests

* fix finally bug and missing test imports

---------

Co-authored-by: Martin Henz <henz@comp.nus.edu.sg>
# Conflicts:
#	src/ast/types/classes.ts
#	src/compiler/__tests__/classOrdering.test.ts
#	src/compiler/__tests__/index.ts
#	src/compiler/compiler.ts
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Coverage report

St.❔
Category Percentage Covered / Total
🟡 Statements
71.8% (+0.29% 🔼)
8590/11964
🟡 Branches
60.32% (+0.45% 🔼)
2878/4771
🟡 Functions
70.32% (+0.35% 🔼)
1528/2173
🟡 Lines
72.71% (+0.31% 🔼)
8069/11097
Show new covered files 🐣
St.❔
File Statements Branches Functions Lines
🟢
... / nestedClasses.test.ts
100% 100% 100% 100%
Show files with reduced coverage 🔻
St.❔
File Statements Branches Functions Lines
🟡
... / classes.ts
64.15% (-2.52% 🔻)
47.83% (-4.81% 🔻)
45.71% (+0.26% 🔼)
67.42% (-1.33% 🔻)
🟡
... / code-generator.ts
78.27% (-0.22% 🔻)
69.51% (+0.05% 🔼)
79.17%
78.64% (-0.23% 🔻)

Test suite run success

1271 tests passing in 76 suites.

Report generated by 🧪jest coverage report action from ecb8a2f

@rmz-oz rmz-oz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The new compilationOrder drops the enums-first ordering from #99, so a top-level enum declared after the class that uses it stops compiling. class Main { ... Day d = Day.MONDAY; ... } enum Day { SUNDAY, MONDAY } works on main but on this branch fails with Unsupported class type conversion: Ljava/lang/Class; -> LDay;. Putting all enums first, then member classes, then top-level classes fixed it for me and the full jest suite still passed (with a JDK on PATH).

Also, two outer classes that each have a static class Node throw SymbolRedeclarationError: "B$Node" has already been declared, since the simple name Node is registered at the root for both.

@kjw142857

Copy link
Copy Markdown
Contributor Author

The new compilationOrder drops the enums-first ordering from #99, so a top-level enum declared after the class that uses it stops compiling. class Main { ... Day d = Day.MONDAY; ... } enum Day { SUNDAY, MONDAY } works on main but on this branch fails with Unsupported class type conversion: Ljava/lang/Class; -> LDay;. Putting all enums first, then member classes, then top-level classes fixed it for me and the full jest suite still passed (with a JDK on PATH).

Also, two outer classes that each have a static class Node throw SymbolRedeclarationError: "B$Node" has already been declared, since the simple name Node is registered at the root for both.

Thanks for the feedback! I have updated such that it now uses a two-pass system to check references, so as long as they are in the same scope the code will compile.

The duplicate naming throwing an error is known and we are planning to fix it in the future when multiclass/multifile support is implemented

@rmz-oz

rmz-oz commented Oct 10, 2026

Copy link
Copy Markdown

the enum case passes on c5e47ea now. one more I ran into: Helper.Inner.f() or new Helper.Inner() called from Main fails with cannot resolve symbol "Inner" in "Helper.Inner.f" / SymbolNotFoundError, whether Helper is declared above or below Main, while Inner.f() inside the outer class works fine. looks like querySymbol only looks up the middle tokens as FIELD, so a nested class name in the middle never matches. if that's part of the multiclass work you mentioned, feel free to ignore.

# Conflicts:
#	package.json
#	src/conductor/JavaEvaluator.ts
#	src/conductor/__tests__/JavaEvaluator.test.ts
#	yarn.lock
# Conflicts:
#	src/compiler/import/lib-closure.ts
#	src/conductor/JavaEvaluator.ts
#	src/conductor/__tests__/JavaEvaluator.test.ts
#	src/jvm/utils/build.ts
#	src/jvm/utils/stdlib-classfiles.ts
#	src/types/checker/prechecks.ts
@martin-henz

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T09:31:06.850925Z 9d126a8 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9d126a8ad3

ℹ️ 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/compiler/code-generator.ts Resolved
Comment thread src/compiler/compiler.ts Resolved
Comment thread src/compiler/compiler.ts Resolved

@martin-henz martin-henz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@martin-henz
martin-henz merged commit 4d56c7b into main Oct 27, 2026
5 checks passed
@martin-henz
martin-henz deleted the inner-classes branch October 27, 2026 12:55
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.

3 participants