Skip to content

Commit 630dcf4

Browse files
committed
Support negative numbers in ZeroJava
1 parent 1de0c68 commit 630dcf4

File tree

13 files changed

+101
-4
lines changed

13 files changed

+101
-4
lines changed

pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,13 @@
309309
<include>**/*.spg</include>
310310
<include>**/*.kg</include>
311311
<include>**/*.zmips</include>
312+
<include>**/*.class</include>
312313
</includes>
313314
<excludes>
314315
<exclude>**/*.java</exclude>
315316
<exclude>**/*.pubtape</exclude>
316317
<exclude>**/*.auxtape</exclude>
318+
<exclude>**/*.txt</exclude>
317319
</excludes>
318320
<followSymlinks>false</followSymlinks>
319321
</fileset>

src/main/java/org/twc/zerojavacompiler/kanga2zmips/Kanga2zMIPS.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,13 @@ public String visit(SpilledArg n) throws Exception {
415415
/**
416416
* f0 -> Reg()
417417
* | IntegerLiteral()
418+
* | NegIntegerLiteral()
418419
* | Label()
419420
*/
420421
// returns a simple register
421422
public String visit(SimpleExp n) throws Exception {
422423
String str = n.f0.accept(this);
423-
if (n.f0.which == 2) { // if label
424+
if (n.f0.which == 3) { // if label
424425
return "__" + str + "__";
425426
} else {
426427
return str;
@@ -466,6 +467,13 @@ public String visit(IntegerLiteral n) throws Exception {
466467
return n.f0.toString();
467468
}
468469

470+
/**
471+
* f0 -> <NEG_INTEGER_LITERAL>
472+
*/
473+
public String visit(NegIntegerLiteral n) throws Exception {
474+
return n.f0.toString();
475+
}
476+
469477
/**
470478
* f0 -> <IDENTIFIER>
471479
*/

src/main/java/org/twc/zerojavacompiler/kanga2zmips/kangaparser/kanga.jj

+9-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ TOKEN :
113113

114114
TOKEN : /* LITERALS */
115115
{
116-
< INTEGER_LITERAL: ( ["1"-"9"] (["0"-"9"])* | "0" ) >
116+
< INTEGER_LITERAL: ( ["1"-"9"] (["0"-"9"])* | "0" ) > |
117+
< NEG_INTEGER_LITERAL: ( "-" ["1"-"9"] (["0"-"9"])* ) >
117118
}
118119

119120
TOKEN : /* IDENTIFIERS */
@@ -382,6 +383,7 @@ void SimpleExp() :
382383
{
383384
Reg()
384385
| IntegerLiteral()
386+
| NegIntegerLiteral()
385387
| Label()
386388
}
387389

@@ -420,6 +422,12 @@ void IntegerLiteral() :
420422
<INTEGER_LITERAL>
421423
}
422424

425+
void NegIntegerLiteral() :
426+
{}
427+
{
428+
<NEG_INTEGER_LITERAL>
429+
}
430+
423431
void Label() :
424432
{}
425433
{

src/main/java/org/twc/zerojavacompiler/spiglet2kanga/GetFlowGraph.java

+8
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ public String visit(NotExp n) throws Exception {
324324
/**
325325
* f0 -> Temp()
326326
* | IntegerLiteral()
327+
* | NegIntegerLiteral()
327328
* | Label()
328329
*/
329330
public String visit(SimpleExp n) throws Exception {
@@ -350,6 +351,13 @@ public String visit(IntegerLiteral n) throws Exception {
350351
return n.f0.toString();
351352
}
352353

354+
/**
355+
* f0 -> <NEG_INTEGER_LITERAL>
356+
*/
357+
public String visit(NegIntegerLiteral n) throws Exception {
358+
return n.f0.toString();
359+
}
360+
353361
/**
354362
* f0 -> <IDENTIFIER>
355363
*/

src/main/java/org/twc/zerojavacompiler/spiglet2kanga/GetFlowGraphVertex.java

+7
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ public String visit(IntegerLiteral n) throws Exception {
153153
return n.f0.toString();
154154
}
155155

156+
/**
157+
* f0 -> <NEG_INTEGER_LITERAL>
158+
*/
159+
public String visit(NegIntegerLiteral n) throws Exception {
160+
return n.f0.toString();
161+
}
162+
156163
/**
157164
* f0 -> <IDENTIFIER>
158165
*/

src/main/java/org/twc/zerojavacompiler/spiglet2kanga/Spiglet2Kanga.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public String visit(NotExp n) throws Exception {
395395
}
396396

397397
/**
398-
* f0 -> Temp() | IntegerLiteral() | Label()
398+
* f0 -> Temp() | IntegerLiteral() | NegIntegerLiteral() | Label()
399399
*/
400400
public String visit(SimpleExp n) throws Exception {
401401
String _ret = n.f0.accept(this);
@@ -420,6 +420,13 @@ public String visit(IntegerLiteral n) throws Exception {
420420
return n.f0.toString();
421421
}
422422

423+
/**
424+
* f0 -> <NEG_INTEGER_LITERAL>
425+
*/
426+
public String visit(NegIntegerLiteral n) throws Exception {
427+
return n.f0.toString();
428+
}
429+
423430
/**
424431
* f0 -> <IDENTIFIER>
425432
*/

src/main/java/org/twc/zerojavacompiler/spiglet2kanga/spigletparser/spiglet.jj

+10-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ TOKEN :
8080

8181
TOKEN : /* LITERALS */
8282
{
83-
< INTEGER_LITERAL: ( ["1"-"9"] (["0"-"9"])* | "0" ) >
83+
< INTEGER_LITERAL: ( ["1"-"9"] (["0"-"9"])* | "0" ) > |
84+
< NEG_INTEGER_LITERAL: ( "-" ["1"-"9"] (["0"-"9"])* ) >
8485
}
8586

8687
TOKEN : /* IDENTIFIERS */
@@ -330,6 +331,8 @@ void SimpleExp() :
330331
Temp()
331332
|
332333
IntegerLiteral()
334+
|
335+
NegIntegerLiteral()
333336
|
334337
Label()
335338
}
@@ -346,6 +349,12 @@ void IntegerLiteral() :
346349
<INTEGER_LITERAL>
347350
}
348351

352+
void NegIntegerLiteral() :
353+
{}
354+
{
355+
<NEG_INTEGER_LITERAL>
356+
}
357+
349358
void Label() :
350359
{}
351360
{

src/main/java/org/twc/zerojavacompiler/spigletoptimizer/FactGeneratorVisitor.java

+8
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ public String visit(Operator n, String argu) throws Exception {
436436
/**
437437
* f0 -> Temp()
438438
* | IntegerLiteral()
439+
* | NegIntegerLiteral()
439440
* | Label()
440441
*/
441442
public String visit(SimpleExp n, String argu) throws Exception {
@@ -474,6 +475,13 @@ public String visit(IntegerLiteral n, String argu) throws Exception {
474475
return n.f0.toString();
475476
}
476477

478+
/**
479+
* f0 -> <NEG_INTEGER_LITERAL>
480+
*/
481+
public String visit(NegIntegerLiteral n, String argu) throws Exception {
482+
return n.f0.toString();
483+
}
484+
477485
/**
478486
* f0 -> <IDENTIFIER>
479487
*/

src/main/java/org/twc/zerojavacompiler/spigletoptimizer/OptimizerVisitor.java

+8
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ public OptimizationWrapper visit(NotExp n, String argu) throws Exception {
413413
/**
414414
* f0 -> Temp()
415415
* | IntegerLiteral()
416+
* | NegIntegerLiteral()
416417
* | Label()
417418
*/
418419
public OptimizationWrapper visit(SimpleExp n, String argu) throws Exception {
@@ -455,6 +456,13 @@ public OptimizationWrapper visit(IntegerLiteral n, String argu) throws Exception
455456
return new OptimizationWrapper(n.f0.toString());
456457
}
457458

459+
/**
460+
* f0 -> <NEG_INTEGER_LITERAL>
461+
*/
462+
public OptimizationWrapper visit(NegIntegerLiteral n, String argu) throws Exception {
463+
return new OptimizationWrapper(n.f0.toString());
464+
}
465+
458466
/**
459467
* f0 -> <IDENTIFIER>
460468
*/

src/main/java/org/twc/zerojavacompiler/zerojava2spiglet/TypeCheckVisitor.java

+7
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,13 @@ public Base_t visit(IntegerLiteral n, Base_t argu) throws Exception {
950950
return new Variable_t("int");
951951
}
952952

953+
/**
954+
* f0 -> <NEG_INTEGER_LITERAL>
955+
*/
956+
public Base_t visit(NegIntegerLiteral n, Base_t argu) throws Exception {
957+
return new Variable_t("int");
958+
}
959+
953960
/**
954961
* f0 -> "true"
955962
*/

src/main/java/org/twc/zerojavacompiler/zerojava2spiglet/ZeroJava2Spiglet.java

+10
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ public Base_t visit(Clause n, Base_t argu) throws Exception {
10201020

10211021
/**
10221022
* f0 -> IntegerLiteral()
1023+
* | NegIntegerLiteral()
10231024
* | TrueLiteral()
10241025
* | FalseLiteral()
10251026
* | Identifier()
@@ -1041,6 +1042,15 @@ public Base_t visit(IntegerLiteral n, Base_t argu) throws Exception {
10411042
return new Variable_t(null, null, ret);
10421043
}
10431044

1045+
/**
1046+
* f0 -> <INTEGER_LITERAL>
1047+
*/
1048+
public Base_t visit(NegIntegerLiteral n, Base_t argu) throws Exception {
1049+
String ret = newTemp();
1050+
this.asm_.append("MOVE ").append(ret).append(" ").append(n.f0.toString()).append("\n");
1051+
return new Variable_t(null, null, ret);
1052+
}
1053+
10441054
/**
10451055
* f0 -> "true"
10461056
*/

src/main/java/org/twc/zerojavacompiler/zerojava2spiglet/zerojavaparser/zeroJava.jj

+7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ TOKEN :
8585
TOKEN : /* LITERALS */
8686
{
8787
< INTEGER_LITERAL: ( ["1"-"9"] (["0"-"9"])* | "0" ) >
88+
| < NEG_INTEGER_LITERAL: ( "-" ["1"-"9"] (["0"-"9"])* ) >
8889
}
8990

9091
TOKEN : /* IDENTIFIERS */
@@ -542,6 +543,7 @@ void PrimaryExpression() :
542543
{}
543544
{
544545
IntegerLiteral()
546+
| NegIntegerLiteral()
545547
| TrueLiteral()
546548
| FalseLiteral()
547549
| Identifier()
@@ -558,6 +560,11 @@ void IntegerLiteral() :
558560
<INTEGER_LITERAL>
559561
}
560562

563+
void NegIntegerLiteral() :
564+
{}
565+
{
566+
<NEG_INTEGER_LITERAL>
567+
}
561568

562569
void TrueLiteral() :
563570
{}

src/test/resources/Negative.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Negative {
2+
3+
public static void main(String[] a) {
4+
int x = -4, y = -3;
5+
Prover.answer( x + y );
6+
}
7+
8+
}

0 commit comments

Comments
 (0)