Skip to content

Commit faeac7b

Browse files
committed
Use direct accessors in type properties.
Since we have the information available, we can switch to direct accessors if a type uses a property from `this object`.
1 parent 1a2eab9 commit faeac7b

File tree

10 files changed

+94
-36
lines changed

10 files changed

+94
-36
lines changed

TODO.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Tasks are categorised by importance and marked by difficulty.
2020

2121
### Medium Importance
2222

23+
- Move some common syntax to the runtime. \
24+
A lot of extracted syntax should be in the runtime. \
25+
This would reduce the size of the output files. \
26+
Difficulty: easy
2327
- Create a library for Java GUIs. \
2428
This will probably need to interact with JavaFX. \
2529
This should be handled by somebody with experience using Java front-end. \

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<dependency>
114114
<groupId>mx.kenzie</groupId>
115115
<artifactId>foundation</artifactId>
116-
<version>1.1.3</version>
116+
<version>1.1.4</version>
117117
<scope>compile</scope>
118118
</dependency>
119119
<dependency>

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public final class ElementTree {
1919
private final Pattern.Match match;
2020
private ElementTree[] nested;
2121
public boolean compile = true;
22+
public boolean treasure = false;
2223
public boolean takeAtomic = false;
2324
public HandlerType type = StandardHandlers.GET;
2425

@@ -28,7 +29,36 @@ public ElementTree(SyntaxElement current, Pattern.Match match, ElementTree... ne
2829
this.nested = nested;
2930
}
3031

32+
public ElementTree[] falseCopy() {
33+
final ElementTree[] trees = new ElementTree[nested.length];
34+
for (int i = 0; i < nested.length; i++) {
35+
trees[i] = nested[i].deepCopy();
36+
trees[i].treasure = true;
37+
}
38+
return trees;
39+
}
40+
41+
public ElementTree shallowCopy() {
42+
final ElementTree tree = new ElementTree(current, match, nested);
43+
tree.compile = compile;
44+
tree.takeAtomic = takeAtomic;
45+
tree.type = type;
46+
return tree;
47+
}
48+
49+
public ElementTree deepCopy() {
50+
final ElementTree tree = new ElementTree(current, match, nested);
51+
tree.compile = compile;
52+
tree.takeAtomic = takeAtomic;
53+
tree.type = type;
54+
for (int i = 0; i < nested.length; i++) {
55+
tree.nested[i] = nested[i].deepCopy();
56+
}
57+
return tree;
58+
}
59+
3160
public void preCompile(Context context) { // Pre-compilation is (outer -> inner)
61+
if (treasure) return;
3262
context.setCompileCurrent(this);
3363
final HandlerType previous = context.getHandlerMode();
3464
context.setHandlerMode(type);
@@ -46,6 +76,7 @@ public void preCompile(Context context) { // Pre-compilation is (outer -> inner)
4676
}
4777

4878
public void compile(Context context) { // Post-compilation is (inner -> outer)
79+
if (treasure) return;
4980
for (ElementTree tree : nested) {
5081
tree.compile(context);
5182
}
@@ -62,6 +93,13 @@ public void compile(Context context) { // Post-compilation is (inner -> outer)
6293
context.setHandlerMode(previous);
6394
}
6495

96+
public void disableCompilation() {
97+
this.compile = false;
98+
for (final ElementTree tree : nested) {
99+
tree.disableCompilation();
100+
}
101+
}
102+
65103
public SyntaxElement current() {
66104
return current;
67105
}
@@ -87,10 +125,11 @@ public ElementTree[] nested() {
87125
public boolean equals(Object obj) {
88126
if (obj == this) return true;
89127
if (obj == null || obj.getClass() != this.getClass()) return false;
90-
var that = (ElementTree) obj;
128+
final ElementTree that = (ElementTree) obj;
91129
return Objects.equals(this.current, that.current) &&
92130
Objects.equals(this.match, that.match) &&
93-
Objects.equals(this.nested, that.nested);
131+
Arrays.equals(this.nested, that.nested) &&
132+
this.compile == that.compile;
94133
}
95134

96135
public void emptyNest() {
@@ -109,9 +148,9 @@ public int hashCode() {
109148
@Override
110149
public String toString() {
111150
return "ElementTree{" +
112-
"current=" + current.getClass().getSimpleName() + ", " +
113-
"nested=" + Arrays.toString(nested) + '}';
151+
"current=" + current.name() +
152+
", nested=" + Arrays.toString(nested) +
153+
", compile=" + compile +
154+
'}';
114155
}
115-
116-
117156
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ public void preCompile(Context context, Pattern.Match match) throws Throwable {
3636
if (!(inputs[0].current() instanceof Referent))
3737
throw new ScriptParseError(context.lineNumber(), "Syntax '" + inputs[0].current()
3838
.name() + "' cannot be set.");
39-
final ElementTree[] replacement = Arrays.copyOf(inputs[0].nested(), inputs[0].nested().length + 2);
40-
replacement[replacement.length - 2] = inputs[1];
4139
inputs[0].type = StandardHandlers.SET;
42-
inputs[0].emptyNest();
40+
final ElementTree[] trees = inputs[0].falseCopy();
41+
final ElementTree[] replacement = Arrays.copyOf(inputs[0].nested(), trees.length + 2);
42+
replacement[replacement.length - 2] = inputs[1];
4343
replacement[replacement.length - 1] = inputs[0];
4444
tree.replaceNest(replacement);
45+
inputs[0].replaceNest(trees);
4546
super.preCompile(context, match);
4647
}
4748

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

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

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

9-
import mx.kenzie.foundation.MethodErasure;
10-
import mx.kenzie.foundation.Type;
11-
import mx.kenzie.foundation.WriteInstruction;
9+
import mx.kenzie.foundation.*;
1210
import org.byteskript.skript.api.HandlerType;
1311
import org.byteskript.skript.api.Referent;
1412
import org.byteskript.skript.api.syntax.RelationalExpression;
15-
import org.byteskript.skript.compiler.CommonTypes;
16-
import org.byteskript.skript.compiler.Context;
17-
import org.byteskript.skript.compiler.Pattern;
18-
import org.byteskript.skript.compiler.SkriptLangSpec;
13+
import org.byteskript.skript.compiler.*;
1914
import org.byteskript.skript.lang.element.StandardElements;
15+
import org.byteskript.skript.lang.handler.StandardHandlers;
16+
import org.byteskript.skript.lang.syntax.type.ThisThingExpression;
2017

2118
import java.util.regex.Matcher;
2219

@@ -89,6 +86,25 @@ public Type getHolderType() {
8986
@Override
9087
public void compile(Context context, Pattern.Match match) throws Throwable {
9188
final String name = (String) match.meta();
89+
final MethodBuilder method = context.getMethod();
90+
final ElementTree tree = context.getCompileCurrent().nested()[0];
91+
if (tree != null && context.hasFlag(AreaFlag.IN_TYPE) && tree.current() instanceof ThisThingExpression) {
92+
if (context.getHandlerMode() == StandardHandlers.SET)
93+
for (final FieldBuilder field : context.getBuilder().getFields()) {
94+
if (!field.getErasure().name().equals(name)) continue;
95+
method.writeCode(WriteInstruction.loadThis());
96+
method.writeCode(WriteInstruction.swap());
97+
method.writeCode(WriteInstruction.setField(context.getBuilder().getType(), field.getErasure()));
98+
return;
99+
}
100+
if (context.getHandlerMode() == StandardHandlers.GET)
101+
for (final FieldBuilder field : context.getBuilder().getFields()) {
102+
if (!field.getErasure().name().equals(name)) continue;
103+
method.writeCode(WriteInstruction.loadThis());
104+
method.writeCode(WriteInstruction.getField(context.getBuilder().getType(), field.getErasure()));
105+
return;
106+
}
107+
}
92108
final HandlerType type = context.getHandlerMode();
93109
final MethodErasure target = context.useHandle(name, type);
94110
context.getMethod().writeCode(WriteInstruction.invokeStatic(context.getType(), target));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
public class ThisThingExpression extends SimpleExpression {
1717

18-
public ThisThingExpression() { // todo test this
18+
public ThisThingExpression() {
1919
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "this (thing|object)");
2020
}
2121

src/main/java/org/byteskript/skript/lang/syntax/type/property/PropertyMember.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public Pattern.Match match(String thing, Context context) {
3030
context.getError().addHint(this, "Properties can be declared only in types.");
3131
return null;
3232
}
33+
if (context.hasFlag(AreaFlag.IN_ABSTRACT_TYPE)) {
34+
context.getError().addHint(this, "Abstract types cannot hold properties.");
35+
return null;
36+
}
3337
if (thing.length() < 12) {
3438
context.getError().addHint(this, "Property names must be at least 3 characters long.");
3539
return null;

src/main/java/org/byteskript/skript/runtime/Skript.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ public Collection<OperationController> getProcesses() {
9999
return processes;
100100
}
101101

102-
private OperationController createController() {
103-
return new OperationController(this, factory); // todo
104-
}
105-
106102
public Future<?> runScript(final ScriptRunner runner) {
107103
return runScript(runner, null);
108104
}
@@ -126,18 +122,6 @@ public Future<?> runScript(final ScriptRunner runner, final Event event) {
126122
};
127123
factory.newThread(controller, runnable, true).start();
128124
return future;
129-
// return executor.submit(() -> {
130-
// final ScriptThread thread = (ScriptThread) Thread.currentThread();
131-
// thread.variables.clear();
132-
// thread.initiator = runner.owner();
133-
// thread.event = event;
134-
// try {
135-
// runner.run();
136-
// } catch (ThreadDeath ignore) {
137-
// } catch (Throwable ex) {
138-
// ex.printStackTrace(); // todo
139-
// }
140-
// });
141125
}
142126

143127
public boolean runEvent(final Event event) {
@@ -360,7 +344,7 @@ public Script loadScript(final InputStream stream)
360344
public Script loadScript(final File source)
361345
throws IOException {
362346
try (InputStream stream = new FileInputStream(source)) {
363-
return loadScript(stream, source.getName()); // todo
347+
return loadScript(stream, source.getName());
364348
}
365349
}
366350

src/test/java/org/byteskript/skript/test/PropertiesTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static void start() throws Throwable {
2323
final PostCompileClass[] classes = skript.compileComplexScript(TypesTest.class.getClassLoader()
2424
.getResourceAsStream("properties.bsk"), "skript.properties");
2525
for (PostCompileClass cls : classes) {
26+
debug(cls);
2627
if (script == null)
2728
script = skript.loadScript(cls);
2829
else skript.loadScript(cls);
@@ -40,6 +41,7 @@ public void basic_use() throws Throwable {
4041
public void simple_example() throws Throwable {
4142
final Member function = script.getFunction("simple_example");
4243
assert function != null;
44+
function.run(skript).get();
4345
function.invoke();
4446
}
4547

src/test/resources/properties.bsk

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@ type Blob:
4040
type: String
4141
property age:
4242
type: Number
43+
property house:
44+
local: true
45+
function set_house(house):
46+
trigger:
47+
set (house of (this object)) to {house}
48+
assert house of this object is {house}: "Set comparison failed."
4349

44-
function simple_example:
50+
function simple_example: // todo - check other property accessors inside types
4551
trigger:
4652
set {thing} to a new Blob
4753
set name of {thing} to "Thing"
4854
set age of {thing} to 62
4955
assert name of {thing} is "Thing": "Name property set/access failed."
5056
assert age of {thing} is 62: "Age property set/access failed."
57+
run set_house("hello") from {thing}
58+
assert house of {thing} is "hello": "Local set failed."

0 commit comments

Comments
 (0)