public RValue bind(Expression expression, boolean isLValue) throws ParserException { final RValue variable = expression.getVariable(name, isLValue); if (variable == null) { throw new ParserException(getPosition(), "Variable '" + name + "' not found"); } return variable; }
@Override public LValue bindVariables(Expression expression, boolean preferLValue) throws ParserException { final RValue variable = expression.getVariable(name, preferLValue); if (variable == null) { throw new ParserException(getPosition(), "Variable '" + name + "' not found"); } return (LValue) variable; }
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit, final Pattern pattern, final String expressionString, final boolean hollow) throws ExpressionException, MaxChangedBlocksException { final Expression expression = Expression.compile(expressionString, "x", "y", "z", "type", "data"); expression.optimize(); final RValue typeVariable = expression.getVariable("type", false); final RValue dataVariable = expression.getVariable("data", false); final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero); expression.setEnvironment(environment); final ArbitraryShape shape = new ArbitraryShape(region) { @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; } } }; return shape.generate(this, pattern, hollow); }
halfProcessed.add(parseFunctionCall(identifierToken)); } else { final RValue variable = expression.getVariable(identifierToken.value, false); if (variable == null) { halfProcessed.add(new UnboundVariable(identifierToken.getPosition(), identifierToken.value));
@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); }
expression.optimize(); final RValue x = expression.getVariable("x", false); final RValue y = expression.getVariable("y", false); final RValue z = expression.getVariable("z", false);
@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); }
RValue variable = expression.getVariable(((IdentifierToken) variableToken).value, true); if (!(variable instanceof LValue)) { throw new ParserException(variableToken.getPosition(), "Expected variable");