|
|
|
@@ -1,14 +1,17 @@
|
|
|
|
|
package worms.programs;
|
|
|
|
|
|
|
|
|
|
import com.sun.org.apache.xpath.internal.operations.Bool;
|
|
|
|
|
import worms.model.GameObject;
|
|
|
|
|
import worms.model.Program;
|
|
|
|
|
import worms.model.Worm;
|
|
|
|
|
import worms.util.ModelException;
|
|
|
|
|
import worms.util.MustNotImplementException;
|
|
|
|
|
import worms.programs.Expression;
|
|
|
|
|
|
|
|
|
|
import javax.swing.plaf.nimbus.State;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
|
|
|
|
|
/**
|
|
|
|
|
* Create a program from the given arguments.
|
|
|
|
|
*
|
|
|
|
@@ -18,7 +21,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @return A new program.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createProgram(List procs, Object main) throws ModelException {
|
|
|
|
|
public Program createProgram(List<Procedure> procs, Statement main) throws ModelException {
|
|
|
|
|
System.out.println("create program");
|
|
|
|
|
return new Program();
|
|
|
|
|
}
|
|
|
|
@@ -44,7 +47,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createProcedureDefinition(String procedureName, Object body, SourceLocation sourceLocation) {
|
|
|
|
|
public Procedure createProcedureDefinition(String procedureName, Statement body, SourceLocation sourceLocation) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -57,7 +60,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createAssignmentStatement(String variableName, Object value, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
public Statement createAssignmentStatement(String variableName, Expression value, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -69,7 +72,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createPrintStatement(Object value, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
public Statement createPrintStatement(Expression value, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -81,7 +84,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createTurnStatement(Object angle, SourceLocation location) throws ModelException {
|
|
|
|
|
public Statement createTurnStatement(Expression angle, SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -92,7 +95,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createMoveStatement(SourceLocation location) throws ModelException {
|
|
|
|
|
public Statement createMoveStatement(SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -102,7 +105,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createJumpStatement(SourceLocation location) throws ModelException {
|
|
|
|
|
public Statement createJumpStatement(SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -112,7 +115,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createEatStatement(SourceLocation location) {
|
|
|
|
|
public Statement createEatStatement(SourceLocation location) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -123,7 +126,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createFireStatement(SourceLocation location) throws ModelException {
|
|
|
|
|
public Statement createFireStatement(SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -134,7 +137,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createSequenceStatement(List statements, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
public Statement createSequenceStatement(List<Statement> statements, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -149,7 +152,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createIfStatement(Object condition, Object ifBody, Object elseBody, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
public Statement createIfStatement(Expression condition, Statement ifBody, Statement elseBody, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -161,7 +164,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createWhileStatement(Object condition, Object body, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
public Statement createWhileStatement(Expression condition, Statement body, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -171,7 +174,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createBreakStatement(SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
|
|
|
|
public Statement createBreakStatement(SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -182,7 +185,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createInvokeStatement(String procedureName, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
|
|
|
|
public Statement createInvokeStatement(String procedureName, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -194,8 +197,13 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createReadVariableExpression(String variableName, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return new Expression() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object execute() {
|
|
|
|
|
return findVar(variableName);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -205,8 +213,13 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createDoubleLiteralExpression(double value, SourceLocation location) throws ModelException {
|
|
|
|
|
return new Expression<Double>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Double execute() {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -216,8 +229,13 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createBooleanLiteralExpression(boolean value, SourceLocation location) throws ModelException {
|
|
|
|
|
return new Expression<Boolean>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean execute() {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -226,8 +244,13 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createNullExpression(SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createNullExpression(SourceLocation location) throws ModelException {
|
|
|
|
|
return new Expression<Object>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object execute() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -237,8 +260,14 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createSelfExpression(SourceLocation location) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createSelfExpression(SourceLocation location) throws ModelException {
|
|
|
|
|
|
|
|
|
|
return new Expression<Worm>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Worm execute() {
|
|
|
|
|
return getWorm();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -250,18 +279,9 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createAdditionExpression(Object left, Object right, SourceLocation location) throws ModelException {
|
|
|
|
|
public Expression createAdditionExpression(Expression left, Expression right, SourceLocation location) throws ModelException {
|
|
|
|
|
|
|
|
|
|
if (!(left instanceof Double && right instanceof Double)) {
|
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new BinaryExpression(left, right) {
|
|
|
|
|
@Override
|
|
|
|
|
public Double execute() {
|
|
|
|
|
return (double) execute((l,r) -> (double) l + (double) r);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return new BinaryExpression<Double, Double>(left, right, (l,r) -> l + r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -273,17 +293,9 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createAndExpression(Object left, Object right, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
if (!(left instanceof Boolean && right instanceof Boolean)) {
|
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
|
}
|
|
|
|
|
public Expression createAndExpression(Expression left, Expression right, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
|
|
|
|
|
return new BinaryExpression(left, right) {
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean execute() {
|
|
|
|
|
return (boolean) execute((l,r) -> (boolean) left && (boolean) right);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return new BinaryExpression<Boolean, Boolean>(left, right, (l, r) -> l && r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -294,8 +306,9 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createNotExpression(Object expression, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createNotExpression(Expression expression, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
|
|
|
|
|
return new UnaryExpression<Boolean, Boolean>(expression, e -> !e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -308,14 +321,9 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createEqualityExpression(Object left, Object right, SourceLocation location) throws ModelException {
|
|
|
|
|
public Expression createEqualityExpression(Expression left, Expression right, SourceLocation location) throws ModelException {
|
|
|
|
|
|
|
|
|
|
return new BinaryExpression(left, right) {
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean execute() {
|
|
|
|
|
return (boolean) execute((l,r) -> left.equals(right));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return new BinaryExpression<Object, Boolean>(left, right, (l,r) -> l.equals(r));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -328,21 +336,13 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param location
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createLessThanExpression(Object left, Object right, SourceLocation location) {
|
|
|
|
|
if (!(left instanceof Double && right instanceof Double)) {
|
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
|
}
|
|
|
|
|
public Expression createLessThanExpression(Expression left, Expression right, SourceLocation location) {
|
|
|
|
|
|
|
|
|
|
return new BinaryExpression(left, right) {
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean execute() {
|
|
|
|
|
return (boolean) execute((l,r) -> (double) left < (double) right);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return new BinaryExpression<Double, Boolean>(left, right, (l,r) -> l < r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object createSearchObjectExpression(Object angleDelta, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
public Expression createSearchObjectExpression(Expression angleDelta, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -355,8 +355,9 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createSameTeamExpression(Object entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createSameTeamExpression(Expression entity, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
|
|
|
|
|
|
|
|
|
|
return new UnaryExpression<Worm, Boolean>(entity, w -> (w).getTeam().equals((w).getWorld().getActiveWorm().getTeam()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -367,8 +368,9 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createDistanceExpression(Object entity, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createDistanceExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
|
|
|
|
|
return new UnaryExpression<GameObject, Double>(entity, (l) -> (l).getWorld().getActiveWorm().getDistance(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -379,7 +381,7 @@ public class ProgramFactory implements IProgramFactory {
|
|
|
|
|
* @param sourceLocation
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object createIsWormExpression(Object entity, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return null;
|
|
|
|
|
public Expression createIsWormExpression(Expression entity, SourceLocation sourceLocation) throws ModelException {
|
|
|
|
|
return new UnaryExpression<>(entity, w -> w instanceof Worm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|