This commit is contained in:
TomyLobo
2011-11-23 02:29:48 +01:00
parent 1a57f6e95d
commit 7e13b60a51
161 changed files with 1433 additions and 1412 deletions

View File

@ -100,8 +100,7 @@ public class Expression {
try {
return root.getValue();
}
catch (ReturnException e) {
} catch (ReturnException e) {
return e.getValue();
}
}

View File

@ -51,50 +51,50 @@ public class Lexer {
}
private final DecisionTree operatorTree = new DecisionTree(null,
'+', new DecisionTree("+",
'=', new DecisionTree("+="),
'+', new DecisionTree("++")
),
'-', new DecisionTree("-",
'=', new DecisionTree("-="),
'-', new DecisionTree("--")
),
'*', new DecisionTree("*",
'=', new DecisionTree("*="),
'*', new DecisionTree("**")
),
'/', new DecisionTree("/",
'=', new DecisionTree("/=")
),
'%', new DecisionTree("%",
'=', new DecisionTree("%=")
),
'^', new DecisionTree("^",
'=', new DecisionTree("^=")
),
'=', new DecisionTree("=",
'=', new DecisionTree("==")
),
'!', new DecisionTree("!",
'=', new DecisionTree("!=")
),
'<', new DecisionTree("<",
'<', new DecisionTree("<<"),
'=', new DecisionTree("<=")
),
'>', new DecisionTree(">",
'>', new DecisionTree(">>"),
'=', new DecisionTree(">=")
),
'&', new DecisionTree(null, // not implemented
'&', new DecisionTree("&&")
),
'|', new DecisionTree(null, // not implemented
'|', new DecisionTree("||")
),
'~', new DecisionTree("~",
'=', new DecisionTree("~=")
)
'+', new DecisionTree("+",
'=', new DecisionTree("+="),
'+', new DecisionTree("++")
),
'-', new DecisionTree("-",
'=', new DecisionTree("-="),
'-', new DecisionTree("--")
),
'*', new DecisionTree("*",
'=', new DecisionTree("*="),
'*', new DecisionTree("**")
),
'/', new DecisionTree("/",
'=', new DecisionTree("/=")
),
'%', new DecisionTree("%",
'=', new DecisionTree("%=")
),
'^', new DecisionTree("^",
'=', new DecisionTree("^=")
),
'=', new DecisionTree("=",
'=', new DecisionTree("==")
),
'!', new DecisionTree("!",
'=', new DecisionTree("!=")
),
'<', new DecisionTree("<",
'<', new DecisionTree("<<"),
'=', new DecisionTree("<=")
),
'>', new DecisionTree(">",
'>', new DecisionTree(">>"),
'=', new DecisionTree(">=")
),
'&', new DecisionTree(null, // not implemented
'&', new DecisionTree("&&")
),
'|', new DecisionTree(null, // not implemented
'|', new DecisionTree("||")
),
'~', new DecisionTree("~",
'=', new DecisionTree("~=")
)
);
private static final Set<Character> characterTokens = new HashSet<Character>();

View File

@ -182,8 +182,7 @@ public class Parser {
if (peek().id() == ';') {
++position;
break;
}
else {
} else {
break loop;
}
@ -205,8 +204,7 @@ public class Parser {
break loop;
}
break;
}
else {
} else {
break loop;
}
}
@ -253,7 +251,7 @@ public class Parser {
} else {
RValue variable = variables.get(identifierToken.value);
if (variable == null) {
if (next instanceof OperatorToken && ((OperatorToken)next).operator.equals("=")) {
if (next instanceof OperatorToken && ((OperatorToken) next).operator.equals("=")) {
// Ugly hack to make temporary variables work while not sacrificing error reporting.
variables.put(identifierToken.value, variable = new Variable(0));
} else {

View File

@ -35,32 +35,32 @@ public final class ParserProcessors {
final Object[][][] binaryOpsLA = {
{
{ "^", "pow" },
{ "**", "pow" },
{ "^", "pow" },
{ "**", "pow" },
},
{
{ "*", "mul" },
{ "/", "div" },
{ "%", "mod" },
{ "*", "mul" },
{ "/", "div" },
{ "%", "mod" },
},
{
{ "+", "add" },
{ "-", "sub" },
{ "+", "add" },
{ "-", "sub" },
},
{
{ "<<", "shl" },
{ ">>", "shr" },
{ "<<", "shl" },
{ ">>", "shr" },
},
{
{ "<", "lth" },
{ ">", "gth" },
{ "<=", "leq" },
{ ">=", "geq" },
{ "<", "lth" },
{ ">", "gth" },
{ "<=", "leq" },
{ ">=", "geq" },
},
{
{ "==", "equ" },
{ "!=", "neq" },
{ "~=", "near" },
{ "==", "equ" },
{ "!=", "neq" },
{ "~=", "near" },
},
{
{ "&&", "and" },
@ -71,13 +71,13 @@ public final class ParserProcessors {
};
final Object[][][] binaryOpsRA = {
{
{ "=", "ass" },
{ "+=", "aadd" },
{ "-=", "asub" },
{ "*=", "amul" },
{ "/=", "adiv" },
{ "%=", "amod" },
{ "^=", "aexp" },
{ "=", "ass" },
{ "+=", "aadd" },
{ "-=", "asub" },
{ "*=", "amul" },
{ "/=", "adiv" },
{ "%=", "amod" },
{ "^=", "aexp" },
},
};
@ -230,19 +230,17 @@ public final class ParserProcessors {
final Identifiable last = input.removeLast();
if (last instanceof OperatorToken) {
postfixes.addLast(new UnaryOperator(last.getPosition(), "x"+((OperatorToken)last).operator));
}
else if (last instanceof UnaryOperator) {
postfixes.addLast(new UnaryOperator(last.getPosition(), "x"+((UnaryOperator)last).operator));
}
else {
postfixes.addLast(new UnaryOperator(last.getPosition(), "x" + ((OperatorToken) last).operator));
} else if (last instanceof UnaryOperator) {
postfixes.addLast(new UnaryOperator(last.getPosition(), "x" + ((UnaryOperator) last).operator));
} else {
center = last;
break;
}
} while (true);
if (!(center instanceof RValue)) {
throw new ParserException(center.getPosition(), "Expected expression, found "+center);
throw new ParserException(center.getPosition(), "Expected expression, found " + center);
}
input.addAll(postfixes);

View File

@ -13,7 +13,7 @@ public class UnaryOperator extends PseudoToken {
public UnaryOperator(OperatorToken operatorToken) {
this(operatorToken.getPosition(), operatorToken.operator);
}
public UnaryOperator(int position, String operator) {
super(position);
this.operator = operator;

View File

@ -17,8 +17,7 @@ public class Conditional extends Node {
public double getValue() throws EvaluationException {
if (condition.getValue() > 0.0) {
return truePart.getValue();
}
else {
} else {
return falsePart == null ? 0 : falsePart.getValue();
}
}
@ -31,9 +30,9 @@ public class Conditional extends Node {
@Override
public String toString() {
if (falsePart == null) {
return "if ("+condition+") { "+truePart+" }";
return "if (" + condition + ") { " + truePart + " }";
} else {
return "if ("+condition+") { "+truePart+" } else { "+falsePart+" }";
return "if (" + condition + ") { " + truePart + " } else { " + falsePart + " }";
}
}

View File

@ -28,8 +28,7 @@ public class For extends Node {
try {
ret = body.getValue();
}
catch (BreakException e) {
} catch (BreakException e) {
if (e.doContinue) {
continue;
} else {
@ -48,7 +47,7 @@ public class For extends Node {
@Override
public String toString() {
return "for ("+init+"; "+condition+"; "+increment+") { "+body+" }";
return "for (" + init + "; " + condition + "; " + increment + ") { " + body + " }";
}
//TODO: optimizer

View File

@ -34,7 +34,7 @@ import com.sk89q.worldedit.expression.runtime.Function.Dynamic;
*/
public final class Functions {
private static class Overload {
private final Method method;
private final Method method;
private final int mask;
private final boolean isSetter;
@ -90,14 +90,12 @@ public final class Functions {
}
}
public static final Function getFunction(int position, String name, RValue... args) throws NoSuchMethodException {
final Method getter = getMethod(name, false, args);
try {
Method setter = getMethod(name, true, args);
return new LValueFunction(position, getter, setter, args);
}
catch (NoSuchMethodException e) {
} catch (NoSuchMethodException e) {
return new Function(position, getter, args);
}
}

View File

@ -28,5 +28,6 @@ import com.sk89q.worldedit.expression.Identifiable;
*/
public interface RValue extends Identifiable {
public double getValue() throws EvaluationException;
public Node optimize() throws EvaluationException;
}

View File

@ -21,7 +21,7 @@ public class Return extends Node {
@Override
public String toString() {
return "return "+value;
return "return " + value;
}
//TODO: optimizer

View File

@ -27,8 +27,7 @@ public class While extends Node {
try {
ret = body.getValue();
}
catch (BreakException e) {
} catch (BreakException e) {
if (e.doContinue) {
continue;
} else {
@ -45,8 +44,7 @@ public class While extends Node {
try {
ret = body.getValue();
}
catch (BreakException e) {
} catch (BreakException e) {
if (e.doContinue) {
continue;
} else {
@ -67,9 +65,9 @@ public class While extends Node {
@Override
public String toString() {
if (footChecked) {
return "do { "+body+" } while ("+condition+")";
return "do { " + body + " } while (" + condition + ")";
} else {
return "while ("+condition+") { "+body+" }";
return "while (" + condition + ") { " + body + " }";
}
}