Skip to content

Commit abe6177

Browse files
committed
Strip out type specializations, misc. cleanup
1 parent a836ab1 commit abe6177

File tree

13 files changed

+79
-124
lines changed

13 files changed

+79
-124
lines changed

Lang/src/main/java/chipmunk/vm/tree/Node.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,6 @@
2020

2121
package chipmunk.vm.tree;
2222

23-
import static chipmunk.vm.tree.Conversions.*;
24-
2523
public interface Node {
2624
Object execute(Fiber ctx);
27-
28-
default boolean executeBoolean(Fiber ctx){
29-
return toBoolean(execute(ctx));
30-
}
31-
32-
default int executeInt(Fiber ctx){
33-
return toInt(execute(ctx));
34-
}
35-
36-
default float executeFloat(Fiber ctx){
37-
return toFloat(execute(ctx));
38-
}
39-
40-
default long executeLong(Fiber ctx){
41-
return toLong(execute(ctx));
42-
}
43-
44-
default double executeDouble(Fiber ctx){
45-
return toDouble(execute(ctx));
46-
}
4725
}

Lang/src/main/java/chipmunk/vm/tree/nodes/Add.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@
2323
import chipmunk.vm.tree.Fiber;
2424
import chipmunk.vm.tree.Node;
2525

26+
import java.math.BigDecimal;
27+
2628
public final class Add implements Node {
2729
public Node l, r;
2830

2931
@Override
3032
public Object execute(Fiber ctx) {
3133
// TODO - determine types & either hit runtime library definition
3234
// or resort to CObject.add()
33-
return executeInt(ctx);
35+
var a = (BigDecimal) l.execute(ctx);
36+
var b = (BigDecimal) r.execute(ctx);
37+
return a.add(b);
3438
}
3539

3640
}

Lang/src/main/java/chipmunk/vm/tree/nodes/FDiv.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
import chipmunk.vm.tree.Fiber;
2424
import chipmunk.vm.tree.Node;
2525

26+
import java.math.BigDecimal;
27+
import java.math.MathContext;
28+
2629
public final class FDiv implements Node {
2730
public Node l, r;
2831

2932
@Override
3033
public Object execute(Fiber ctx) {
31-
return executeFloat(ctx);
32-
}
33-
34-
@Override
35-
public float executeFloat(Fiber ctx){
36-
return l.executeFloat(ctx) / r.executeFloat(ctx);
34+
var a = (BigDecimal) l.execute(ctx);
35+
var b = (BigDecimal) r.execute(ctx);
36+
return a.divide(b, MathContext.DECIMAL64);
3737
}
3838
}

Lang/src/main/java/chipmunk/vm/tree/nodes/FSub.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
import chipmunk.vm.tree.Fiber;
2424
import chipmunk.vm.tree.Node;
2525

26+
import java.math.BigDecimal;
27+
2628
public class FSub implements Node {
2729
public Node l, r;
2830

2931
@Override
3032
public Object execute(Fiber ctx) {
31-
return executeFloat(ctx);
32-
}
33-
34-
@Override
35-
public float executeFloat(Fiber ctx){
36-
return l.executeFloat(ctx) - r.executeFloat(ctx);
33+
var a = (BigDecimal) l.execute(ctx);
34+
var b = (BigDecimal) r.execute(ctx);
35+
return a.subtract(b);
3736
}
3837
}

Lang/src/main/java/chipmunk/vm/tree/nodes/For.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import chipmunk.vm.tree.Fiber;
2424
import chipmunk.vm.tree.Node;
2525

26-
import static chipmunk.vm.tree.Conversions.*;
27-
2826
public class For implements Node {
2927
public Node pre;
3028
public Node test;
@@ -42,22 +40,22 @@ public Object doPre(Fiber ctx) {
4240
try {
4341
pre.execute(ctx);
4442
} catch (Exception e) {
45-
ctx.suspendStateless(e, (c, s) -> doTest(c));
43+
ctx.suspendStateless(e, this::doTest);
4644
}
4745
}
4846
return null;
4947
}
5048

51-
public boolean doTest(Fiber ctx) {
49+
public Boolean doTest(Fiber ctx) {
5250
try {
53-
return test.executeBoolean(ctx);
51+
return (Boolean) test.execute(ctx);
5452
} catch (Exception e) {
55-
ctx.suspendStateless(e, (c, s) -> doBody(c, toBoolean(s)));
53+
ctx.suspendStateless(e, (c, s) -> doBody(c, (Boolean) s));
5654
}
5755
return false; // suspend() will rethrow so this will never be reached
5856
}
5957

60-
public Object doBody(Fiber ctx, boolean test) {
58+
public Object doBody(Fiber ctx, Boolean test) {
6159
var t = test;
6260
while (t && !ctx.checkInterrupt()) {
6361
try {
@@ -66,18 +64,18 @@ public Object doBody(Fiber ctx, boolean test) {
6664
ctx.suspendStateless(e, this::doPost);
6765
}
6866

69-
doPost(ctx, 0);
67+
doPost(ctx);
7068
t = doTest(ctx);
7169
}
7270
return 0;
7371
}
7472

75-
public Object doPost(Fiber ctx, Object prior) {
73+
public Object doPost(Fiber ctx) {
7674
if (post != null) {
7775
try {
7876
post.execute(ctx);
7977
} catch (Exception e) {
80-
ctx.suspendStateless(e, (c, s) -> doTest(c));
78+
ctx.suspendStateless(e, this::doTest);
8179
}
8280
}
8381
return null;

Lang/src/main/java/chipmunk/vm/tree/nodes/I2F.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

Lang/src/main/java/chipmunk/vm/tree/nodes/If.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import chipmunk.vm.tree.Fiber;
2424
import chipmunk.vm.tree.Node;
2525

26+
import java.math.BigDecimal;
27+
2628
public class If implements Node {
2729
public Node test;
2830
public Node _if;
@@ -32,7 +34,7 @@ public class If implements Node {
3234
public Object execute(Fiber ctx) {
3335
boolean t;
3436
try {
35-
t = test.executeBoolean(ctx);
37+
t = (Boolean) test.execute(ctx);
3638
} catch (Exception e) {
3739
throw e;
3840
}
@@ -45,6 +47,6 @@ public Object execute(Fiber ctx) {
4547
} else if (_else != null) {
4648
return _else.execute(ctx);
4749
}
48-
return 0;
50+
return new BigDecimal(0);
4951
}
5052
}

Lang/src/main/java/chipmunk/vm/tree/nodes/Iflt.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@ public class Iflt implements Node {
2727
public Node l, r;
2828

2929
@Override
30+
@SuppressWarnings({"unchecked", "rawtypes"})
3031
public Object execute(Fiber ctx) {
31-
int a;
32+
Object a;
3233
try {
33-
a = l.executeInt(ctx);
34+
a = l.execute(ctx);
3435
} catch (Exception e) {
3536
throw e;
3637
}
3738

38-
int b;
39+
Object b;
3940
try {
40-
b = r.executeInt(ctx);
41+
b = r.execute(ctx);
4142
} catch (Exception e) {
4243
throw e;
4344
}
44-
return a < b ? 1 : 0;
45+
return ((Comparable)a).compareTo(b) < 0;
4546
}
4647
}

Lang/src/main/java/chipmunk/vm/tree/nodes/Mul.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
import chipmunk.vm.tree.Fiber;
2424
import chipmunk.vm.tree.Node;
2525

26+
import java.math.BigDecimal;
27+
2628
public class Mul implements Node {
2729
public Node l, r;
2830

2931
@Override
3032
public Object execute(Fiber ctx) {
31-
return executeInt(ctx);
33+
var a = (BigDecimal) l.execute(ctx);
34+
var b = (BigDecimal) r.execute(ctx);
35+
return a.multiply(b);
3236
}
3337

34-
@Override
35-
public int executeInt(Fiber ctx) {
36-
return l.executeInt(ctx) * r.executeInt(ctx);
37-
}
3838
}

Lang/src/main/java/chipmunk/vm/tree/nodes/Sub.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
import chipmunk.vm.tree.Fiber;
2424
import chipmunk.vm.tree.Node;
2525

26+
import java.math.BigDecimal;
27+
2628
public final class Sub implements Node {
2729
public Node l, r;
2830

2931
@Override
3032
public Object execute(Fiber ctx) {
31-
return executeInt(ctx);
32-
}
33-
34-
@Override
35-
public int executeInt(Fiber ctx) {
36-
return l.executeInt(ctx) - r.executeInt(ctx);
33+
var a = (BigDecimal) l.execute(ctx);
34+
var b = (BigDecimal) r.execute(ctx);
35+
return a.subtract(b);
3736
}
3837
}

0 commit comments

Comments
 (0)