Skip to content

Commit 8f3c162

Browse files
committed
The 'repeat' statement now adds an index variable, so that its inner scope can know the iteration index
1 parent a5384e9 commit 8f3c162

3 files changed

Lines changed: 38 additions & 11 deletions

File tree

dsl/src/main/java/fr/jamailun/ultimatespellsystem/dsl/nodes/statements/blocks/RepeatStatement.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414

1515
import java.util.Optional;
1616

17+
/**
18+
* Statement of a {@code repeat <N> times}.
19+
*/
1720
public class RepeatStatement extends BlockHolder {
1821

22+
/**
23+
* Name of the variable set by this statement.
24+
*/
25+
public static final String INDEX_VARIABLE = "_repeat_index";
26+
1927
private final ExpressionNode delay; // optional
2028
@Getter private @NotNull final ExpressionNode count;
2129
@Getter private @NotNull final ExpressionNode period;
@@ -29,12 +37,15 @@ public RepeatStatement(StatementNode child, ExpressionNode delay, @NotNull Expre
2937

3038
@Override
3139
public void validateTypes(@NotNull TypesContext context) {
40+
TypesContext childContext = context.childContext();
41+
childContext.promiseVariable(INDEX_VARIABLE, TypePrimitive.NUMBER.asType());
42+
3243
if(delay != null)
33-
assertExpressionType(delay, CollectionFilter.MONO_ELEMENT, context, TypePrimitive.DURATION);
34-
assertExpressionType(count, CollectionFilter.MONO_ELEMENT, context, TypePrimitive.NUMBER);
35-
assertExpressionType(period, CollectionFilter.MONO_ELEMENT, context, TypePrimitive.DURATION);
44+
assertExpressionType(delay, CollectionFilter.MONO_ELEMENT, childContext, TypePrimitive.DURATION);
45+
assertExpressionType(count, CollectionFilter.MONO_ELEMENT, childContext, TypePrimitive.NUMBER);
46+
assertExpressionType(period, CollectionFilter.MONO_ELEMENT, childContext, TypePrimitive.DURATION);
3647

37-
child.validateTypes(context.childContext());
48+
child.validateTypes(childContext.childContext());
3849
}
3950

4051
public Optional<ExpressionNode> getDelay() {

dsl/src/main/java/fr/jamailun/ultimatespellsystem/dsl/nodes/type/TypesContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Type computeType(TypesContext context) {
4646
* Create a new context, and register the '%caster' variable.
4747
*/
4848
public TypesContext() {
49-
variables.put("caster", new VariableDefinition(TypePrimitive.ENTITY.asType()));
49+
promiseVariable("caster", TypePrimitive.ENTITY.asType());
5050
}
5151

5252
/**
@@ -61,6 +61,15 @@ public void registerVariable(String varName, ExpressionNode variable) {
6161
variables.put(varName, new VariableDefinition(variable));
6262
}
6363

64+
/**
65+
* Guarantee a variable will be set by a runtime implementation.
66+
* @param name the name of the variable.
67+
* @param type the type of the variable.
68+
*/
69+
public void promiseVariable(String name, Type type) {
70+
variables.put(name, new VariableDefinition(type));
71+
}
72+
6473
public void registerVariable(String varName, TokenPosition position, Type type) {
6574
if("caster".equals(varName)) {
6675
throw new SyntaxException(position, "Cannot override variable '%" + varName + "'.");

plugin/src/main/java/fr/jamailun/ultimatespellsystem/plugin/runner/nodes/blocks/RunRepeatNode.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.jamailun.ultimatespellsystem.plugin.runner.nodes.blocks;
22

33
import fr.jamailun.ultimatespellsystem.api.UltimateSpellSystem;
4+
import fr.jamailun.ultimatespellsystem.dsl.nodes.statements.blocks.RepeatStatement;
45
import fr.jamailun.ultimatespellsystem.dsl.nodes.type.Duration;
56
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
67
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeStatement;
@@ -29,12 +30,18 @@ public void run(@NotNull SpellRuntime runtime) {
2930
long delayTicks = delay == null ? 0 : delay.toTicks();
3031

3132
UltimateSpellSystem.runTaskRepeat(
32-
() -> {
33-
try {
34-
child.run(runtime);
35-
} catch (Throwable t) {
36-
UltimateSpellSystem.logError("Uncaught "+t.getClass().getSimpleName()+" on RunRepeatNode#run : " + t.getMessage());
37-
t.printStackTrace();
33+
new Runnable() {
34+
private int count = 0;
35+
@Override
36+
public void run() {
37+
runtime.variables().set(RepeatStatement.INDEX_VARIABLE, count);
38+
try {
39+
child.run(runtime);
40+
} catch (Throwable t) {
41+
UltimateSpellSystem.logError("Uncaught "+t.getClass().getSimpleName()+" on RunRepeatNode#run : " + t.getMessage());
42+
t.printStackTrace();
43+
}
44+
count++;
3845
}
3946
},
4047
count,

0 commit comments

Comments
 (0)