@Override public boolean test(BlockVector2 vector) { try { return expression.evaluate(vector.getX(), 0, vector.getZ()) > 0; } catch (EvaluationException e) { return false; } }
/** * Try to parse numeric input as either a number or a mathematical expression. * * @param input input * @return a number * @throws ParameterException thrown on parse error */ private @Nullable Double parseNumericInput(@Nullable String input) throws ParameterException { if (input == null) { return null; } try { return Double.parseDouble(input); } catch (NumberFormatException e1) { try { Expression expression = Expression.compile(input); return expression.evaluate(); } catch (EvaluationException e) { throw new ParameterException(String.format( "Expected '%s' to be a valid number (or a valid mathematical expression)", input)); } catch (ExpressionException e) { throw new ParameterException(String.format( "Expected '%s' to be a number or valid math expression (error: %s)", input, e.getMessage())); } } }
@Command( aliases = { "/calc", "/calculate", "/eval", "/evaluate", "/solve" }, usage = "<expression>", desc = "Evaluate a mathematical expression" ) @CommandPermissions("worldedit.calc") public void calc(Actor actor, @Text String input) throws CommandException { try { Expression expression = Expression.compile(input); actor.print("= " + expression.evaluate()); } catch (EvaluationException e) { actor.printError(String.format( "'%s' could not be parsed as a valid expression", input)); } catch (ExpressionException e) { actor.printError(String.format( "'%s' could not be evaluated (error: %s)", input, e.getMessage())); } }
@Override protected BaseBiome getBiome(int x, int z, BaseBiome defaultBiomeType) { final Vector2 current = Vector2.at(x, z); environment.setCurrentBlock(current.toVector3(0)); final Vector2 scaled = current.subtract(zero2D).divide(unit2D); try { if (expression.evaluate(scaled.getX(), scaled.getZ()) <= 0) { return null; // TODO should return OUTSIDE? seems to cause issues otherwise, workedaround for now } // TODO: Allow biome setting via a script variable (needs BiomeType<->int mapping) return defaultBiomeType; } catch (Exception e) { log.log(Level.WARNING, "Failed to create shape", e); return null; } } };
@Override public boolean test(BlockVector3 vector) { try { if (expression.getEnvironment() instanceof WorldEditExpressionEnvironment) { ((WorldEditExpressionEnvironment) expression.getEnvironment()).setCurrentBlock(vector.toVector3()); } return expression.evaluate(vector.getX(), vector.getY(), vector.getZ()) > 0; } catch (EvaluationException e) { return false; } }
private double simpleEval(String expressionString) throws ExpressionException { final Expression expression = compile(expressionString); expression.setEnvironment(new ExpressionEnvironment() { @Override public int getBlockType(double x, double y, double z) { return (int) x; } @Override public int getBlockData(double x, double y, double z) { return (int) y; } @Override public int getBlockTypeAbs(double x, double y, double z) { return (int) x*10; } @Override public int getBlockDataAbs(double x, double y, double z) { return (int) y*10; } @Override public int getBlockTypeRel(double x, double y, double z) { return (int) x*100; } @Override public int getBlockDataRel(double x, double y, double z) { return (int) y*100; } }); return expression.evaluate(); }
@Override protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial) { final Vector3 current = Vector3.at(x, y, z); environment.setCurrentBlock(current); final Vector3 scaled = current.subtract(zero).divide(unit); try { if (expression.evaluate(scaled.getX(), scaled.getY(), scaled.getZ(), defaultMaterial.getBlockType().getLegacyId(), 0) <= 0) { // TODO data return null; } return LegacyMapper.getInstance().getBlockFromLegacy((int) typeVariable.getValue(), (int) dataVariable.getValue()).toBaseBlock(); } catch (Exception e) { log.log(Level.WARNING, "Failed to create shape", e); return null; } } };
expression.evaluate(scaled.getX(), scaled.getY(), scaled.getZ());
@Test public void testIf() throws ExpressionException { assertEquals(40, simpleEval("if (1) x=4; else y=5; x*10+y;"), 0); assertEquals(5, simpleEval("if (0) x=4; else y=5; x*10+y;"), 0); // test 'dangling else' final Expression expression1 = compile("if (1) if (0) x=4; else y=5;", "x", "y"); expression1.evaluate(1, 2); assertEquals(1, expression1.getVariable("x", false).getValue(), 0); assertEquals(5, expression1.getVariable("y", false).getValue(), 0); // test if the if construct is correctly recognized as a statement final Expression expression2 = compile("if (0) if (1) x=5; y=4;", "x", "y"); expression2.evaluate(1, 2); assertEquals(4, expression2.getVariable("y", false).getValue(), 0); }
@Test public void testEvaluate() throws ExpressionException { // check assertEquals(1 - 2 + 3, simpleEval("1 - 2 + 3"), 0); // check unary ops assertEquals(2 + +4, simpleEval("2 + +4"), 0); assertEquals(2 - -4, simpleEval("2 - -4"), 0); assertEquals(2 * -4, simpleEval("2 * -4"), 0); // check functions assertEquals(sin(5), simpleEval("sin(5)"), 0); assertEquals(atan2(3, 4), simpleEval("atan2(3, 4)"), 0); // check variables assertEquals(8, compile("foo+bar", "foo", "bar").evaluate(5, 3), 0); }
@Test public void testAssign() throws ExpressionException { Expression foo = compile("{a=x} b=y; c=z", "x", "y", "z", "a", "b", "c"); foo.evaluate(2, 3, 5); assertEquals(2, foo.getVariable("a", false).getValue(), 0); assertEquals(3, foo.getVariable("b", false).getValue(), 0); assertEquals(5, foo.getVariable("c", false).getValue(), 0); }