Skip to content

Commit 409f27a

Browse files
committed
Fix broken test cases in type checker
1 parent ecae18c commit 409f27a

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

src/types/ast/astExtractor/expression-extractor.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,21 @@ export class ExpressionExtractor extends BaseJavaCstVisitorWithDefaults {
207207
const primary = this.primary(newPrimaryCtx)
208208
const primaryRest = this.visit(lastSuffix)
209209
if (!primaryRest) return {} as Primary // Temporary
210-
if (primary.kind === 'FieldAccess' && primaryRest.kind === 'MethodInvocation') {
211-
// example.test() -> primary: example.test, primaryRest: ()
212-
return {
213-
...primaryRest,
214-
identifier: primary.identifier,
215-
location: primary.location,
216-
primary: primary.primary
217-
} as Primary
210+
if (primaryRest.kind === 'MethodInvocation') {
211+
if (primary.kind === 'FieldAccess') {
212+
return {
213+
...primaryRest,
214+
identifier: primary.identifier,
215+
location: primary.location,
216+
primary: primary.primary
217+
} as Primary
218+
} else if (primary.kind === 'ExpressionName') {
219+
return {
220+
...primaryRest,
221+
identifier: primary.name,
222+
location: primary.location
223+
}
224+
}
218225
}
219226
return { ...primaryRest, primary }
220227
}

src/types/ast/types/blocks-and-statements.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export interface MethodInvocation extends BaseNode {
8585
kind: 'MethodInvocation'
8686
identifier: Identifier
8787
argumentList: ArgumentList
88+
primary?: Primary
8889
}
8990

9091
export type ArgumentList = Array<Expression>

src/types/checker/__tests__/methodInvocation.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ const testcases: {
6767
public static void main(String[] args) { int test = getStringLength("Hello World!"); }
6868
public static String getStringLength(String input) { return input; }
6969
`,
70-
result: { type: null, errors: [new IncompatibleTypesError()] }
70+
result: { type: null, errors: [new IncompatibleTypesError()] },
71+
only: true
7172
},
7273
{
7374
input: `

src/types/inputs/Main.class

61 Bytes
Binary file not shown.

src/types/inputs/Main.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public class Main {
2-
public static void main(String[] args) {
3-
int test = 3;
4-
int[] numbers = {1, 2, 3, 4, 5};
5-
int number = test[2]; // Accessing the third element
6-
}
2+
int test;
3+
4+
public static void main(String[] args) { }
5+
6+
public String test(){ return ""; }
77
}

0 commit comments

Comments
 (0)