Skip to content

Commit

Permalink
Add comments for pow program
Browse files Browse the repository at this point in the history
  • Loading branch information
StenAL committed Feb 21, 2021
1 parent 8a4783b commit 8ee5d82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
21 changes: 13 additions & 8 deletions src/pld/Main.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/pld/common/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8ee5d82

Please sign in to comment.