Skip to content

Commit 8763795

Browse files
committed
Проверка по типам вместо instanceof
1 parent a17bef1 commit 8763795

12 files changed

+63
-20
lines changed

src/com/annimon/ownlang/lib/ArrayValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public ArrayValue(ArrayValue array) {
4242
this(array.elements);
4343
}
4444

45+
@Override
46+
public int type() {
47+
return Types.ARRAY;
48+
}
49+
4550
public Value get(int index) {
4651
return elements[index];
4752
}

src/com/annimon/ownlang/lib/FunctionValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public FunctionValue(Function value) {
1414
this.value = value;
1515
}
1616

17+
@Override
18+
public int type() {
19+
return Types.FUNCTION;
20+
}
21+
1722
@Override
1823
public double asNumber() {
1924
throw new RuntimeException("Cannot cast function to number");

src/com/annimon/ownlang/lib/MapValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public MapValue(int size) {
2020
public MapValue(Map<Value, Value> map) {
2121
this.map = map;
2222
}
23+
24+
@Override
25+
public int type() {
26+
return Types.MAP;
27+
}
2328

2429
public Value get(Value key) {
2530
return map.get(key);

src/com/annimon/ownlang/lib/NumberValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public NumberValue(double value) {
1919
this.value = value;
2020
}
2121

22+
@Override
23+
public int type() {
24+
return Types.NUMBER;
25+
}
26+
2227
@Override
2328
public double asNumber() {
2429
return value;

src/com/annimon/ownlang/lib/StringValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public StringValue(String value) {
1414
this.value = value;
1515
}
1616

17+
@Override
18+
public int type() {
19+
return Types.STRING;
20+
}
21+
1722
@Override
1823
public double asNumber() {
1924
try {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.annimon.ownlang.lib;
2+
3+
public final class Types {
4+
5+
public static final int
6+
OBJECT = 0,
7+
NUMBER = 1,
8+
STRING = 2,
9+
ARRAY = 3,
10+
MAP = 4,
11+
FUNCTION = 5;
12+
}

src/com/annimon/ownlang/lib/Value.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public interface Value {
99
double asNumber();
1010

1111
String asString();
12+
13+
int type();
1214
}

src/com/annimon/ownlang/parser/ast/ArrayAccessExpression.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.annimon.ownlang.lib.ArrayValue;
44
import com.annimon.ownlang.lib.MapValue;
5+
import com.annimon.ownlang.lib.Types;
56
import com.annimon.ownlang.lib.Value;
67
import com.annimon.ownlang.lib.Variables;
78
import java.util.List;
@@ -23,7 +24,7 @@ public ArrayAccessExpression(String variable, List<Expression> indices) {
2324
@Override
2425
public Value eval() {
2526
Value container = Variables.get(variable);
26-
if (container instanceof ArrayValue) {
27+
if (container.type() == Types.ARRAY) {
2728
return getArray().get(lastIndex());
2829
}
2930
return consumeMap(container).get(indices.get(0).eval());
@@ -47,19 +48,17 @@ private int index(int index) {
4748
}
4849

4950
private ArrayValue consumeArray(Value value) {
50-
if (value instanceof ArrayValue) {
51-
return (ArrayValue) value;
52-
} else {
51+
if (value.type() != Types.ARRAY) {
5352
throw new RuntimeException("Array expected");
5453
}
54+
return (ArrayValue) value;
5555
}
5656

5757
public MapValue consumeMap(Value value) {
58-
if (value instanceof MapValue) {
59-
return (MapValue) value;
60-
} else {
58+
if (value.type() != Types.MAP) {
6159
throw new RuntimeException("Map expected");
6260
}
61+
return (MapValue) value;
6362
}
6463

6564
@Override

src/com/annimon/ownlang/parser/ast/ArrayAssignmentStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.annimon.ownlang.parser.ast;
22

3-
import com.annimon.ownlang.lib.ArrayValue;
3+
import com.annimon.ownlang.lib.Types;
44
import com.annimon.ownlang.lib.Value;
55
import com.annimon.ownlang.lib.Variables;
66

@@ -21,7 +21,7 @@ public ArrayAssignmentStatement(ArrayAccessExpression array, Expression expressi
2121
@Override
2222
public void execute() {
2323
final Value container = Variables.get(array.variable);
24-
if (container instanceof ArrayValue) {
24+
if (container.type() == Types.ARRAY) {
2525
array.getArray().set(array.lastIndex(), expression.eval());
2626
return;
2727
}

src/com/annimon/ownlang/parser/ast/BinaryExpression.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.annimon.ownlang.lib.ArrayValue;
44
import com.annimon.ownlang.lib.NumberValue;
55
import com.annimon.ownlang.lib.StringValue;
6+
import com.annimon.ownlang.lib.Types;
67
import com.annimon.ownlang.lib.Value;
78

89
/**
@@ -52,13 +53,15 @@ public Value eval() {
5253
final Value value1 = expr1.eval();
5354
final Value value2 = expr2.eval();
5455

55-
if (value1 instanceof StringValue) {
56-
return eval((StringValue) value1, value2);
57-
}
58-
if (value1 instanceof ArrayValue) {
59-
return eval((ArrayValue) value1, value2);
56+
switch (value1.type()) {
57+
case Types.STRING:
58+
return eval((StringValue) value1, value2);
59+
case Types.ARRAY:
60+
return eval((ArrayValue) value1, value2);
61+
case Types.NUMBER:
62+
default:
63+
return eval(value1, value2);
6064
}
61-
return eval(value1, value2);
6265
}
6366

6467
private Value eval(StringValue value1, Value value2) {
@@ -81,7 +84,7 @@ private Value eval(StringValue value1, Value value2) {
8184
private Value eval(ArrayValue value1, Value value2) {
8285
switch (operation) {
8386
case LSHIFT:
84-
if (!(value2 instanceof ArrayValue))
87+
if (value2.type() != Types.ARRAY)
8588
throw new RuntimeException("Cannot merge non array value to array");
8689
return ArrayValue.merge(value1, (ArrayValue) value2);
8790
case PUSH:

0 commit comments

Comments
 (0)