Skip to content

Commit 40c7842

Browse files
committed
Improve efficiency of some basic syntax.
1 parent faeac7b commit 40c7842

File tree

15 files changed

+113
-134
lines changed

15 files changed

+113
-134
lines changed

src/main/java/org/byteskript/skript/lang/syntax/comparison/IsArray.java

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,30 @@
88

99
import mx.kenzie.foundation.MethodBuilder;
1010
import mx.kenzie.foundation.Type;
11-
import mx.kenzie.foundation.WriteInstruction;
12-
import org.byteskript.skript.api.note.ForceExtract;
1311
import org.byteskript.skript.api.syntax.RelationalExpression;
1412
import org.byteskript.skript.compiler.*;
1513
import org.byteskript.skript.lang.element.StandardElements;
16-
import org.byteskript.skript.lang.handler.StandardHandlers;
17-
import org.byteskript.skript.lang.syntax.type.TypeExpression;
18-
19-
import java.lang.reflect.Method;
2014

2115
public class IsArray extends RelationalExpression {
2216

2317
public IsArray() {
2418
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Object% (is|are) a[n] array");
25-
try {
26-
handlers.put(StandardHandlers.FIND, this.getClass().getMethod("check", Object.class));
27-
handlers.put(StandardHandlers.GET, this.getClass().getMethod("check", Object.class));
28-
} catch (NoSuchMethodException e) {
29-
e.printStackTrace();
30-
}
3119
}
3220

3321
@Override
3422
public Pattern.Match match(String thing, Context context) {
35-
if (!thing.endsWith("array")) return null;
23+
if (!thing.endsWith(" array")) return null;
3624
return super.match(thing, context);
3725
}
3826

39-
@ForceExtract
40-
public static Boolean check(Object object) {
41-
if (object == null) return false;
42-
return (object.getClass().isArray());
43-
}
44-
45-
@Override
46-
public void preCompile(Context context, Pattern.Match match) throws Throwable {
47-
super.preCompile(context, match);
48-
final ElementTree tree = context.getCompileCurrent().nested()[1];
49-
if (tree.current() instanceof TypeExpression) {
50-
tree.compile = false;
51-
}
52-
}
53-
5427
@Override
5528
public void compile(Context context, Pattern.Match match) throws Throwable {
5629
final MethodBuilder method = context.getMethod();
57-
assert method != null;
58-
final ElementTree tree = context.getCompileCurrent();
59-
if (tree.nested()[1].current() instanceof TypeExpression expression) {
60-
final String string = match.groups()[1];
61-
final Type type = expression.getType(string, context);
62-
method.writeCode(WriteInstruction.instanceOf(type));
63-
method.writeCode(WriteInstruction.invokeStatic(Boolean.class.getMethod("valueOf", boolean.class)));
64-
} else {
65-
final Method target = handlers.get(StandardHandlers.FIND);
66-
assert target != null;
67-
this.writeCall(method, target, context);
68-
}
30+
method.writeCode((writer, visitor) -> {
31+
visitor.visitMethodInsn(182, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
32+
visitor.visitMethodInsn(182, "java/lang/Class", "isArray", "()Z", false);
33+
visitor.visitMethodInsn(184, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
34+
});
6935
context.setState(CompileState.STATEMENT);
7036
}
7137

src/main/java/org/byteskript/skript/lang/syntax/comparison/IsEqual.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,10 @@ public Pattern.Match match(String thing, Context context) {
3333

3434
@Override
3535
public void compile(Context context, Pattern.Match match) {
36-
try {
37-
final MethodBuilder method = context.getMethod();
38-
assert method != null;
39-
final Method target = OperatorHandler.class.getDeclaredMethod("equals", Object.class, Object.class);
40-
method.writeCode(WriteInstruction.invokeStatic(target));
41-
context.setState(CompileState.STATEMENT);
42-
} catch (NoSuchMethodException e) {
43-
e.printStackTrace();
44-
}
36+
final MethodBuilder method = context.getMethod();
37+
final Method target = findMethod(OperatorHandler.class, "equals", Object.class, Object.class);
38+
method.writeCode(WriteInstruction.invokeStatic(target));
39+
context.setState(CompileState.STATEMENT);
4540
}
4641

4742
@Override

src/main/java/org/byteskript/skript/lang/syntax/comparison/Matches.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class Matches extends RelationalExpression {
2020

2121
public Matches() {
22-
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%String% matches %Pattern%");
22+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Object% matches %Query%");
2323
}
2424

2525
@Override
@@ -30,15 +30,11 @@ public Pattern.Match match(String thing, Context context) {
3030

3131
@Override
3232
public void compile(Context context, Pattern.Match match) {
33-
try {
34-
final MethodBuilder method = context.getMethod();
35-
assert method != null;
36-
final Method target = OperatorHandler.class.getDeclaredMethod("matches", Object.class, Object.class);
37-
method.writeCode(WriteInstruction.invokeStatic(target));
38-
context.setState(CompileState.STATEMENT);
39-
} catch (NoSuchMethodException e) {
40-
e.printStackTrace();
41-
}
33+
final MethodBuilder method = context.getMethod();
34+
assert method != null;
35+
final Method target = findMethod(OperatorHandler.class, "matches", Object.class, Object.class);
36+
method.writeCode(WriteInstruction.invokeStatic(target));
37+
context.setState(CompileState.STATEMENT);
4238
}
4339

4440
@Override
@@ -49,7 +45,9 @@ public Type getReturnType() {
4945
@Override
5046
public String description() {
5147
return """
52-
Check whether the first string matches the given RegEx pattern.""";
48+
Check whether the first string matches the given RegEx pattern.
49+
Check whether the first object matches the given query.
50+
""";
5351
}
5452

5553
}

src/main/java/org/byteskript/skript/lang/syntax/flow/AssertEffect.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@
88

99
import mx.kenzie.foundation.MethodBuilder;
1010
import mx.kenzie.foundation.WriteInstruction;
11-
import org.byteskript.skript.api.note.ForceExtract;
1211
import org.byteskript.skript.api.syntax.Effect;
1312
import org.byteskript.skript.compiler.CompileState;
1413
import org.byteskript.skript.compiler.Context;
1514
import org.byteskript.skript.compiler.Pattern;
1615
import org.byteskript.skript.compiler.SkriptLangSpec;
17-
import org.byteskript.skript.error.ScriptAssertionError;
1816
import org.byteskript.skript.lang.element.StandardElements;
1917
import org.byteskript.skript.lang.handler.StandardHandlers;
18+
import org.byteskript.skript.runtime.internal.OperatorHandler;
2019

2120
import java.lang.reflect.Method;
2221

2322
public class AssertEffect extends Effect {
2423

2524
public AssertEffect() {
2625
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "assert %Boolean%");
27-
handlers.put(StandardHandlers.RUN, findMethod(AssertEffect.class, "assertion", Object.class, Class.class, int.class));
26+
handlers.put(StandardHandlers.RUN, findMethod(OperatorHandler.class, "assertion", Object.class, Class.class, int.class));
2827
}
2928

3029
@Override
@@ -46,14 +45,4 @@ public Pattern.Match match(String thing, Context context) {
4645
return super.match(thing, context);
4746
}
4847

49-
@ForceExtract
50-
public static void assertion(Object object, Class<?> script, int line) {
51-
if (object == null)
52-
throw new ScriptAssertionError(script, line);
53-
else if (object instanceof Boolean boo && !boo)
54-
throw new ScriptAssertionError(script, line);
55-
else if (object instanceof Number number && number.intValue() == 0)
56-
throw new ScriptAssertionError(script, line);
57-
}
58-
5948
}

src/main/java/org/byteskript/skript/lang/syntax/flow/AssertWithErrorEffect.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@
88

99
import mx.kenzie.foundation.MethodBuilder;
1010
import mx.kenzie.foundation.WriteInstruction;
11-
import org.byteskript.skript.api.note.ForceExtract;
1211
import org.byteskript.skript.api.syntax.Effect;
1312
import org.byteskript.skript.compiler.CompileState;
1413
import org.byteskript.skript.compiler.Context;
1514
import org.byteskript.skript.compiler.Pattern;
1615
import org.byteskript.skript.compiler.SkriptLangSpec;
17-
import org.byteskript.skript.error.ScriptAssertionError;
1816
import org.byteskript.skript.lang.element.StandardElements;
1917
import org.byteskript.skript.lang.handler.StandardHandlers;
18+
import org.byteskript.skript.runtime.internal.OperatorHandler;
2019

2120
import java.lang.reflect.Method;
2221

2322
public class AssertWithErrorEffect extends Effect {
2423

2524
public AssertWithErrorEffect() {
2625
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "assert %Boolean%: %String%");
27-
handlers.put(StandardHandlers.RUN, findMethod(AssertWithErrorEffect.class, "assertion", Object.class, Object.class, Class.class, int.class));
26+
handlers.put(StandardHandlers.RUN, findMethod(OperatorHandler.class, "assertion", Object.class, Object.class, Class.class, int.class));
2827
}
2928

3029
@Override
@@ -47,14 +46,4 @@ public Pattern.Match match(String thing, Context context) {
4746
return super.match(thing, context);
4847
}
4948

50-
@ForceExtract
51-
public static void assertion(Object object, Object message, Class<?> script, int line) {
52-
if (object == null)
53-
throw new ScriptAssertionError(script, line, message + "");
54-
else if (object instanceof Boolean boo && !boo)
55-
throw new ScriptAssertionError(script, line, message + "");
56-
else if (object instanceof Number number && number.intValue() == 0)
57-
throw new ScriptAssertionError(script, line, message + "");
58-
}
59-
6049
}

src/main/java/org/byteskript/skript/lang/syntax/generic/BinaryOtherwiseExpression.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,18 @@
88

99
import mx.kenzie.foundation.MethodBuilder;
1010
import mx.kenzie.foundation.Type;
11-
import org.byteskript.skript.api.note.ForceExtract;
1211
import org.byteskript.skript.api.syntax.SimpleExpression;
1312
import org.byteskript.skript.compiler.CommonTypes;
1413
import org.byteskript.skript.compiler.Context;
1514
import org.byteskript.skript.compiler.Pattern;
1615
import org.byteskript.skript.compiler.SkriptLangSpec;
1716
import org.byteskript.skript.lang.element.StandardElements;
18-
import org.byteskript.skript.lang.handler.StandardHandlers;
19-
20-
import java.lang.reflect.Method;
17+
import org.objectweb.asm.Label;
2118

2219
public class BinaryOtherwiseExpression extends SimpleExpression {
2320

2421
public BinaryOtherwiseExpression() {
2522
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Object% (otherwise|\\\\?) %Object%");
26-
try {
27-
handlers.put(StandardHandlers.GET, BinaryOtherwiseExpression.class.getMethod("getResult", Object.class, Object.class));
28-
} catch (NoSuchMethodException e) {
29-
e.printStackTrace();
30-
}
3123
}
3224

3325
@Override
@@ -43,16 +35,18 @@ public boolean allowAsInputFor(Type type) {
4335
@Override
4436
public void compile(Context context, Pattern.Match match) throws Throwable {
4537
final MethodBuilder method = context.getMethod();
46-
assert method != null;
47-
final Method target = handlers.get(context.getHandlerMode());
48-
assert target != null;
49-
this.writeCall(method, target, context);
50-
}
51-
52-
@ForceExtract
53-
public static Object getResult(Object first, Object def) {
54-
if (first == null) return def;
55-
return first;
38+
final Label first = new Label(), second = new Label();
39+
method.writeCode((writer, visitor) -> {
40+
visitor.visitInsn(95); // swap
41+
visitor.visitInsn(89); // dup
42+
visitor.visitJumpInsn(199, first); // notnull
43+
visitor.visitInsn(87); // pop
44+
visitor.visitJumpInsn(167, second); // goto
45+
visitor.visitLabel(first);
46+
visitor.visitInsn(95); // swap
47+
visitor.visitInsn(87); // pop
48+
visitor.visitLabel(second); // x or y
49+
});
5650
}
5751

5852
}

src/main/java/org/byteskript/skript/lang/syntax/generic/CurrentScriptExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class CurrentScriptExpression extends SimpleExpression {
2020

2121
public CurrentScriptExpression() {
22-
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "[the ][current ]script");
22+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "[the] [current] script");
2323
}
2424

2525
@Override

src/main/java/org/byteskript/skript/lang/syntax/generic/PrintEffect.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ public PrintEffect() {
2626
@Override
2727
public void preCompile(Context context, Pattern.Match match) throws Throwable {
2828
final MethodBuilder method = context.getMethod();
29-
assert method != null;
3029
method.writeCode(WriteInstruction.getField(System.class.getField("out")));
3130
super.preCompile(context, match);
3231
}
3332

3433
@Override
3534
public void compile(Context context, Pattern.Match match) throws Throwable {
3635
final MethodBuilder method = context.getMethod();
37-
assert method != null;
3836
method.writeCode(WriteInstruction.invokeVirtual(PrintStream.class.getMethod("println", Object.class)));
3937
context.setState(CompileState.CODE_BODY);
4038
}

src/main/java/org/byteskript/skript/lang/syntax/generic/SystemInputExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public boolean allowAsInputFor(Type type) {
3131

3232
@Override
3333
public Pattern.Match match(String thing, Context context) {
34-
if (!thing.endsWith("input")) return null;
34+
if (!thing.endsWith(" input")) return null;
3535
return super.match(thing, context);
3636
}
3737

@@ -50,7 +50,7 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
5050

5151
@Override
5252
public Type getReturnType() {
53-
return CommonTypes.OBJECT;
53+
return CommonTypes.STRING;
5454
}
5555

5656
}

src/main/java/org/byteskript/skript/lang/syntax/generic/SystemPropertyExpression.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class SystemPropertyExpression extends SimpleExpression implements Referent {
2424

2525
public SystemPropertyExpression() {
26-
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "[the ]system property %String%");
26+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "[the] system property %String%");
2727
try {
2828
handlers.put(StandardHandlers.GET, SystemPropertyExpression.class.getMethod("getProperty", String.class));
2929
handlers.put(StandardHandlers.FIND, SystemPropertyExpression.class.getMethod("getProperty", String.class));
@@ -34,6 +34,12 @@ public SystemPropertyExpression() {
3434
}
3535
}
3636

37+
@Override
38+
public Pattern.Match match(String thing, Context context) {
39+
if (!thing.contains("system property ")) return null;
40+
return super.match(thing, context);
41+
}
42+
3743
@Override
3844
public Type getReturnType() {
3945
return CommonTypes.STRING;

0 commit comments

Comments
 (0)