Skip to content

Commit b9e0a37

Browse files
committed
Fix duplicate null in ast extractor and add array testcases
1 parent 8717640 commit b9e0a37

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { check } from "..";
2+
import { IncompatibleTypesError } from "../../errors";
3+
import { parse } from "../../../ast/parser";
4+
import { Type } from "../../types/type";
5+
6+
const createProgram = (statement: string) => `
7+
public class Main {
8+
public static void main(String args[]) {
9+
${statement}
10+
}
11+
}
12+
`;
13+
14+
const testcases: {
15+
input: string;
16+
result: { type: Type | null; errors: Error[] };
17+
only?: boolean;
18+
}[] = [
19+
{
20+
input: `int[] numbers = {1, 2, 3, 4, 5};`,
21+
result: { type: null, errors: [] },
22+
},
23+
{
24+
input: `double[] values;`,
25+
result: { type: null, errors: [] },
26+
},
27+
{
28+
input: `String[] names = {1, 2, 3};`,
29+
result: { type: null, errors: [new IncompatibleTypesError()] },
30+
},
31+
{
32+
input: `
33+
int[][] matrix = {
34+
{1, 2, 3},
35+
{4, 5, 6},
36+
{7, 8, 9}
37+
};
38+
`,
39+
result: { type: null, errors: [] },
40+
},
41+
{
42+
input: `
43+
int[] numbers = {1, 2, 3, 4, 5};
44+
int number = numbers[2]; // Accessing the third element
45+
`,
46+
result: { type: null, errors: [] },
47+
},
48+
{
49+
input: `
50+
String[] names = {"Alice", "Bob", "Charlie"};
51+
String name = names["1"]; // Incorrect index type
52+
`,
53+
result: { type: null, errors: [new IncompatibleTypesError()] },
54+
},
55+
{
56+
input: `
57+
int[] numbers = new int[5];
58+
numbers[0] = 10; // Correct assignment
59+
`,
60+
result: { type: null, errors: [] },
61+
},
62+
{
63+
input: `
64+
boolean[] flags = new boolean[3];
65+
flags[0] = 1;
66+
`,
67+
result: { type: null, errors: [new IncompatibleTypesError()] },
68+
},
69+
{
70+
input: `
71+
int[] numbers = {1, 2, 3, 4, 5};
72+
int length = numbers.length; // Accessing array length
73+
`,
74+
result: { type: null, errors: [] },
75+
},
76+
{
77+
input: `
78+
char[] chars = new char[5];
79+
chars = new char[]{'a', 'b', 'c', 'd', 'e'}; // Reinitialization with values
80+
`,
81+
result: { type: null, errors: [] },
82+
},
83+
{
84+
input: `int[] numbers = new int[-1]; // Attempting to create an array with negative size`,
85+
result: { type: null, errors: [new IncompatibleTypesError()] },
86+
},
87+
];
88+
89+
describe("Type Checker", () => {
90+
testcases.map((testcase) => {
91+
let it = test;
92+
if (testcase.only) it = test.only;
93+
it(`Checking arrays for '${testcase.input}'`, () => {
94+
const program = createProgram(testcase.input);
95+
const ast = parse(program);
96+
if (!ast) throw new Error("Program parsing returns null.");
97+
const result = check(ast);
98+
if (result.currentType === null)
99+
expect(result.currentType).toBe(testcase.result.type);
100+
else expect(result.currentType).toBeInstanceOf(testcase.result.type);
101+
if (testcase.result.errors.length > result.errors.length) {
102+
testcase.result.errors.forEach((error, index) => {
103+
if (!result.errors[index]) expect("").toBe(error.message);
104+
expect(result.errors[index].message).toBe(error.message);
105+
});
106+
} else {
107+
result.errors.forEach((error, index) => {
108+
if (!testcase.result.errors[index]) expect(error.message).toBe("");
109+
expect(error.message).toBe(testcase.result.errors[index].message);
110+
});
111+
}
112+
});
113+
});
114+
});

src/types/checker/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export const check = (
5757
throw new Error(
5858
"Right side of assignment statment should return a type."
5959
);
60-
console.log(node.left, leftType, node.right, currentType);
6160
if (!leftType.canBeAssigned(currentType))
6261
return newResult(null, [new IncompatibleTypesError()]);
6362
return OK_RESULT;

0 commit comments

Comments
 (0)