Skip to content

Commit c39e52f

Browse files
committed
Added list operators back to plugin runner
1 parent d6729e5 commit c39e52f

7 files changed

Lines changed: 222 additions & 3 deletions

File tree

‎plugin/src/main/java/fr/jamailun/ultimatespellsystem/plugin/runner/builder/ExpressionQueue.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import fr.jamailun.ultimatespellsystem.dsl2.nodes.expressions.operators.MonoOperator;
1818
import fr.jamailun.ultimatespellsystem.dsl2.visitor.ExpressionVisitor;
1919
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.expressions.*;
20+
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list.RunListAddOpe;
21+
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list.RunListContainsOpe;
22+
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list.RunListRemIndexOpe;
23+
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list.RunListRemOpe;
2024
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.objects.ArrayGetNode;
2125
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.objects.GlobalFunctionCallNode;
2226
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.objects.StructFieldGetNode;
@@ -119,15 +123,21 @@ public void handleBiOperator(@NotNull BiOperator operator) {
119123
case LESSER -> new RunCompOpe(left, right, false, false);
120124
case AND -> new RunAndOrOpe(left, right, true);
121125
case OR -> new RunAndOrOpe(left, right, false);
126+
case LIST_ADD -> new RunListAddOpe(left, right);
127+
case LIST_REM -> new RunListRemOpe(left, right);
128+
case LIST_REM_INDEX -> new RunListRemIndexOpe(left, right);
129+
case LIST_CONTAINS -> new RunListContainsOpe(left, right);
122130
});
123131
}
124132

125133
@Override
126134
public void handleMonoOperator(@NotNull MonoOperator operator) {
127135
RuntimeExpression child = evaluate(operator.getChild());
128-
if(operator.getType() == MonoOperator.MonoOpeType.NOT) {
129-
add(new RunNotOpe(child));
130-
}
136+
RuntimeMonoOperator expr = switch (operator.getType()) {
137+
case NOT -> new RunNotOpe(child);
138+
case SIZE_OF -> new RunSizeofOpe(child);
139+
};
140+
add(expr);
131141
}
132142

133143
@Override
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators;
2+
3+
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
4+
import fr.jamailun.ultimatespellsystem.api.runner.errors.UnreachableRuntimeException;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.util.Collection;
8+
9+
/**
10+
* Sizeof-operator
11+
*/
12+
public final class RunSizeofOpe extends RuntimeMonoOperator {
13+
14+
public RunSizeofOpe(@NotNull RuntimeExpression child) {
15+
super(child);
16+
}
17+
18+
@Override
19+
protected @NotNull Object evaluate(@NotNull Object value) {
20+
if(value instanceof Collection<?> coll) {
21+
return coll.size();
22+
}
23+
throw new UnreachableRuntimeException("Unexpected type for SIZE_OF-operator : " + value + " | " + value.getClass());
24+
}
25+
26+
@Override
27+
public @NotNull String toString() {
28+
return "size_of(" + child + ")";
29+
}
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list;
2+
3+
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
4+
import fr.jamailun.ultimatespellsystem.api.runner.errors.UnreachableRuntimeException;
5+
import fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.RuntimeBiOperator;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.Collection;
10+
import java.util.HashSet;
11+
import java.util.List;
12+
import java.util.Set;
13+
14+
/**
15+
* Specialized bi-operator for list.
16+
*/
17+
public abstract class ListOperator extends RuntimeBiOperator {
18+
19+
public ListOperator(@NotNull RuntimeExpression left, @NotNull RuntimeExpression right) {
20+
super(left, right);
21+
}
22+
23+
/**
24+
* Evaluate with a list.
25+
* @param collection list of objects.
26+
* @param arg argument of the operator.
27+
* @return something, according to the operator.
28+
*/
29+
protected abstract Object evaluateList(@NotNull Collection<Object> collection, Object arg);
30+
31+
@Override
32+
@SuppressWarnings("unchecked")
33+
protected final Object evaluate(Object left, Object right) {
34+
if(!(left instanceof Collection<?> list))
35+
throw new UnreachableRuntimeException("Cannot have a left non-list: " + left);
36+
37+
List<Object> listObj = (List<Object>) list;
38+
return evaluateList(listObj, right);
39+
}
40+
41+
/**
42+
* Transform a single element or a collection (or a {@code null} value into a collection).
43+
* @param object object to handle.
44+
* @return a non-null collection.
45+
*/
46+
protected @NotNull Collection<Object> elementAsCollection(@Nullable Object object) {
47+
if(object instanceof Collection<?> coll) {
48+
return new HashSet<>(coll);
49+
}
50+
if(object == null)
51+
return new HashSet<>();
52+
return Set.of(object);
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list;
2+
3+
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.Collection;
7+
8+
/**
9+
* "Append" operation on a collection.
10+
*/
11+
public class RunListAddOpe extends ListOperator {
12+
13+
public RunListAddOpe(@NotNull RuntimeExpression left, @NotNull RuntimeExpression right) {
14+
super(left, right);
15+
}
16+
17+
@Override
18+
protected Object evaluateList(@NotNull Collection<Object> collection, Object arg) {
19+
// Can add either another collection or a single element.
20+
Collection<Object> allOthers = elementAsCollection(arg);
21+
22+
// Append values
23+
collection.addAll(allOthers);
24+
return collection;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return leftExpression +".add(" + rightExpression + ")";
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list;
2+
3+
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.Collection;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* Test if a collection contains an element.
12+
*/
13+
public class RunListContainsOpe extends ListOperator {
14+
15+
public RunListContainsOpe(@NotNull RuntimeExpression left, @NotNull RuntimeExpression right) {
16+
super(left, right);
17+
}
18+
19+
@Override
20+
protected Object evaluateList(@NotNull Collection<Object> collection, Object arg) {
21+
Set<Object> hashedList = new HashSet<>(collection);
22+
23+
if(arg instanceof Collection<?> others) {
24+
return hashedList.containsAll(new HashSet<>(others));
25+
}
26+
return hashedList.contains(arg);
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return leftExpression + ".contains(" + rightExpression + ")";
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list;
2+
3+
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
4+
import fr.jamailun.ultimatespellsystem.api.runner.errors.UnreachableRuntimeException;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.util.Collection;
8+
9+
/**
10+
* "Remove" operation on a collection, by index.
11+
*/
12+
public class RunListRemIndexOpe extends ListOperator {
13+
14+
public RunListRemIndexOpe(@NotNull RuntimeExpression left, @NotNull RuntimeExpression right) {
15+
super(left, right);
16+
}
17+
18+
@Override
19+
protected Object evaluateList(@NotNull Collection<Object> collection, Object arg) {
20+
if(!(arg instanceof Number index))
21+
throw new UnreachableRuntimeException("Cannot have a right non-number: " + arg);
22+
collection.remove(index.intValue());
23+
return arg;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return leftExpression + ".remove_idx(" + rightExpression + ")";
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fr.jamailun.ultimatespellsystem.plugin.runner.nodes.operators.list;
2+
3+
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.Collection;
7+
8+
/**
9+
* "Remove" operation on a collection.
10+
*/
11+
public class RunListRemOpe extends ListOperator {
12+
13+
public RunListRemOpe(@NotNull RuntimeExpression left, @NotNull RuntimeExpression right) {
14+
super(left, right);
15+
}
16+
17+
@Override
18+
protected Object evaluateList(@NotNull Collection<Object> collection, Object arg) {
19+
// Can add either another collection or a single element.
20+
Collection<Object> allOthers = elementAsCollection(arg);
21+
22+
// Append values
23+
collection.addAll(allOthers);
24+
return collection;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return leftExpression +".remove(" + rightExpression + ")";
30+
}
31+
}

0 commit comments

Comments
 (0)