Skip to content

Commit 49b1272

Browse files
committed
Repair stacking section closers.
1 parent 4be16a6 commit 49b1272

File tree

10 files changed

+40
-24
lines changed

10 files changed

+40
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public PostCompileClass[] compile(String source, Type path) {
5858

5959
@Override
6060
protected FileContext createContext(Type path) {
61-
return new FileContext(path, 0);
61+
return new FileContext(path, 1);
6262
}
6363

6464
protected void debug(ElementTree tree, FileContext context) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.byteskript.skript.api.SyntaxElement;
1111
import org.byteskript.skript.compiler.Context;
1212
import org.byteskript.skript.error.ScriptCompileError;
13-
import org.byteskript.skript.lang.syntax.flow.conditional.ElseIfSection;
14-
import org.byteskript.skript.lang.syntax.flow.conditional.ElseSection;
1513
import org.objectweb.asm.Label;
1614

1715
public class ExtractionTree extends ProgrammaticSplitTree {
@@ -67,7 +65,7 @@ public void close(Context context) {
6765

6866
@Override
6967
public boolean permit(SyntaxElement element) {
70-
return element instanceof ElseIfSection || element instanceof ElseSection;
68+
return false;
7169
}
7270

7371
@Override

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@
66

77
package org.byteskript.skript.compiler.structure;
88

9+
import mx.kenzie.foundation.MethodBuilder;
10+
import org.byteskript.skript.compiler.Context;
11+
import org.byteskript.skript.error.ScriptCompileError;
12+
import org.objectweb.asm.Label;
13+
914
public class WhileTree extends LoopTree {
1015

1116
public WhileTree(SectionMeta owner) {
1217
super(owner);
1318
}
1419

20+
@Override
21+
public void close(Context context) {
22+
final MethodBuilder method = context.getMethod();
23+
if (method == null) throw new ScriptCompileError(context.lineNumber(), "While block left unclosed.");
24+
final Label top = this.getTop();
25+
method.writeCode((writer, visitor) -> visitor.visitJumpInsn(167, top));
26+
super.close(context);
27+
}
1528
}

src/main/java/org/byteskript/skript/lang/syntax/flow/lambda/RunnableSection.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import mx.kenzie.foundation.MethodBuilder;
1010
import mx.kenzie.foundation.MethodErasure;
1111
import mx.kenzie.foundation.Type;
12-
import mx.kenzie.foundation.WriteInstruction;
1312
import org.byteskript.skript.api.note.Documentation;
1413
import org.byteskript.skript.api.syntax.ExtractedSection;
1514
import org.byteskript.skript.compiler.CommonTypes;
@@ -78,17 +77,13 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
7877
context.addInnerClass(Type.of("java/lang/invoke/MethodHandles$Lookup"), Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
7978
final MethodBuilder method = context.getMethod();
8079
final int index = context.getLambdaIndex();
81-
final int load = context.getVariableCount();
8280
context.increaseLambdaIndex();
8381
final String internal = context.getType().internalName();
8482
final String name = "lambda$L" + index;
8583
final MethodBuilder child = context.getBuilder().addMethod(name)
8684
.setModifiers(Modifier.PUBLIC | Modifier.STATIC | 0x00001000)
8785
.setReturnType(new Type(void.class));
88-
for (int i = 0; i < load; i++) {
89-
child.addParameter(CommonTypes.OBJECT);
90-
method.writeCode(WriteInstruction.loadObject(i));
91-
}
86+
SupplierSection.extractVariables(context, method, child);
9287
final MethodErasure target = child.getErasure();
9388
final MethodErasure creator = new MethodErasure(CommonTypes.RUNNABLE, "run", child.getErasure()
9489
.parameterTypes());

src/main/java/org/byteskript/skript/lang/syntax/flow/lambda/SupplierSection.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import mx.kenzie.foundation.MethodBuilder;
1010
import mx.kenzie.foundation.MethodErasure;
1111
import mx.kenzie.foundation.Type;
12-
import mx.kenzie.foundation.WriteInstruction;
1312
import org.byteskript.skript.api.note.Documentation;
1413
import org.byteskript.skript.api.syntax.ExtractedSection;
1514
import org.byteskript.skript.compiler.CommonTypes;
1615
import org.byteskript.skript.compiler.Context;
1716
import org.byteskript.skript.compiler.Pattern;
1817
import org.byteskript.skript.compiler.SkriptLangSpec;
18+
import org.byteskript.skript.compiler.structure.PreVariable;
1919
import org.byteskript.skript.error.ScriptCompileError;
2020
import org.byteskript.skript.lang.element.StandardElements;
2121
import org.objectweb.asm.Handle;
@@ -79,17 +79,13 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
7979
context.addInnerClass(Type.of("java/lang/invoke/MethodHandles$Lookup"), Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
8080
final MethodBuilder method = context.getMethod();
8181
final int index = context.getLambdaIndex();
82-
final int load = context.getVariableCount();
8382
context.increaseLambdaIndex();
8483
final String internal = context.getType().internalName();
8584
final String name = "lambda$L" + index;
8685
final MethodBuilder child = context.getBuilder().addMethod(name)
8786
.setModifiers(Modifier.PUBLIC | Modifier.STATIC | 0x00001000)
8887
.setReturnType(CommonTypes.OBJECT);
89-
for (int i = 0; i < load; i++) {
90-
child.addParameter(CommonTypes.OBJECT);
91-
method.writeCode(WriteInstruction.loadObject(i));
92-
}
88+
extractVariables(context, method, child);
9389
final MethodErasure target = child.getErasure();
9490
final MethodErasure creator = new MethodErasure(CommonTypes.SUPPLIER, "get", child.getErasure()
9591
.parameterTypes());
@@ -98,5 +94,16 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
9894
method.writeCode((writer, visitor) -> visitor.visitInvokeDynamicInsn("get", creator.getDescriptor(), new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", bootstrap.name(), bootstrap.getDescriptor(), false), org.objectweb.asm.Type.getType("()Ljava/lang/Object;"), new Handle(6, internal, target.name(), target.getDescriptor(), false), org.objectweb.asm.Type.getType("()Ljava/lang/Object;")));
9995
}
10096

97+
static void extractVariables(Context context, MethodBuilder method, MethodBuilder child) {
98+
int i = 0;
99+
for (final PreVariable variable : context.getVariables()) {
100+
if (!variable.internal) {
101+
child.addParameter(CommonTypes.OBJECT);
102+
method.writeCode(variable.load(i));
103+
}
104+
++i;
105+
}
106+
}
107+
101108

102109
}

src/main/java/org/byteskript/skript/lang/syntax/flow/loop/LoopInSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void onSectionExit(Context context, SectionMeta meta) {
127127
final MethodBuilder method = context.getMethod();
128128
final Label top = tree.getTop();
129129
method.writeCode((writer, visitor) -> visitor.visitJumpInsn(Opcodes.GOTO, top));
130-
method.writeCode(tree.getEnd().instruction());
130+
tree.close(context);
131131
}
132132

133133
@Override

src/main/java/org/byteskript/skript/lang/syntax/flow/loop/LoopTimesSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void onSectionExit(Context context, SectionMeta meta) {
9797
final int slot = tree.slot;
9898
method.writeCode(WriteInstruction.loadSmall(slot));
9999
method.writeCode((writer, visitor) -> visitor.visitJumpInsn(Opcodes.IFGT, top));
100-
method.writeCode(tree.getEnd().instruction());
100+
tree.close(context);
101101
}
102102

103103
@Override

src/main/java/org/byteskript/skript/lang/syntax/flow/loop/WhileSection.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ public void onSectionExit(Context context, SectionMeta meta) {
8787
if (!(current instanceof WhileTree tree))
8888
throw new ScriptCompileError(context.lineNumber(), "Unable to balance while flow tree.");
8989
context.setState(CompileState.CODE_BODY);
90-
final MethodBuilder method = context.getMethod();
91-
final Label top = tree.getTop();
92-
method.writeCode((writer, visitor) -> visitor.visitJumpInsn(167, top));
93-
method.writeCode(tree.getEnd().instruction());
90+
tree.close(context);
9491
}
9592

9693
@Override

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class FlowTest extends SkriptTest {
2222
public static void start() throws Throwable {
2323
final PostCompileClass cls = skript.compileScript(FlowTest.class.getClassLoader()
2424
.getResourceAsStream("flow.bsk"), "skript.flow");
25-
debug(cls); //todo
2625
script = skript.loadScript(cls);
2726
}
2827

src/test/resources/flow.bsk

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ function loop_flow:
8080
if {item} is greater than 2:
8181
continue loop
8282
assert {item} is less than 3: "Continue-loop instruction failed."
83+
set {var} to a new runnable:
84+
set {count} to 0
85+
loop 4 times:
86+
set {phase} to {count} as a string
87+
set {tag} to "a" + {phase}
88+
assert {tag} is a string
89+
set {count} to {count} + 1
90+
run {var}
8391

8492
function while_flow:
8593
trigger:
@@ -104,7 +112,6 @@ function while_flow:
104112
set {tag} to "a" + {phase}
105113
assert {tag} is a string
106114
set {count} to {count} + 1
107-
assert true is true // todo - currently un-fixable
108115
run {var}
109116
return "ended"
110117

0 commit comments

Comments
 (0)