Skip to content

Commit e084895

Browse files
committed
This object for local function calls.
1 parent 4726702 commit e084895

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.byteskript.skript.lang.syntax.map.MapCreator;
5858
import org.byteskript.skript.lang.syntax.maths.*;
5959
import org.byteskript.skript.lang.syntax.timing.*;
60+
import org.byteskript.skript.lang.syntax.type.ThisThingExpression;
6061
import org.byteskript.skript.lang.syntax.type.TypeCreator;
6162
import org.byteskript.skript.lang.syntax.type.TypeMember;
6263
import org.byteskript.skript.lang.syntax.variable.AtomicVariableExpression;
@@ -166,6 +167,7 @@ private SkriptLangSpec() {
166167
new ImplicitArrayCreator(),
167168
new BracketExpression(),
168169
new BooleanLiteral(),
170+
new ThisThingExpression(),
169171
new ThreadVariableExpression(),
170172
new AtomicVariableExpression(),
171173
new GlobalVariableExpression(),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.lang.syntax.type;
8+
9+
import mx.kenzie.foundation.MethodBuilder;
10+
import mx.kenzie.foundation.Type;
11+
import mx.kenzie.foundation.WriteInstruction;
12+
import org.byteskript.skript.api.syntax.SimpleExpression;
13+
import org.byteskript.skript.compiler.*;
14+
import org.byteskript.skript.lang.element.StandardElements;
15+
16+
public class ThisThingExpression extends SimpleExpression {
17+
18+
public ThisThingExpression() { // todo test this
19+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "this (thing|object)");
20+
}
21+
22+
@Override
23+
public Type getReturnType() {
24+
return CommonTypes.STRING;
25+
}
26+
27+
@Override
28+
public Pattern.Match match(String thing, Context context) {
29+
if (!thing.startsWith("this ")) return null;
30+
if (!context.hasFlag(AreaFlag.IN_TYPE)) return null;
31+
return super.match(thing, context);
32+
}
33+
34+
@Override
35+
public void compile(Context context, Pattern.Match match) throws Throwable {
36+
final MethodBuilder method = context.getMethod();
37+
assert method != null;
38+
method.writeCode(WriteInstruction.loadThis());
39+
}
40+
41+
}

src/main/java/org/byteskript/skript/lang/syntax/type/TypeCreator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ public static Object create(Object object)
3434
if (object == null) return null;
3535
if (!(object instanceof Class<?> type))
3636
throw new ScriptRuntimeError("Tried to create a new non-type thing: " + object);
37-
return type.newInstance();
37+
try {
38+
return type.newInstance();
39+
} catch (InstantiationException ex) {
40+
throw new ScriptRuntimeError("The type '" + ((Class<?>) object).getSimpleName() + "' cannot be created like this.");
41+
} catch (IllegalAccessException ex) {
42+
throw new ScriptRuntimeError("The type '" + ((Class<?>) object).getSimpleName() + "' does not permit creation.");
43+
}
3844
}
3945

4046
}

src/test/resources/types.bsk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// function another_func
55

66
type Box:
7+
function bonk:
8+
return: Boolean
9+
trigger:
10+
set {var} to my_func() from this object
11+
assert {var} is "hello": "Member-local function didn't return properly."
12+
return true
713
function my_func:
814
return: String
915
trigger:
@@ -27,3 +33,5 @@ function basic_use:
2733
assert {word} is "hello": "Member function call failed."
2834
set {number} to func_with_args(3) from {thing}
2935
assert {number} is 211: "Member function call with args failed."
36+
set {boolean} to bonk() from {thing}
37+
assert {boolean} is true: "Member function didn't return correctly."

0 commit comments

Comments
 (0)