Skip to content

Commit 8a396ab

Browse files
committed
Merge branch 'property-expressions'
2 parents 996d77b + 5816906 commit 8a396ab

File tree

9 files changed

+76
-42
lines changed

9 files changed

+76
-42
lines changed

src/main/java/org/byteskript/skript/api/ModifiableLibrary.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.byteskript.skript.compiler.Context;
1515

1616
import java.lang.reflect.Method;
17-
import java.lang.reflect.Modifier;
1817
import java.util.*;
1918

2019
public class ModifiableLibrary implements SyntaxAnnotationUnwrapper, Library {
@@ -51,26 +50,12 @@ public void registerEvent(EventHolder event) {
5150
this.registerValues(event);
5251
}
5352

53+
public void registerProperty(PropertyHandler handler) {
54+
this.properties.add(handler);
55+
}
56+
5457
public void registerProperty(String name, HandlerType type, Method handler) {
55-
final Type holder;
56-
final Type value;
57-
if (type.expectInputs()) {
58-
final Class<?>[] classes = handler.getParameterTypes();
59-
assert classes.length > 0;
60-
value = new Type(classes[classes.length - 1]);
61-
} else if (type.expectReturn()) {
62-
value = new Type(handler.getReturnType());
63-
} else {
64-
value = new Type(void.class);
65-
}
66-
if (Modifier.isStatic(handler.getModifiers())) {
67-
final Class<?>[] classes = handler.getParameterTypes();
68-
assert classes.length > 0;
69-
holder = new Type(classes[0]);
70-
} else {
71-
holder = new Type(handler.getDeclaringClass());
72-
}
73-
this.properties.add(new PropertyHandler(name, type, holder, value, handler));
58+
this.properties.add(new PropertyHandler(type, handler, name));
7459
}
7560

7661
public void registerTypes(Class<?>... classes) {

src/main/java/org/byteskript/skript/api/PropertyHandler.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,35 @@
99
import mx.kenzie.foundation.Type;
1010

1111
import java.lang.reflect.Method;
12+
import java.lang.reflect.Modifier;
1213

1314
public record PropertyHandler(String name, HandlerType type, Type holder, Type value, Method method) {
15+
16+
public PropertyHandler(HandlerType type, Method method, String name) {
17+
this(name, type, createHolder(method), createValue(type, method), method);
18+
}
19+
20+
private static Type createValue(HandlerType type, Method method) {
21+
if (type.expectInputs()) {
22+
final Class<?>[] classes = method.getParameterTypes();
23+
assert classes.length > 0;
24+
return new Type(classes[classes.length - 1]);
25+
} else if (type.expectReturn()) {
26+
return new Type(method.getReturnType());
27+
} else {
28+
return new Type(void.class);
29+
}
30+
}
31+
32+
private static Type createHolder(Method method) {
33+
if (Modifier.isStatic(method.getModifiers())) {
34+
final Class<?>[] classes = method.getParameterTypes();
35+
assert classes.length > 0;
36+
return new Type(classes[0]);
37+
} else {
38+
return new Type(method.getDeclaringClass());
39+
}
40+
}
41+
42+
1443
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ public void setStoredVariableName(String storedVariableName) {
126126

127127
public abstract int methodIndexShift();
128128

129+
public abstract boolean hasHandle(String property, HandlerType type);
130+
129131
public abstract MethodErasure useHandle(String property, HandlerType type);
130132

131133
public abstract void setHandlerMode(HandlerType type);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ public int methodIndexShift() {
345345
return indexShift++;
346346
}
347347

348+
@Override
349+
public boolean hasHandle(String property, HandlerType type) {
350+
for (final Library library : this.libraries) {
351+
for (final PropertyHandler handler : library.getProperties()) {
352+
if (handler.name().equals(property)) return true;
353+
}
354+
}
355+
return false;
356+
}
357+
348358
@Override
349359
public MethodErasure useHandle(String property, HandlerType type) {
350360
this.usedProperties.putIfAbsent(type, new ArrayList<>());
@@ -357,6 +367,7 @@ public MethodErasure useHandle(String property, HandlerType type) {
357367
int uses = 0;
358368
for (PropertyAccessGenerator generator : list) {
359369
if (!generator.getName().equals(property)) continue;
370+
if (!generator.getType().equals(type)) continue;
360371
uses++;
361372
generator.addUse(handler.holder(), handler.method());
362373
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.byteskript.skript.error.ScriptCompileError;
2020
import org.byteskript.skript.error.ScriptRuntimeError;
2121
import org.byteskript.skript.lang.element.StandardElements;
22-
import org.byteskript.skript.lang.handler.StandardHandlers;
2322
import org.byteskript.skript.lang.syntax.comparison.*;
2423
import org.byteskript.skript.lang.syntax.control.AddEffect;
2524
import org.byteskript.skript.lang.syntax.control.DeleteEffect;
@@ -72,6 +71,8 @@
7271
import java.util.zip.ZipEntry;
7372
import java.util.zip.ZipInputStream;
7473

74+
import static org.byteskript.skript.lang.handler.StandardHandlers.GET;
75+
7576
public final class SkriptLangSpec extends ModifiableLibrary implements LanguageDefinition, Library {
7677
public static final Pattern LINE_COMMENT = Pattern.compile("//.*(?=(\\R|$|\\n))");
7778
public static final Pattern BLOCK_COMMENT = Pattern.compile("/\\*[\\s\\S]*?\\*/");
@@ -231,13 +232,13 @@ private SkriptLangSpec() {
231232
generateSyntaxFrom(IOHandlers.class);
232233
generateSyntaxFrom(JavaRelay.class);
233234
try {
234-
registerProperty("keys", StandardHandlers.GET, DataMap.class.getMethod("getKeys", Map.class));
235-
registerProperty("values", StandardHandlers.GET, DataMap.class.getMethod("getValues", Map.class));
236-
registerProperty("size", StandardHandlers.GET, DataMap.class.getMethod("getSize", Map.class));
237-
registerProperty("size", StandardHandlers.GET, DataList.class.getMethod("getSize", Collection.class));
238-
registerProperty("class", StandardHandlers.GET, Object.class.getMethod("getClass"));
239-
registerProperty("name", StandardHandlers.GET, Class.class.getMethod("getSimpleName"));
240-
registerProperty("path", StandardHandlers.GET, Class.class.getMethod("getName"));
235+
registerProperty("keys", GET, DataMap.class.getMethod("getKeys", Map.class));
236+
registerProperty("values", GET, DataMap.class.getMethod("getValues", Map.class));
237+
registerProperty("size", GET, DataMap.class.getMethod("getSize", Map.class));
238+
registerProperty("size", GET, DataList.class.getMethod("getSize", Collection.class));
239+
registerProperty("class", GET, Object.class.getMethod("getClass"));
240+
registerProperty("name", GET, Class.class.getMethod("getSimpleName"));
241+
registerProperty("path", GET, Class.class.getMethod("getName"));
241242
} catch (NoSuchMethodException e) {
242243
e.printStackTrace();
243244
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class BreakIfEffect extends Effect {
2222

2323
public BreakIfEffect() {
24-
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "(break|exit) [[the ]current ]section if %Boolean%");
24+
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "(break|exit) [[the ]current] section if %Boolean%");
2525
}
2626

2727
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class ContinueEffect extends Effect {
2222

2323
public ContinueEffect() {
24-
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "continue [[the ]current ]loop");
24+
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "continue [[the ]current] loop");
2525
}
2626

2727
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class ExitEffect extends Effect {
1919

2020
public ExitEffect() {
21-
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "exit[ the] program");
21+
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "exit [the] program");
2222
}
2323

2424
@Override

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
import java.util.regex.Matcher;
2222

23+
/**
24+
* This handles all properties and delegates the compilation
25+
* to the expression stubs.
26+
*/
2327
public class PropertyExpression extends RelationalExpression implements Referent {
2428
protected final java.util.regex.Pattern[] patterns;
2529

@@ -40,17 +44,19 @@ public boolean allowAsInputFor(Type type) {
4044

4145
@Override
4246
public Pattern.Match match(String thing, Context context) {
43-
if (!(thing.contains(" of ") || thing.contains("'s ") || thing.contains("-"))) return null;
44-
for (int i = 0; i < patterns.length; i++) {
45-
final java.util.regex.Pattern pattern = patterns[i];
46-
final Matcher matcher = pattern.matcher(thing);
47-
if (!matcher.find()) continue;
48-
final String name = matcher.group("name");
49-
final Matcher dummy = createDummy(thing, i, matcher);
50-
dummy.find();
51-
return new Pattern.Match(dummy, name, CommonTypes.OBJECT);
52-
}
53-
return null;
47+
final int i;
48+
if (thing.contains(" of ")) i = 0;
49+
else if (thing.contains("'s ")) i = 1;
50+
else if (thing.contains("-")) i = 2;
51+
else return null;
52+
final java.util.regex.Pattern pattern = patterns[i];
53+
final Matcher matcher = pattern.matcher(thing);
54+
if (!matcher.find()) return null;
55+
final String name = matcher.group("name");
56+
if (!context.hasHandle(name, context.getHandlerMode())) return null;
57+
final Matcher dummy = createDummy(thing, i, matcher);
58+
dummy.find();
59+
return new Pattern.Match(dummy, name, CommonTypes.OBJECT);
5460
}
5561

5662
private Matcher createDummy(String thing, int index, Matcher matcher) {

0 commit comments

Comments
 (0)