Building Parsers with Java

sjm.imperative
Class ForCommand

java.lang.Object
  |
  +--sjm.imperative.Command
        |
        +--sjm.imperative.ForCommand

public class ForCommand
extends Command

This command mimics a normal "for" loop, such as:


     for (int i = 0; i < limit; i++){
         // body
     }
 
This class retains four parameters which the constructors receive or establish. These parameters are a setup command, a condition, an end command, and a body command.

The execute method executes a "for" loop, essentially executing:


     for (setup; condition; endCommand){
         bodyCommand;
     }
 


Field Summary
protected  Command bodyCommand
           
protected  BooleanTerm condition
           
protected  Command endCommand
           
protected  Command setupCommand
           
 
Constructor Summary
ForCommand(Command setupCommand, BooleanTerm condition, Command endCommand, Command bodyCommand)
          Construct a "for" command from the given setup command, condition, endCommand, and bodyCommand.
ForCommand(Variable v, double from, double to, double step, Command bodyCommand)
          Construct a "for" command that iterates the supplied variable over the doubles from the "from" parameter to the "to" parameter, stepping by the "step" parameter.
ForCommand(Variable v, int from, int to, Command bodyCommand)
          Construct a "for" command that iterates the supplied variable over the integers from the "from" parameter to the "to" parameter.
 
Method Summary
 void execute()
          Execute this "for" command.
 java.lang.String toString()
          Returns a string description of this for command.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

setupCommand

protected Command setupCommand

condition

protected BooleanTerm condition

endCommand

protected Command endCommand

bodyCommand

protected Command bodyCommand
Constructor Detail

ForCommand

public ForCommand(Variable v,
                  double from,
                  double to,
                  double step,
                  Command bodyCommand)
Construct a "for" command that iterates the supplied variable over the doubles from the "from" parameter to the "to" parameter, stepping by the "step" parameter. This command will iterate, adding step to the initial value, ceasing the loop when the value is greater than the "to" limit.
Parameters:
Variable - the variable to iterate
from - the initial value for the variable
to - a value not to exceed in the loop
step - the amount to increment by
Command - the command to repeatedly execute

ForCommand

public ForCommand(Variable v,
                  int from,
                  int to,
                  Command bodyCommand)
Construct a "for" command that iterates the supplied variable over the integers from the "from" parameter to the "to" parameter. For example, the following code segment prints out "my\n" five times.
 
     Variable i = new Variable("i");
     Fact f = new Fact("my");
     PrintCommand p = new PrintCommand(f); *     
     new ForCommand(i, 1, 5, p).execute();
 
 
Parameters:
Variable - the variable to iterate
int - the initial value for the variable
int - the final value for the variable
Command - the command to repeatedly execute

ForCommand

public ForCommand(Command setupCommand,
                  BooleanTerm condition,
                  Command endCommand,
                  Command bodyCommand)
Construct a "for" command from the given setup command, condition, endCommand, and bodyCommand.
Parameters:
setupCommand - the command to execute before the "for" loop begins
condition - the condition to check before executing the body
endCommand - the condition to check after executing the body
bodyCommand - the command to repeatedly execute
Method Detail

execute

public void execute()
Execute this "for" command. This means executing the setup command and then looping. The loop checks that the condition it true, executes the body command, and executes the end command.
Overrides:
execute in class Command

toString

public java.lang.String toString()
Returns a string description of this for command.
Overrides:
toString in class java.lang.Object
Returns:
a string description of this for command

by Steve Metsker