-
Notifications
You must be signed in to change notification settings - Fork 1
Enhance support for switch statements #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { check } from '..' | ||
| import { parse } from '../../ast' | ||
| import { IncompatibleTypesError, TypeCheckerError } from '../../errors' | ||
| import { IncompatibleTypesError, SelectorTypeNotAllowedError, TypeCheckerError } from '../../errors' | ||
| import { Type } from '../../types/type' | ||
|
|
||
| const createProgram = (statement: string) => ` | ||
|
|
@@ -27,6 +27,27 @@ const testcases: { | |
| `, | ||
| result: { type: null, errors: [] } | ||
| }, | ||
| { | ||
| input: ` | ||
| String selector = "Tuesday"; | ||
| switch(selector) { | ||
| case "Tuesday": { | ||
| selector = "Wednesday"; | ||
| } | ||
| default: | ||
| } | ||
| `, | ||
| result: { type: null, errors: [] } | ||
| }, | ||
| { | ||
| input: ` | ||
| Boolean selector = true; | ||
| switch(selector) { | ||
| default: {} | ||
| } | ||
| `, | ||
| result: { type: null, errors: [new SelectorTypeNotAllowedError()] } | ||
| }, | ||
| { | ||
| input: ` | ||
| int selector = 1; | ||
|
|
@@ -52,6 +73,31 @@ const testcases: { | |
| } | ||
| `, | ||
| result: { type: null, errors: [new IncompatibleTypesError()] } | ||
| }, | ||
| { | ||
| input: ` | ||
| enum Color { RED, BLUE } | ||
| Color selector = Color.RED; | ||
| switch(selector) { | ||
| case Color.RED: { | ||
| selector = Color.BLUE; | ||
| } | ||
| default: {} | ||
| } | ||
| `, | ||
| result: { type: null, errors: [] } | ||
| }, | ||
| { | ||
| input: ` | ||
| enum Color { RED, BLUE } | ||
| enum Other { X } | ||
| Color selector = Color.RED; | ||
| switch(selector) { | ||
| case Other.X: {} | ||
| default: {} | ||
| } | ||
| `, | ||
| result: { type: null, errors: [new IncompatibleTypesError()] } | ||
|
Comment on lines
+76
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win These tests use qualified enum constants in case labels, which Java does not permit. Java requires the unqualified constant name in an enum switch label. Two consequences:
The unqualified form is the one that matters, and it is untested. Resolving a bare Add a test using Both tests also declare the enum inside the 🤖 Prompt for AI Agents |
||
| } | ||
| ] | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: source-academy/java-slang
Length of output: 50381
🏁 Script executed:
Repository: source-academy/java-slang
Length of output: 30302
🏁 Script executed:
Repository: source-academy/java-slang
Length of output: 50382
🏁 Script executed:
Repository: source-academy/java-slang
Length of output: 21495
Model enum declarations separately from class modifiers
ClassModifierandClassModifier*do not includeenum. The compiler therefore cannot placeenuminclassModifier; its grammar does not parse enum declarations asNormalClassDeclaration. This entry cannot setACC_ENUM. Add enum declaration support and set the flag from the declaration kind, or remove this unreachable mapping.🤖 Prompt for AI Agents