Skip to content

Commit 8a3719d

Browse files
committed
Расширенные числовые типы
1 parent d737844 commit 8a3719d

36 files changed

+621
-124
lines changed

src/com/annimon/ownlang/exceptions/OperationIsNotSupportedException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ public final class OperationIsNotSupportedException extends RuntimeException {
55
public OperationIsNotSupportedException(Object operation) {
66
super("Operation " + operation + " is not supported");
77
}
8+
9+
public OperationIsNotSupportedException(Object operation, String message) {
10+
super("Operation " + operation + " is not supported " + message);
11+
}
812
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public void set(int index, Value value) {
7272
elements[index] = value;
7373
}
7474

75+
@Override
76+
public int asInt() {
77+
throw new TypeException("Cannot cast array to integer");
78+
}
79+
7580
@Override
7681
public double asNumber() {
7782
throw new TypeException("Cannot cast array to number");

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public int type() {
2222
return Types.FUNCTION;
2323
}
2424

25+
@Override
26+
public int asInt() {
27+
throw new TypeException("Cannot cast function to integer");
28+
}
29+
2530
@Override
2631
public double asNumber() {
2732
throw new TypeException("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
@@ -44,6 +44,11 @@ public Value get(Value key) {
4444
public void set(Value key, Value value) {
4545
map.put(key, value);
4646
}
47+
48+
@Override
49+
public int asInt() {
50+
throw new TypeException("Cannot cast map to integer");
51+
}
4752

4853
@Override
4954
public double asNumber() {

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

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
*/
77
public final class NumberValue implements Value {
88

9+
public static final NumberValue MINUS_ONE = new NumberValue(-1);
910
public static final NumberValue ZERO = new NumberValue(0);
1011
public static final NumberValue ONE = new NumberValue(1);
1112

1213
public static NumberValue fromBoolean(boolean b) {
1314
return b ? ONE : ZERO;
1415
}
1516

16-
private final double value;
17+
private final Number value;
1718

18-
public NumberValue(double value) {
19+
public NumberValue(Number value) {
1920
this.value = value;
2021
}
2122

@@ -24,20 +25,53 @@ public int type() {
2425
return Types.NUMBER;
2526
}
2627

28+
public Number raw() {
29+
return value;
30+
}
31+
32+
public boolean asBoolean() {
33+
return value.intValue() != 0;
34+
}
35+
36+
public byte asByte() {
37+
return value.byteValue();
38+
}
39+
40+
public short asShort() {
41+
return value.shortValue();
42+
}
43+
44+
@Override
45+
public int asInt() {
46+
return value.intValue();
47+
}
48+
49+
public long asLong() {
50+
return value.longValue();
51+
}
52+
53+
public float asFloat() {
54+
return value.floatValue();
55+
}
56+
57+
public double asDouble() {
58+
return value.doubleValue();
59+
}
60+
2761
@Override
2862
public double asNumber() {
29-
return value;
63+
return value.doubleValue();
3064
}
3165

3266
@Override
3367
public String asString() {
34-
return Double.toString(value);
68+
return value.toString();
3569
}
3670

3771
@Override
3872
public int hashCode() {
3973
int hash = 3;
40-
hash = 71 * hash + (int) (Double.doubleToLongBits(this.value) ^ (Double.doubleToLongBits(this.value) >>> 32));
74+
hash = 71 * hash + value.hashCode();
4175
return hash;
4276
}
4377

@@ -47,14 +81,33 @@ public boolean equals(Object obj) {
4781
if (obj == null) return false;
4882
if (getClass() != obj.getClass())
4983
return false;
50-
final NumberValue other = (NumberValue) obj;
51-
return Double.doubleToLongBits(this.value) == Double.doubleToLongBits(other.value);
84+
final Number other = ((NumberValue) obj).value;
85+
if (value instanceof Double || other instanceof Double) {
86+
return Double.compare(value.doubleValue(), other.doubleValue()) == 0;
87+
}
88+
if (value instanceof Float || other instanceof Float) {
89+
return Float.compare(value.floatValue(), other.floatValue()) == 0;
90+
}
91+
if (value instanceof Long || other instanceof Long) {
92+
return Long.compare(value.longValue(), other.longValue()) == 0;
93+
}
94+
return Integer.compare(value.intValue(), other.intValue()) == 0;
5295
}
5396

5497
@Override
5598
public int compareTo(Value o) {
5699
if (o.type() == Types.NUMBER) {
57-
return Double.compare(value, ((NumberValue)o).value);
100+
final Number other = ((NumberValue) o).value;
101+
if (value instanceof Double || other instanceof Double) {
102+
return Double.compare(value.doubleValue(), other.doubleValue());
103+
}
104+
if (value instanceof Float || other instanceof Float) {
105+
return Float.compare(value.floatValue(), other.floatValue());
106+
}
107+
if (value instanceof Long || other instanceof Long) {
108+
return Long.compare(value.longValue(), other.longValue());
109+
}
110+
return Integer.compare(value.intValue(), other.intValue());
58111
}
59112
return asString().compareTo(o.asString());
60113
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public int type() {
2525
return Types.STRING;
2626
}
2727

28+
@Override
29+
public int asInt() {
30+
try {
31+
return Integer.parseInt(value);
32+
} catch (NumberFormatException e) {
33+
return 0;
34+
}
35+
}
36+
2837
@Override
2938
public double asNumber() {
3039
try {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@ public final class Types {
99
ARRAY = 3,
1010
MAP = 4,
1111
FUNCTION = 5;
12+
13+
private static int FIRST = OBJECT, LAST = FUNCTION;
14+
private static final String[] NAMES = {"object", "number", "string", "array", "map", "function"};
15+
16+
public static String typeToString(int type) {
17+
if (FIRST <= type && type <= LAST) {
18+
return NAMES[type];
19+
}
20+
return "unknown";
21+
}
1222
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
public interface Value extends Comparable<Value> {
88

9+
int asInt();
10+
911
double asNumber();
1012

1113
String asString();

src/com/annimon/ownlang/lib/modules/canvas.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
*/
2323
public final class canvas implements Module {
2424

25-
private static final NumberValue MINUS_ONE = new NumberValue(-1);
26-
2725
private static JFrame frame;
2826
private static CanvasPanel panel;
2927
private static Graphics2D graphics;
@@ -55,7 +53,7 @@ public void init() {
5553
Variables.set("VK_FIRE", new NumberValue(KeyEvent.VK_ENTER));
5654
Variables.set("VK_ESCAPE", new NumberValue(KeyEvent.VK_ESCAPE));
5755

58-
lastKey = MINUS_ONE;
56+
lastKey = NumberValue.MINUS_ONE;
5957
mouseHover = new ArrayValue(new Value[] { NumberValue.ZERO, NumberValue.ZERO });
6058
}
6159

@@ -91,11 +89,7 @@ private static void clip(int x, int y, int w, int h) {
9189
private static Function intConsumer4Convert(IntConsumer4 consumer) {
9290
return args -> {
9391
if (args.length != 4) throw new ArgumentsMismatchException("Four args expected");
94-
int x = (int) args[0].asNumber();
95-
int y = (int) args[1].asNumber();
96-
int w = (int) args[2].asNumber();
97-
int h = (int) args[3].asNumber();
98-
consumer.accept(x, y, w, h);
92+
consumer.accept(args[0].asInt(), args[1].asInt(), args[2].asInt(), args[3].asInt());
9993
return NumberValue.ZERO;
10094
};
10195
}
@@ -116,7 +110,7 @@ public void keyPressed(KeyEvent e) {
116110
}
117111
@Override
118112
public void keyReleased(KeyEvent e) {
119-
lastKey = MINUS_ONE;
113+
lastKey = NumberValue.MINUS_ONE;
120114
}
121115
});
122116
addMouseMotionListener(new MouseMotionAdapter() {
@@ -147,13 +141,13 @@ public Value execute(Value... args) {
147141
title = args[0].asString();
148142
break;
149143
case 2:
150-
width = (int) args[0].asNumber();
151-
height = (int) args[1].asNumber();
144+
width = args[0].asInt();
145+
height = args[1].asInt();
152146
break;
153147
case 3:
154148
title = args[0].asString();
155-
width = (int) args[1].asNumber();
156-
height = (int) args[2].asNumber();
149+
width = args[1].asInt();
150+
height = args[2].asInt();
157151
break;
158152
}
159153
panel = new CanvasPanel(width, height);
@@ -188,8 +182,8 @@ private static class DrawString implements Function {
188182
@Override
189183
public Value execute(Value... args) {
190184
if (args.length != 3) throw new ArgumentsMismatchException("Three args expected");
191-
int x = (int) args[1].asNumber();
192-
int y = (int) args[2].asNumber();
185+
int x = args[1].asInt();
186+
int y = args[2].asInt();
193187
graphics.drawString(args[0].asString(), x, y);
194188
return NumberValue.ZERO;
195189
}
@@ -219,12 +213,12 @@ private static class SetColor implements Function {
219213
@Override
220214
public Value execute(Value... args) {
221215
if (args.length == 1) {
222-
graphics.setColor(new Color((int) args[0].asNumber()));
216+
graphics.setColor(new Color(args[0].asInt()));
223217
return NumberValue.ZERO;
224218
}
225-
int r = (int) args[0].asNumber();
226-
int g = (int) args[1].asNumber();
227-
int b = (int) args[2].asNumber();
219+
int r = args[0].asInt();
220+
int g = args[1].asInt();
221+
int b = args[2].asInt();
228222
graphics.setColor(new Color(r, g, b));
229223
return NumberValue.ZERO;
230224
}

src/com/annimon/ownlang/lib/modules/files.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Value execute(Value... args) {
6969
}
7070
return process(file, "r");
7171
} catch (IOException ioe) {
72-
return new NumberValue(-1);
72+
return NumberValue.MINUS_ONE;
7373
}
7474
}
7575

@@ -101,7 +101,7 @@ private static abstract class FileFunction implements Function {
101101
@Override
102102
public Value execute(Value... args) {
103103
if (args.length < 1) throw new ArgumentsMismatchException("File descriptor expected");
104-
final int key = (int) args[0].asNumber();
104+
final int key = args[0].asInt();
105105
try {
106106
return execute(files.get(key), args);
107107
} catch (IOException ioe) {
@@ -115,7 +115,7 @@ public Value execute(Value... args) {
115115
private static class readBoolean extends FileFunction {
116116
@Override
117117
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
118-
return new NumberValue(fileInfo.dis.readBoolean() ? 1 : 0);
118+
return NumberValue.fromBoolean(fileInfo.dis.readBoolean());
119119
}
120120
}
121121

@@ -132,8 +132,8 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
132132
final ArrayValue array = (ArrayValue) args[1];
133133
int offset = 0, length = array.size();
134134
if (args.length > 3) {
135-
offset = (int) args[2].asNumber();
136-
length = (int) args[3].asNumber();
135+
offset = args[2].asInt();
136+
length = args[3].asInt();
137137
}
138138

139139
final byte[] buffer = new byte[length];
@@ -170,7 +170,7 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
170170
private static class readChar extends FileFunction {
171171
@Override
172172
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
173-
return new NumberValue(fileInfo.dis.readChar());
173+
return new NumberValue((short)fileInfo.dis.readChar());
174174
}
175175
}
176176

@@ -239,15 +239,15 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
239239
private static class writeBoolean extends FileFunction {
240240
@Override
241241
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
242-
fileInfo.dos.writeBoolean(args[1].asNumber() != 0);
242+
fileInfo.dos.writeBoolean(args[1].asInt() != 0);
243243
return NumberValue.ONE;
244244
}
245245
}
246246

247247
private static class writeByte extends FileFunction {
248248
@Override
249249
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
250-
fileInfo.dos.writeByte((byte) args[1].asNumber());
250+
fileInfo.dos.writeByte((byte) args[1].asInt());
251251
return NumberValue.ONE;
252252
}
253253
}
@@ -256,7 +256,7 @@ private static class writeChar extends FileFunction {
256256
@Override
257257
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
258258
final char ch = (args[1].type() == Types.NUMBER)
259-
? ((char) args[1].asNumber())
259+
? ((char) args[1].asInt())
260260
: args[1].asString().charAt(0);
261261
fileInfo.dos.writeChar(ch);
262262
return NumberValue.ONE;
@@ -266,15 +266,15 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
266266
private static class writeShort extends FileFunction {
267267
@Override
268268
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
269-
fileInfo.dos.writeShort((short) args[1].asNumber());
269+
fileInfo.dos.writeShort((short) args[1].asInt());
270270
return NumberValue.ONE;
271271
}
272272
}
273273

274274
private static class writeInt extends FileFunction {
275275
@Override
276276
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
277-
fileInfo.dos.writeInt((int) args[1].asNumber());
277+
fileInfo.dos.writeInt(args[1].asInt());
278278
return NumberValue.ONE;
279279
}
280280
}

0 commit comments

Comments
 (0)