Skip to content

Implement type casting#89

Merged
martin-henz merged 5 commits into
mainfrom
type-casting
Apr 1, 2026
Merged

Implement type casting#89
martin-henz merged 5 commits into
mainfrom
type-casting

Conversation

@kjw142857
Copy link
Copy Markdown
Contributor

This PR implements type casting for the Java implementation in Source Academy. Two forms of type casting will be supported:

  1. Type casting between primitive types (including narrowing type conversions). To maintain ease of readability, type casting will be limited to within the int, long, float and double types.
  2. Type casting between non-primitive types when one is a subtype of the other.

Explicit type casting will take the form of (TYPE) expression, where expression is of the target type and TYPE denotes the type to be cast to.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces comprehensive type casting capabilities to the Java implementation in Source Academy. It enables explicit conversions between primitive types, including narrowing conversions, and supports casting between non-primitive types where a subtype relationship exists. This enhancement significantly expands the language's expressiveness and aligns it more closely with standard Java behavior.

Highlights

  • Type Casting Implementation: Implemented explicit type casting for both primitive types (int, long, float, double) and non-primitive types, supporting narrowing conversions and subtyping relationships.
  • AST and Grammar Updates: Introduced a new CastExpression interface into the Abstract Syntax Tree (AST) and updated the grammar to correctly parse and represent cast expressions, distinguishing between primitive and reference type casts.
  • Code Generation for Primitive Casts: Added code generation logic to emit appropriate JVM bytecode instructions (e.g., F2D, L2I) for handling primitive type conversions during compilation.
  • Type Checking Integration: Integrated type checking for cast expressions, ensuring type compatibility and raising IncompatibleTypesError when necessary.
  • Documentation and Testing: Updated the project documentation to reflect the newly supported type casting feature and added a dedicated test file with initial test cases for type conversions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@kjw142857 kjw142857 changed the title Type casting Implement type casting Mar 25, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 25, 2026

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
73.57% (-0.38% 🔻)
7194/9779
🔴 Branches
59.87% (-0.52% 🔻)
2359/3940
🟡 Functions
69.52% (+0.07% 🔼)
1291/1857
🟡 Lines
74.49% (-0.4% 🔻)
6770/9089
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🟢
... / typeConversion.test.ts
100% 100% 100% 100%
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢
... / index.ts
71.68% (-2.7% 🔻)
49.62% (-3.39% 🔻)
95.65%
81.05% (-3.19% 🔻)
🟡 types/ast/utils.ts
76.92% (-2.02% 🔻)
42.86% (-3.3% 🔻)
62.5%
71.43% (-2.65% 🔻)
🟢
... / code-generator.ts
88.24% (-6.17% 🔻)
77.62% (-7.04% 🔻)
93.33% (+0.15% 🔼)
88.46% (-6.29% 🔻)

Test suite run success

1121 tests passing in 63 suites.

Report generated by 🧪jest coverage report action from e14a512

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces comprehensive support for type casting in the Java-slang compiler, encompassing both primitive and reference type conversions. Key changes include defining a CastExpression AST node, updating the grammar to parse cast expressions, implementing code generation for various primitive type conversions, and adding type-checking logic to ensure compatibility. New test cases for type conversion have also been added. Feedback highlights several areas for improvement: a critical bug in the code generator where missing break statements lead to incorrect bytecode for primitive casts, an incomplete implementation for non-primitive casts lacking checkcast instructions, an inaccuracy in the README.md regarding explicit type conversion being a future feature, a suggestion to use the boolean primitive type instead of Boolean for isPrimitiveCast, a readability improvement for a destructuring assignment in tests, and a refactoring opportunity to reduce duplicated type-checking logic for CastExpression.

Comment thread src/compiler/code-generator.ts
Comment thread src/compiler/code-generator.ts Outdated
Comment thread README.md Outdated
Comment thread src/ast/types/blocks-and-statements.ts Outdated
Comment thread src/compiler/__tests__/tests/typeConversion.test.ts
Comment thread src/types/checker/index.ts
@kjw142857 kjw142857 marked this pull request as ready for review March 31, 2026 23:36
@martin-henz martin-henz self-requested a review April 1, 2026 00:43
Copy link
Copy Markdown
Member

@martin-henz martin-henz left a comment

Choose a reason for hiding this comment

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

approved

@martin-henz martin-henz merged commit c6159d0 into main Apr 1, 2026
4 checks passed
@martin-henz martin-henz deleted the type-casting branch April 1, 2026 00:43
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.

2 participants