Skip to content

Add support for static inner classes - #107

Open
kjw142857 wants to merge 63 commits into
mainfrom
inner-classes
Open

kjw142857 wants to merge 63 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>
kjw142857 and others added 18 commits September 2, 2026 12:03
Resolved conflicts:
- src/compiler/import/lib-info.ts: kept the fuller java.lang exception
  class list from std-imports (superset of main's addition)
- src/jvm/exception-table.ts, src/compiler/code-generator.ts,
  src/compiler/__tests__/try.test.ts: took main's reviewed exception-
  handling implementation (PR #96), which supersedes the equivalent
  commits carried individually on std-imports
- src/types/typeFactories/methodFactory.ts: took main's cleaner
  optional-chaining form
- src/jvm/types/class/Method.ts: dropped duplicate ExceptionTable import

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Brings in the standard-library import work (class-file metadata extraction,
non-hardcoded import verification, generated symbol/type-checker libraries,
primitive autoboxing/unboxing) alongside the enum + switch-statement work.

Resolved conflict:
- src/types/checker/environment.ts: took std-imports' data-driven
  buildStandardLibraryTypes() (which subsumes the hard-coded System /
  PrintStream / Throwable / Exception stubs) and re-added the enum base
  type as a BUILT_IN_TYPE_FACTORIES entry so prechecks.ts can still
  resolve 'Enum'.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
`compile()` compiled member enums before the classes that reference them
(needed so their synthetic members are in the symbol table first), and
pushed the resulting class files in that same order. A program with a
member enum therefore returned `[Day, Main]` instead of `[Main]`, so a
runner that treats element 0 as the entry point executed the enum class
(no `main`) and produced no output.

Keep the enum-first compilation order, but collect the results and return
them in source-declaration order (top-level types first, member enums
appended) so the entry class is always at index 0.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Previously enum class files extended java/lang/Object with a hand-rolled
$ordinal field and a bytecode ordinal() method; the constant name passed
to the constructor was discarded, so name()/toString() never worked.

Now:
- enum classes declare super_class java/lang/Enum (+ ACC_ENUM)
- the synthetic <init>(String,int) chains to Enum.<init>(String,int)
- $ordinal and the hand-written ordinal() are removed; name(), ordinal(),
  toString(), compareTo() are inherited, and registered in the symbol
  table so Source programs can call them
- the type checker's 'Enum' base type carries those methods too

values(), valueOf(String) and <clinit> are unchanged (they are the enum's
own synthetic members in real javac as well; valueOf already delegated to
java.lang.Enum.valueOf).

Verified on the reference JVM: ordinal(), name(), toString(), switch and
valueOf() all behave like javac output. Running an enum program in the
java-slang JVM now requires java/lang/Enum.class in its class bundle
(links with Object/Comparable/Serializable/Constable).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
# 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
70.02% (-0.6% 🔻)
8286/11833
🔴 Branches
58.47% (-0.72% 🔻)
2765/4729
🟡 Functions
68.98% (+0.08% 🔼)
1472/2134
🟡 Lines
71.08% (-0.48% 🔻)
7797/10969
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
🟡
... / index.ts
62.19% (-5.63% 🔻)
45.19% (-3.06% 🔻)
67.57% (-9.85% 🔻)
69.4% (-6.34% 🔻)
🟡
... / classes.ts
62.26% (-3.33% 🔻)
39.13% (-8.24% 🔻)
45.71% (+0.26% 🔼)
67.42% (-1.33% 🔻)
🟢
... / generated-lib-info.ts
100%
75% (-25% 🔻)
100% 100%
🔴
... / prechecks.ts
50.2% (-9.92% 🔻)
37.4% (-11.32% 🔻)
88.24% (-5.1% 🔻)
57.14% (-10.03% 🔻)
🟢 compiler/index.ts 100%
75% (-25% 🔻)
100% 100%
🟢
... / compiler.ts
98.91% (+0.21% 🔼)
68.75% (-4.42% 🔻)
100%
98.84% (+0.23% 🔼)
🟡
... / code-generator.ts
78.27% (-0.22% 🔻)
69.51% (+0.05% 🔼)
79.17%
78.64% (-0.23% 🔻)
🟡
... / binary-writer.ts
77.36% (-3.89% 🔻)
38.46% (-11.54% 🔻)
70.59% (+0.89% 🔼)
78.32% (-4.62% 🔻)

Test suite run success

1200 tests passing in 71 suites.

Report generated by 🧪jest coverage report action from c5e47ea

@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

This branch has not been deployed

No deployments
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