Skip to content

Commit fbdbead

Browse files
committed
Add tests for basic effects, move handlers to referents for adding and removing.
1 parent 0d4ef29 commit fbdbead

17 files changed

+234
-73
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,17 @@ public PostCompileClass[] compile() {
9191
}
9292
}
9393
final List<PostCompileClass> classes = new ArrayList<>();
94-
classes.add(new PostCompileClass(writer.compile(), writer.getName(), writer.getInternalName()));
94+
try {
95+
classes.add(new PostCompileClass(writer.compile(), writer.getName(), writer.getInternalName()));
96+
} catch (ArrayIndexOutOfBoundsException ex) {
97+
if (ex.getStackTrace()[0].getClassName().endsWith("Frame")) {
98+
throw new ScriptCompileError(-1, """
99+
Error during assembly phase.
100+
This error cannot be directly triaged, but likely comes from a malformed syntax (in which case the library-maintainer needs to fix it.)
101+
Experienced developers may check the `debug` output to see where the stack calculation error is.
102+
""");
103+
}
104+
}
95105
for (ClassBuilder builder : writer.getSuppressed()) {
96106
try {
97107
classes.add(new PostCompileClass(builder.compile(), builder.getName(), builder.getInternalName()));

src/main/java/org/byteskript/skript/lang/syntax/control/AddEffect.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
package org.byteskript.skript.lang.syntax.control;
88

9-
import mx.kenzie.foundation.MethodBuilder;
109
import org.byteskript.skript.api.HandlerType;
1110
import org.byteskript.skript.api.Referent;
1211
import org.byteskript.skript.api.note.Documentation;
@@ -39,7 +38,7 @@ public AddEffect() {
3938

4039
@Override
4140
public void preCompile(Context context, Pattern.Match match) throws Throwable {
42-
final ElementTree tree = context.getLine();
41+
final ElementTree tree = context.getCompileCurrent();
4342
final ElementTree[] inputs = tree.nested();
4443
assert inputs.length == 2;
4544
if (!(inputs[1].current() instanceof final Referent referent))
@@ -49,19 +48,20 @@ public void preCompile(Context context, Pattern.Match match) throws Throwable {
4948
if (target == null)
5049
throw new ScriptParseError(context.lineNumber(), "Syntax '" + inputs[1].current()
5150
.name() + "' cannot be added to.");
51+
inputs[1].type = StandardHandlers.ADD; // todo
5252
super.preCompile(context, match);
5353
}
5454

5555
@Override
5656
public void compile(Context context, Pattern.Match match) throws Throwable {
57-
final MethodBuilder method = context.getMethod();
58-
assert method != null;
59-
final ElementTree tree = context.getLine();
60-
final ElementTree[] inputs = tree.nested();
61-
final Referent referent = (Referent) inputs[1].current();
62-
final Method target = referent.getHandler(StandardHandlers.ADD);
63-
assert target != null;
64-
this.writeCall(method, target, context);
57+
// final MethodBuilder method = context.getMethod();
58+
// assert method != null;
59+
// final ElementTree tree = context.getCompileCurrent();
60+
// final ElementTree[] inputs = tree.nested();
61+
// final Referent referent = (Referent) inputs[1].current();
62+
// final Method target = referent.getHandler(StandardHandlers.ADD);
63+
// assert target != null;
64+
// this.writeCall(method, target, context);
6565
context.setState(CompileState.CODE_BODY);
6666
}
6767

src/main/java/org/byteskript/skript/lang/syntax/control/DeleteEffect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Pattern.Match match(String thing, Context context) {
4646

4747
@Override
4848
public void preCompile(Context context, Pattern.Match match) throws Throwable {
49-
final ElementTree tree = context.getLine();
49+
final ElementTree tree = context.getCompileCurrent();
5050
final ElementTree[] inputs = tree.nested();
5151
assert inputs.length == 1;
5252
inputs[0].type = StandardHandlers.DELETE;
@@ -68,7 +68,7 @@ public void preCompile(Context context, Pattern.Match match) throws Throwable {
6868
public void compile(Context context, Pattern.Match match) throws Throwable {
6969
final MethodBuilder method = context.getMethod();
7070
assert method != null;
71-
final ElementTree tree = context.getLine();
71+
final ElementTree tree = context.getCompileCurrent();
7272
final ElementTree[] inputs = tree.nested();
7373
if (!(inputs[0].current() instanceof VariableExpression)) { // variables have to handle their own deletion
7474
final Referent referent = (Referent) inputs[0].current();

src/main/java/org/byteskript/skript/lang/syntax/control/RemoveEffect.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
package org.byteskript.skript.lang.syntax.control;
88

9-
import mx.kenzie.foundation.MethodBuilder;
109
import org.byteskript.skript.api.HandlerType;
1110
import org.byteskript.skript.api.Referent;
1211
import org.byteskript.skript.api.note.Documentation;
@@ -39,30 +38,31 @@ public RemoveEffect() {
3938

4039
@Override
4140
public void preCompile(Context context, Pattern.Match match) throws Throwable {
42-
final ElementTree tree = context.getLine();
41+
final ElementTree tree = context.getCompileCurrent();
4342
final ElementTree[] inputs = tree.nested();
4443
assert inputs.length == 2;
4544
if (!(inputs[1].current() instanceof final Referent referent))
4645
throw new ScriptParseError(context.lineNumber(), "Syntax '" + inputs[1].current()
4746
.name() + "' cannot be removed from.");
4847
final Method target = referent.getHandler(StandardHandlers.REMOVE);
4948
if (target == null)
50-
throw new ScriptParseError(context.lineNumber(), "Syntax '" + inputs[0].current()
49+
throw new ScriptParseError(context.lineNumber(), "Syntax '" + inputs[1].current()
5150
.name() + "' cannot be removed from.");
52-
inputs[1].compile = false;
51+
inputs[1].type = StandardHandlers.REMOVE; // todo
52+
// inputs[1].compile = false;
5353
super.preCompile(context, match);
5454
}
5555

5656
@Override
5757
public void compile(Context context, Pattern.Match match) throws Throwable {
58-
final MethodBuilder method = context.getMethod();
59-
assert method != null;
60-
final ElementTree tree = context.getLine();
61-
final ElementTree[] inputs = tree.nested();
62-
final Referent referent = (Referent) inputs[1].current();
63-
final Method target = referent.getHandler(StandardHandlers.REMOVE);
64-
assert target != null;
65-
this.writeCall(method, target, context);
58+
// final MethodBuilder method = context.getMethod();
59+
// assert method != null;
60+
// final ElementTree tree = context.getCompileCurrent();
61+
// final ElementTree[] inputs = tree.nested();
62+
// final Referent referent = (Referent) inputs[1].current();
63+
// final Method target = referent.getHandler(StandardHandlers.REMOVE);
64+
// assert target != null;
65+
// this.writeCall(method, target, context);
6666
context.setState(CompileState.CODE_BODY);
6767
}
6868

src/main/java/org/byteskript/skript/lang/syntax/variable/AtomicVariableExpression.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public AtomicVariableExpression() {
3939
handlers.put(StandardHandlers.FIND, AtomicVariable.class.getMethod("get", Object.class));
4040
handlers.put(StandardHandlers.SET, AtomicVariable.class.getMethod("set", Object.class, Object.class));
4141
handlers.put(StandardHandlers.DELETE, AtomicVariable.class.getMethod("delete", Object.class));
42+
handlers.put(StandardHandlers.ADD, AtomicVariable.class.getMethod("add", Object.class, Object.class));
43+
handlers.put(StandardHandlers.REMOVE, AtomicVariable.class.getMethod("remove", Object.class, Object.class));
4244
} catch (NoSuchMethodException e) {
4345
e.printStackTrace();
4446
}

src/main/java/org/byteskript/skript/lang/syntax/variable/GlobalVariableExpression.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public GlobalVariableExpression() {
4242
handlers.put(StandardHandlers.FIND, GlobalVariableMap.class.getMethod("getVariable", Object.class));
4343
handlers.put(StandardHandlers.SET, GlobalVariableMap.class.getMethod("setVariable", Object.class, Object.class));
4444
handlers.put(StandardHandlers.DELETE, GlobalVariableMap.class.getMethod("deleteVariable", Object.class));
45+
handlers.put(StandardHandlers.ADD, GlobalVariableMap.class.getMethod("addVariable", Object.class, Object.class));
46+
handlers.put(StandardHandlers.REMOVE, GlobalVariableMap.class.getMethod("removeVariable", Object.class, Object.class));
4547
} catch (NoSuchMethodException e) {
4648
e.printStackTrace();
4749
}

src/main/java/org/byteskript/skript/lang/syntax/variable/ThreadVariableExpression.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public ThreadVariableExpression() {
4242
handlers.put(StandardHandlers.FIND, ThreadVariableMap.class.getMethod("getVariable", Object.class));
4343
handlers.put(StandardHandlers.SET, ThreadVariableMap.class.getMethod("setVariable", Object.class, Object.class));
4444
handlers.put(StandardHandlers.DELETE, ThreadVariableMap.class.getMethod("deleteVariable", Object.class));
45+
handlers.put(StandardHandlers.ADD, ThreadVariableMap.class.getMethod("addVariable", Object.class, Object.class));
46+
handlers.put(StandardHandlers.REMOVE, ThreadVariableMap.class.getMethod("removeVariable", Object.class, Object.class));
4547
} catch (NoSuchMethodException e) {
4648
e.printStackTrace();
4749
}

src/main/java/org/byteskript/skript/lang/syntax/variable/VariableExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public VariableExpression() {
4242
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "variable");
4343
try {
4444
handlers.put(StandardHandlers.ADD, OperatorHandler.class.getMethod("addObject", Object.class, Object.class));
45+
handlers.put(StandardHandlers.REMOVE, OperatorHandler.class.getMethod("removeObject", Object.class, Object.class));
4546
} catch (NoSuchMethodException e) {
4647
e.printStackTrace();
4748
}
@@ -96,6 +97,10 @@ public void compile(Context context, Pattern.Match match) {
9697
} else if (context.getHandlerMode().equals(StandardHandlers.DELETE)) {
9798
method.writeCode(WriteInstruction.pushNull());
9899
method.writeCode(variable.store(slot));
100+
} else {
101+
method.writeCode(variable.load(slot));
102+
this.writeCall(method, handlers.get(context.getHandlerMode()), context);
103+
method.writeCode(variable.store(slot));
99104
}
100105
}
101106

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
""")
1919
public class GlobalVariableMap extends ConcurrentHashMap<String, Object> {
2020

21+
public static void deleteVariable(Object name) {
22+
if (name == null) return;
23+
Skript.getVariables().remove(name + "");
24+
}
25+
26+
public static void addVariable(Object name, Object value) {
27+
if (name == null) return;
28+
if (value == null) return;
29+
final Object original = getVariable(name);
30+
setVariable(name, OperatorHandler.addObject(value, original));
31+
}
32+
2133
public static Object getVariable(Object name) {
2234
if (name == null) return null;
2335
return Skript.getVariables().get(name + "");
@@ -29,9 +41,11 @@ public static void setVariable(Object name, Object value) {
2941
else Skript.getVariables().put(name + "", value);
3042
}
3143

32-
public static void deleteVariable(Object name) {
44+
public static void removeVariable(Object name, Object value) {
3345
if (name == null) return;
34-
Skript.getVariables().remove(name + "");
46+
if (value == null) return;
47+
final Object original = getVariable(name);
48+
setVariable(name, OperatorHandler.subtract(original, value));
3549
}
3650

3751
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,17 @@ public Future<?> run(Skript skript, Object... arguments) {
181181
private Object value;
182182

183183
@Override
184-
public Object result() {
184+
public void start() {
185+
final Object result = invoker.invoke(arguments);
185186
synchronized (this) {
186-
return value;
187+
this.value = result;
187188
}
188189
}
189190

190191
@Override
191-
public void start() {
192-
final Object result = invoker.invoke(arguments);
192+
public Object result() {
193193
synchronized (this) {
194-
this.value = result;
194+
return value;
195195
}
196196
}
197197

0 commit comments

Comments
 (0)