Skip to content

Commit 2d93c8b

Browse files
committed
Добавлены свойства для массива
1 parent 3f129a1 commit 2d93c8b

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

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

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

33
import com.annimon.ownlang.exceptions.TypeException;
4+
import com.annimon.ownlang.exceptions.UnknownPropertyException;
45
import java.util.Arrays;
56
import java.util.Iterator;
67
import java.util.List;
@@ -78,29 +79,45 @@ public Value[] getCopyElements() {
7879
public int type() {
7980
return Types.ARRAY;
8081
}
81-
82+
8283
public int size() {
8384
return elements.length;
8485
}
85-
86+
8687
public Value get(int index) {
8788
return elements[index];
8889
}
89-
90+
91+
public Value get(Value index) {
92+
final String prop = index.asString();
93+
switch (prop) {
94+
// Properties
95+
case "length":
96+
return NumberValue.of(size());
97+
98+
// Functions
99+
case "isEmpty":
100+
return NumberValue.fromBoolean(size() == 0);
101+
102+
default:
103+
return get(index.asInt());
104+
}
105+
}
106+
90107
public void set(int index, Value value) {
91108
elements[index] = value;
92109
}
93-
110+
94111
@Override
95112
public Object raw() {
96113
return elements;
97114
}
98-
115+
99116
@Override
100117
public int asInt() {
101118
throw new TypeException("Cannot cast array to integer");
102119
}
103-
120+
104121
@Override
105122
public double asNumber() {
106123
throw new TypeException("Cannot cast array to number");
@@ -132,7 +149,7 @@ public boolean equals(Object obj) {
132149
final ArrayValue other = (ArrayValue) obj;
133150
return Arrays.deepEquals(this.elements, other.elements);
134151
}
135-
152+
136153
@Override
137154
public int compareTo(Value o) {
138155
if (o.type() == Types.ARRAY) {

src/main/java/com/annimon/ownlang/parser/ast/ContainerAccessExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public Value get() {
4343
final Value lastIndex = lastIndex();
4444
switch (container.type()) {
4545
case Types.ARRAY:
46-
final int arrayIndex = lastIndex.asInt();
47-
return ((ArrayValue) container).get(arrayIndex);
46+
return ((ArrayValue) container).get(lastIndex);
4847

4948
case Types.MAP:
5049
return ((MapValue) container).get(lastIndex);

src/test/java/com/annimon/ownlang/parser/ProgramsTest.java

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

33
import com.annimon.ownlang.Console;
4+
import com.annimon.ownlang.lib.FunctionValue;
45
import com.annimon.ownlang.lib.Functions;
56
import com.annimon.ownlang.lib.NumberValue;
67
import com.annimon.ownlang.lib.Variables;
@@ -83,6 +84,15 @@ public void initialize() {
8384
assertFalse(args[0].asInt() != 0);
8485
return NumberValue.ONE;
8586
});
87+
Functions.set("assertFail", (args) -> {
88+
try {
89+
((FunctionValue) args[0]).getValue().execute();
90+
fail("Function should fail");
91+
} catch (Throwable thr) {
92+
93+
}
94+
return NumberValue.ONE;
95+
});
8696
}
8797

8898
@Test
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def testSetUnknownKey() = assertFail(def() {
2+
arr = [1, 2, 3]
3+
arr.one = 1
4+
})
5+
6+
def testSetLengthProperty() = assertFail(def() {
7+
arr = [1, 2, 3]
8+
arr.length = 10
9+
})
10+
11+
def testGetLength() {
12+
arr = [1, 2, 3]
13+
assertEquals(3, arr.length)
14+
}
15+
16+
def testGetLengthInnerArray() {
17+
arr = [[1, 2, 3], [1, 2, [3, 4, 5, 6]]]
18+
assertEquals(2, arr.length)
19+
assertEquals(3, arr[0].length)
20+
assertEquals(4, arr[1][2].length)
21+
}
22+
23+
def testIsEmpty() {
24+
arr = [1, 2, 3]
25+
assertFalse(arr.isEmpty)
26+
}

0 commit comments

Comments
 (0)