Добавлены свойства для массива

This commit is contained in:
Victor 2019-04-04 13:24:57 +03:00
parent 3f129a14b8
commit 2d93c8b9a7
4 changed files with 61 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package com.annimon.ownlang.lib;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.exceptions.UnknownPropertyException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@ -78,29 +79,45 @@ public class ArrayValue implements Value, Iterable<Value> {
public int type() {
return Types.ARRAY;
}
public int size() {
return elements.length;
}
public Value get(int index) {
return elements[index];
}
public Value get(Value index) {
final String prop = index.asString();
switch (prop) {
// Properties
case "length":
return NumberValue.of(size());
// Functions
case "isEmpty":
return NumberValue.fromBoolean(size() == 0);
default:
return get(index.asInt());
}
}
public void set(int index, Value value) {
elements[index] = value;
}
@Override
public Object raw() {
return elements;
}
@Override
public int asInt() {
throw new TypeException("Cannot cast array to integer");
}
@Override
public double asNumber() {
throw new TypeException("Cannot cast array to number");
@ -132,7 +149,7 @@ public class ArrayValue implements Value, Iterable<Value> {
final ArrayValue other = (ArrayValue) obj;
return Arrays.deepEquals(this.elements, other.elements);
}
@Override
public int compareTo(Value o) {
if (o.type() == Types.ARRAY) {

View File

@ -43,8 +43,7 @@ public final class ContainerAccessExpression implements Expression, Accessible {
final Value lastIndex = lastIndex();
switch (container.type()) {
case Types.ARRAY:
final int arrayIndex = lastIndex.asInt();
return ((ArrayValue) container).get(arrayIndex);
return ((ArrayValue) container).get(lastIndex);
case Types.MAP:
return ((MapValue) container).get(lastIndex);

View File

@ -1,6 +1,7 @@
package com.annimon.ownlang.parser;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.lib.FunctionValue;
import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.Variables;
@ -83,6 +84,15 @@ public class ProgramsTest {
assertFalse(args[0].asInt() != 0);
return NumberValue.ONE;
});
Functions.set("assertFail", (args) -> {
try {
((FunctionValue) args[0]).getValue().execute();
fail("Function should fail");
} catch (Throwable thr) {
}
return NumberValue.ONE;
});
}
@Test

View File

@ -0,0 +1,26 @@
def testSetUnknownKey() = assertFail(def() {
arr = [1, 2, 3]
arr.one = 1
})
def testSetLengthProperty() = assertFail(def() {
arr = [1, 2, 3]
arr.length = 10
})
def testGetLength() {
arr = [1, 2, 3]
assertEquals(3, arr.length)
}
def testGetLengthInnerArray() {
arr = [[1, 2, 3], [1, 2, [3, 4, 5, 6]]]
assertEquals(2, arr.length)
assertEquals(3, arr[0].length)
assertEquals(4, arr[1][2].length)
}
def testIsEmpty() {
arr = [1, 2, 3]
assertFalse(arr.isEmpty)
}