Skip to content

Commit daca8ee

Browse files
1001meiAprupKale
andauthored
Compiler bug fixes (#52)
* Fix bug * Fix instance method with this keyword * Fix linting * Throw error when method resolution fails * Abstract out peggy functions to avoid duplication * Fix linting * Resolve TODO markers * Fix linting * Fix boolean expression * Fix linting --------- Co-authored-by: Aprup Kale <[email protected]>
1 parent fb944b3 commit daca8ee

File tree

11 files changed

+421
-211
lines changed

11 files changed

+421
-211
lines changed

src/compiler/__tests__/__utils__/test-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { compile } from "../../index";
33
import { BinaryWriter } from "../../binary-writer";
44
import { AST } from "../../../ast/types/packages-and-modules";
55
import { javaPegGrammar } from "../../grammar"
6+
import { peggyFunctions } from '../../peggy-functions'
67
import { execSync } from "child_process";
78

89
import * as peggy from "peggy";
@@ -16,7 +17,7 @@ export type testCase = {
1617

1718
const debug = false;
1819
const pathToTestDir = "./src/compiler/__tests__/";
19-
const parser = peggy.generate(javaPegGrammar, {
20+
const parser = peggy.generate(peggyFunctions + javaPegGrammar, {
2021
allowedStartRules: ["CompilationUnit"],
2122
});
2223
const binaryWriter = new BinaryWriter();

src/compiler/__tests__/tests/array.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ const testCases: testCase[] = [
181181
`,
182182
expectedLines: ["J", "a", "v", "a"],
183183
},
184+
{
185+
comment: "array of string",
186+
program: `
187+
public class Main {
188+
public static void main(String[] args) {
189+
String[] sarr = {"asdf", "qwer", "zxcv"};
190+
for (int i = 0; i < sarr.length; i++) {
191+
System.out.println(sarr[sarr.length - 1 - i]);
192+
}
193+
}
194+
}
195+
`,
196+
expectedLines: ["zxcv", "qwer", "asdf"],
197+
},
184198
{
185199
comment: "array of reference type",
186200
program: `

src/compiler/__tests__/tests/class.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,31 @@ const testCases: testCase[] = [
144144
`,
145145
expectedLines: ["100", "101", "100"],
146146
},
147+
{
148+
comment: "instance method using this keyword",
149+
program: `
150+
public class Main {
151+
public void f() {
152+
System.out.println("In f");
153+
}
154+
public void g() {
155+
System.out.println("In g");
156+
}
157+
158+
public void print() {
159+
this.f();
160+
System.out.println("In print");
161+
this.g();
162+
}
163+
164+
public static void main(String[] args) {
165+
Main m = new Main();
166+
m.print();
167+
}
168+
}
169+
`,
170+
expectedLines: ["In f", "In print", "In g"],
171+
},
147172
];
148173

149174
export const classTest = () => describe("fields and methods of class", () => {

src/compiler/__tests__/tests/ifElse.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,31 @@ const testCases: testCase[] = [
246246
} else {
247247
System.out.println("Yes3");
248248
}
249+
}
250+
}
251+
`,
252+
expectedLines: ["Yes1", "Yes2", "Yes3"],
253+
},
254+
{
255+
comment: "boolean expression",
256+
program: `
257+
public class Main {
258+
public static void main(String[] args) {
259+
boolean b = true;
260+
boolean c = !b;
249261
250262
boolean d = !(!(!(b == c)));
251263
System.out.println(d);
264+
265+
boolean e = null == null;
266+
System.out.println(e);
267+
268+
Main m = new Main();
269+
System.out.println(null == m);
252270
}
253271
}
254272
`,
255-
expectedLines: ["Yes1", "Yes2", "Yes3", "true"],
273+
expectedLines: ["true", "true", "false"],
256274
},
257275
{
258276
comment: "complex conditional expression",

src/compiler/__tests__/tests/unaryExpression.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,75 @@ const testCases: testCase[] = [
7171
}`,
7272
expectedLines: ["4", "1", "3", "2", "2", "3", "3"],
7373
},
74+
{
75+
comment: "increment/decrement on class fields",
76+
program: `
77+
public class Main {
78+
public static int g;
79+
public static int h;
80+
public static void main(String[] args) {
81+
g = 10;
82+
System.out.println(g);
83+
g++;
84+
System.out.println(g);
85+
++g;
86+
System.out.println(g);
87+
88+
h = 20;
89+
System.out.println(h);
90+
--h;
91+
System.out.println(h);
92+
h--;
93+
System.out.println(h);
94+
}
95+
}`,
96+
expectedLines: ["10", "11", "12", "20", "19", "18"],
97+
},
98+
{
99+
comment: "increment/decrement on instance fields",
100+
program: `
101+
public class Main {
102+
public int x;
103+
public void inc() {
104+
this.x++;
105+
}
106+
public void dec() {
107+
this.x--;
108+
}
109+
110+
public static void main(String[] args) {
111+
Main m = new Main();
112+
System.out.println(m.x);
113+
m.inc();
114+
m.inc();
115+
System.out.println(m.x);
116+
m.x = 100;
117+
m.dec();
118+
System.out.println(m.x);
119+
}
120+
}`,
121+
expectedLines: ["0", "2", "99"],
122+
},
123+
{
124+
comment: "increment/decrement on arrays",
125+
program: `
126+
public class Main {
127+
public static void main(String[] args) {
128+
int[] arr = {1, 5, 10, 15, 20};
129+
arr[0]++;
130+
--arr[1];
131+
System.out.println(arr[0]);
132+
System.out.println(arr[1]);
133+
134+
arr[3] = arr[2]++;
135+
arr[4] = --arr[2];
136+
System.out.println(arr[3]);
137+
System.out.println(arr[4]);
138+
System.out.println(arr[2]);
139+
}
140+
}`,
141+
expectedLines: ["2", "4", "10", "10", "10"],
142+
},
74143
{
75144
comment: "unary plus/minus",
76145
program: `

0 commit comments

Comments
 (0)