Skip to content

Commit 4726702

Browse files
committed
Merge branch 'type-member'
2 parents 60000e5 + ef819ed commit 4726702

23 files changed

+625
-110
lines changed

TODO.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Tasks are categorised by importance and marked by difficulty.
1212
- Finish documentation for built-in syntax. \
1313
Some syntax is missing proper [documentation](https://moderocky.gitbook.io/byteskript/). \
1414
Difficulty: trivial
15+
- Make proper handlers for property expressions. \
16+
The current system is inadequate for complex properties.
17+
Difficulty: medium
1518
- Write more comprehensive tests. \
1619
It is important to test all expected behaviour - some syntax are missing full tests. \
1720
It is also important to test forbidden behaviour does *not* work, and proper negative tests are not implemented yet. \
@@ -20,11 +23,6 @@ Tasks are categorised by importance and marked by difficulty.
2023

2124
### Medium Importance
2225

23-
- Create an object/type member. \
24-
Interacting with a lot of object-oriented JVM languages will require some sort of object system. \
25-
These need implement/extend functionality, and may also require method behaviour. \
26-
Language structure will need to be decided. \
27-
Difficulty: hard
2826
- Create a library for interacting with Java directly. \
2927
This will allow a lot more to be done within scripts, rather than relying on libraries to provide complex
3028
functionality. \

pom.xml

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

77
<groupId>org.byteskript</groupId>
88
<artifactId>byteskript</artifactId>
9-
<version>1.0.1</version>
9+
<version>1.0.2</version>
1010
<name>ByteSkript</name>
1111

1212
<properties>
@@ -90,6 +90,17 @@
9090
</plugins>
9191
</build>
9292

93+
<distributionManagement>
94+
<repository>
95+
<id>pan-repo</id>
96+
<url>https://gitlab.com/api/v4/projects/18568066/packages/maven</url>
97+
</repository>
98+
<snapshotRepository>
99+
<id>pan-repo</id>
100+
<url>https://gitlab.com/api/v4/projects/18568066/packages/maven</url>
101+
</snapshotRepository>
102+
</distributionManagement>
103+
93104
<repositories>
94105
<repository>
95106
<id>pan-repo</id>
@@ -102,7 +113,7 @@
102113
<dependency>
103114
<groupId>mx.kenzie</groupId>
104115
<artifactId>foundation</artifactId>
105-
<version>1.1.1</version>
116+
<version>1.1.2</version>
106117
<scope>compile</scope>
107118
</dependency>
108119
<dependency>
@@ -120,7 +131,7 @@
120131
<dependency>
121132
<groupId>mx.kenzie</groupId>
122133
<artifactId>mirror</artifactId>
123-
<version>5.0.0</version>
134+
<version>5.0.1</version>
124135
<scope>compile</scope>
125136
</dependency>
126137
<dependency>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.api;
8+
9+
/**
10+
* Compiler flags to provide trivial information to the matcher.
11+
* This should be implemented by an enum.
12+
*/
13+
public interface Flag {
14+
15+
String name();
16+
17+
}

src/main/java/org/byteskript/skript/api/syntax/EventHolder.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
import org.byteskript.skript.api.AsyncEvent;
1212
import org.byteskript.skript.api.Event;
1313
import org.byteskript.skript.api.Library;
14-
import org.byteskript.skript.compiler.CommonTypes;
15-
import org.byteskript.skript.compiler.CompileState;
16-
import org.byteskript.skript.compiler.Context;
17-
import org.byteskript.skript.compiler.Pattern;
14+
import org.byteskript.skript.compiler.*;
15+
import org.byteskript.skript.compiler.structure.SectionMeta;
1816
import org.byteskript.skript.lang.element.StandardElements;
1917
import org.byteskript.skript.runtime.data.EventData;
2018
import org.byteskript.skript.runtime.data.SourceData;
@@ -29,6 +27,12 @@ public EventHolder(Library provider, String... patterns) {
2927

3028
public abstract Class<? extends Event> eventClass();
3129

30+
@Override
31+
public void onSectionExit(Context context, SectionMeta meta) {
32+
context.removeFlag(AreaFlag.IN_EVENT);
33+
super.onSectionExit(context, meta);
34+
}
35+
3236
@Override
3337
public void compile(Context context, Pattern.Match match) {
3438
final Class<? extends Event> eventClass = eventClass();
@@ -47,6 +51,7 @@ public void compile(Context context, Pattern.Match match) {
4751
.addValue("name", name())
4852
.addValue("event", org.objectweb.asm.Type.getObjectType(new Type(eventClass).internalName()))
4953
.addValue("async", AsyncEvent.class.isAssignableFrom(eventClass));
54+
context.addFlag(AreaFlag.IN_EVENT);
5055
}
5156

5257
@Override
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.api.syntax;
8+
9+
import mx.kenzie.foundation.compiler.State;
10+
import org.byteskript.skript.api.LanguageElement;
11+
import org.byteskript.skript.api.Library;
12+
import org.byteskript.skript.compiler.CompileState;
13+
import org.byteskript.skript.compiler.Context;
14+
import org.byteskript.skript.compiler.Pattern;
15+
16+
public abstract class SimpleEntry extends Element {
17+
public SimpleEntry(Library provider, LanguageElement type, String... patterns) {
18+
super(provider, type, patterns);
19+
}
20+
21+
@Override
22+
public Pattern.Match match(String thing, Context context) {
23+
return super.match(thing, context);
24+
}
25+
26+
@Override
27+
public boolean allowedIn(State state, Context context) {
28+
return state == CompileState.MEMBER_BODY;
29+
}
30+
31+
@Override
32+
public CompileState getSubState() {
33+
return CompileState.MEMBER_BODY;
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.compiler;
8+
9+
import org.byteskript.skript.api.Flag;
10+
11+
public enum AreaFlag implements Flag {
12+
IN_FUNCTION,
13+
IN_TYPE,
14+
IN_EVENT
15+
16+
}

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

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import java.lang.reflect.Method;
1919
import java.util.Random;
2020

21-
import static org.objectweb.asm.Opcodes.*;
22-
2321
/**
2422
* The function call-site class compiler.
2523
* This cannot use {@link mx.kenzie.foundation} because it is required for
@@ -48,7 +46,7 @@ public Class<?> createClass()
4846
final Class<?>[] parameters = target.getParameterTypes();
4947
final Class<?> expected = source.returnType();
5048
final Class<?> result = target.getReturnType();
51-
if (arguments.length != parameters.length)
49+
if (arguments.length != parameters.length) // todo dynamic?
5250
throw new ScriptRuntimeError("Function argument count did not match target parameter count.");
5351
final ClassWriter writer = new ClassWriter(0);
5452
writer.visit(Skript.JAVA_VERSION, 0x0001 | 0x1000, location, null, "java/lang/Object", null);
@@ -62,13 +60,18 @@ public Class<?> createClass()
6260
final Class<?> parameter = parameters[i];
6361
visitor.visitVarInsn(20 + this.instructionOffset(argument), i);
6462
this.boxAtomic(visitor, parameter);
65-
visitor.visitTypeInsn(CHECKCAST, Type.getInternalName(this.getUnboxingType(parameter)));
63+
visitor.visitTypeInsn(192, Type.getInternalName(this.getUnboxingType(parameter)));
6664
this.unbox(visitor, parameter);
6765
}
6866
this.invoke(visitor);
69-
this.box(visitor, result);
70-
visitor.visitTypeInsn(CHECKCAST, Type.getInternalName(this.getWrapperType(expected)));
71-
visitor.visitInsn(171 + this.instructionOffset(expected));
67+
if (result == void.class) {
68+
visitor.visitInsn(1);
69+
visitor.visitInsn(176);
70+
} else {
71+
this.box(visitor, result);
72+
visitor.visitTypeInsn(192, Type.getInternalName(this.getWrapperType(expected)));
73+
visitor.visitInsn(171 + this.instructionOffset(expected));
74+
}
7275
visitor.visitMaxs(Math.max(parameters.length + 1 + this.wideIndexOffset(parameters, result), 1), arguments.length);
7376
visitor.visitEnd();
7477
writer.visitEnd();
@@ -85,38 +88,7 @@ public CallSite getCallSite()
8588
//region Utilities
8689
protected void invoke(MethodVisitor visitor) {
8790
final boolean special = target.getDeclaringClass().isInterface();
88-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(target.getDeclaringClass()), target.getName(), Type.getMethodDescriptor(target), special);
89-
}
90-
91-
protected void doTypeConversion(MethodVisitor visitor, Class<?> from, Class<?> to) {
92-
if (from == to) return;
93-
if (from == void.class || to == void.class) return;
94-
if (from.isPrimitive() && to.isPrimitive()) {
95-
final int opcode;
96-
if (from == float.class) {
97-
if (to == double.class) opcode = F2D;
98-
else if (to == long.class) opcode = F2L;
99-
else opcode = F2I;
100-
} else if (from == double.class) {
101-
if (to == float.class) opcode = D2F;
102-
else if (to == long.class) opcode = D2L;
103-
else opcode = D2I;
104-
} else if (from == long.class) {
105-
if (to == float.class) opcode = L2F;
106-
else if (to == double.class) opcode = L2D;
107-
else opcode = L2I;
108-
} else {
109-
if (to == float.class) opcode = I2F;
110-
else if (to == double.class) opcode = I2D;
111-
else if (to == byte.class) opcode = I2B;
112-
else if (to == short.class) opcode = I2S;
113-
else if (to == char.class) opcode = I2C;
114-
else opcode = I2L;
115-
}
116-
visitor.visitInsn(opcode);
117-
} else if (from.isPrimitive() ^ to.isPrimitive()) {
118-
throw new IllegalArgumentException("Type wrapping is currently unsupported due to side-effects: '" + from.getSimpleName() + "' -> '" + to.getSimpleName() + "'");
119-
} else visitor.visitTypeInsn(CHECKCAST, Type.getInternalName(to));
91+
visitor.visitMethodInsn(184, Type.getInternalName(target.getDeclaringClass()), target.getName(), Type.getMethodDescriptor(target), special);
12092
}
12193

12294
protected Class<?> getUnboxingType(Class<?> primitive) {
@@ -143,48 +115,48 @@ protected Class<?> getWrapperType(Class<?> primitive) {
143115

144116
protected void boxAtomic(MethodVisitor visitor, Class<?> parameter) {
145117
if (parameter == AtomicVariable.class)
146-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(AtomicVariable.class), "wrap", "(Ljava/lang/Object;)" + Type.getDescriptor(AtomicVariable.class), false);
118+
visitor.visitMethodInsn(184, Type.getInternalName(AtomicVariable.class), "wrap", "(Ljava/lang/Object;)" + Type.getDescriptor(AtomicVariable.class), false);
147119
else
148-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(AtomicVariable.class), "unwrap", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
120+
visitor.visitMethodInsn(184, Type.getInternalName(AtomicVariable.class), "unwrap", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
149121
}
150122

151123
protected void unbox(MethodVisitor visitor, Class<?> parameter) {
152124
final String source = Type.getInternalName(OperatorHandler.class);
153125
if (parameter == byte.class)
154-
visitor.visitMethodInsn(INVOKESTATIC, source, "unboxB", "(Ljava/lang/Number;)B", false);
126+
visitor.visitMethodInsn(184, source, "unboxB", "(Ljava/lang/Number;)B", false);
155127
if (parameter == short.class)
156-
visitor.visitMethodInsn(INVOKESTATIC, source, "unboxS", "(Ljava/lang/Number;)S", false);
128+
visitor.visitMethodInsn(184, source, "unboxS", "(Ljava/lang/Number;)S", false);
157129
if (parameter == int.class)
158-
visitor.visitMethodInsn(INVOKESTATIC, source, "unboxI", "(Ljava/lang/Number;)I", false);
130+
visitor.visitMethodInsn(184, source, "unboxI", "(Ljava/lang/Number;)I", false);
159131
if (parameter == long.class)
160-
visitor.visitMethodInsn(INVOKESTATIC, source, "unboxJ", "(Ljava/lang/Number;)J", false);
132+
visitor.visitMethodInsn(184, source, "unboxJ", "(Ljava/lang/Number;)J", false);
161133
if (parameter == float.class)
162-
visitor.visitMethodInsn(INVOKESTATIC, source, "unboxF", "(Ljava/lang/Number;)F", false);
134+
visitor.visitMethodInsn(184, source, "unboxF", "(Ljava/lang/Number;)F", false);
163135
if (parameter == double.class)
164-
visitor.visitMethodInsn(INVOKESTATIC, source, "unboxD", "(Ljava/lang/Number;)D", false);
136+
visitor.visitMethodInsn(184, source, "unboxD", "(Ljava/lang/Number;)D", false);
165137
if (parameter == boolean.class)
166-
visitor.visitMethodInsn(INVOKESTATIC, source, "unbox", "(Ljava/lang/Boolean;)Z", false);
138+
visitor.visitMethodInsn(184, source, "unbox", "(Ljava/lang/Boolean;)Z", false);
167139
if (parameter == char.class)
168-
visitor.visitMethodInsn(INVOKESTATIC, source, "unbox", "(Ljava/lang/Character;)C", false);
140+
visitor.visitMethodInsn(184, source, "unbox", "(Ljava/lang/Character;)C", false);
169141
}
170142

171143
protected void box(MethodVisitor visitor, Class<?> value) {
172144
if (value == byte.class)
173-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Byte.class), "valueOf", "(B)Ljava/lang/Byte;", false);
145+
visitor.visitMethodInsn(184, Type.getInternalName(Byte.class), "valueOf", "(B)Ljava/lang/Byte;", false);
174146
if (value == short.class)
175-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Short.class), "valueOf", "(S)Ljava/lang/Short;", false);
147+
visitor.visitMethodInsn(184, Type.getInternalName(Short.class), "valueOf", "(S)Ljava/lang/Short;", false);
176148
if (value == int.class)
177-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Integer.class), "valueOf", "(I)Ljava/lang/Integer;", false);
149+
visitor.visitMethodInsn(184, Type.getInternalName(Integer.class), "valueOf", "(I)Ljava/lang/Integer;", false);
178150
if (value == long.class)
179-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Long.class), "valueOf", "(J)Ljava/lang/Long;", false);
151+
visitor.visitMethodInsn(184, Type.getInternalName(Long.class), "valueOf", "(J)Ljava/lang/Long;", false);
180152
if (value == float.class)
181-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Float.class), "valueOf", "(F)Ljava/lang/Float;", false);
153+
visitor.visitMethodInsn(184, Type.getInternalName(Float.class), "valueOf", "(F)Ljava/lang/Float;", false);
182154
if (value == double.class)
183-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Double.class), "valueOf", "(D)Ljava/lang/Double;", false);
155+
visitor.visitMethodInsn(184, Type.getInternalName(Double.class), "valueOf", "(D)Ljava/lang/Double;", false);
184156
if (value == boolean.class)
185-
visitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Boolean.class), "valueOf", "(Z)Ljava/lang/Boolean;", false);
157+
visitor.visitMethodInsn(184, Type.getInternalName(Boolean.class), "valueOf", "(Z)Ljava/lang/Boolean;", false);
186158
if (value == void.class)
187-
visitor.visitInsn(ACONST_NULL);
159+
visitor.visitInsn(1);
188160
}
189161

190162
protected int wideIndexOffset(Class<?>[] params, Class<?> ret) {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88

99
import mx.kenzie.foundation.*;
1010
import mx.kenzie.foundation.compiler.State;
11-
import org.byteskript.skript.api.HandlerType;
12-
import org.byteskript.skript.api.LanguageElement;
13-
import org.byteskript.skript.api.Library;
14-
import org.byteskript.skript.api.SyntaxElement;
11+
import org.byteskript.skript.api.*;
1512
import org.byteskript.skript.api.syntax.Section;
1613
import org.byteskript.skript.compiler.structure.*;
1714

@@ -35,6 +32,12 @@ public void setStoredVariableName(String storedVariableName) {
3532

3633
protected String storedVariableName;
3734

35+
public abstract boolean hasFlag(Flag flag);
36+
37+
public abstract void addFlag(Flag flag);
38+
39+
public abstract void removeFlag(Flag flag);
40+
3841
public abstract LanguageElement getExpected();
3942

4043
public abstract Collection<Type> getAvailableTypes();
@@ -55,8 +58,16 @@ public void setStoredVariableName(String storedVariableName) {
5558

5659
public abstract ClassBuilder getBuilder();
5760

61+
public abstract void useSubBuilder(final ClassBuilder builder);
62+
63+
public abstract ClassBuilder endSubBuilder();
64+
5865
public abstract ClassBuilder addSuppressedBuilder(final Type type);
5966

67+
public abstract ClassBuilder getSuppressedBuilder(Type type);
68+
69+
public abstract ClassBuilder getSuppressedBuilder();
70+
6071
public abstract MethodBuilder getMethod();
6172

6273
public abstract FieldBuilder getField();

0 commit comments

Comments
 (0)