Allowed usage of non-existant variables in all LValue expressions.

This commit is contained in:
TomyLobo 2013-09-21 19:00:20 +02:00
parent 4fa5daf974
commit 57bb5470eb
17 changed files with 238 additions and 18 deletions

View File

@ -38,6 +38,7 @@ public interface Identifiable {
* *
* PseudoTokens: * PseudoTokens:
* p - UnaryOperator * p - UnaryOperator
* V - UnboundVariable
* *
* Nodes: * Nodes:
* c - Constant * c - Constant

View File

@ -33,6 +33,7 @@ import com.sk89q.worldedit.expression.runtime.Break;
import com.sk89q.worldedit.expression.runtime.Conditional; import com.sk89q.worldedit.expression.runtime.Conditional;
import com.sk89q.worldedit.expression.runtime.Constant; import com.sk89q.worldedit.expression.runtime.Constant;
import com.sk89q.worldedit.expression.runtime.For; import com.sk89q.worldedit.expression.runtime.For;
import com.sk89q.worldedit.expression.runtime.Function;
import com.sk89q.worldedit.expression.runtime.Functions; import com.sk89q.worldedit.expression.runtime.Functions;
import com.sk89q.worldedit.expression.runtime.LValue; import com.sk89q.worldedit.expression.runtime.LValue;
import com.sk89q.worldedit.expression.runtime.RValue; import com.sk89q.worldedit.expression.runtime.RValue;
@ -84,6 +85,9 @@ public class Parser {
final Token token = peek(); final Token token = peek();
throw new ParserException(token.getPosition(), "Extra token at the end of the input: " + token); throw new ParserException(token.getPosition(), "Extra token at the end of the input: " + token);
} }
ret.bindVariables(expression, false);
return ret; return ret;
} }
@ -267,9 +271,6 @@ public class Parser {
default: default:
throw new ParserException(current.getPosition(), "Unexpected keyword '" + keyword + "'"); throw new ParserException(current.getPosition(), "Unexpected keyword '" + keyword + "'");
} }
switch (1) {
default:
}
break; break;
@ -331,13 +332,12 @@ public class Parser {
if (next.id() == '(') { if (next.id() == '(') {
halfProcessed.add(parseFunctionCall(identifierToken)); halfProcessed.add(parseFunctionCall(identifierToken));
} else { } else {
// Ugly hack to make temporary variables work while not sacrificing error reporting. final RValue variable = expression.getVariable(identifierToken.value, false);
final boolean isSimpleAssignment = next instanceof OperatorToken && ((OperatorToken) next).operator.equals("=");
RValue variable = expression.getVariable(identifierToken.value, isSimpleAssignment);
if (variable == null) { if (variable == null) {
throw new ParserException(current.getPosition(), "Variable '" + identifierToken.value + "' not found"); halfProcessed.add(new UnboundVariable(identifierToken.getPosition(), identifierToken.value));
} else {
halfProcessed.add(variable);
} }
halfProcessed.add(variable);
} }
expressionStart = false; expressionStart = false;
break; break;
@ -388,7 +388,7 @@ public class Parser {
return tokens.get(position); return tokens.get(position);
} }
private Identifiable parseFunctionCall(IdentifierToken identifierToken) throws ParserException { private Function parseFunctionCall(IdentifierToken identifierToken) throws ParserException {
consumeCharacter('('); consumeCharacter('(');
try { try {

View File

@ -0,0 +1,61 @@
package com.sk89q.worldedit.expression.parser;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.runtime.EvaluationException;
import com.sk89q.worldedit.expression.runtime.LValue;
import com.sk89q.worldedit.expression.runtime.RValue;
public class UnboundVariable extends PseudoToken implements LValue {
public final String name;
public UnboundVariable(int position, String name) {
super(position);
this.name = name;
// TODO Auto-generated constructor stub
}
@Override
public char id() {
// TODO Auto-generated method stub
return 'V';
}
@Override
public String toString() {
return "UnboundVariable(" + name + ")";
}
@Override
public double getValue() throws EvaluationException {
throw new EvaluationException(getPosition(), "Tried to evaluate unbound variable!");
}
@Override
public LValue optimize() throws EvaluationException {
throw new EvaluationException(getPosition(), "Tried to optimize unbound variable!");
}
@Override
public double assign(double value) throws EvaluationException {
throw new EvaluationException(getPosition(), "Tried to assign unbound variable!");
}
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;
}
}

View File

@ -19,15 +19,18 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* An if/else statement or a ternary operator. * An if/else statement or a ternary operator.
* *
* @author TomyLobo * @author TomyLobo
*/ */
public class Conditional extends Node { public class Conditional extends Node {
RValue condition; private RValue condition;
RValue truePart; private RValue truePart;
RValue falsePart; private RValue falsePart;
public Conditional(int position, RValue condition, RValue truePart, RValue falsePart) { public Conditional(int position, RValue condition, RValue truePart, RValue falsePart) {
super(position); super(position);
@ -76,4 +79,15 @@ public class Conditional extends Node {
return new Conditional(getPosition(), newCondition, truePart.optimize(), falsePart == null ? null : falsePart.optimize()); return new Conditional(getPosition(), newCondition, truePart.optimize(), falsePart == null ? null : falsePart.optimize());
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
condition = condition.bindVariables(expression, false);
truePart = truePart.bindVariables(expression, false);
if (falsePart != null) {
falsePart = falsePart.bindVariables(expression, false);
}
return this;
}
} }

View File

@ -19,6 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A Java/C-style for loop. * A Java/C-style for loop.
* *
@ -87,4 +90,14 @@ public class For extends Node {
//return new Sequence(getPosition(), init.optimize(), new While(getPosition(), condition, new Sequence(getPosition(), body, increment), false)).optimize(); //return new Sequence(getPosition(), init.optimize(), new While(getPosition(), condition, new Sequence(getPosition(), body, increment), false)).optimize();
return new For(getPosition(), init.optimize(), newCondition, increment.optimize(), body.optimize()); return new For(getPosition(), init.optimize(), newCondition, increment.optimize(), body.optimize());
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
init = init.bindVariables(expression, false);
condition = condition.bindVariables(expression, false);
increment = increment.bindVariables(expression, false);
body = body.bindVariables(expression, false);
return this;
}
} }

View File

@ -24,6 +24,9 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* Wrapper for a Java method and its arguments (other Nodes) * Wrapper for a Java method and its arguments (other Nodes)
* *
@ -102,10 +105,19 @@ public class Function extends Node {
if (optimizable) { if (optimizable) {
return new Constant(position, invokeMethod(method, optimizedArgs)); return new Constant(position, invokeMethod(method, optimizedArgs));
} else if (this instanceof LValueFunction) {
return new LValueFunction(position, method, ((LValueFunction) this).setter, optimizedArgs);
} else { } else {
return new Function(position, method, optimizedArgs); return new Function(position, method, optimizedArgs);
} }
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
final Class<?>[] parameters = method.getParameterTypes();
for (int i = 0; i < args.length; ++i) {
final boolean argumentPrefersLValue = LValue.class.isAssignableFrom(parameters[i]);
args[i] = args[i].bindVariables(expression, argumentPrefersLValue);
}
return this;
}
} }

View File

@ -117,7 +117,7 @@ public final class Functions {
} }
} }
throw new NoSuchMethodException(); throw new NoSuchMethodException(); // TODO: return null (check for side-effects first)
} }
private static final Map<String, List<Overload>> functions = new HashMap<String, List<Overload>>(); private static final Map<String, List<Overload>> functions = new HashMap<String, List<Overload>>();

View File

@ -19,6 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A value that can be used on the left side of an assignment. * A value that can be used on the left side of an assignment.
* *
@ -26,4 +29,8 @@ package com.sk89q.worldedit.expression.runtime;
*/ */
public interface LValue extends RValue { public interface LValue extends RValue {
public double assign(double value) throws EvaluationException; public double assign(double value) throws EvaluationException;
public LValue optimize() throws EvaluationException;
public LValue bindVariables(Expression expression, boolean preferLValue) throws ParserException;
} }

View File

@ -21,6 +21,9 @@ package com.sk89q.worldedit.expression.runtime;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* Wrapper for a pair of Java methods and their arguments (other Nodes), forming an LValue * Wrapper for a pair of Java methods and their arguments (other Nodes), forming an LValue
* *
@ -28,10 +31,11 @@ import java.lang.reflect.Method;
*/ */
public class LValueFunction extends Function implements LValue { public class LValueFunction extends Function implements LValue {
private final Object[] setterArgs; private final Object[] setterArgs;
final Method setter; private final Method setter;
LValueFunction(int position, Method getter, Method setter, RValue... args) { LValueFunction(int position, Method getter, Method setter, RValue... args) {
super(position, getter, args); super(position, getter, args);
assert (getter.isAnnotationPresent(Dynamic.class));
setterArgs = new Object[args.length + 1]; setterArgs = new Object[args.length + 1];
System.arraycopy(args, 0, setterArgs, 0, args.length); System.arraycopy(args, 0, setterArgs, 0, args.length);
@ -48,4 +52,25 @@ public class LValueFunction extends Function implements LValue {
setterArgs[setterArgs.length - 1] = value; setterArgs[setterArgs.length - 1] = value;
return invokeMethod(setter, setterArgs); return invokeMethod(setter, setterArgs);
} }
@Override
public LValue optimize() throws EvaluationException {
final RValue optimized = super.optimize();
if (optimized == this) {
return this;
}
if (optimized instanceof Function) {
return new LValueFunction(optimized.getPosition(), method, setter, ((Function) optimized).args);
}
return (LValue) optimized;
}
@Override
public LValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
super.bindVariables(expression, preferLValue);
return this;
}
} }

View File

@ -19,6 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A node in the execution tree of an expression. * A node in the execution tree of an expression.
* *
@ -42,4 +45,9 @@ public abstract class Node implements RValue {
public final int getPosition() { public final int getPosition() {
return position; return position;
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
return this;
}
} }

View File

@ -19,7 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.Identifiable; import com.sk89q.worldedit.expression.Identifiable;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A value that can be used on the right side of an assignment. * A value that can be used on the right side of an assignment.
@ -30,4 +32,6 @@ public interface RValue extends Identifiable {
public double getValue() throws EvaluationException; public double getValue() throws EvaluationException;
public RValue optimize() throws EvaluationException; public RValue optimize() throws EvaluationException;
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException;
} }

View File

@ -19,6 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A return statement. * A return statement.
* *
@ -47,4 +50,11 @@ public class Return extends Node {
public String toString() { public String toString() {
return "return " + value; return "return " + value;
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
value = value.bindVariables(expression, false);
return this;
}
} }

View File

@ -22,6 +22,9 @@ package com.sk89q.worldedit.expression.runtime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A sequence of operations, usually separated by semicolons in the input stream. * A sequence of operations, usually separated by semicolons in the input stream.
* *
@ -94,4 +97,13 @@ public class Sequence extends Node {
return new Sequence(getPosition(), newSequence.toArray(new RValue[newSequence.size()])); return new Sequence(getPosition(), newSequence.toArray(new RValue[newSequence.size()]));
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
for (int i = 0; i < sequence.length; ++i) {
sequence[i] = sequence[i].bindVariables(expression, false);
}
return this;
}
} }

View File

@ -19,6 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A simple-style for loop. * A simple-style for loop.
* *
@ -84,4 +87,14 @@ public class SimpleFor extends Node {
return new SimpleFor(getPosition(), (LValue) counter.optimize(), first.optimize(), last.optimize(), body.optimize()); return new SimpleFor(getPosition(), (LValue) counter.optimize(), first.optimize(), last.optimize(), body.optimize());
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
counter = counter.bindVariables(expression, true);
first = first.bindVariables(expression, false);
last = last.bindVariables(expression, false);
body = body.bindVariables(expression, false);
return this;
}
} }

View File

@ -26,16 +26,19 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A switch/case construct. * A switch/case construct.
* *
* @author TomyLobo * @author TomyLobo
*/ */
public class Switch extends Node implements RValue { public class Switch extends Node implements RValue {
private final RValue parameter; private RValue parameter;
private final Map<Double, Integer> valueMap; private final Map<Double, Integer> valueMap;
private final RValue[] caseStatements; private final RValue[] caseStatements;
private final RValue defaultCase; private RValue defaultCase;
public Switch(int position, RValue parameter, List<Double> values, List<RValue> caseStatements, RValue defaultCase) { public Switch(int position, RValue parameter, List<Double> values, List<RValue> caseStatements, RValue defaultCase) {
this(position, parameter, invertList(values), caseStatements, defaultCase); this(position, parameter, invertList(values), caseStatements, defaultCase);
@ -191,4 +194,17 @@ public class Switch extends Node implements RValue {
return new Switch(getPosition(), optimizedParameter, newValueMap, newSequence, defaultCase.optimize()); return new Switch(getPosition(), optimizedParameter, newValueMap, newSequence, defaultCase.optimize());
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
parameter = parameter.bindVariables(expression, false);
for (int i = 0; i < caseStatements.length; ++i) {
caseStatements[i] = caseStatements[i].bindVariables(expression, false);
}
defaultCase = defaultCase.bindVariables(expression, false);
return this;
}
} }

View File

@ -19,6 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A variable. * A variable.
* *
@ -51,4 +54,14 @@ public final class Variable extends Node implements LValue {
public double assign(double value) { public double assign(double value) {
return this.value = value; return this.value = value;
} }
@Override
public LValue optimize() throws EvaluationException {
return this;
}
@Override
public LValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
return this;
}
} }

View File

@ -19,6 +19,9 @@
package com.sk89q.worldedit.expression.runtime; package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.parser.ParserException;
/** /**
* A while loop. * A while loop.
* *
@ -112,4 +115,12 @@ public class While extends Node {
return new While(getPosition(), newCondition, body.optimize(), footChecked); return new While(getPosition(), newCondition, body.optimize(), footChecked);
} }
@Override
public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException {
condition = condition.bindVariables(expression, false);
body = body.bindVariables(expression, false);
return this;
}
} }