Skip to content

Commit ebd06c4

Browse files
committed
Add loop and section tests.
1 parent 1169dd8 commit ebd06c4

34 files changed

+482
-16
lines changed

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

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

106106
public abstract <Tree extends ProgrammaticSplitTree> Tree findTree(Class<Tree> type);
107107

108-
public abstract ProgrammaticSplitTree getTree(SectionMeta meta);
109-
110108
public abstract void closeAllTrees();
111109

112110
public abstract void removeTree(ProgrammaticSplitTree tree);
@@ -174,8 +172,12 @@ public void destroySection() {
174172
final Section section = handlers[i];
175173
section.onSectionExit(this, meta);
176174
}
175+
final ProgrammaticSplitTree tree = this.getTree(meta);
176+
if (tree != null) tree.close(this);
177177
}
178178

179+
public abstract ProgrammaticSplitTree getTree(SectionMeta meta);
180+
179181
public abstract boolean hasFunction(String name, int arguments);
180182

181183
public Function getDefaultFunction(String name, int arguments) {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,6 @@ public <Tree extends ProgrammaticSplitTree> Tree findTree(Class<Tree> type) {
296296
return null;
297297
}
298298

299-
@Override
300-
public ProgrammaticSplitTree getTree(SectionMeta meta) {
301-
for (final ProgrammaticSplitTree tree : trees) {
302-
if (tree.owner() == meta) return tree;
303-
}
304-
return null;
305-
}
306-
307299
@Override
308300
public synchronized void closeAllTrees() {
309301
for (final ProgrammaticSplitTree tree : trees.toArray(new ProgrammaticSplitTree[0])) {
@@ -412,6 +404,14 @@ public ProgrammaticSplitTree getCurrentTree() {
412404
return trees.isEmpty() ? null : trees.get(0);
413405
}
414406

407+
@Override
408+
public ProgrammaticSplitTree getTree(SectionMeta meta) {
409+
for (final ProgrammaticSplitTree tree : trees) {
410+
if (tree.owner() == meta) return tree;
411+
}
412+
return null;
413+
}
414+
415415
@Override
416416
public boolean hasFunction(String name, int arguments) {
417417
for (Function function : functions) {

src/main/java/org/byteskript/skript/compiler/structure/TryCatchTree.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
package org.byteskript.skript.compiler.structure;
88

99
import mx.kenzie.foundation.MethodBuilder;
10+
import mx.kenzie.foundation.WriteInstruction;
1011
import org.byteskript.skript.api.SyntaxElement;
1112
import org.byteskript.skript.compiler.CommonTypes;
1213
import org.byteskript.skript.compiler.Context;
1314
import org.byteskript.skript.error.ScriptCompileError;
1415
import org.byteskript.skript.lang.syntax.flow.error.CatchSection;
1516
import org.objectweb.asm.Label;
17+
import org.objectweb.asm.Opcodes;
1618

1719
public class TryCatchTree extends ProgrammaticSplitTree {
1820

1921
private final SectionMeta owner;
2022
private final Label startTry = new Label();
2123
private final Label startCatch = new Label();
2224
private final MultiLabel end;
25+
protected boolean caught;
2326
private boolean open;
2427

2528
public TryCatchTree(SectionMeta owner, MultiLabel end) {
@@ -57,13 +60,21 @@ public void start(Context context) {
5760

5861
@Override
5962
public void branch(Context context) {
63+
this.caught = true;
6064
}
6165

6266
@Override
6367
public void close(Context context) {
6468
this.open = false;
6569
final MethodBuilder method = context.getMethod();
6670
if (method == null) throw new ScriptCompileError(context.lineNumber(), "Try/catch section left unclosed.");
71+
if (!caught) {
72+
context.getMethod().writeCode(((writer, visitor) -> {
73+
visitor.visitJumpInsn(Opcodes.GOTO, end.use());
74+
visitor.visitLabel(startCatch);
75+
}));
76+
method.writeCode(WriteInstruction.pop());
77+
}
6778
method.writeCode(end.instruction());
6879
context.removeTree(this);
6980
}

src/main/java/org/byteskript/skript/lang/syntax/entry/Extends.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Extends() {
3232
@Override
3333
public void compile(Context context, Pattern.Match match) {
3434
final String name = match.meta();
35-
final Type type = context.getType(name);
35+
final Type type = context.findType(name);
3636
if (type != null) context.getBuilder().setSuperclass(type);
3737
else context.getBuilder().setSuperclass(new Type(name.replace('.', '/')));
3838
context.setState(CompileState.ROOT);

src/main/java/org/byteskript/skript/lang/syntax/entry/ReturnType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ReturnType() {
3737
@Override
3838
public void compile(Context context, Pattern.Match match) {
3939
final String name = match.meta();
40-
final Type type = context.getType(name);
40+
final Type type = context.findType(name);
4141
if (type != null) context.getMethod().setReturnType(type);
4242
else context.getMethod().setReturnType(new Type(name.replace('.', '/')));
4343
context.setState(CompileState.MEMBER_BODY);

src/main/java/org/byteskript/skript/lang/syntax/entry/Template.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Template() {
3232
@Override
3333
public void compile(Context context, Pattern.Match match) {
3434
final String name = match.meta();
35-
final Type type = context.getType(name);
35+
final Type type = context.findType(name);
3636
if (type != null) context.getBuilder().addInterfaces(type);
3737
else context.getBuilder().addInterfaces(new Type(name.replace('.', '/')));
3838
context.setState(CompileState.ROOT);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.byteskript.skript.lang.syntax.flow;
88

99
import mx.kenzie.foundation.MethodBuilder;
10+
import mx.kenzie.foundation.Type;
1011
import mx.kenzie.foundation.WriteInstruction;
1112
import org.byteskript.skript.api.note.Documentation;
1213
import org.byteskript.skript.api.syntax.Effect;
@@ -42,8 +43,12 @@ public StopEffect() {
4243
public void compile(Context context, Pattern.Match match) {
4344
final MethodBuilder method = context.getMethod();
4445
assert method != null;
45-
method.writeCode(WriteInstruction.pushNull());
46-
method.writeCode(WriteInstruction.returnObject());
46+
if (method.getErasure().returnType().equals(new Type(void.class))) {
47+
method.writeCode(WriteInstruction.returnEmpty());
48+
} else {
49+
method.writeCode(WriteInstruction.pushNull());
50+
method.writeCode(WriteInstruction.returnObject());
51+
}
4752
context.setState(CompileState.CODE_BODY);
4853
}
4954

src/main/java/org/byteskript/skript/lang/syntax/flow/error/CatchSection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
7171
final Label label = tree.getEnd().use();
7272
final Label next = tree.getStartCatch();
7373
final MethodBuilder method = context.getMethod();
74+
tree.branch(context);
7475
if (method == null) throw new ScriptCompileError(context.lineNumber(), "Try/catch used outside method.");
7576
context.getMethod().writeCode(((writer, visitor) -> {
7677
visitor.visitJumpInsn(Opcodes.GOTO, label);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public TypeEntry() {
3434
@Override
3535
public void compile(Context context, Pattern.Match match) {
3636
final String name = match.meta();
37-
final Type type = context.getType(name);
37+
final Type type = context.findType(name);
3838
if (type != null) context.getField().setType(type);
3939
else context.getField().setType(new Type(name.replace('.', '/')));
4040
context.setState(CompileState.MEMBER_BODY);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public void all() throws Throwable {
7878
try (final OutputStream output = new FileOutputStream(test)) {
7979
output.write(classes[0].code());
8080
}
81+
} catch (Throwable ex) {
82+
ex.printStackTrace();
8183
}
8284
}
8385
assert failure < 1 : failure + " tests have failed.";

0 commit comments

Comments
 (0)