diff --git a/src/pld/Main.java b/src/pld/Main.java index c00d9d9..2c69bfd 100644 --- a/src/pld/Main.java +++ b/src/pld/Main.java @@ -1,8 +1,13 @@ package pld; -import pld.bool.*; +import pld.bool.BooleanExpression; +import pld.bool.ComparisonOperator; +import pld.bool.IntegerComparison; +import pld.bool.LogicalNegation; import pld.command.*; -import pld.common.*; +import pld.common.Evaluator; +import pld.common.Program; +import pld.common.VariableName; import pld.integer.*; public class Main { @@ -30,14 +35,14 @@ public static Program pow(int x, int n) { return new Skip(); } - Command initializeResult = new VariableAssignment(new VariableName("result"), new IntegerValue(1)); - Command initializeX = new VariableAssignment(new VariableName("x"), new IntegerValue(x)); - Command initializeN = new VariableAssignment(new VariableName("n"), new IntegerValue(n)); + Command initializeResult = new VariableAssignment(new VariableName("result"), new IntegerValue(1)); // result := 1 + Command initializeX = new VariableAssignment(new VariableName("x"), new IntegerValue(x)); // x := (parameter x) + Command initializeN = new VariableAssignment(new VariableName("n"), new IntegerValue(n)); // n := (parameter n) Command initialization = new Chain(new Chain(initializeResult, initializeX), initializeN); - BooleanExpression condition = new IntegerComparison(new VariableRead(new VariableName("n")), new IntegerValue(0), ComparisonOperator.GREATER_THAN); - Command multiplyResult = new VariableAssignment(new VariableName("result"), new IntegerOperation(new VariableRead(new VariableName("result")), new VariableRead(new VariableName("x")), IntegerOperator.MULT)); - Command decrementN = new VariableAssignment(new VariableName("n"), new IntegerOperation(new VariableRead(new VariableName("n")), new IntegerValue(1), IntegerOperator.MINUS)); + BooleanExpression condition = new IntegerComparison(new VariableRead(new VariableName("n")), new IntegerValue(0), ComparisonOperator.GREATER_THAN); // !n > 0 + Command multiplyResult = new VariableAssignment(new VariableName("result"), new IntegerOperation(new VariableRead(new VariableName("result")), new VariableRead(new VariableName("x")), IntegerOperator.MULT)); // result = !result * !x + Command decrementN = new VariableAssignment(new VariableName("n"), new IntegerOperation(new VariableRead(new VariableName("n")), new IntegerValue(1), IntegerOperator.MINUS)); // n = !n - 1 Command whileLoop = new While(condition, new Chain(multiplyResult, decrementN)); return new Chain(initialization, whileLoop); diff --git a/src/pld/common/Evaluator.java b/src/pld/common/Evaluator.java index e8f6229..89b4b54 100644 --- a/src/pld/common/Evaluator.java +++ b/src/pld/common/Evaluator.java @@ -36,7 +36,6 @@ public void step() { ControlStackable top = controlStack.remove(controlStack.size() - 1); if (top instanceof IntegerExpression || top instanceof IntegerOperator) { processInteger(top); - } else if (top instanceof BooleanExpression || top instanceof ComparisonOperator || top instanceof LogicalOperator) { processBoolean(top); } else if (top instanceof Command || top instanceof CommandToken) {