added everything to program factory

This commit is contained in:
2018-05-07 17:18:13 +02:00
parent 518dcaf3fc
commit 903ad34c64
5 changed files with 137 additions and 26 deletions

View File

@@ -6,6 +6,7 @@ import worms.model.Food;
import worms.model.Projectile; import worms.model.Projectile;
import worms.model.Worm; import worms.model.Worm;
import worms.programs.IProgramFactory; import worms.programs.IProgramFactory;
import worms.programs.ProgramFactory;
import worms.util.IllegalNameException; import worms.util.IllegalNameException;
import worms.util.ModelException; import worms.util.ModelException;
import worms.util.Coordinate; import worms.util.Coordinate;
@@ -1025,7 +1026,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public IProgramFactory<?, ?, ?, ? extends Program> createProgramFactory() throws ModelException { public IProgramFactory<?, ?, ?, ? extends Program> createProgramFactory() throws ModelException {
return null; return new ProgramFactory();
} }
/** /**

View File

@@ -1,8 +1,23 @@
package worms.model; package worms.model;
import worms.programs.Procedure;
import worms.programs.Statement;
import java.util.List;
public class Program { public class Program {
private final List<Procedure> procedureList;
private final Statement main;
public Program(List<Procedure> proc, Statement main) {
this.procedureList = proc;
this.main = main;
}
public Worm getWorm() { public Worm getWorm() {
return null; return null;

View File

@@ -2,5 +2,22 @@ package worms.programs;
import worms.model.Program; import worms.model.Program;
public abstract class Procedure extends Program { public class Procedure extends Program {
private final String name;
private final Statement body;
public Procedure(String name, Statement body) {
this.name = name;
this.body = body;
}
public String getName() {
return name;
}
public Statement getBody() {
return body;
}
} }

View File

@@ -1,6 +1,5 @@
package worms.programs; package worms.programs;
import com.sun.org.apache.xpath.internal.operations.Bool;
import worms.model.GameObject; import worms.model.GameObject;
import worms.model.Program; import worms.model.Program;
import worms.model.Worm; import worms.model.Worm;
@@ -8,7 +7,6 @@ import worms.util.ModelException;
import worms.util.MustNotImplementException; import worms.util.MustNotImplementException;
import worms.programs.Expression; import worms.programs.Expression;
import javax.swing.plaf.nimbus.State;
import java.util.List; import java.util.List;
public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> { public class ProgramFactory implements IProgramFactory<Expression, Statement, Procedure, Program> {
@@ -22,8 +20,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Program createProgram(List<Procedure> procs, Statement main) throws ModelException { public Program createProgram(List<Procedure> procs, Statement main) throws ModelException {
System.out.println("create program"); return new Program(procs, main);
return new Program();
} }
// TODO checken of dit moet // TODO checken of dit moet
@@ -48,7 +45,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Procedure createProcedureDefinition(String procedureName, Statement body, SourceLocation sourceLocation) { public Procedure createProcedureDefinition(String procedureName, Statement body, SourceLocation sourceLocation) {
return null; return new Procedure(procedureName, body);
} }
/** /**
@@ -61,7 +58,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createAssignmentStatement(String variableName, Expression value, SourceLocation sourceLocation) throws ModelException { public Statement createAssignmentStatement(String variableName, Expression value, SourceLocation sourceLocation) throws ModelException {
return null; return new Statement(Statement.Type.ASSIGN, new Statement.Assign(variableName, value));
} }
/** /**
@@ -73,7 +70,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createPrintStatement(Expression value, SourceLocation sourceLocation) throws ModelException { public Statement createPrintStatement(Expression value, SourceLocation sourceLocation) throws ModelException {
return null; return new Statement(Statement.Type.PRINT, value);
} }
/** /**
@@ -85,7 +82,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createTurnStatement(Expression angle, SourceLocation location) throws ModelException { public Statement createTurnStatement(Expression angle, SourceLocation location) throws ModelException {
return null; return new Statement(Statement.Type.ACTION, new Statement.Action(Statement.Action.Type.TURN, angle));
} }
/** /**
@@ -96,7 +93,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createMoveStatement(SourceLocation location) throws ModelException { public Statement createMoveStatement(SourceLocation location) throws ModelException {
return null; return new Statement(Statement.Type.ACTION, new Statement.Action(Statement.Action.Type.MOVE, null));
} }
/** /**
@@ -106,7 +103,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createJumpStatement(SourceLocation location) throws ModelException { public Statement createJumpStatement(SourceLocation location) throws ModelException {
return null; return new Statement(Statement.Type.ACTION, new Statement.Action(Statement.Action.Type.JUMP, null));
} }
/** /**
@@ -116,7 +113,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createEatStatement(SourceLocation location) { public Statement createEatStatement(SourceLocation location) {
return null; return new Statement(Statement.Type.ACTION, new Statement.Action(Statement.Action.Type.EAT, null));
} }
/** /**
@@ -127,7 +124,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createFireStatement(SourceLocation location) throws ModelException { public Statement createFireStatement(SourceLocation location) throws ModelException {
return null; return new Statement(Statement.Type.ACTION, new Statement.Action(Statement.Action.Type.FIRE, null));
} }
/** /**
@@ -138,7 +135,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createSequenceStatement(List<Statement> statements, SourceLocation sourceLocation) throws ModelException { public Statement createSequenceStatement(List<Statement> statements, SourceLocation sourceLocation) throws ModelException {
return null; return new Statement(Statement.Type.BLOCK, statements);
} }
/** /**
@@ -153,7 +150,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createIfStatement(Expression condition, Statement ifBody, Statement elseBody, SourceLocation sourceLocation) throws ModelException { public Statement createIfStatement(Expression condition, Statement ifBody, Statement elseBody, SourceLocation sourceLocation) throws ModelException {
return null; return new Statement(Statement.Type.IF, new Statement.If(condition, ifBody, elseBody));
} }
/** /**
@@ -165,7 +162,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createWhileStatement(Expression condition, Statement body, SourceLocation sourceLocation) throws ModelException { public Statement createWhileStatement(Expression condition, Statement body, SourceLocation sourceLocation) throws ModelException {
return null; return new Statement(Statement.Type.WHILE, new Statement.While(condition, body));
} }
/** /**
@@ -175,7 +172,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createBreakStatement(SourceLocation sourceLocation) throws ModelException, MustNotImplementException { public Statement createBreakStatement(SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
return null; return new Statement(Statement.Type.BREAK, null);
} }
/** /**
@@ -186,7 +183,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
*/ */
@Override @Override
public Statement createInvokeStatement(String procedureName, SourceLocation sourceLocation) throws ModelException, MustNotImplementException { public Statement createInvokeStatement(String procedureName, SourceLocation sourceLocation) throws ModelException, MustNotImplementException {
return null; return new Statement(Statement.Type.INVOKE, procedureName);
} }
/** /**

View File

@@ -5,15 +5,21 @@ import worms.model.Program;
public class Statement extends Program { public class Statement extends Program {
private final Type type; private final Type type;
private final Object data;
public Statement(Type type) { public Statement(Type type, Object data) {
this.type = type; this.type = type;
this.data = data;
} }
public Type getType() { public Type getType() {
return this.type; return this.type;
} }
public Object getData() {
return data;
}
public enum Type { public enum Type {
ASSIGN, ASSIGN,
PRINT, PRINT,
@@ -24,11 +30,86 @@ public class Statement extends Program {
INVOKE, INVOKE,
BREAK BREAK
} }
public enum Action {
public static class If {
private final Statement ifBody;
private final Statement elseBody;
private final Expression condition;
public If(Expression condition, Statement ifBody, Statement elseBody) {
this.ifBody = ifBody;
this.elseBody = elseBody;
this.condition = condition;
}
public Statement getIfBody() {
return ifBody;
}
public Statement getElseBody() {
return elseBody;
}
public Expression getCondition() {
return condition;
}
}
public static class While {
private final Expression condition;
private final Statement body;
public While(Expression condition, Statement body) {
this.condition = condition;
this.body = body;
}
public Statement getBody() {
return body;
}
public Expression getCondition() {
return condition;
}
}
public static class Action {
private final Type type;
private final Expression value;
public Action(Type type, Expression value) {
this.type = type;
this.value = value;
}
public Type getType() {
return type;
}
public Expression getValue() {
return value;
}
public enum Type {
TURN, TURN,
MOVE, MOVE,
JUMP, JUMP,
EAT, EAT,
FIRE FIRE
} }
}
public static class Assign {
private final String variableName;
private final Expression value;
public Assign(String variableName, Expression value) {
this.variableName = variableName;
this.value = value;
}
}
} }