refactoring program
This commit is contained in:
@@ -21,28 +21,5 @@ public class Main {
|
||||
{ false, false, false, false } };
|
||||
|
||||
public static void main(String[] args) {
|
||||
//
|
||||
// IFacade facade = new Facade();
|
||||
// World world = facade.createWorld(4.0, 4.0, passableMap);
|
||||
//
|
||||
// Worm worm = facade.createWorm(world, new double[] { 1, 1.5 }, Math.PI / 2, 0.5, "Test", null);
|
||||
// System.out.println(world.isPassable(worm.getLocationArray(), worm.getRadius()));
|
||||
|
||||
|
||||
Expression test = new Expression() {
|
||||
@Override
|
||||
public Object execute() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static Object l = false;
|
||||
private static Object r = true;
|
||||
|
||||
public static void test(BinaryOperator test) {
|
||||
System.out.println(test.apply(l,r));
|
||||
}
|
||||
}
|
||||
|
@@ -196,7 +196,7 @@ public class Program {
|
||||
return ((ArgumentExpression) e).execute(worm.getWorld().getGameObjects());
|
||||
}
|
||||
} else {
|
||||
return e.execute();
|
||||
return e.execute(this);
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@@ -212,4 +212,8 @@ public class Program {
|
||||
public void setWorm(Worm worm) {
|
||||
this.worm = worm;
|
||||
}
|
||||
|
||||
public Map<String, Expression> getVariables() {
|
||||
return this.varMap;
|
||||
}
|
||||
}
|
||||
|
@@ -4,22 +4,18 @@ import worms.model.Program;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ArgumentBinaryExpression<T, R> implements Expression<R> {
|
||||
public class BinaryArgumentExpression<T, R> implements Expression<R> {
|
||||
|
||||
private final Expression<T> expression;
|
||||
private final BiFunction<T, Program, R> function;
|
||||
|
||||
ArgumentBinaryExpression(Expression<T> expression, BiFunction<T, Program, R> function) {
|
||||
BinaryArgumentExpression(Expression<T> expression, BiFunction<T, Program, R> function) {
|
||||
this.expression = expression;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public R execute(Program program) {
|
||||
try {
|
||||
return function.apply(expression.execute(program), program);
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return function.apply(expression.execute(program), program);
|
||||
}
|
||||
}
|
@@ -7,9 +7,7 @@ import worms.util.ModelException;
|
||||
import worms.util.MustNotImplementException;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
|
||||
/**
|
||||
@@ -197,12 +195,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
||||
return new ArgumentExpression<Object, HashMap<String, Expression>>(ArgumentExpression.Type.VAR) {
|
||||
@Override
|
||||
public Object execute(HashMap<String, Expression> map) {
|
||||
return map.get(variableName).execute();
|
||||
}
|
||||
};
|
||||
return (Expression<Object>) program -> program.getVariables().get(variableName).execute(program);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,12 +206,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
||||
return new Expression() {
|
||||
@Override
|
||||
public Object execute(Program program) {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
return program -> value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,12 +217,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
||||
return new Expression() {
|
||||
@Override
|
||||
public Object execute(Program program) {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
return program -> value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,12 +227,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
*/
|
||||
@Override
|
||||
public Expression createNullExpression(SourceLocation location) throws ModelException {
|
||||
return new Expression() {
|
||||
@Override
|
||||
public Object execute(Program program) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
return program -> null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -338,7 +316,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createSearchObjectExpression(Expression angleDelta, SourceLocation sourceLocation) throws ModelException {
|
||||
|
||||
return new ArgumentBinaryExpression<Double, GameObject>(
|
||||
return new BinaryArgumentExpression<Double, GameObject>(
|
||||
angleDelta, (a, p) -> p.getWorm()
|
||||
.getWorld()
|
||||
.getGameObjects()
|
||||
@@ -359,7 +337,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createSameTeamExpression(Expression entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
||||
|
||||
return new ArgumentBinaryExpression<Worm, Boolean>(entity, (w, p) -> w.getTeam().equals(p.getWorm().getTeam()));
|
||||
return new BinaryArgumentExpression<Worm, Boolean>(entity, (w, p) -> w.getTeam().equals(p.getWorm().getTeam()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -372,7 +350,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createDistanceExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
||||
|
||||
return new ArgumentBinaryExpression<GameObject, Double>(entity, (e, p) -> e.getDistance(p.getWorm());
|
||||
return new BinaryArgumentExpression<GameObject, Double>(entity, (e, p) -> e.getDistance(p.getWorm()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -385,6 +363,6 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
|
||||
@Override
|
||||
public Expression createIsWormExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
||||
|
||||
return new UnaryExpression<>(entity, w -> w instanceof Worm);
|
||||
return new UnaryExpression<GameObject, Boolean>(entity, w -> w instanceof Worm);
|
||||
}
|
||||
}
|
||||
|
@@ -13,13 +13,7 @@ public class UnaryArgumentExpression<R> implements Expression<R> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public R execute(Program program) {
|
||||
|
||||
try {
|
||||
return function.apply(program);
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return function.apply(program);
|
||||
}
|
||||
}
|
||||
|
@@ -15,13 +15,7 @@ public class UnaryExpression<T, R> implements Expression<R> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public R execute(Program program) {
|
||||
|
||||
try {
|
||||
return function.apply( expression.execute(program));
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return function.apply(expression.execute(program));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user