'break' and 'continue' support for 'scan' and 'volume'

This commit is contained in:
gnembon committed 2020-03-23 14:14:25 -03:00
1 parent d8abd3ac4c
commit 153b04ed1c
3 files changed
+43 -8

No files matched your search

+8
View File
@@ -8,11 +8,19 @@ Evaluates expression over area of blocks defined by its center (`cx, cy, cz`), e
by `dx, dy, dz` blocks, or optionally in negative with `d` coords, and `p` coords in positive values. `expr` by `dx, dy, dz` blocks, or optionally in negative with `d` coords, and `p` coords in positive values. `expr`
receives `_x, _y, _z` as coords of current analyzed block and `_`, which represents the block itself. receives `_x, _y, _z` as coords of current analyzed block and `_`, which represents the block itself.
Returns number of successful evaluations of `expr` (with `true` boolean result) unless called in void context,
which would cause the expression not be evaluated for their boolean value.
`scan` also handles `continue` and `break` statements, using `continue`'s return value to use in place of expression
return value. `break` return value has no effect.
### `volume(x1, y1, z1, x2, y2, z2, expr)` ### `volume(x1, y1, z1, x2, y2, z2, expr)`
Evaluates expression for each block in the area, the same as the `scan`function, but using two opposite corners of Evaluates expression for each block in the area, the same as the `scan`function, but using two opposite corners of
the rectangular cuboid. Any corners can be specified, its like you would do with `/fill` command. the rectangular cuboid. Any corners can be specified, its like you would do with `/fill` command.
For return value and handling `break` and `continue` statements, see `scan` function above.
### `neighbours(x, y, z), neighbours(block), neighbours(l(x,y,z))` ### `neighbours(x, y, z), neighbours(block), neighbours(l(x,y,z))`
Returns the list of 6 neighbouring blocks to the argument. Commonly used with other loop functions like `for`. Returns the list of 6 neighbouring blocks to the argument. Commonly used with other loop functions like `for`.
@@ -10,16 +10,17 @@ and maps, where the iterator returns all the map keys
### `break(), break(expr), continue(), continue(expr)` ### `break(), break(expr), continue(), continue(expr)`
These allow to control execution of a loop either skipping current iteration code, using `continue`, or finishing the These allow to control execution of a loop either skipping current iteration code, using `continue`, or finishing the
current loop, using `break`. `break` and `continue` can only be used inside `for, while, loop, map, filter and reduce` current loop, using `break`. `break` and `continue` can only be used inside `for`, `c_for`, `while`, `loop`, `map`,
`filter`, `reduce` as well as Minecraft API block loops, `scan` and `volume`
functions, while `break` can be used in `first` as well. Outside of the internal expressions of these functions, functions, while `break` can be used in `first` as well. Outside of the internal expressions of these functions,
calling `break` or `continue` will cause an error. In case of the nexted loops, and more complex setups, use calling `break` or `continue` will cause an error. In case of the nested loops, and more complex setups, use
custom `try` and `throw` setup. custom `try` and `throw` setup.
Please check corresponding loop function description what `continue` and `break` do in their contexts, but in Please check corresponding loop function description what `continue` and `break` do in their contexts, but in
general case, passed values to `break` and `continue` will be used in place of the return value of the internal general case, passed values to `break` and `continue` will be used in place of the return value of the internal
iteration expression. iteration expression.
### `cfor(init, condition, increment, body)` ### `c_for(init, condition, increment, body)`
`c_for` Mimics c-style tri-arg (plus body) for loops. Return value of `c_for` is number of iterations performed in the `c_for` Mimics c-style tri-arg (plus body) for loops. Return value of `c_for` is number of iterations performed in the
loop. Unlike other loops, the `body` is not provided with pre-initialized `_` style variables - all initialization loop. Unlike other loops, the `body` is not provided with pre-initialized `_` style variables - all initialization
@@ -11,11 +11,13 @@ import carpet.mixins.PointOfInterest_scarpetMixin;
import carpet.mixins.ServerChunkManager_scarpetMixin; import carpet.mixins.ServerChunkManager_scarpetMixin;
import carpet.script.Fluff.TriFunction; import carpet.script.Fluff.TriFunction;
import carpet.script.bundled.Module; import carpet.script.bundled.Module;
import carpet.CarpetSettings;
import carpet.script.exception.BreakStatement;
import carpet.script.exception.CarpetExpressionException; import carpet.script.exception.CarpetExpressionException;
import carpet.script.exception.ContinueStatement;
import carpet.script.exception.ExitStatement; import carpet.script.exception.ExitStatement;
import carpet.script.exception.ExpressionException; import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.CarpetSettings;
import carpet.script.value.BlockValue; import carpet.script.value.BlockValue;
import carpet.script.value.EntityValue; import carpet.script.value.EntityValue;
import carpet.script.value.FunctionValue; import carpet.script.value.FunctionValue;
@@ -1824,7 +1826,7 @@ public class CarpetExpression
LazyValue _z = c.getVariable("_z"); LazyValue _z = c.getVariable("_z");
LazyValue __ = c.getVariable("_"); LazyValue __ = c.getVariable("_");
int sCount = 0; int sCount = 0;
for (int y=cy-yrange; y <= cy+yprange; y++) outer:for (int y=cy-yrange; y <= cy+yprange; y++)
{ {
int yFinal = y; int yFinal = y;
c.setVariable("_y", (c_, t_) -> new NumericValue(yFinal).bindTo("_y")); c.setVariable("_y", (c_, t_) -> new NumericValue(yFinal).bindTo("_y"));
@@ -1839,7 +1841,19 @@ public class CarpetExpression
c.setVariable("_z", (c_, t_) -> new NumericValue(zFinal).bindTo("_z")); c.setVariable("_z", (c_, t_) -> new NumericValue(zFinal).bindTo("_z"));
Value blockValue = BlockValue.fromCoords(((CarpetContext)c), xFinal,yFinal,zFinal).bindTo("_"); Value blockValue = BlockValue.fromCoords(((CarpetContext)c), xFinal,yFinal,zFinal).bindTo("_");
c.setVariable( "_", (cc_, t_c) -> blockValue); c.setVariable( "_", (cc_, t_c) -> blockValue);
Value result = expr.evalValue(c, t); Value result;
try
{
result = expr.evalValue(c, t);
}
catch (ContinueStatement notIgnored)
{
result = notIgnored.retval;
}
catch (BreakStatement notIgnored)
{
break outer;
}
if (t != Context.VOID && result.getBoolean()) if (t != Context.VOID && result.getBoolean())
{ {
sCount += 1; sCount += 1;
@@ -1878,7 +1892,7 @@ public class CarpetExpression
LazyValue _z = c.getVariable("_z"); LazyValue _z = c.getVariable("_z");
LazyValue __ = c.getVariable("_"); LazyValue __ = c.getVariable("_");
int sCount = 0; int sCount = 0;
for (int y=miny; y <= maxy; y++) outer:for (int y=miny; y <= maxy; y++)
{ {
int yFinal = y; int yFinal = y;
c.setVariable("_y", (c_, t_) -> new NumericValue(yFinal).bindTo("_y")); c.setVariable("_y", (c_, t_) -> new NumericValue(yFinal).bindTo("_y"));
@@ -1892,7 +1906,19 @@ public class CarpetExpression
c.setVariable("_z", (c_, t_) -> new NumericValue(zFinal).bindTo("_z")); c.setVariable("_z", (c_, t_) -> new NumericValue(zFinal).bindTo("_z"));
Value blockValue = BlockValue.fromCoords(((CarpetContext)c), xFinal,yFinal,zFinal).bindTo("_"); Value blockValue = BlockValue.fromCoords(((CarpetContext)c), xFinal,yFinal,zFinal).bindTo("_");
c.setVariable( "_", (cc_, t_c) -> blockValue); c.setVariable( "_", (cc_, t_c) -> blockValue);
Value result = expr.evalValue(c, t); Value result;
try
{
result = expr.evalValue(c, t);
}
catch (ContinueStatement notIgnored)
{
result = notIgnored.retval;
}
catch (BreakStatement notIgnored)
{
break outer;
}
if (t != Context.VOID && result.getBoolean()) if (t != Context.VOID && result.getBoolean())
{ {
sCount += 1; sCount += 1;