Skip to content

Commit 3ecfb54

Browse files
committed
List and collection tests.
1 parent 0d795ec commit 3ecfb54

File tree

8 files changed

+129
-13
lines changed

8 files changed

+129
-13
lines changed

src/main/java/org/byteskript/skript/compiler/SkriptLangSpec.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@
5151
import org.byteskript.skript.lang.syntax.flow.loop.WhileSection;
5252
import org.byteskript.skript.lang.syntax.function.*;
5353
import org.byteskript.skript.lang.syntax.generic.*;
54-
import org.byteskript.skript.lang.syntax.list.ClearList;
55-
import org.byteskript.skript.lang.syntax.list.ImplicitArrayCreator;
56-
import org.byteskript.skript.lang.syntax.list.IndexOfList;
57-
import org.byteskript.skript.lang.syntax.list.ListCreator;
54+
import org.byteskript.skript.lang.syntax.list.*;
5855
import org.byteskript.skript.lang.syntax.literal.*;
5956
import org.byteskript.skript.lang.syntax.map.KeyInMap;
6057
import org.byteskript.skript.lang.syntax.map.MapCreator;
@@ -240,6 +237,7 @@ private SkriptLangSpec() {
240237
new ThreadExpression(),
241238
new NewLineExpression(),
242239
new ResultOfExpression(), // must try before property
240+
new SizeOfList(), // must try before property
243241
new PropertyExpression(),
244242
new ConverterExpression(),
245243
new SystemInputExpression(),

src/main/java/org/byteskript/skript/lang/syntax/list/IndexOfList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
""",
2828
examples = {
2929
"""
30-
set {var} index 0 of {list}
31-
set {var} index 1 of {array}
30+
set {var} index 0 in {list}
31+
set {var} index 1 in {array}
3232
"""
3333
}
3434
)
3535
public class IndexOfList extends RelationalExpression implements Referent {
3636

3737
public IndexOfList() {
38-
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "index %Number% in [list ]%List%");
38+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "index %Number% in [list] %List%");
3939
try {
4040
handlers.put(StandardHandlers.GET, ExtractedSyntaxCalls.class.getMethod("getListValue", Object.class, Object.class));
4141
handlers.put(StandardHandlers.FIND, ExtractedSyntaxCalls.class.getMethod("getListValue", Object.class, Object.class));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.lang.syntax.list;
8+
9+
import mx.kenzie.foundation.Type;
10+
import org.byteskript.skript.api.Referent;
11+
import org.byteskript.skript.api.note.Documentation;
12+
import org.byteskript.skript.api.syntax.RelationalExpression;
13+
import org.byteskript.skript.compiler.CommonTypes;
14+
import org.byteskript.skript.compiler.Context;
15+
import org.byteskript.skript.compiler.Pattern;
16+
import org.byteskript.skript.compiler.SkriptLangSpec;
17+
import org.byteskript.skript.lang.element.StandardElements;
18+
import org.byteskript.skript.lang.handler.StandardHandlers;
19+
import org.byteskript.skript.runtime.internal.ExtractedSyntaxCalls;
20+
21+
@Documentation(
22+
name = "Size of Collection",
23+
description = """
24+
Returns the size of a collection.
25+
Indices of the collection start at *zero*.
26+
""",
27+
examples = {
28+
"""
29+
print size of {list}
30+
"""
31+
}
32+
)
33+
public class SizeOfList extends RelationalExpression implements Referent {
34+
35+
public SizeOfList() {
36+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "size of [list] %Object%");
37+
try {
38+
handlers.put(StandardHandlers.GET, ExtractedSyntaxCalls.class.getMethod("getListSize", Object.class));
39+
handlers.put(StandardHandlers.FIND, ExtractedSyntaxCalls.class.getMethod("getListSize", Object.class));
40+
} catch (NoSuchMethodException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
@Override
46+
public Pattern.Match match(String thing, Context context) {
47+
if (!thing.startsWith("size of ")) return null;
48+
return super.match(thing, context);
49+
}
50+
51+
@Override
52+
public boolean allowAsInputFor(Type type) {
53+
return super.allowAsInputFor(type);
54+
}
55+
56+
@Override
57+
public Type getReturnType() {
58+
return CommonTypes.OBJECT;
59+
}
60+
61+
@Override
62+
public Type getHolderType() {
63+
return CommonTypes.LIST;
64+
}
65+
66+
67+
}

src/main/java/org/byteskript/skript/runtime/internal/ExtractedSyntaxCalls.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.BufferedReader;
2020
import java.io.InputStreamReader;
2121
import java.lang.reflect.Method;
22+
import java.util.Collection;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.concurrent.Future;
@@ -112,18 +113,23 @@ public static void runOnAsyncThread(final Instruction<?> runnable) {
112113
thread.skript.runOnAsyncThread(runnable);
113114
}
114115

116+
public static Object getListSize(Object target) {
117+
if (target instanceof Collection list) return list.size();
118+
if (target instanceof Object[] list) return list.length;
119+
if (target instanceof Map map) return map.size();
120+
throw new ScriptRuntimeError("The given collection must be a list.");
121+
}
122+
115123
public static Object getListValue(Object key, Object target) {
116-
if (!(key instanceof Number number))
117-
throw new ScriptRuntimeError("The given index must be a number.");
124+
final Number number = Skript.convert(key, Number.class);
118125
if (target instanceof List list) return list.get(number.intValue());
119126
if (target instanceof Object[] list) return list[number.intValue()];
120127
throw new ScriptRuntimeError("The given collection must be a list.");
121128
}
122129

123130
@SuppressWarnings("unchecked")
124131
public static void setListValue(Object key, Object target, Object value) {
125-
if (!(key instanceof Number number))
126-
throw new ScriptRuntimeError("The given index must be a number.");
132+
final Number number = Skript.convert(key, Number.class);
127133
if (target instanceof List list) {
128134
list.remove(number.intValue());
129135
list.add(number.intValue(), value);
@@ -136,8 +142,7 @@ public static void setListValue(Object key, Object target, Object value) {
136142
}
137143

138144
public static void deleteListValue(Object key, Object target) {
139-
if (!(key instanceof Number number))
140-
throw new ScriptRuntimeError("The given index must be a number.");
145+
final Number number = Skript.convert(key, Number.class);
141146
if (target instanceof List list) {
142147
list.remove(number.intValue());
143148
return;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
function test:
3+
trigger:
4+
set {var} to a new list
5+
add 1 to {var}
6+
assert {var} contains 1
7+
clear {var}
8+
assert {var} exists
9+
assert {var} is a list
10+
assert ({var} contains 1) is false
11+
return true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
function test:
3+
trigger:
4+
set {var} to (1, 2, 3)
5+
assert {var} contains 1
6+
try:
7+
add 1 to {var}
8+
catch {error}:
9+
assert {error} exists
10+
loop {number} in (1, 2, 3):
11+
assert {number} exists
12+
return true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
function test:
3+
trigger:
4+
set {var} to a new list
5+
add 1 to {var}
6+
assert {var} contains 1
7+
assert index 0 in {var} is 1
8+
add "hello" to {var}
9+
assert index 1 in {var} is "hello"
10+
set index 1 in {var} to "hello there"
11+
assert index 1 in {var} is "hello there"
12+
return true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
function test:
3+
trigger:
4+
set {var} to a new list
5+
add 1 to {var}
6+
assert {var} contains 1
7+
assert size of {var} is 1
8+
add "hello" to {var}
9+
assert size of {var} is 2
10+
assert size of (1, 2, 3) is 3
11+
return true

0 commit comments

Comments
 (0)