refactoring program

This commit is contained in:
2018-05-19 21:27:29 +02:00
parent fa65975994
commit 4a1a48e1fe
6 changed files with 18 additions and 75 deletions

View File

@@ -21,28 +21,5 @@ public class Main {
{ false, false, false, false } }; { false, false, false, false } };
public static void main(String[] args) { 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));
} }
} }

View File

@@ -196,7 +196,7 @@ public class Program {
return ((ArgumentExpression) e).execute(worm.getWorld().getGameObjects()); return ((ArgumentExpression) e).execute(worm.getWorld().getGameObjects());
} }
} else { } else {
return e.execute(); return e.execute(this);
} }
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@@ -212,4 +212,8 @@ public class Program {
public void setWorm(Worm worm) { public void setWorm(Worm worm) {
this.worm = worm; this.worm = worm;
} }
public Map<String, Expression> getVariables() {
return this.varMap;
}
} }

View File

@@ -4,22 +4,18 @@ import worms.model.Program;
import java.util.function.BiFunction; 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 Expression<T> expression;
private final BiFunction<T, Program, R> function; 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.expression = expression;
this.function = function; this.function = function;
} }
@Override @Override
public R execute(Program program) { public R execute(Program program) {
try { return function.apply(expression.execute(program), program);
return function.apply(expression.execute(program), program);
} catch (ClassCastException e) {
throw new IllegalArgumentException();
}
} }
} }

View File

@@ -7,9 +7,7 @@ import worms.util.ModelException;
import worms.util.MustNotImplementException; import worms.util.MustNotImplementException;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Set;
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> { public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
/** /**
@@ -197,12 +195,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException { public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
return new ArgumentExpression<Object, HashMap<String, Expression>>(ArgumentExpression.Type.VAR) { return (Expression<Object>) program -> program.getVariables().get(variableName).execute(program);
@Override
public Object execute(HashMap<String, Expression> map) {
return map.get(variableName).execute();
}
};
} }
/** /**
@@ -213,12 +206,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException { public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
return new Expression() { return program -> value;
@Override
public Object execute(Program program) {
return value;
}
};
} }
/** /**
@@ -229,12 +217,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException { public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
return new Expression() { return program -> value;
@Override
public Object execute(Program program) {
return value;
}
};
} }
/** /**
@@ -244,12 +227,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Expression createNullExpression(SourceLocation location) throws ModelException { public Expression createNullExpression(SourceLocation location) throws ModelException {
return new Expression() { return program -> null;
@Override
public Object execute(Program program) {
return null;
}
};
} }
/** /**
@@ -338,7 +316,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
@Override @Override
public Expression createSearchObjectExpression(Expression angleDelta, SourceLocation sourceLocation) throws ModelException { public Expression createSearchObjectExpression(Expression angleDelta, SourceLocation sourceLocation) throws ModelException {
return new ArgumentBinaryExpression<Double, GameObject>( return new BinaryArgumentExpression<Double, GameObject>(
angleDelta, (a, p) -> p.getWorm() angleDelta, (a, p) -> p.getWorm()
.getWorld() .getWorld()
.getGameObjects() .getGameObjects()
@@ -359,7 +337,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
@Override @Override
public Expression createSameTeamExpression(Expression entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException { 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 @Override
public Expression createDistanceExpression(Expression entity, SourceLocation sourceLocation) throws ModelException { 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 @Override
public Expression createIsWormExpression(Expression entity, SourceLocation sourceLocation) throws ModelException { 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);
} }
} }

View File

@@ -13,13 +13,7 @@ public class UnaryArgumentExpression<R> implements Expression<R> {
} }
@Override @Override
@SuppressWarnings("unchecked")
public R execute(Program program) { public R execute(Program program) {
return function.apply(program);
try {
return function.apply(program);
} catch (ClassCastException e) {
throw new IllegalArgumentException();
}
} }
} }

View File

@@ -15,13 +15,7 @@ public class UnaryExpression<T, R> implements Expression<R> {
} }
@Override @Override
@SuppressWarnings("unchecked")
public R execute(Program program) { public R execute(Program program) {
return function.apply(expression.execute(program));
try {
return function.apply( expression.execute(program));
} catch (ClassCastException e) {
throw new IllegalArgumentException();
}
} }
} }