Skip to content

Commit 3d2da41

Browse files
committed
Fix unary expression stand-alone bug
1 parent 75173cd commit 3d2da41

File tree

6 files changed

+97
-25
lines changed

6 files changed

+97
-25
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export interface ExpressionStatement extends BaseNode {
7979
stmtExp: StatementExpression;
8080
}
8181

82-
export type StatementExpression = MethodInvocation | Assignment;
82+
export type StatementExpression = MethodInvocation | Assignment | UnaryExpression;
8383

8484
export interface MethodInvocation extends BaseNode {
8585
kind: "MethodInvocation";
@@ -142,13 +142,13 @@ export interface ClassInstanceCreationExpression extends BaseNode {
142142
export interface Literal extends BaseNode {
143143
kind: "Literal";
144144
literalType:
145-
| IntegerLiteral
146-
| FloatingPointLiteral
147-
| BooleanLiteral
148-
| CharacterLiteral
149-
| TextBlockLiteral
150-
| StringLiteral
151-
| NullLiteral;
145+
| IntegerLiteral
146+
| FloatingPointLiteral
147+
| BooleanLiteral
148+
| CharacterLiteral
149+
| TextBlockLiteral
150+
| StringLiteral
151+
| NullLiteral;
152152
}
153153

154154
export type IntegerLiteral =
@@ -243,18 +243,18 @@ export interface Assignment extends BaseNode {
243243
kind: "Assignment";
244244
left: LeftHandSide;
245245
operator:
246-
| "="
247-
| "+="
248-
| "-="
249-
| "*="
250-
| "/="
251-
| "%="
252-
| "|="
253-
| "&="
254-
| "^="
255-
| "<<="
256-
| ">>="
257-
| ">>>=";
246+
| "="
247+
| "+="
248+
| "-="
249+
| "*="
250+
| "/="
251+
| "%="
252+
| "|="
253+
| "&="
254+
| "^="
255+
| "<<="
256+
| ">>="
257+
| ">>>=";
258258
right: Expression;
259259
}
260260

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ import {
44
} from "../__utils__/test-utils";
55

66
const testCases: testCase[] = [
7+
{
8+
comment: "single increment/decrement statement",
9+
program: `
10+
public class Main {
11+
public static void main(String[] args) {
12+
int a = 1;
13+
int b = 2;
14+
15+
++a;
16+
b++;
17+
System.out.println(a);
18+
System.out.println(b);
19+
20+
a--;
21+
--b;
22+
System.out.println(a);
23+
System.out.println(b);
24+
}
25+
}`,
26+
expectedLines: ["2", "3", "1", "2"],
27+
},
728
{
829
comment: "postfix increment/decrement",
930
program: `

src/compiler/code-generator.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,9 @@ const codeGenerators: { [type: string]: (node: Node, cg: CodeGenerator) => Compi
550550

551551
ExpressionStatement: (node: Node, cg: CodeGenerator) => {
552552
const { stmtExp } = node as ExpressionStatement
553+
if (stmtExp.kind === 'PrefixExpression' || stmtExp.kind === 'PostfixExpression') {
554+
return codeGenerators['IncrementDecrementExpression'](stmtExp, cg)
555+
}
553556
return compile(stmtExp, cg)
554557
},
555558

@@ -708,6 +711,18 @@ const codeGenerators: { [type: string]: (node: Node, cg: CodeGenerator) => Compi
708711
}
709712
},
710713

714+
IncrementDecrementExpression: (node: Node, cg: CodeGenerator) => {
715+
// handle cases of ++x, x++, x--, --x that do not add object to operand stack
716+
if (node.kind === 'PrefixExpression' || node.kind === 'PostfixExpression') {
717+
const { name } = node.expression as ExpressionName
718+
const info = cg.symbolTable.queryVariable(name)
719+
if (!Array.isArray(info)) {
720+
cg.code.push(OPCODE.IINC, info.index, node.operator === '++' ? 1 : -1)
721+
}
722+
}
723+
return { stackSize: 0, resultType: EMPTY_TYPE }
724+
},
725+
711726
PrefixExpression: (node: Node, cg: CodeGenerator) => {
712727
const { operator: op, expression: expr } = node as PrefixExpression
713728
if (op === '++' || op === '--') {

src/compiler/grammar.pegjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,11 +862,20 @@ ForUpdate
862862
863863
StatementExpressionList
864864
= head:StatementExpression tail:(comma @StatementExpression)* {
865-
return [head, ...tail];
865+
return [head, ...tail].map(s => {
866+
return {
867+
kind: "ExpressionStatement", stmtExp: s
868+
}
869+
});
866870
}
867871
868872
ExpressionStatement
869-
= @StatementExpression semicolon
873+
= se:StatementExpression semicolon {
874+
return {
875+
kind: "ExpressionStatement",
876+
stmtExp: se,
877+
}
878+
}
870879
871880
StatementExpression
872881
= Assignment

src/compiler/grammar.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,11 +864,20 @@ ForUpdate
864864
865865
StatementExpressionList
866866
= head:StatementExpression tail:(comma @StatementExpression)* {
867-
return [head, ...tail];
867+
return [head, ...tail].map(s => {
868+
return {
869+
kind: "ExpressionStatement", stmtExp: s
870+
}
871+
});
868872
}
869873
870874
ExpressionStatement
871-
= @StatementExpression semicolon
875+
= se:StatementExpression semicolon {
876+
return {
877+
kind: "ExpressionStatement",
878+
stmtExp: se,
879+
}
880+
}
872881
873882
StatementExpression
874883
= Assignment

src/compiler/import/lib-info.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,25 @@ export const rawLibInfo = {
3434
"public void println(float)",
3535
"public void println(double)",
3636
"public void println(char)",
37-
"public void println(boolean)"
37+
"public void println(boolean)",
38+
"public void print(java.lang.String)",
39+
"public void print(int)",
40+
"public void print(long)",
41+
"public void print(float)",
42+
"public void print(double)",
43+
"public void print(char)",
44+
"public void print(boolean)"
45+
]
46+
}
47+
]
48+
},
49+
{
50+
"name": "java.util",
51+
"classes": [
52+
{
53+
"name": "public java.util.Arrays",
54+
"methods": [
55+
"public static java.lang.String toString(int[])"
3856
]
3957
}
4058
]

0 commit comments

Comments
 (0)