Skip to content

Commit dfc64e2

Browse files
committed
Fix balancing of nested trees.
1 parent 8f65f2b commit dfc64e2

File tree

11 files changed

+50
-17
lines changed

11 files changed

+50
-17
lines changed

hello.csk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
key: value
3+
thing: hello there!

pom.xml

Lines changed: 1 addition & 1 deletion
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.21</version>
9+
<version>1.0.22</version>
1010
<name>ByteSkript</name>
1111
<description>A compiled JVM implementation of the Skript language.</description>
1212

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public Section getParent() {
163163
return sections.get(0).handler();
164164
}
165165

166+
public void destroySections() {
167+
do destroySection();
168+
while (!sections.isEmpty());
169+
}
170+
166171
public void destroySection() {
167172
if (sections.isEmpty()) return;
168173
final SectionMeta meta = sections.remove(0);
@@ -173,15 +178,6 @@ public void destroySection() {
173178
}
174179
}
175180

176-
public void destroySections() {
177-
for (final SectionMeta section : sections) {
178-
for (final Section handler : section.getHandlers()) {
179-
handler.onSectionExit(this, section);
180-
}
181-
}
182-
this.sections.clear();
183-
}
184-
185181
public abstract boolean hasFunction(String name, int arguments);
186182

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class FileContext extends Context {
4545
LanguageElement expected;
4646
SyntaxElement currentEffect;
4747
private HandlerType mode = StandardHandlers.GET;
48-
4948
public FileContext(Type type) {
5049
this.type = type;
5150
this.state = CompileState.ROOT;
@@ -72,6 +71,10 @@ private void addSkriptFunctions() {
7271
}
7372
}
7473

74+
public List<ProgrammaticSplitTree> getTrees() {
75+
return trees;
76+
}
77+
7578
public PostCompileClass[] compile() {
7679
for (List<PropertyAccessGenerator> value : usedProperties.values()) {
7780
for (PropertyAccessGenerator generator : value) {

src/main/java/org/byteskript/skript/lang/syntax/flow/conditional/ElseIfSection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.byteskript.skript.api.syntax.Section;
1414
import org.byteskript.skript.compiler.*;
1515
import org.byteskript.skript.compiler.structure.IfElseTree;
16+
import org.byteskript.skript.compiler.structure.ProgrammaticSplitTree;
1617
import org.byteskript.skript.compiler.structure.SectionMeta;
1718
import org.byteskript.skript.error.ScriptCompileError;
1819
import org.byteskript.skript.lang.element.StandardElements;
@@ -68,7 +69,10 @@ public boolean allowedIn(State state, Context context) {
6869

6970
@Override
7071
public void onSectionExit(Context context, SectionMeta meta) {
71-
if (!(context.getTree(context.getSection()) instanceof IfElseTree tree))
72+
final ProgrammaticSplitTree current;
73+
if (context.getTree(context.getSection()) instanceof IfElseTree found) current = found;
74+
else current = context.getCurrentTree();
75+
if (!(current instanceof IfElseTree tree))
7276
throw new ScriptCompileError(context.lineNumber(), "Unable to balance if/else flow tree.");
7377
context.setState(CompileState.CODE_BODY);
7478
final MethodBuilder method = context.getMethod();

src/main/java/org/byteskript/skript/lang/syntax/flow/conditional/ElseSection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.byteskript.skript.compiler.Pattern;
1616
import org.byteskript.skript.compiler.SkriptLangSpec;
1717
import org.byteskript.skript.compiler.structure.IfElseTree;
18+
import org.byteskript.skript.compiler.structure.ProgrammaticSplitTree;
1819
import org.byteskript.skript.compiler.structure.SectionMeta;
1920
import org.byteskript.skript.error.ScriptCompileError;
2021
import org.byteskript.skript.lang.element.StandardElements;
@@ -67,7 +68,10 @@ public boolean allowedIn(State state, Context context) {
6768

6869
@Override
6970
public void onSectionExit(Context context, SectionMeta meta) {
70-
if (!(context.getTree(context.getSection()) instanceof IfElseTree tree))
71+
final ProgrammaticSplitTree current;
72+
if (context.getTree(context.getSection()) instanceof IfElseTree found) current = found;
73+
else current = context.getCurrentTree();
74+
if (!(current instanceof IfElseTree tree))
7175
throw new ScriptCompileError(context.lineNumber(), "Unable to balance if/else flow tree.");
7276
context.setState(CompileState.CODE_BODY);
7377
tree.close(context);

src/main/java/org/byteskript/skript/lang/syntax/flow/conditional/IfSection.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public boolean allowedIn(State state, Context context) {
6969

7070
@Override
7171
public void onSectionExit(Context context, SectionMeta meta) {
72-
final ProgrammaticSplitTree current = context.getCurrentTree();
72+
final ProgrammaticSplitTree current;
73+
if (context.getTree(context.getSection()) instanceof IfElseTree found) current = found;
74+
else current = context.getCurrentTree();
7375
if (current instanceof IfElseTree tree) {
7476
context.setState(CompileState.CODE_BODY);
7577
final MethodBuilder method = context.getMethod();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.byteskript.skript.compiler.*;
1515
import org.byteskript.skript.compiler.structure.LoopTree;
1616
import org.byteskript.skript.compiler.structure.PreVariable;
17+
import org.byteskript.skript.compiler.structure.ProgrammaticSplitTree;
1718
import org.byteskript.skript.compiler.structure.SectionMeta;
1819
import org.byteskript.skript.error.ScriptCompileError;
1920
import org.byteskript.skript.error.ScriptParseError;
@@ -117,7 +118,10 @@ private PreVariable getHolderVariable(Context context, Pattern.Match match) {
117118

118119
@Override
119120
public void onSectionExit(Context context, SectionMeta meta) {
120-
if (!(context.getTree(context.getSection()) instanceof LoopTree tree))
121+
final ProgrammaticSplitTree current;
122+
if (context.getTree(context.getSection()) instanceof LoopTree found) current = found;
123+
else current = context.getCurrentTree();
124+
if (!(current instanceof LoopTree tree))
121125
throw new ScriptCompileError(context.lineNumber(), "Unable to balance loop flow tree.");
122126
context.setState(CompileState.CODE_BODY);
123127
final MethodBuilder method = context.getMethod();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.byteskript.skript.compiler.*;
1515
import org.byteskript.skript.compiler.structure.LoopTree;
1616
import org.byteskript.skript.compiler.structure.PreVariable;
17+
import org.byteskript.skript.compiler.structure.ProgrammaticSplitTree;
1718
import org.byteskript.skript.compiler.structure.SectionMeta;
1819
import org.byteskript.skript.error.ScriptCompileError;
1920
import org.byteskript.skript.lang.element.StandardElements;
@@ -85,7 +86,10 @@ public boolean allowedIn(State state, Context context) {
8586

8687
@Override
8788
public void onSectionExit(Context context, SectionMeta meta) {
88-
if (!(context.getTree(context.getSection()) instanceof LoopTree tree))
89+
final ProgrammaticSplitTree current;
90+
if (context.getTree(context.getSection()) instanceof LoopTree found) current = found;
91+
else current = context.getCurrentTree();
92+
if (!(current instanceof LoopTree tree))
8993
throw new ScriptCompileError(context.lineNumber(), "Unable to balance loop flow tree.");
9094
context.setState(CompileState.CODE_BODY);
9195
final MethodBuilder method = context.getMethod();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.byteskript.skript.compiler.Context;
1616
import org.byteskript.skript.compiler.Pattern;
1717
import org.byteskript.skript.compiler.SkriptLangSpec;
18+
import org.byteskript.skript.compiler.structure.ProgrammaticSplitTree;
1819
import org.byteskript.skript.compiler.structure.SectionMeta;
1920
import org.byteskript.skript.compiler.structure.WhileTree;
2021
import org.byteskript.skript.error.ScriptCompileError;
@@ -80,7 +81,10 @@ public boolean allowedIn(State state, Context context) {
8081

8182
@Override
8283
public void onSectionExit(Context context, SectionMeta meta) {
83-
if (!(context.getTree(context.getSection()) instanceof WhileTree tree))
84+
final ProgrammaticSplitTree current;
85+
if (context.getTree(context.getSection()) instanceof WhileTree found) current = found;
86+
else current = context.getCurrentTree();
87+
if (!(current instanceof WhileTree tree))
8488
throw new ScriptCompileError(context.lineNumber(), "Unable to balance while flow tree.");
8589
context.setState(CompileState.CODE_BODY);
8690
final MethodBuilder method = context.getMethod();

0 commit comments

Comments
 (0)