diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 87ef120d..73a21a73 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -189,7 +189,7 @@ public class CarpetExpression private static final Map DIRECTION_MAP = Arrays.stream(Direction.values()).collect(Collectors.toMap(Direction::getName, (direction) -> direction)); - private LazyValue booleanStateTest( + private static LazyValue booleanStateTest( Context c, String name, List params, @@ -212,7 +212,7 @@ public class CarpetExpression return (c_, t_) -> retval; } - private LazyValue stateStringQuery( + private static LazyValue stateStringQuery( Context c, String name, List params, @@ -238,7 +238,7 @@ public class CarpetExpression return (c_, t_) -> retval; } - private LazyValue genericStateTest( + private static LazyValue genericStateTest( Context c, String name, List params, @@ -268,7 +268,7 @@ public class CarpetExpression return (c_, t_) -> retval; } - private > BlockState setProperty(Property property, String name, String value, + private static > BlockState setProperty(Property property, String name, String value, BlockState bs) { Optional optional = property.parse(value); @@ -301,11 +301,11 @@ public class CarpetExpression } } - private boolean tryBreakBlock_copy_from_ServerPlayerInteractionManager(ServerPlayerEntity player, BlockPos blockPos_1) + private static boolean tryBreakBlock_copy_from_ServerPlayerInteractionManager(ServerPlayerEntity player, BlockPos blockPos_1) { //this could be done little better, by hooking up event handling not in try_break_block but wherever its called // so we can safely call it here - // but that woudl do for now. + // but that would do for now. BlockState blockState_1 = player.world.getBlockState(blockPos_1); if (!player.getMainHandStack().getItem().canMine(blockState_1, player.world, blockPos_1, player)) { return false; @@ -367,16 +367,16 @@ public class CarpetExpression return MapValue.wrap(ret); } - private void BooYah(ChunkGenerator generator) + private static void BooYah(ChunkGenerator generator) { synchronized (generator) { ((ChunkGeneratorInterface)generator).initStrongholds(); } } - private void API_BlockManipulation() + private static void API_BlockManipulation(Expression expression) { - this.expr.addLazyFunction("block", -1, (c, t, lv) -> + expression.addLazyFunction("block", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; if (lv.size() == 0) @@ -390,7 +390,7 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("block_data", -1, (c, t, lv) -> + expression.addLazyFunction("block_data", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; if (lv.size() == 0) @@ -405,7 +405,7 @@ public class CarpetExpression }); // poi_get(pos, radius?, type?, occupation?, column_mode?) - this.expr.addLazyFunction("poi", -1, (c, t, lv) -> + expression.addLazyFunction("poi", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; if (lv.size() == 0) throw new InternalExpressionException("'poi' requires at least one parameter"); @@ -480,7 +480,7 @@ public class CarpetExpression }); //poi_set(pos, null) poi_set(pos, type, occupied?, - this.expr.addLazyFunction("set_poi", -1, (c, t, lv) -> + expression.addLazyFunction("set_poi", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; if (lv.size() == 0) throw new InternalExpressionException("'set_poi' requires at least one parameter"); @@ -524,7 +524,7 @@ public class CarpetExpression }); - this.expr.addLazyFunction("pos", 1, (c, t, lv) -> + expression.addLazyFunction("pos", 1, (c, t, lv) -> { Value arg = lv.get(0).evalValue(c); if (arg instanceof BlockValue) @@ -549,7 +549,7 @@ public class CarpetExpression } }); - this.expr.addLazyFunction("pos_offset", -1, (c, t, lv) -> + expression.addLazyFunction("pos_offset", -1, (c, t, lv) -> { BlockArgument locator = BlockArgument.findIn((CarpetContext)c, lv, 0); BlockPos pos = locator.block.getPos(); @@ -567,19 +567,19 @@ public class CarpetExpression return (cc, tt) -> ret; }); - this.expr.addLazyFunction("solid", -1, (c, t, lv) -> + expression.addLazyFunction("solid", -1, (c, t, lv) -> genericStateTest(c, "solid", lv, (s, p, w) -> new NumericValue(s.isSolidBlock(w, p)))); // isSimpleFullBlock - this.expr.addLazyFunction("air", -1, (c, t, lv) -> + expression.addLazyFunction("air", -1, (c, t, lv) -> booleanStateTest(c, "air", lv, (s, p) -> s.isAir())); - this.expr.addLazyFunction("liquid", -1, (c, t, lv) -> + expression.addLazyFunction("liquid", -1, (c, t, lv) -> booleanStateTest(c, "liquid", lv, (s, p) -> !s.getFluidState().isEmpty())); - this.expr.addLazyFunction("flammable", -1, (c, t, lv) -> + expression.addLazyFunction("flammable", -1, (c, t, lv) -> booleanStateTest(c, "flammable", lv, (s, p) -> s.getMaterial().isBurnable())); - this.expr.addLazyFunction("transparent", -1, (c, t, lv) -> + expression.addLazyFunction("transparent", -1, (c, t, lv) -> booleanStateTest(c, "transparent", lv, (s, p) -> !s.getMaterial().isSolid())); /*this.expr.addLazyFunction("opacity", -1, (c, t, lv) -> @@ -588,31 +588,31 @@ public class CarpetExpression this.expr.addLazyFunction("blocks_daylight", -1, (c, t, lv) -> genericStateTest(c, "blocks_daylight", lv, (s, p, w) -> new NumericValue(s.propagatesSkylightDown(w, p))));*/ // investigate - this.expr.addLazyFunction("emitted_light", -1, (c, t, lv) -> + expression.addLazyFunction("emitted_light", -1, (c, t, lv) -> genericStateTest(c, "emitted_light", lv, (s, p, w) -> new NumericValue(s.getLuminance()))); - this.expr.addLazyFunction("light", -1, (c, t, lv) -> + expression.addLazyFunction("light", -1, (c, t, lv) -> genericStateTest(c, "light", lv, (s, p, w) -> new NumericValue(Math.max(w.getLightLevel(LightType.BLOCK, p), w.getLightLevel(LightType.SKY, p))))); - this.expr.addLazyFunction("block_light", -1, (c, t, lv) -> + expression.addLazyFunction("block_light", -1, (c, t, lv) -> genericStateTest(c, "block_light", lv, (s, p, w) -> new NumericValue(w.getLightLevel(LightType.BLOCK, p)))); - this.expr.addLazyFunction("sky_light", -1, (c, t, lv) -> + expression.addLazyFunction("sky_light", -1, (c, t, lv) -> genericStateTest(c, "sky_light", lv, (s, p, w) -> new NumericValue(w.getLightLevel(LightType.SKY, p)))); - this.expr.addLazyFunction("see_sky", -1, (c, t, lv) -> + expression.addLazyFunction("see_sky", -1, (c, t, lv) -> genericStateTest(c, "see_sky", lv, (s, p, w) -> new NumericValue(w.isSkyVisible(p)))); - this.expr.addLazyFunction("brightness", -1, (c, t, lv) -> + expression.addLazyFunction("brightness", -1, (c, t, lv) -> genericStateTest(c, "brightness", lv, (s, p, w) -> new NumericValue(w.getBrightness(p)))); - this.expr.addLazyFunction("hardness", -1, (c, t, lv) -> + expression.addLazyFunction("hardness", -1, (c, t, lv) -> genericStateTest(c, "hardness", lv, (s, p, w) -> new NumericValue(s.getHardness(w, p)))); - this.expr.addLazyFunction("blast_resistance", -1, (c, t, lv) -> + expression.addLazyFunction("blast_resistance", -1, (c, t, lv) -> genericStateTest(c, "blast_resistance", lv, (s, p, w) -> new NumericValue(s.getBlock().getBlastResistance()))); - this.expr.addLazyFunction("in_slime_chunk", -1, (c, t, lv) -> + expression.addLazyFunction("in_slime_chunk", -1, (c, t, lv) -> { BlockPos pos = BlockArgument.findIn((CarpetContext)c, lv, 0).block.getPos(); ChunkPos chunkPos = new ChunkPos(pos); @@ -625,7 +625,7 @@ public class CarpetExpression }); - this.expr.addLazyFunction("top", -1, (c, t, lv) -> + expression.addLazyFunction("top", -1, (c, t, lv) -> { String type = lv.get(0).evalValue(c).getString().toLowerCase(Locale.ROOT); Heightmap.Type htype; @@ -648,21 +648,21 @@ public class CarpetExpression //return new BlockValue(source.getWorld().getBlockState(pos), pos); }); - this.expr.addLazyFunction("loaded", -1, (c, t, lv) -> + expression.addLazyFunction("loaded", -1, (c, t, lv) -> { Value retval = ((CarpetContext) c).s.getWorld().isChunkLoaded(BlockArgument.findIn((CarpetContext) c, lv, 0).block.getPos()) ? Value.TRUE : Value.FALSE; return (c_, t_) -> retval; }); // Deprecated, use loaded_status as more indicative - this.expr.addLazyFunction("loaded_ep", -1, (c, t, lv) -> + expression.addLazyFunction("loaded_ep", -1, (c, t, lv) -> { BlockPos pos = BlockArgument.findIn((CarpetContext)c, lv, 0).block.getPos(); Value retval = ((CarpetContext)c).s.getWorld().getChunkManager().shouldTickChunk(new ChunkPos(pos))?Value.TRUE : Value.FALSE; return (c_, t_) -> retval; }); - this.expr.addLazyFunction("loaded_status", -1, (c, t, lv) -> + expression.addLazyFunction("loaded_status", -1, (c, t, lv) -> { BlockPos pos = BlockArgument.findIn((CarpetContext)c, lv, 0).block.getPos(); WorldChunk chunk = ((CarpetContext)c).s.getWorld().getChunkManager().getWorldChunk(pos.getX()>>4, pos.getZ()>>4, false); @@ -672,7 +672,7 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("is_chunk_generated", -1, (c, t, lv) -> + expression.addLazyFunction("is_chunk_generated", -1, (c, t, lv) -> { BlockArgument locator = BlockArgument.findIn((CarpetContext)c, lv, 0); BlockPos pos = locator.block.getPos(); @@ -683,7 +683,7 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("generation_status", -1, (c, t, lv) -> + expression.addLazyFunction("generation_status", -1, (c, t, lv) -> { BlockArgument blockArgument = BlockArgument.findIn((CarpetContext)c, lv, 0); BlockPos pos = blockArgument.block.getPos(); @@ -697,7 +697,7 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("chunk_tickets", -1, (c, t, lv) -> + expression.addLazyFunction("chunk_tickets", -1, (c, t, lv) -> { ServerWorld world = ((CarpetContext) c).s.getWorld(); Long2ObjectOpenHashMap>> levelTickets = ( @@ -742,23 +742,23 @@ public class CarpetExpression return (_c, _t) -> returnValue; }); - this.expr.addLazyFunction("suffocates", -1, (c, t, lv) -> + expression.addLazyFunction("suffocates", -1, (c, t, lv) -> genericStateTest(c, "suffocates", lv, (s, p, w) -> new NumericValue(s.shouldSuffocate(w, p)))); // canSuffocate - this.expr.addLazyFunction("power", -1, (c, t, lv) -> + expression.addLazyFunction("power", -1, (c, t, lv) -> genericStateTest(c, "power", lv, (s, p, w) -> new NumericValue(w.getReceivedRedstonePower(p)))); - this.expr.addLazyFunction("ticks_randomly", -1, (c, t, lv) -> + expression.addLazyFunction("ticks_randomly", -1, (c, t, lv) -> booleanStateTest(c, "ticks_randomly", lv, (s, p) -> s.hasRandomTicks())); - this.expr.addLazyFunction("update", -1, (c, t, lv) -> + expression.addLazyFunction("update", -1, (c, t, lv) -> booleanStateTest(c, "update", lv, (s, p) -> { ((CarpetContext) c).s.getWorld().updateNeighbor(p, s.getBlock(), p); return true; })); - this.expr.addLazyFunction("block_tick", -1, (c, t, lv) -> + expression.addLazyFunction("block_tick", -1, (c, t, lv) -> booleanStateTest(c, "block_tick", lv, (s, p) -> { ServerWorld w = ((CarpetContext)c).s.getWorld(); @@ -766,7 +766,7 @@ public class CarpetExpression return true; })); - this.expr.addLazyFunction("random_tick", -1, (c, t, lv) -> + expression.addLazyFunction("random_tick", -1, (c, t, lv) -> booleanStateTest(c, "random_tick", lv, (s, p) -> { ServerWorld w = ((CarpetContext)c).s.getWorld(); @@ -775,7 +775,7 @@ public class CarpetExpression return true; })); - this.expr.addLazyFunction("without_updates", 1, (c, t, lv) -> + expression.addLazyFunction("without_updates", 1, (c, t, lv) -> { boolean previous = CarpetSettings.impendingFillSkipUpdates; if (previous) return lv.get(0); @@ -795,7 +795,7 @@ public class CarpetExpression return (cc, tt) -> result[0]; }); - this.expr.addLazyFunction("set", -1, (c, t, lv) -> + expression.addLazyFunction("set", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; ServerWorld world = cc.s.getWorld(); @@ -875,7 +875,7 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("destroy", -1, (c, t, lv) -> + expression.addLazyFunction("destroy", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; ServerWorld world = cc.s.getWorld(); @@ -976,7 +976,7 @@ public class CarpetExpression }); - this.expr.addLazyFunction("harvest", -1, (c, t, lv) -> + expression.addLazyFunction("harvest", -1, (c, t, lv) -> { if (lv.size()<2) throw new InternalExpressionException("'harvest' takes at least 2 parameters: entity and block, or position, to harvest"); @@ -1002,7 +1002,7 @@ public class CarpetExpression }); // TODO rename to use_item - this.expr.addLazyFunction("place_item", -1, (c, t, lv) -> + expression.addLazyFunction("place_item", -1, (c, t, lv) -> { if (lv.size()<2) throw new InternalExpressionException("'place_item' takes at least 2 parameters: item and block, or position, to place onto"); @@ -1056,23 +1056,23 @@ public class CarpetExpression return (_c, _t) -> Value.FALSE; }); - this.expr.addLazyFunction("blocks_movement", -1, (c, t, lv) -> + expression.addLazyFunction("blocks_movement", -1, (c, t, lv) -> booleanStateTest(c, "blocks_movement", lv, (s, p) -> !s.canPathfindThrough(((CarpetContext) c).s.getWorld(), p, NavigationType.LAND))); - this.expr.addLazyFunction("block_sound", -1, (c, t, lv) -> + expression.addLazyFunction("block_sound", -1, (c, t, lv) -> stateStringQuery(c, "block_sound", lv, (s, p) -> BlockInfo.soundName.get(s.getSoundGroup()))); - this.expr.addLazyFunction("material", -1, (c, t, lv) -> + expression.addLazyFunction("material", -1, (c, t, lv) -> stateStringQuery(c, "material", lv, (s, p) -> BlockInfo.materialName.get(s.getMaterial()))); - this.expr.addLazyFunction("map_colour", -1, (c, t, lv) -> + expression.addLazyFunction("map_colour", -1, (c, t, lv) -> stateStringQuery(c, "map_colour", lv, (s, p) -> BlockInfo.mapColourName.get(s.getTopMaterialColor(((CarpetContext)c).s.getWorld(), p)))); - this.expr.addLazyFunction("property", -1, (c, t, lv) -> + expression.addLazyFunction("property", -1, (c, t, lv) -> { BlockArgument locator = BlockArgument.findIn((CarpetContext) c, lv, 0); BlockState state = locator.block.getBlockState(); @@ -1087,7 +1087,7 @@ public class CarpetExpression return (_c, _t) -> retval; }); - this.expr.addLazyFunction("block_properties", -1, (c, t, lv) -> + expression.addLazyFunction("block_properties", -1, (c, t, lv) -> { BlockArgument locator = BlockArgument.findIn((CarpetContext) c, lv, 0); BlockState state = locator.block.getBlockState(); @@ -1098,7 +1098,7 @@ public class CarpetExpression return (_c, _t) -> res; }); - this.expr.addLazyFunction("biome", -1, (c, t, lv) -> { + expression.addLazyFunction("biome", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); ServerWorld world = cc.s.getWorld(); @@ -1110,7 +1110,7 @@ public class CarpetExpression return (_c, _t) -> res; }); - this.expr.addLazyFunction("set_biome", -1, (c, t, lv) -> + expression.addLazyFunction("set_biome", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -1134,7 +1134,7 @@ public class CarpetExpression return LazyValue.TRUE; }); - this.expr.addLazyFunction("reload_chunk", -1, (c, t, lv) -> { + expression.addLazyFunction("reload_chunk", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockPos pos = BlockArgument.findIn(cc, lv, 0).block.getPos(); ServerWorld world = cc.s.getWorld(); @@ -1142,7 +1142,7 @@ public class CarpetExpression return LazyValue.TRUE; }); - this.expr.addLazyFunction("structure_references", -1, (c, t, lv) -> { + expression.addLazyFunction("structure_references", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); ServerWorld world = cc.s.getWorld(); @@ -1170,7 +1170,7 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("structure_eligibility", -1, (c, t, lv) -> + expression.addLazyFunction("structure_eligibility", -1, (c, t, lv) -> {// TODO rename structureName to class CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -1218,7 +1218,7 @@ public class CarpetExpression return (_c, _t) -> retMap; }); - this.expr.addLazyFunction("structures", -1, (c, t, lv) -> { + expression.addLazyFunction("structures", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -1248,7 +1248,7 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("set_structure", -1, (c, t, lv) -> + expression.addLazyFunction("set_structure", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -1306,7 +1306,7 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("custom_dimension", -1, (c, t, lv) -> + expression.addLazyFunction("custom_dimension", -1, (c, t, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'custom_dimension' requires at least one argument"); CarpetContext cc = (CarpetContext)c; @@ -1333,7 +1333,7 @@ public class CarpetExpression }); - this.expr.addLazyFunction("reset_chunk", -1, (c, t, lv) -> + expression.addLazyFunction("reset_chunk", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; List requestedChunks = new ArrayList<>(); @@ -1404,7 +1404,7 @@ public class CarpetExpression return (_c, _t) -> result[0]; }); - this.expr.addLazyFunction("inhabited_time", -1, (c, t, lv) -> + expression.addLazyFunction("inhabited_time", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -1413,7 +1413,7 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("spawn_potential", -1, (c, t, lv) -> + expression.addLazyFunction("spawn_potential", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -1430,7 +1430,7 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("add_chunk_ticket", -1, (c, t, lv) -> + expression.addLazyFunction("add_chunk_ticket", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -1454,7 +1454,7 @@ public class CarpetExpression }); } - private Map> ticketTypes = new HashMap>(){{ + private final static Map> ticketTypes = new HashMap>(){{ put("portal", ChunkTicketType.PORTAL); put("teleport", ChunkTicketType.POST_TELEPORT); put("unknown", ChunkTicketType.UNKNOWN); // unknown @@ -1480,11 +1480,11 @@ public class CarpetExpression } } - private void API_Scoreboard() + private static void API_Scoreboard(Expression expression) { // scoreboard(player,'objective') // scoreboard(player, objective, newValue) - this.expr.addLazyFunction("scoreboard", -1, (c, t, lv) -> + expression.addLazyFunction("scoreboard", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; Scoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard(); @@ -1513,7 +1513,7 @@ public class CarpetExpression return (_c, _t) -> retval; }); - this.expr.addLazyFunction("scoreboard_remove", -1, (c, t, lv)-> { + expression.addLazyFunction("scoreboard_remove", -1, (c, t, lv)-> { if (lv.size()==0) throw new InternalExpressionException("'scoreboard_remove' requires at least one parameter"); CarpetContext cc = (CarpetContext)c; Scoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard(); @@ -1537,7 +1537,7 @@ public class CarpetExpression // objective_add('lvl','level') // objective_add('counter') - this.expr.addLazyFunction("scoreboard_add", -1, (c, t, lv)-> { + expression.addLazyFunction("scoreboard_add", -1, (c, t, lv)-> { CarpetContext cc = (CarpetContext)c; Scoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard(); if (lv.size() == 0 || lv.size()>2) throw new InternalExpressionException("'scoreboard_add' should have one or two parameters"); @@ -1565,7 +1565,7 @@ public class CarpetExpression return LazyValue.TRUE; }); - this.expr.addLazyFunction("scoreboard_display", 2, (c, t, lv) -> + expression.addLazyFunction("scoreboard_display", 2, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; Scoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard(); @@ -1586,23 +1586,23 @@ public class CarpetExpression }); } - private void API_InventoryManipulation() + private static void API_InventoryManipulation(Expression expression) { - this.expr.addLazyFunction("stack_limit", 1, (c, t, lv) -> + expression.addLazyFunction("stack_limit", 1, (c, t, lv) -> { ItemStackArgument item = NBTSerializableValue.parseItem(lv.get(0).evalValue(c).getString()); Value res = new NumericValue(item.getItem().getMaxCount()); return (_c, _t) -> res; }); - this.expr.addLazyFunction("item_category", 1, (c, t, lv) -> + expression.addLazyFunction("item_category", 1, (c, t, lv) -> { ItemStackArgument item = NBTSerializableValue.parseItem(lv.get(0).evalValue(c).getString()); Value res = new StringValue(item.getItem().getGroup().getName()); return (_c, _t) -> res; }); - this.expr.addLazyFunction("recipe_data", -1, (c, t, lv) -> + expression.addLazyFunction("recipe_data", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; if (lv.size() < 1) throw new InternalExpressionException("'recipe_data' requires at least one argument"); @@ -1695,7 +1695,7 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("crafting_remaining_item", 1, (c, t, lv) -> + expression.addLazyFunction("crafting_remaining_item", 1, (c, t, lv) -> { String itemStr = lv.get(0).evalValue(c).getString(); Item item; @@ -1716,7 +1716,7 @@ public class CarpetExpression }); - this.expr.addLazyFunction("inventory_size", -1, (c, t, lv) -> + expression.addLazyFunction("inventory_size", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); @@ -1726,7 +1726,7 @@ public class CarpetExpression return (_c, _t) -> res; }); - this.expr.addLazyFunction("inventory_has_items", -1, (c, t, lv) -> + expression.addLazyFunction("inventory_has_items", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); @@ -1737,7 +1737,7 @@ public class CarpetExpression }); //inventory_get(, ) -> item_triple - this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) -> + expression.addLazyFunction("inventory_get", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); @@ -1762,7 +1762,7 @@ public class CarpetExpression }); //inventory_set(, , , , ) - this.expr.addLazyFunction("inventory_set", -1, (c, t, lv) -> + expression.addLazyFunction("inventory_set", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); @@ -1822,7 +1822,7 @@ public class CarpetExpression }); //inventory_find(, or null (first empty slot), ) -> or null - this.expr.addLazyFunction("inventory_find", -1, (c, t, lv) -> + expression.addLazyFunction("inventory_find", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); @@ -1853,11 +1853,8 @@ public class CarpetExpression return (_c, _t) -> Value.NULL; }); - - //inventory_remove(, , ) -> bool - - this.expr.addLazyFunction("inventory_remove", -1, (c, t, lv) -> { + expression.addLazyFunction("inventory_remove", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); if (inventoryLocator == null) @@ -1904,7 +1901,7 @@ public class CarpetExpression }); //inventory_drop(, , ) -> entity_item (and sets slot) or null if cannot - this.expr.addLazyFunction("drop_item", -1, (c, t, lv) -> + expression.addLazyFunction("drop_item", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); @@ -1959,7 +1956,7 @@ public class CarpetExpression return (_c, _t) -> res; }); } - private void syncPlayerInventory(NBTSerializableValue.InventoryLocator inventory, int int_1) + private static void syncPlayerInventory(NBTSerializableValue.InventoryLocator inventory, int int_1) { if (inventory.owner instanceof ServerPlayerEntity && !inventory.isEnder) { @@ -1972,9 +1969,9 @@ public class CarpetExpression } } - private void API_EntityManipulation() + private static void API_EntityManipulation(Expression expression) { - this.expr.addLazyFunction("player", -1, (c, t, lv) -> { + expression.addLazyFunction("player", -1, (c, t, lv) -> { if (lv.size() ==0) { @@ -2041,7 +2038,7 @@ public class CarpetExpression return (cc, tt) -> finalVar; }); - this.expr.addLazyFunction("spawn", -1, (c, t, lv) -> { + expression.addLazyFunction("spawn", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; if (lv.size() < 2) throw new InternalExpressionException("'spawn' function takes mob name, and position to spawn"); @@ -2092,7 +2089,7 @@ public class CarpetExpression } }); - this.expr.addLazyFunction("entity_id", 1, (c, t, lv) -> + expression.addLazyFunction("entity_id", 1, (c, t, lv) -> { Value who = lv.get(0).evalValue(c); Entity e; @@ -2111,7 +2108,7 @@ public class CarpetExpression return (cc, tt) -> new EntityValue(e); }); - this.expr.addLazyFunction("entity_list", 1, (c, t, lv) -> { + expression.addLazyFunction("entity_list", 1, (c, t, lv) -> { String who = lv.get(0).evalValue(c).getString(); Pair, Predicate> pair = EntityValue.getPredicate(who); if (pair == null) @@ -2123,7 +2120,7 @@ public class CarpetExpression return (_c, _t ) -> retval; }); - this.expr.addLazyFunction("entity_area", 7, (c, t, lv) -> { + expression.addLazyFunction("entity_area", 7, (c, t, lv) -> { Vec3d center = new Vec3d( NumericValue.asNumber(lv.get(1).evalValue(c)).getDouble(), NumericValue.asNumber(lv.get(2).evalValue(c)).getDouble(), @@ -2145,7 +2142,7 @@ public class CarpetExpression return (_c, _t ) -> retval; }); - this.expr.addLazyFunction("entity_selector", -1, (c, t, lv) -> + expression.addLazyFunction("entity_selector", -1, (c, t, lv) -> { String selector = lv.get(0).evalValue(c).getString(); List retlist = new ArrayList<>(); @@ -2156,7 +2153,7 @@ public class CarpetExpression return (c_, t_) -> ListValue.wrap(retlist); }); - this.expr.addLazyFunction("query", -1, (c, t, lv) -> { + expression.addLazyFunction("query", -1, (c, t, lv) -> { if (lv.size()<2) { throw new InternalExpressionException("'query' takes entity as a first argument, and queried feature as a second"); @@ -2176,7 +2173,7 @@ public class CarpetExpression }); // or update - this.expr.addLazyFunction("modify", -1, (c, t, lv) -> { + expression.addLazyFunction("modify", -1, (c, t, lv) -> { if (lv.size()<2) { throw new InternalExpressionException("'modify' takes entity as a first argument, and queried feature as a second"); @@ -2195,7 +2192,7 @@ public class CarpetExpression }); // or update - this.expr.addLazyFunction("entity_event", -1, (c, t, lv) -> + expression.addLazyFunction("entity_event", -1, (c, t, lv) -> { if (lv.size()<3) throw new InternalExpressionException("'entity_event' requires at least 3 arguments, entity, event to be handled, and function name, with optional arguments"); @@ -2209,7 +2206,7 @@ public class CarpetExpression else if (!(functionValue instanceof FunctionValue)) { String name = functionValue.getString(); - functionValue = c.host.getAssertFunction(this.expr.module, name); + functionValue = c.host.getAssertFunction(expression.module, name); } FunctionValue function = (FunctionValue)functionValue; List args = null; @@ -2226,9 +2223,9 @@ public class CarpetExpression }); } - private void API_IteratingOverAreasOfBlocks() + private static void API_IteratingOverAreasOfBlocks(Expression expression) { - this.expr.addLazyFunction("scan", -1, (c, t, lv) -> + expression.addLazyFunction("scan", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument centerLocator = BlockArgument.findIn(cc, lv, 0); @@ -2333,7 +2330,7 @@ public class CarpetExpression return (c_, t_) -> new NumericValue(finalSCount); }); - this.expr.addLazyFunction("volume", -1, (c, t, lv) -> + expression.addLazyFunction("volume", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; @@ -2405,7 +2402,7 @@ public class CarpetExpression return (c_, t_) -> new NumericValue(finalSCount); }); - this.expr.addLazyFunction("neighbours", -1, (c, t, lv)-> + expression.addLazyFunction("neighbours", -1, (c, t, lv)-> { BlockPos center = BlockArgument.findIn((CarpetContext) c, lv,0).block.getPos(); @@ -2422,7 +2419,7 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("rect", -1, (c, t, lv)-> + expression.addLazyFunction("rect", -1, (c, t, lv)-> { CarpetContext cc = (CarpetContext) c; int cx, cy, cz; @@ -2546,7 +2543,7 @@ public class CarpetExpression }; }); - this.expr.addLazyFunction("diamond", -1, (c, t, lv)-> + expression.addLazyFunction("diamond", -1, (c, t, lv)-> { CarpetContext cc = (CarpetContext)c; @@ -2709,19 +2706,19 @@ public class CarpetExpression }); } - private static Map mixerMap = Arrays.stream(SoundCategory.values()).collect(Collectors.toMap(SoundCategory::getName, k -> k)); + private static final Map mixerMap = Arrays.stream(SoundCategory.values()).collect(Collectors.toMap(SoundCategory::getName, k -> k)); - private void API_Interapperability() + private static void API_Interapperability(Expression expression) { //todo doc and test all - this.expr.addLazyFunction("loaded_apps", 0, (c, t, lv) -> + expression.addLazyFunction("loaded_apps", 0, (c, t, lv) -> { // return a set of all loaded apps Value ret = new MapValue(((CarpetScriptHost) c.host).getScriptServer().modules.keySet().stream().map(StringValue::new).collect(Collectors.toSet())); return (cc, tt) -> ret; }); - this.expr.addLazyFunction("is_app_loaded", 1, (c, t, lv) -> + expression.addLazyFunction("is_app_loaded", 1, (c, t, lv) -> { Value ret = new NumericValue(((CarpetScriptHost) c.host).getScriptServer().modules.containsKey(lv.get(0).evalValue(c).getString())); return (cc, tt) -> ret; @@ -2741,9 +2738,9 @@ public class CarpetExpression return file; } - private void API_AuxiliaryAspects() + private static void API_AuxiliaryAspects(Expression expression) { - this.expr.addLazyFunction("sound", -1, (c, t, lv) -> { + expression.addLazyFunction("sound", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; Identifier soundName = new Identifier(lv.get(0).evalValue(c).getString()); Vector3Argument locator = Vector3Argument.findIn(cc, lv, 1); @@ -2778,7 +2775,7 @@ public class CarpetExpression return (_c, _t) -> new NumericValue(totalPlayed); }); - this.expr.addLazyFunction("particle", -1, (c, t, lv) -> + expression.addLazyFunction("particle", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; MinecraftServer ms = cc.s.getMinecraftServer(); @@ -2825,7 +2822,7 @@ public class CarpetExpression return (c_, t_) -> Value.TRUE; }); - this.expr.addLazyFunction("particle_line", -1, (c, t, lv) -> + expression.addLazyFunction("particle_line", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; ServerWorld world = cc.s.getWorld(); @@ -2865,7 +2862,7 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("particle_box", -1, (c, t, lv) -> + expression.addLazyFunction("particle_box", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; ServerWorld world = cc.s.getWorld(); @@ -2909,10 +2906,10 @@ public class CarpetExpression return (c_, t_) -> new NumericValue(particleCount); }); // deprecated - this.expr.alias("particle_rect", "particle_box"); + expression.alias("particle_rect", "particle_box"); - this.expr.addLazyFunction("draw_shape", -1, (c, t, lv) -> + expression.addLazyFunction("draw_shape", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; ServerPlayerEntity player[] = {null}; @@ -2941,7 +2938,7 @@ public class CarpetExpression return LazyValue.TRUE; }); - this.expr.addLazyFunction("create_marker", -1, (c, t, lv) ->{ + expression.addLazyFunction("create_marker", -1, (c, t, lv) ->{ CarpetContext cc = (CarpetContext)c; BlockState targetBlock = null; Vector3Argument pointLocator; @@ -3015,7 +3012,7 @@ public class CarpetExpression return (_c, _t) -> result; }); - this.expr.addLazyFunction("remove_all_markers", 0, (c, t, lv) -> { + expression.addLazyFunction("remove_all_markers", 0, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; int total = 0; String markerName = MARKER_STRING+"_"+((cc.host.getName()==null)?"":cc.host.getName()); @@ -3028,19 +3025,19 @@ public class CarpetExpression return (_cc, _tt) -> new NumericValue(finalTotal); }); - this.expr.addLazyFunction("nbt", 1, (c, t, lv) -> { + expression.addLazyFunction("nbt", 1, (c, t, lv) -> { Value ret = NBTSerializableValue.fromValue(lv.get(0).evalValue(c)); return (cc, tt) -> ret; }); - this.expr.addLazyFunction("escape_nbt", 1, (c, t, lv) -> { + expression.addLazyFunction("escape_nbt", 1, (c, t, lv) -> { Value v = lv.get(0).evalValue(c); String string = v.getString(); Value ret = new StringValue(StringTag.escape(string)); return (cc, tt) -> ret; }); - this.expr.addLazyFunction("parse_nbt", 1, (c, t, lv) -> { + expression.addLazyFunction("parse_nbt", 1, (c, t, lv) -> { Value v = lv.get(0).evalValue(c); if (v instanceof NBTSerializableValue) { @@ -3054,7 +3051,7 @@ public class CarpetExpression return (cc, tt) -> parsed; }); - this.expr.addLazyFunction("encode_nbt", -1, (c, t, lv) -> { + expression.addLazyFunction("encode_nbt", -1, (c, t, lv) -> { int argSize = lv.size(); if (argSize==0 || argSize > 2) throw new InternalExpressionException("'encode_nbt' requires 1 or 2 parameters"); Value v = lv.get(0).evalValue(c); @@ -3073,7 +3070,7 @@ public class CarpetExpression }); //"overridden" native call that prints to stderr - this.expr.addLazyFunction("print", -1, (c, t, lv) -> + expression.addLazyFunction("print", -1, (c, t, lv) -> { if (lv.size() == 0 || lv.size() > 2) throw new InternalExpressionException("'print' takes one or two arguments"); ServerCommandSource s = ((CarpetContext)c).s; @@ -3103,7 +3100,7 @@ public class CarpetExpression return (_c, _t) -> finalRes; // pass through for variables }); - this.expr.addLazyFunction("format", -1, (c, t, lv) -> { + expression.addLazyFunction("format", -1, (c, t, lv) -> { if (lv.size() == 0 ) throw new InternalExpressionException("'format' requires at least one component"); List values = lv.stream().map(lazy -> lazy.evalValue(c)).collect(Collectors.toList()); if (values.get(0) instanceof ListValue && values.size()==1) @@ -3113,7 +3110,7 @@ public class CarpetExpression }); //"overidden" native call to cancel if on main thread - this.expr.addLazyFunction("task_join", 1, (c, t, lv) -> { + expression.addLazyFunction("task_join", 1, (c, t, lv) -> { if (((CarpetContext)c).s.getMinecraftServer().isOnThread()) throw new InternalExpressionException("'task_join' cannot be called from main thread to avoid deadlocks"); Value v = lv.get(0).evalValue(c); @@ -3123,7 +3120,7 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunctionWithDelegation("task_dock", 1, (c, t, expr, tok, lv) -> { + expression.addLazyFunctionWithDelegation("task_dock", 1, (c, t, expr, tok, lv) -> { CarpetContext cc = (CarpetContext)c; MinecraftServer server = cc.s.getMinecraftServer(); if (server.isOnThread()) return lv.get(0); // pass through for on thread tasks @@ -3166,7 +3163,7 @@ public class CarpetExpression // implmenetation should dock the task on the main thread. }); - this.expr.addLazyFunction("run", 1, (c, t, lv) -> { + expression.addLazyFunction("run", 1, (c, t, lv) -> { BlockPos target = ((CarpetContext)c).origin; Vec3d posf = new Vec3d((double)target.getX()+0.5D,(double)target.getY(),(double)target.getZ()+0.5D); ServerCommandSource s = ((CarpetContext)c).s; @@ -3180,7 +3177,7 @@ public class CarpetExpression return LazyValue.NULL; }); - this.expr.addLazyFunction("save", 0, (c, t, lv) -> { + expression.addLazyFunction("save", 0, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; s.getMinecraftServer().getPlayerManager().saveAllPlayerData(); s.getMinecraftServer().save(true,true,true); @@ -3189,19 +3186,19 @@ public class CarpetExpression return (cc, tt) -> Value.TRUE; }); - this.expr.addLazyFunction("tick_time", 0, (c, t, lv) -> + expression.addLazyFunction("tick_time", 0, (c, t, lv) -> { Value time = new NumericValue(((CarpetContext) c).s.getMinecraftServer().getTicks()); return (cc, tt) -> time; }); - this.expr.addLazyFunction("world_time", 0, (c, t, lv) -> + expression.addLazyFunction("world_time", 0, (c, t, lv) -> { Value time = new NumericValue(((CarpetContext) c).s.getWorld().getTime()); return (cc, tt) -> time; }); - this.expr.addLazyFunction("day_time", -1, (c, t, lv) -> + expression.addLazyFunction("day_time", -1, (c, t, lv) -> { Value time = new NumericValue(((CarpetContext) c).s.getWorld().getTimeOfDay()); if (lv.size() > 0) @@ -3213,7 +3210,7 @@ public class CarpetExpression return (cc, tt) -> time; }); - this.expr.addLazyFunction("last_tick_times", -1, (c, t, lv) -> + expression.addLazyFunction("last_tick_times", -1, (c, t, lv) -> { //assuming we are in the tick world section // might be off one tick when run in the off tasks or asynchronously. @@ -3229,7 +3226,7 @@ public class CarpetExpression }); - this.expr.addLazyFunction("game_tick", -1, (c, t, lv) -> { + expression.addLazyFunction("game_tick", -1, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; if (!s.getMinecraftServer().isOnThread()) throw new InternalExpressionException("Unable to run ticks from threads"); ((MinecraftServerInterface)s.getMinecraftServer()).forceTick( () -> System.nanoTime()- CarpetServer.scriptServer.tickStart<50000000L); @@ -3256,13 +3253,13 @@ public class CarpetExpression return (cc, tt) -> Value.TRUE; }); - this.expr.addLazyFunction("seed", -1, (c, t, lv) -> { + expression.addLazyFunction("seed", -1, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; Value ret = new NumericValue(s.getWorld().getSeed()); return (cc, tt) -> ret; }); - this.expr.addLazyFunction("relight", -1, (c, t, lv) -> + expression.addLazyFunction("relight", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext) c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); @@ -3273,19 +3270,19 @@ public class CarpetExpression return LazyValue.TRUE; }); - this.expr.addLazyFunction("current_dimension", 0, (c, t, lv) -> { + expression.addLazyFunction("current_dimension", 0, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; Value retval = ValueConversions.dimName(s.getWorld()); return (cc, tt) -> retval; }); - this.expr.addLazyFunction("view_distance", 0, (c, t, lv) -> { + expression.addLazyFunction("view_distance", 0, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; Value retval = new NumericValue(s.getMinecraftServer().getPlayerManager().getViewDistance()); return (cc, tt) -> retval; }); - this.expr.addLazyFunction("in_dimension", 2, (c, t, lv) -> { + expression.addLazyFunction("in_dimension", 2, (c, t, lv) -> { ServerCommandSource outerSource = ((CarpetContext)c).s; Value dimensionValue = lv.get(0).evalValue(c); ServerCommandSource innerSource = outerSource; @@ -3347,7 +3344,7 @@ public class CarpetExpression return (cc, tt) -> retval; }); - this.expr.addLazyFunction("plop", 4, (c, t, lv) ->{ + expression.addLazyFunction("plop", 4, (c, t, lv) ->{ BlockArgument locator = BlockArgument.findIn((CarpetContext)c, lv, 0); if (lv.size() <= locator.offset) throw new InternalExpressionException("'plop' needs extra argument indicating what to plop"); @@ -3368,12 +3365,12 @@ public class CarpetExpression return (_c, _t) -> ret; }); - this.expr.addLazyFunction("schedule", -1, (c, t, lv) -> { + expression.addLazyFunction("schedule", -1, (c, t, lv) -> { if (lv.size()<2) throw new InternalExpressionException("'schedule' should have at least 2 arguments, delay and call name"); long delay = NumericValue.asNumber(lv.get(0).evalValue(c)).getLong(); - FunctionArgument functionArgument = FunctionArgument.findIn(c, this.expr.module, lv, 1, true, true); + FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, true, true); CarpetServer.scriptServer.events.scheduleCall( (CarpetContext) c, @@ -3384,7 +3381,7 @@ public class CarpetExpression return (c_, t_) -> Value.TRUE; }); - this.expr.addLazyFunction("logger", -1, (c, t, lv) -> + expression.addLazyFunction("logger", -1, (c, t, lv) -> { //CarpetSettings.LOG.error("Standard Structures:"); //CarpetSettings.LOG.error(Registry.STRUCTURE_FEATURE.getIds().stream().map(i -> "`'"+NBTSerializableValue.nameFromRegistryId(i)+"'`").sorted(). collect(Collectors.joining(", "))); @@ -3428,7 +3425,7 @@ public class CarpetExpression return (_c, _t) -> res; // pass through for variables }); - this.expr.addLazyFunction("read_file", 2, (c, t, lv) -> { + expression.addLazyFunction("read_file", 2, (c, t, lv) -> { String resource = recognizeResource(lv.get(0).evalValue(c)); String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT); boolean shared = origtype.startsWith("shared_"); @@ -3451,7 +3448,7 @@ public class CarpetExpression return (cc, tt) -> retVal; }); - this.expr.addLazyFunction("delete_file", 2, (c, t, lv) -> { + expression.addLazyFunction("delete_file", 2, (c, t, lv) -> { String resource = recognizeResource(lv.get(0).evalValue(c)); String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT); boolean shared = origtype.startsWith("shared_"); @@ -3462,7 +3459,7 @@ public class CarpetExpression return success?LazyValue.TRUE:LazyValue.FALSE; }); - this.expr.addLazyFunction("write_file", -1, (c, t, lv) -> { + expression.addLazyFunction("write_file", -1, (c, t, lv) -> { if (lv.size() < 3) throw new InternalExpressionException("'write_file' requires three or more arguments"); String resource = recognizeResource(lv.get(0).evalValue(c)); String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT); @@ -3510,7 +3507,7 @@ public class CarpetExpression //write_file - this.expr.addLazyFunction("load_app_data", -1, (c, t, lv) -> + expression.addLazyFunction("load_app_data", -1, (c, t, lv) -> { String file = null; boolean shared = false; @@ -3529,7 +3526,7 @@ public class CarpetExpression return (cc, tt) -> retVal; }); - this.expr.addLazyFunction("store_app_data", -1, (c, t, lv) -> + expression.addLazyFunction("store_app_data", -1, (c, t, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'store_app_data' needs NBT tag and an optional file"); @@ -3552,7 +3549,7 @@ public class CarpetExpression return success?LazyValue.TRUE:LazyValue.FALSE; }); - this.expr.addLazyFunction("statistic", 3, (c, t, lv) -> + expression.addLazyFunction("statistic", 3, (c, t, lv) -> { Value playerValue = lv.get(0).evalValue(c); CarpetContext cc = (CarpetContext)c; @@ -3579,10 +3576,10 @@ public class CarpetExpression }); } - private void API_GaemMonitoring() + private static void API_GaemMonitoring(Expression expression) { // game processed snooper functions - this.expr.addLazyFunction("get_mob_counts", -1, (c, t, lv) -> + expression.addLazyFunction("get_mob_counts", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; ServerWorld world = cc.s.getWorld(); @@ -3620,7 +3617,7 @@ public class CarpetExpression }); } - private Stat getStat(StatType type, Identifier id) + private static Stat getStat(StatType type, Identifier id) { T key = type.getRegistry().get(id); if (key == null || !((StatTypeInterface)type).hasStatCreated(key)) @@ -3642,14 +3639,14 @@ public class CarpetExpression this.expr = new Expression(expression); this.expr.asAModule(module); - API_BlockManipulation(); - API_EntityManipulation(); - API_InventoryManipulation(); - API_IteratingOverAreasOfBlocks(); - API_AuxiliaryAspects(); - API_Scoreboard(); - API_Interapperability(); - API_GaemMonitoring(); + API_BlockManipulation(this.expr); + API_EntityManipulation(this.expr); + API_InventoryManipulation(this.expr); + API_IteratingOverAreasOfBlocks(this.expr); + API_AuxiliaryAspects(this.expr); + API_Scoreboard(this.expr); + API_Interapperability(this.expr); + API_GaemMonitoring(this.expr); CarpetServer.extensions.forEach(e -> e.scarpetApi(this)); } diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index a154cb6d..95a96393 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -133,17 +133,10 @@ public class Expression public List getExpressionSnippet(Tokenizer.Token token) { String code = this.getCodeString(); - List output = new ArrayList<>(); - for (String line: getExpressionSnippetLeftContext(token, code, 1)) - { - output.add(line); - } + List output = new ArrayList<>(getExpressionSnippetLeftContext(token, code, 1)); List context = getExpressionSnippetContext(token, code); output.add(context.get(0)+" HERE>> "+context.get(1)); - for (String line: getExpressionSnippetRightContext(token, code, 1)) - { - output.add(line); - } + output.addAll(getExpressionSnippetRightContext(token, code, 1)); return output; } @@ -421,30 +414,30 @@ public class Expression } - private void UserDefinedFunctionsAndControlFlow() // public just to get the javadoc right + private static void UserDefinedFunctionsAndControlFlow(Expression expression) // public just to get the javadoc right { // artificial construct to handle user defined functions and function definitions - addLazyFunction("import", -1, (c, t, lv) -> + expression.addLazyFunction("import", -1, (c, t, lv) -> { if (lv.size() < 1) throw new InternalExpressionException("'import' needs at least a module name to import, and list of values to import"); String moduleName = lv.get(0).evalValue(c).getString(); c.host.importModule(c, moduleName); moduleName = moduleName.toLowerCase(Locale.ROOT); if (lv.size() > 1) - c.host.importNames(c, module, moduleName, lv.subList(1, lv.size()).stream().map((l) -> l.evalValue(c).getString()).collect(Collectors.toList())); + c.host.importNames(c, expression.module, moduleName, lv.subList(1, lv.size()).stream().map((l) -> l.evalValue(c).getString()).collect(Collectors.toList())); if (t == Context.VOID) return LazyValue.NULL; ListValue list = ListValue.wrap(c.host.availableImports(moduleName).map(StringValue::new).collect(Collectors.toList())); return (cc, tt) -> list; }); - addLazyFunctionWithDelegation("call",-1, (c, t, expr, tok, lv) -> { // adjust based on c + expression.addLazyFunctionWithDelegation("call",-1, (c, t, expr, tok, lv) -> { // adjust based on c if (lv.size() == 0) throw new InternalExpressionException("'call' expects at least function name to call"); //lv.remove(lv.size()-1); // aint gonna cut it // maybe it will because of the eager eval changes if (t != Context.SIGNATURE) // just call the function { - FunctionArgument functionArgument = FunctionArgument.findIn(c, module, lv, 0, false, true); + FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true); FunctionValue fun = functionArgument.function; Value retval = fun.callInContext(expr, c, t, fun.getExpression(), fun.getToken(), functionArgument.args).evalValue(c); return (cc, tt) -> retval; ///!!!! dono might need to store expr and token in statics? (e? t?) @@ -472,13 +465,13 @@ public class Expression Value retval = new FunctionSignatureValue(name, args, globals); return (cc, tt) -> retval; }); - addLazyFunction("outer", 1, (c, t, lv) -> { + expression.addLazyFunction("outer", 1, (c, t, lv) -> { if (t != Context.LOCALIZATION) throw new InternalExpressionException("Outer scoping of variables is only possible in function signatures"); return (cc, tt) -> new GlobalValue(lv.get(0).evalValue(c)); }); - addLazyBinaryOperator(";",precedence.get("nextop;"), true, (c, t, lv1, lv2) -> + expression.addLazyBinaryOperator(";",precedence.get("nextop;"), true, (c, t, lv1, lv2) -> { lv1.evalValue(c, Context.VOID); Value v2 = lv2.evalValue(c, t); @@ -486,7 +479,7 @@ public class Expression }); //assigns const procedure to the lhs, returning its previous value - addLazyBinaryOperatorWithDelegation("->", precedence.get("def->"), false, (c, type, e, t, lv1, lv2) -> + expression.addLazyBinaryOperatorWithDelegation("->", precedence.get("def->"), false, (c, type, e, t, lv1, lv2) -> { if (type == Context.MAPDEF) { @@ -497,15 +490,15 @@ public class Expression if (!(v1 instanceof FunctionSignatureValue)) throw new InternalExpressionException("'->' operator requires a function signature on the LHS"); FunctionSignatureValue sign = (FunctionSignatureValue) v1; - Value result = addContextFunction(c, sign.getName(), e, t, sign.getArgs(), sign.getGlobals(), lv2); + Value result = expression.addContextFunction(c, sign.getName(), e, t, sign.getArgs(), sign.getGlobals(), lv2); return (cc, tt) -> result; }); - addFunction("exit", (lv) -> { throw new ExitStatement(lv.size()==0?Value.NULL:lv.get(0)); }); - addFunction("return", (lv) -> { throw new ReturnStatement(lv.size()==0?Value.NULL:lv.get(0));} ); - addFunction("throw", (lv)-> {throw new ThrowStatement(lv.size()==0?Value.NULL:lv.get(0)); }); + expression.addFunction("exit", (lv) -> { throw new ExitStatement(lv.size()==0?Value.NULL:lv.get(0)); }); + expression.addFunction("return", (lv) -> { throw new ReturnStatement(lv.size()==0?Value.NULL:lv.get(0));} ); + expression.addFunction("throw", (lv)-> {throw new ThrowStatement(lv.size()==0?Value.NULL:lv.get(0)); }); - addLazyFunction("try", -1, (c, t, lv) -> + expression.addLazyFunction("try", -1, (c, t, lv) -> { if (lv.size()==0) throw new InternalExpressionException("'try' needs at least an expression block"); @@ -527,7 +520,7 @@ public class Expression }); // if(cond1, expr1, cond2, expr2, ..., ?default) => value - addLazyFunction("if", -1, (c, t, lv) -> + expression.addLazyFunction("if", -1, (c, t, lv) -> { if ( lv.size() < 2 ) throw new InternalExpressionException("'if' statement needs to have at least one condition and one case"); @@ -549,47 +542,47 @@ public class Expression }); } - private void Operators() + private static void Operators(Expression expression) { - addBinaryOperator("+", precedence.get("addition+-"), true, Value::add); - addBinaryOperator("-", precedence.get("addition+-"), true, Value::subtract); - addBinaryOperator("*", precedence.get("multiplication*/%"), true, Value::multiply); - addBinaryOperator("/", precedence.get("multiplication*/%"), true, Value::divide); - addBinaryOperator("%", precedence.get("multiplication*/%"), true, (v1, v2) -> + expression.addBinaryOperator("+", precedence.get("addition+-"), true, Value::add); + expression.addBinaryOperator("-", precedence.get("addition+-"), true, Value::subtract); + expression.addBinaryOperator("*", precedence.get("multiplication*/%"), true, Value::multiply); + expression.addBinaryOperator("/", precedence.get("multiplication*/%"), true, Value::divide); + expression.addBinaryOperator("%", precedence.get("multiplication*/%"), true, (v1, v2) -> new NumericValue(NumericValue.asNumber(v1).getDouble() % NumericValue.asNumber(v2).getDouble())); - addBinaryOperator("^", precedence.get("exponent^"), false, (v1, v2) -> + expression.addBinaryOperator("^", precedence.get("exponent^"), false, (v1, v2) -> new NumericValue(Math.pow(NumericValue.asNumber(v1).getDouble(), NumericValue.asNumber(v2).getDouble()))); - addLazyBinaryOperator("&&", precedence.get("and&&"), false, (c, t, lv1, lv2) -> + expression.addLazyBinaryOperator("&&", precedence.get("and&&"), false, (c, t, lv1, lv2) -> { Value v1 = lv1.evalValue(c, Context.BOOLEAN); if (!v1.getBoolean()) return (cc, tt) -> v1; return lv2; }); - addLazyBinaryOperator("||", precedence.get("or||"), false, (c, t, lv1, lv2) -> + expression.addLazyBinaryOperator("||", precedence.get("or||"), false, (c, t, lv1, lv2) -> { Value v1 = lv1.evalValue(c, Context.BOOLEAN); if (v1.getBoolean()) return (cc, tt) -> v1; return lv2; }); - addBinaryOperator("~", precedence.get("attribute~:"), true, Value::in); + expression.addBinaryOperator("~", precedence.get("attribute~:"), true, Value::in); - addBinaryOperator(">", precedence.get("compare>=><=<"), false, (v1, v2) -> + expression.addBinaryOperator(">", precedence.get("compare>=><=<"), false, (v1, v2) -> v1.compareTo(v2) > 0 ? Value.TRUE : Value.FALSE); - addBinaryOperator(">=", precedence.get("compare>=><=<"), false, (v1, v2) -> + expression.addBinaryOperator(">=", precedence.get("compare>=><=<"), false, (v1, v2) -> v1.compareTo(v2) >= 0 ? Value.TRUE : Value.FALSE); - addBinaryOperator("<", precedence.get("compare>=><=<"), false, (v1, v2) -> + expression.addBinaryOperator("<", precedence.get("compare>=><=<"), false, (v1, v2) -> v1.compareTo(v2) < 0 ? Value.TRUE : Value.FALSE); - addBinaryOperator("<=", precedence.get("compare>=><=<"), false, (v1, v2) -> + expression.addBinaryOperator("<=", precedence.get("compare>=><=<"), false, (v1, v2) -> v1.compareTo(v2) <= 0 ? Value.TRUE : Value.FALSE); - addBinaryOperator("==", precedence.get("equal==!="), false, (v1, v2) -> + expression.addBinaryOperator("==", precedence.get("equal==!="), false, (v1, v2) -> v1.equals(v2) ? Value.TRUE : Value.FALSE); - addBinaryOperator("!=", precedence.get("equal==!="), false, (v1, v2) -> + expression.addBinaryOperator("!=", precedence.get("equal==!="), false, (v1, v2) -> v1.equals(v2) ? Value.FALSE : Value.TRUE); - addLazyBinaryOperator("=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) -> + expression.addLazyBinaryOperator("=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) -> { Value v1 = lv1.evalValue(c, Context.LVALUE); Value v2 = lv2.evalValue(c); @@ -606,7 +599,7 @@ public class Expression { String lname = li.next().getVariable(); Value vval = ri.next().reboundedTo(lname); - setAnyVariable(c, lname, (cc, tt) -> vval); + expression.setAnyVariable(c, lname, (cc, tt) -> vval); } return (cc, tt) -> Value.TRUE; } @@ -623,11 +616,11 @@ public class Expression String varname = v1.getVariable(); Value copy = v2.reboundedTo(varname); LazyValue boundedLHS = (cc, tt) -> copy; - setAnyVariable(c, varname, boundedLHS); + expression.setAnyVariable(c, varname, boundedLHS); return boundedLHS; }); - addLazyBinaryOperator("+=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) -> + expression.addLazyBinaryOperator("+=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) -> { Value v1 = lv1.evalValue(c, Context.LVALUE); Value v2 = lv2.evalValue(c); @@ -645,7 +638,7 @@ public class Expression Value lval = li.next(); String lname = lval.getVariable(); Value result = lval.add(ri.next()).bindTo(lname); - setAnyVariable(c, lname, (cc, tt) -> result); + expression.setAnyVariable(c, lname, (cc, tt) -> result); } return (cc, tt) -> Value.TRUE; } @@ -683,11 +676,11 @@ public class Expression Value result = v1.add(v2).bindTo(varname); boundedLHS = (cc, tt) -> result; } - setAnyVariable(c, varname, boundedLHS); + expression.setAnyVariable(c, varname, boundedLHS); return boundedLHS; }); - addLazyBinaryOperator("<>", precedence.get("assign=<>"), false, (c, t, lv1, lv2) -> + expression.addLazyBinaryOperator("<>", precedence.get("assign=<>"), false, (c, t, lv1, lv2) -> { Value v1 = lv1.evalValue(c); Value v2 = lv2.evalValue(c); @@ -709,8 +702,8 @@ public class Expression String rname = rval.getVariable(); lval.reboundedTo(rname); rval.reboundedTo(lname); - setAnyVariable(c, lname, (cc, tt) -> rval); - setAnyVariable(c, rname, (cc, tt) -> lval); + expression.setAnyVariable(c, lname, (cc, tt) -> rval); + expression.setAnyVariable(c, rname, (cc, tt) -> lval); } return (cc, tt) -> Value.TRUE; } @@ -720,24 +713,24 @@ public class Expression String rvalvar = v2.getVariable(); Value lval = v2.reboundedTo(lvalvar); Value rval = v1.reboundedTo(rvalvar); - setAnyVariable(c, lvalvar, (cc, tt) -> lval); - setAnyVariable(c, rvalvar, (cc, tt) -> rval); + expression.setAnyVariable(c, lvalvar, (cc, tt) -> lval); + expression.setAnyVariable(c, rvalvar, (cc, tt) -> rval); return (cc, tt) -> lval; }); - addUnaryOperator("-", false, (v) -> new NumericValue(-NumericValue.asNumber(v).getDouble())); + expression.addUnaryOperator("-", false, (v) -> new NumericValue(-NumericValue.asNumber(v).getDouble())); - addUnaryOperator("+", false, (v) -> new NumericValue(NumericValue.asNumber(v).getDouble())); + expression.addUnaryOperator("+", false, (v) -> new NumericValue(NumericValue.asNumber(v).getDouble())); - addLazyUnaryOperator("!", precedence.get("unary+-!"), false, (c, t, lv)-> lv.evalValue(c, Context.BOOLEAN).getBoolean() ? (cc, tt)-> Value.FALSE : (cc, tt) -> Value.TRUE); // might need context boolean + expression.addLazyUnaryOperator("!", precedence.get("unary+-!"), false, (c, t, lv)-> lv.evalValue(c, Context.BOOLEAN).getBoolean() ? (cc, tt)-> Value.FALSE : (cc, tt) -> Value.TRUE); // might need context boolean } - private void ArithmeticOperations() + private static void ArithmeticOperations(Expression expression) { - addLazyFunction("not", 1, (c, t, lv) -> lv.get(0).evalValue(c, Context.BOOLEAN).getBoolean() ? ((cc, tt) -> Value.FALSE) : ((cc, tt) -> Value.TRUE)); + expression.addLazyFunction("not", 1, (c, t, lv) -> lv.get(0).evalValue(c, Context.BOOLEAN).getBoolean() ? ((cc, tt) -> Value.FALSE) : ((cc, tt) -> Value.TRUE)); - addUnaryFunction("fact", (v) -> + expression.addUnaryFunction("fact", (v) -> { long number = NumericValue.asNumber(v).getLong(); long factorial = 1; @@ -747,45 +740,45 @@ public class Expression } return new NumericValue(factorial); }); - addMathematicalUnaryFunction("sin", (d) -> Math.sin(Math.toRadians(d))); - addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d))); - addMathematicalUnaryFunction("tan", (d) -> Math.tan(Math.toRadians(d))); - addMathematicalUnaryFunction("asin", (d) -> Math.toDegrees(Math.asin(d))); - addMathematicalUnaryFunction("acos", (d) -> Math.toDegrees(Math.acos(d))); - addMathematicalUnaryFunction("atan", (d) -> Math.toDegrees(Math.atan(d))); - addMathematicalBinaryFunction("atan2", (d, d2) -> Math.toDegrees(Math.atan2(d, d2)) ); - addMathematicalUnaryFunction("sinh", Math::sinh ); - addMathematicalUnaryFunction("cosh", Math::cosh ); - addMathematicalUnaryFunction("tanh", Math::tanh ); - addMathematicalUnaryFunction("sec", (d) -> 1.0 / Math.cos(Math.toRadians(d)) ); // Formula: sec(x) = 1 / cos(x) - addMathematicalUnaryFunction("csc", (d) -> 1.0 / Math.sin(Math.toRadians(d)) ); // Formula: csc(x) = 1 / sin(x) - addMathematicalUnaryFunction("sech", (d) -> 1.0 / Math.cosh(d) ); // Formula: sech(x) = 1 / cosh(x) - addMathematicalUnaryFunction("csch", (d) -> 1.0 / Math.sinh(d) ); // Formula: csch(x) = 1 / sinh(x) - addMathematicalUnaryFunction("cot", (d) -> 1.0 / Math.tan(Math.toRadians(d)) ); // Formula: cot(x) = cos(x) / sin(x) = 1 / tan(x) - addMathematicalUnaryFunction("acot", (d) -> Math.toDegrees(Math.atan(1.0 / d)) );// Formula: acot(x) = atan(1/x) - addMathematicalUnaryFunction("coth", (d) -> 1.0 / Math.tanh(d) ); // Formula: coth(x) = 1 / tanh(x) - addMathematicalUnaryFunction("asinh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) + 1)))); // Formula: asinh(x) = ln(x + sqrt(x^2 + 1)) - addMathematicalUnaryFunction("acosh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) - 1)))); // Formula: acosh(x) = ln(x + sqrt(x^2 - 1)) - addMathematicalUnaryFunction("atanh", (d) -> // Formula: atanh(x) = 0.5*ln((1 + x)/(1 - x)) + expression.addMathematicalUnaryFunction("sin", (d) -> Math.sin(Math.toRadians(d))); + expression.addMathematicalUnaryFunction("cos", (d) -> Math.cos(Math.toRadians(d))); + expression.addMathematicalUnaryFunction("tan", (d) -> Math.tan(Math.toRadians(d))); + expression.addMathematicalUnaryFunction("asin", (d) -> Math.toDegrees(Math.asin(d))); + expression.addMathematicalUnaryFunction("acos", (d) -> Math.toDegrees(Math.acos(d))); + expression.addMathematicalUnaryFunction("atan", (d) -> Math.toDegrees(Math.atan(d))); + expression.addMathematicalBinaryFunction("atan2", (d, d2) -> Math.toDegrees(Math.atan2(d, d2)) ); + expression.addMathematicalUnaryFunction("sinh", Math::sinh ); + expression.addMathematicalUnaryFunction("cosh", Math::cosh ); + expression.addMathematicalUnaryFunction("tanh", Math::tanh ); + expression.addMathematicalUnaryFunction("sec", (d) -> 1.0 / Math.cos(Math.toRadians(d)) ); // Formula: sec(x) = 1 / cos(x) + expression.addMathematicalUnaryFunction("csc", (d) -> 1.0 / Math.sin(Math.toRadians(d)) ); // Formula: csc(x) = 1 / sin(x) + expression.addMathematicalUnaryFunction("sech", (d) -> 1.0 / Math.cosh(d) ); // Formula: sech(x) = 1 / cosh(x) + expression.addMathematicalUnaryFunction("csch", (d) -> 1.0 / Math.sinh(d) ); // Formula: csch(x) = 1 / sinh(x) + expression.addMathematicalUnaryFunction("cot", (d) -> 1.0 / Math.tan(Math.toRadians(d)) ); // Formula: cot(x) = cos(x) / sin(x) = 1 / tan(x) + expression.addMathematicalUnaryFunction("acot", (d) -> Math.toDegrees(Math.atan(1.0 / d)) );// Formula: acot(x) = atan(1/x) + expression.addMathematicalUnaryFunction("coth", (d) -> 1.0 / Math.tanh(d) ); // Formula: coth(x) = 1 / tanh(x) + expression.addMathematicalUnaryFunction("asinh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) + 1)))); // Formula: asinh(x) = ln(x + sqrt(x^2 + 1)) + expression.addMathematicalUnaryFunction("acosh", (d) -> Math.log(d + (Math.sqrt(Math.pow(d, 2) - 1)))); // Formula: acosh(x) = ln(x + sqrt(x^2 - 1)) + expression.addMathematicalUnaryFunction("atanh", (d) -> // Formula: atanh(x) = 0.5*ln((1 + x)/(1 - x)) { if (Math.abs(d) > 1 || Math.abs(d) == 1) throw new InternalExpressionException("Number must be |x| < 1"); return 0.5 * Math.log((1 + d) / (1 - d)); }); - addMathematicalUnaryFunction("rad", Math::toRadians); - addMathematicalUnaryFunction("deg", Math::toDegrees); - addMathematicalUnaryFunction("ln", Math::log); - addMathematicalUnaryFunction("ln1p", Math::log1p); - addMathematicalUnaryFunction("log10", Math::log10); - addMathematicalUnaryFunction("log", a -> Math.log(a)/Math.log(2)); - addMathematicalUnaryFunction("log1p", x -> Math.log1p(x)/Math.log(2)); - addMathematicalUnaryFunction("sqrt", Math::sqrt); - addMathematicalUnaryFunction("abs", Math::abs); - addMathematicalUnaryFunction("round", (d) -> (double)Math.round(d)); - addMathematicalUnaryFunction("floor", Math::floor); - addMathematicalUnaryFunction("ceil", Math::ceil); + expression.addMathematicalUnaryFunction("rad", Math::toRadians); + expression.addMathematicalUnaryFunction("deg", Math::toDegrees); + expression.addMathematicalUnaryFunction("ln", Math::log); + expression.addMathematicalUnaryFunction("ln1p", Math::log1p); + expression.addMathematicalUnaryFunction("log10", Math::log10); + expression.addMathematicalUnaryFunction("log", a -> Math.log(a)/Math.log(2)); + expression.addMathematicalUnaryFunction("log1p", x -> Math.log1p(x)/Math.log(2)); + expression.addMathematicalUnaryFunction("sqrt", Math::sqrt); + expression.addMathematicalUnaryFunction("abs", Math::abs); + expression.addMathematicalUnaryFunction("round", (d) -> (double)Math.round(d)); + expression.addMathematicalUnaryFunction("floor", Math::floor); + expression.addMathematicalUnaryFunction("ceil", Math::ceil); - addLazyFunction("mandelbrot", 3, (c, t, lv) -> { + expression.addLazyFunction("mandelbrot", 3, (c, t, lv) -> { double a0 = NumericValue.asNumber(lv.get(0).evalValue(c)).getDouble(); double b0 = NumericValue.asNumber(lv.get(1).evalValue(c)).getDouble(); long maxiter = NumericValue.asNumber(lv.get(2).evalValue(c)).getLong(); @@ -803,7 +796,7 @@ public class Expression return (cc, tt) -> new NumericValue(iFinal); }); - addFunction("max", (lv) -> + expression.addFunction("max", (lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'max' requires at least one parameter"); @@ -817,7 +810,7 @@ public class Expression return max; }); - addFunction("min", (lv) -> + expression.addFunction("min", (lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'min' requires at least one parameter"); @@ -831,27 +824,27 @@ public class Expression return min; }); - addUnaryFunction("relu", (v) -> v.compareTo(Value.ZERO) < 0 ? Value.ZERO : v); + expression.addUnaryFunction("relu", (v) -> v.compareTo(Value.ZERO) < 0 ? Value.ZERO : v); } - private void LoopsAndHigherOrderFunctions() + private static void LoopsAndHigherOrderFunctions(Expression expression) { // condition and expression will get a bound '_i' // returns last successful expression or false // while(cond, limit, expr) => ?? - addFunction("break", lv -> { + expression.addFunction("break", lv -> { if (lv.size()==0) throw new BreakStatement(null); if (lv.size()==1) throw new BreakStatement(lv.get(0)); throw new InternalExpressionException("'break' can only be called with zero or one argument"); }); - addFunction("continue", lv -> { + expression.addFunction("continue", lv -> { if (lv.size()==0) throw new ContinueStatement(null); if (lv.size()==1) throw new ContinueStatement(lv.get(0)); throw new InternalExpressionException("'continue' can only be called with zero or one argument"); }); - addLazyFunction("while", 3, (c, t, lv) -> + expression.addLazyFunction("while", 3, (c, t, lv) -> { long limit = NumericValue.asNumber(lv.get(1).evalValue(c)).getLong(); LazyValue condition = lv.get(0); @@ -884,7 +877,7 @@ public class Expression // loop(Num, expr) => last_value // expr receives bounded variable '_' indicating iteration - addLazyFunction("loop", 2, (c, t, lv) -> + expression.addLazyFunction("loop", 2, (c, t, lv) -> { long limit = NumericValue.asNumber(lv.get(0).evalValue(c)).getLong(); Value lastOne = Value.NULL; @@ -913,7 +906,7 @@ public class Expression // map(list or Num, expr) => list_results // receives bounded variable '_' with the expression - addLazyFunction("map", 2, (c, t, lv) -> + expression.addLazyFunction("map", 2, (c, t, lv) -> { Value rval= lv.get(0).evalValue(c); if (!(rval instanceof AbstractListValue)) @@ -958,7 +951,7 @@ public class Expression // grep(list or num, expr) => list // receives bounded variable '_' with the expression, and "_i" with index // produces list of values for which the expression is true - addLazyFunction("filter", 2, (c, t, lv) -> + expression.addLazyFunction("filter", 2, (c, t, lv) -> { Value rval= lv.get(0).evalValue(c); if (!(rval instanceof AbstractListValue)) @@ -1004,7 +997,7 @@ public class Expression // first(list, expr) => elem or null // receives bounded variable '_' with the expression, and "_i" with index // returns first element on the list for which the expr is true - addLazyFunction("first", 2, (c, t, lv) -> + expression.addLazyFunction("first", 2, (c, t, lv) -> { Value rval= lv.get(0).evalValue(c); @@ -1056,7 +1049,7 @@ public class Expression // all(list, expr) => boolean // receives bounded variable '_' with the expression, and "_i" with index // returns true if expr is true for all items - addLazyFunction("all", 2, (c, t, lv) -> + expression.addLazyFunction("all", 2, (c, t, lv) -> { Value rval= lv.get(0).evalValue(c); if (!(rval instanceof AbstractListValue)) @@ -1091,7 +1084,7 @@ public class Expression }); // runs traditional for(init, condition, increment, body) tri-argument for loop with body in between - addLazyFunction("c_for", 4, (c, t, lv) -> { + expression.addLazyFunction("c_for", 4, (c, t, lv) -> { LazyValue initial = lv.get(0); LazyValue condition = lv.get(1); LazyValue increment = lv.get(2); @@ -1119,7 +1112,7 @@ public class Expression // similar to map, but returns total number of successes // for(list, expr) => success_count // can be substituted for first and all, but first is more efficient and all doesn't require knowing list size - addLazyFunction("for", 2, (c, t, lv) -> + expression.addLazyFunction("for", 2, (c, t, lv) -> { Value rval= lv.get(0).evalValue(c); if (!(rval instanceof AbstractListValue)) @@ -1169,7 +1162,7 @@ public class Expression // reduces values in the list with expression that gets accumulator // each iteration expr receives acc - accumulator, and '_' - current list value // returned value is substituted to the accumulator - addLazyFunction("reduce", 3, (c, t, lv) -> + expression.addLazyFunction("reduce", 3, (c, t, lv) -> { LazyValue expr = lv.get(1); @@ -1226,16 +1219,16 @@ public class Expression }); } - private void BasicDataStructures() + private static void BasicDataStructures(Expression expression) { - addFunction("l", lv -> + expression.addFunction("l", lv -> { if (lv.size() == 1 && lv.get(0) instanceof LazyListValue) return ListValue.wrap(((LazyListValue) lv.get(0)).unroll()); return new ListValue.ListConstructorValue(lv); }); - addFunction("join", (lv) -> + expression.addFunction("join", (lv) -> { if (lv.size() < 2) throw new InternalExpressionException("'join' takes at least 2 arguments"); @@ -1257,13 +1250,13 @@ public class Expression return new StringValue(toJoin.stream().map(Value::getString).collect(Collectors.joining(delimiter))); }); - addBinaryFunction("split", (d, v) -> { + expression.addBinaryFunction("split", (d, v) -> { String delimiter = d.getString(); String hwat = v.getString(); return ListValue.wrap(Arrays.stream(hwat.split(delimiter)).map(StringValue::new).collect(Collectors.toList())); }); - addFunction("slice", (lv) -> { + expression.addFunction("slice", (lv) -> { if (lv.size() != 2 && lv.size() != 3) throw new InternalExpressionException("'slice' takes 2 or 3 arguments"); @@ -1275,7 +1268,7 @@ public class Expression return hwat.slice(from, to); }); - addFunction("sort", (lv) -> + expression.addFunction("sort", (lv) -> { List toSort = lv; if (lv.size()==1 && lv.get(0) instanceof ListValue) @@ -1286,7 +1279,7 @@ public class Expression return ListValue.wrap(toSort); }); - addLazyFunction("sort_key", -1, (c, t, lv) -> //get working with iterators + expression.addLazyFunction("sort_key", -1, (c, t, lv) -> //get working with iterators { if (lv.size() == 0) throw new InternalExpressionException("First argument for 'sort_key' should be a List"); @@ -1315,7 +1308,7 @@ public class Expression return (cc, tt) -> ListValue.wrap(toSort); }); - addFunction("range", (lv) -> + expression.addFunction("range", (lv) -> { long from = 0; long to = 0; @@ -1336,7 +1329,7 @@ public class Expression return LazyListValue.range(from, to, step); }); - addLazyFunction("m", -1, (c, t, llv) -> + expression.addLazyFunction("m", -1, (c, t, llv) -> { List lv = new ArrayList<>(); for (LazyValue lazyParam : llv) { @@ -1350,19 +1343,19 @@ public class Expression return (cc, tt) -> ret; }); - addUnaryFunction("keys", v -> { + expression.addUnaryFunction("keys", v -> { if (v instanceof MapValue) return new ListValue(((MapValue) v).getMap().keySet()); return Value.NULL; }); - addUnaryFunction("values", v -> { + expression.addUnaryFunction("values", v -> { if (v instanceof MapValue) return new ListValue(((MapValue) v).getMap().values()); return Value.NULL; }); - addUnaryFunction("pairs", v -> { + expression.addUnaryFunction("pairs", v -> { if (v instanceof MapValue) return ListValue.wrap(((MapValue) v).getMap().entrySet().stream().map( (p) -> ListValue.of(p.getKey(), p.getValue()) @@ -1370,7 +1363,7 @@ public class Expression return Value.NULL; }); - addLazyBinaryOperator(":", precedence.get("attribute~:"),true, (c, t, container_lv, key_lv) -> + expression.addLazyBinaryOperator(":", precedence.get("attribute~:"),true, (c, t, container_lv, key_lv) -> { Value container = container_lv.evalValue(c); if (container instanceof LContainerValue) @@ -1405,7 +1398,7 @@ public class Expression return (cc, ct) -> retVal; }); - addLazyFunction("get", -1, (c, t, lv) -> + expression.addLazyFunction("get", -1, (c, t, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'get' requires parameters"); @@ -1432,7 +1425,7 @@ public class Expression return (cc, tt) -> finalContainer; }); - addLazyFunction("has", -1, (c, t, lv) -> + expression.addLazyFunction("has", -1, (c, t, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'has' requires parameters"); @@ -1459,7 +1452,7 @@ public class Expression return (cc, tt) -> ret; }); - addLazyFunction("put", -1, (c, t, lv) -> + expression.addLazyFunction("put", -1, (c, t, lv) -> { if(lv.size()<2) { @@ -1497,7 +1490,7 @@ public class Expression return (cc, tt) -> retVal; }); - addLazyFunction("delete", -1, (c, t, lv) -> + expression.addLazyFunction("delete", -1, (c, t, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'delete' requires parameters"); @@ -1525,13 +1518,13 @@ public class Expression }); } - private void SystemFunctions() + private static void SystemFunctions(Expression expression) { - addUnaryFunction("hash_code", v -> new NumericValue(v.hashCode())); + expression.addUnaryFunction("hash_code", v -> new NumericValue(v.hashCode())); - addUnaryFunction("copy", Value::deepcopy); + expression.addUnaryFunction("copy", Value::deepcopy); - addLazyFunction("bool", 1, (c, t, lv) -> { + expression.addLazyFunction("bool", 1, (c, t, lv) -> { Value v = lv.get(0).evalValue(c, Context.BOOLEAN); if (v instanceof StringValue) { @@ -1544,7 +1537,7 @@ public class Expression Value retval = new NumericValue(v.getBoolean()); return (cc, tt) -> retval; }); - addUnaryFunction("number", v -> { + expression.addUnaryFunction("number", v -> { if (v instanceof NumericValue) { return v; @@ -1558,7 +1551,7 @@ public class Expression return Value.NULL; } }); - addFunction("str", lv -> + expression.addFunction("str", lv -> { if (lv.size() == 0) throw new InternalExpressionException("'str' requires at least one argument"); @@ -1636,13 +1629,13 @@ public class Expression } }); - addUnaryFunction("lower", v -> new StringValue(v.getString().toLowerCase(Locale.ROOT))); + expression.addUnaryFunction("lower", v -> new StringValue(v.getString().toLowerCase(Locale.ROOT))); - addUnaryFunction("upper", v -> new StringValue(v.getString().toUpperCase(Locale.ROOT))); + expression.addUnaryFunction("upper", v -> new StringValue(v.getString().toUpperCase(Locale.ROOT))); - addUnaryFunction("title", v -> new StringValue(WordUtils.capitalizeFully(v.getString()))); + expression.addUnaryFunction("title", v -> new StringValue(WordUtils.capitalizeFully(v.getString()))); - addFunction("replace", (lv) -> + expression.addFunction("replace", (lv) -> { if (lv.size() != 3 && lv.size() !=2) throw new InternalExpressionException("'replace' expects string to read, pattern regex, and optional replacement string"); @@ -1654,7 +1647,7 @@ public class Expression return new StringValue(data.replaceAll(regex, replacement)); }); - addFunction("replace_first", (lv) -> + expression.addFunction("replace_first", (lv) -> { if (lv.size() != 3 && lv.size() !=2) throw new InternalExpressionException("'replace_first' expects string to read, pattern regex, and optional replacement string"); @@ -1666,10 +1659,10 @@ public class Expression return new StringValue(data.replaceFirst(regex, replacement)); }); - addUnaryFunction("type", v -> new StringValue(v.getTypeString())); + expression.addUnaryFunction("type", v -> new StringValue(v.getTypeString())); - addUnaryFunction("length", v -> new NumericValue(v.length())); - addLazyFunction("rand", -1, (c, t, lv) -> { + expression.addUnaryFunction("length", v -> new NumericValue(v.length())); + expression.addLazyFunction("rand", -1, (c, t, lv) -> { int argsize = lv.size(); Random randomizer = Expression.randomizer; if (argsize != 1 && argsize != 2) @@ -1692,7 +1685,7 @@ public class Expression return (cc, tt) -> retval; }); - addLazyFunction("perlin", -1, (c, t, lv) -> { + expression.addLazyFunction("perlin", -1, (c, t, lv) -> { PerlinNoiseSampler sampler; Value x, y, z; @@ -1735,7 +1728,7 @@ public class Expression return (cc, tt) -> ret; }); - addLazyFunction("simplex", -1, (c, t, lv) -> { + expression.addLazyFunction("simplex", -1, (c, t, lv) -> { SimplexNoiseSampler sampler; Value x, y, z; @@ -1770,12 +1763,12 @@ public class Expression return (cc, tt) -> ret; }); - addUnaryFunction("print", (v) -> + expression.addUnaryFunction("print", (v) -> { System.out.println(v.getString()); return v; // pass through for variables }); - addUnaryFunction("sleep", (v) -> + expression.addUnaryFunction("sleep", (v) -> { long time = NumericValue.asNumber(v).getLong(); try @@ -1786,19 +1779,19 @@ public class Expression catch (InterruptedException ignored) { } return v; // pass through for variables }); - addLazyFunction("time", 0, (c, t, lv) -> + expression.addLazyFunction("time", 0, (c, t, lv) -> { Value time = new NumericValue((System.nanoTime() / 1000) / 1000.0); return (cc, tt) -> time; }); - addLazyFunction("unix_time", 0, (c, t, lv) -> + expression.addLazyFunction("unix_time", 0, (c, t, lv) -> { Value time = new NumericValue(System.currentTimeMillis()); return (cc, tt) -> time; }); - addFunction("convert_date", lv -> + expression.addFunction("convert_date", lv -> { int argsize = lv.size(); if (lv.size() == 0) throw new InternalExpressionException("'convert_date' requires at least one parameter"); @@ -1852,7 +1845,7 @@ public class Expression return new NumericValue(cal.getTimeInMillis()); }); - addLazyFunction("profile_expr", 1, (c, t, lv) -> + expression.addLazyFunction("profile_expr", 1, (c, t, lv) -> { LazyValue lazy = lv.get(0); long end = System.nanoTime()+50000000L; @@ -1866,17 +1859,17 @@ public class Expression return (cc, tt) -> res; }); - addLazyFunction("var", 1, (c, t, lv) -> { + expression.addLazyFunction("var", 1, (c, t, lv) -> { String varname = lv.get(0).evalValue(c).getString(); - return getOrSetAnyVariable(c, varname); + return expression.getOrSetAnyVariable(c, varname); }); - addLazyFunction("undef", 1, (c, t, lv) -> + expression.addLazyFunction("undef", 1, (c, t, lv) -> { Value remove = lv.get(0).evalValue(c); if (remove instanceof FunctionValue) { - c.host.delFunction(module, remove.getString()); + c.host.delFunction(expression.module, remove.getString()); return (cc, tt) -> Value.NULL; } String varname = remove.getString(); @@ -1885,10 +1878,10 @@ public class Expression varname = varname.replaceAll("\\*+$", ""); if (isPrefix) { - c.host.delFunctionWithPrefix(module, varname); + c.host.delFunctionWithPrefix(expression.module, varname); if (varname.startsWith("global_")) { - c.host.delGlobalVariableWithPrefix(module, varname); + c.host.delGlobalVariableWithPrefix(expression.module, varname); } else if (!varname.startsWith("_")) { @@ -1897,10 +1890,10 @@ public class Expression } else { - c.host.delFunction(module, varname); + c.host.delFunction(expression.module, varname); if (varname.startsWith("global_")) { - c.host.delGlobalVariable(module, varname); + c.host.delGlobalVariable(expression.module, varname); } else if (!varname.startsWith("_")) { @@ -1911,12 +1904,12 @@ public class Expression }); //deprecate - addLazyFunction("vars", 1, (c, t, lv) -> { + expression.addLazyFunction("vars", 1, (c, t, lv) -> { String prefix = lv.get(0).evalValue(c).getString(); List values = new ArrayList<>(); if (prefix.startsWith("global")) { - c.host.globaVariableNames(module, (s) -> s.startsWith(prefix)).forEach(s -> values.add(new StringValue(s))); + c.host.globaVariableNames(expression.module, (s) -> s.startsWith(prefix)).forEach(s -> values.add(new StringValue(s))); } else { @@ -1926,10 +1919,10 @@ public class Expression return (cc, tt) -> retval; }); - addLazyFunctionWithDelegation("task", -1, (c, t, expr, tok, lv) -> { + expression.addLazyFunctionWithDelegation("task", -1, (c, t, expr, tok, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'task' requires at least function to call as a parameter"); - FunctionArgument functionArgument = FunctionArgument.findIn(c, module, lv, 0, true, false); + FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, true, false); Value queue = Value.NULL; if (lv.size() > functionArgument.offset) queue = lv.get(functionArgument.offset).evalValue(c); ThreadValue thread = new ThreadValue(queue, functionArgument.function, expr, tok, c, functionArgument.args); @@ -1937,7 +1930,7 @@ public class Expression return (cc, tt) -> thread; }); - addFunction("task_count", (lv) -> + expression.addFunction("task_count", (lv) -> { if (lv.size() > 0) { @@ -1946,31 +1939,31 @@ public class Expression return new NumericValue(ThreadValue.taskCount()); }); - addUnaryFunction("task_value", (v) -> { + expression.addUnaryFunction("task_value", (v) -> { if (!(v instanceof ThreadValue)) throw new InternalExpressionException("'task_value' could only be used with a task value"); return ((ThreadValue) v).getValue(); }); - addUnaryFunction("task_join", (v) -> { + expression.addUnaryFunction("task_join", (v) -> { if (!(v instanceof ThreadValue)) throw new InternalExpressionException("'task_join' could only be used with a task value"); return ((ThreadValue) v).join(); }); - addLazyFunction("task_dock", 1, (c, t, lv) -> { + expression.addLazyFunction("task_dock", 1, (c, t, lv) -> { // pass through placeholder // implmenetation should dock the task on the main thread. return lv.get(0); }); - addUnaryFunction("task_completed", (v) -> { + expression.addUnaryFunction("task_completed", (v) -> { if (!(v instanceof ThreadValue)) throw new InternalExpressionException("'task_completed' could only be used with a task value"); return new NumericValue(((ThreadValue) v).isFinished()); }); - addLazyFunction("synchronize", -1, (c, t, lv) -> + expression.addLazyFunction("synchronize", -1, (c, t, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'synchronize' require at least an expression to synchronize"); Value lockValue = Value.NULL; @@ -1987,7 +1980,7 @@ public class Expression } }); - addLazyFunction("system_variable_get", -1, (c, t, lv) -> + expression.addLazyFunction("system_variable_get", -1, (c, t, lv) -> { if (lv.size() == 0) throw new InternalExpressionException("'system_variable_get' expects at least a key to be fetched"); Value key = lv.get(0).evalValue(c); @@ -2000,7 +1993,7 @@ public class Expression return (cc, tt) -> Value.NULL; }); - addLazyFunction("system_variable_set", 2, (c, t, lv) -> + expression.addLazyFunction("system_variable_set", 2, (c, t, lv) -> { Value key = lv.get(0).evalValue(c); Value value = lv.get(1).evalValue(c); @@ -2012,7 +2005,7 @@ public class Expression } - private void setAnyVariable(Context c, String name, LazyValue lv) + public void setAnyVariable(Context c, String name, LazyValue lv) { if (name.startsWith("global_")) { @@ -2046,12 +2039,12 @@ public class Expression public Expression(String expression) { this.expression = expression.trim().replaceAll("\\r\\n?", "\n").replaceAll("\\t"," "); - UserDefinedFunctionsAndControlFlow(); - Operators(); - ArithmeticOperations(); - SystemFunctions(); - LoopsAndHigherOrderFunctions(); - BasicDataStructures(); + UserDefinedFunctionsAndControlFlow(this); + Operators(this); + ArithmeticOperations(this); + SystemFunctions(this); + LoopsAndHigherOrderFunctions(this); + BasicDataStructures(this); }