If Instruction Next: GrammarUp: External InstructionsPrevious: Extern Node

If Instruction

The boolean test If  extends BinaryInstruction and condition evaluation is performed by calling the parseBooleanExpression method of External.

public class If extends BinaryInstruction
{
  public String condition;
  private boolean condEvaluated = false;
  private boolean value;

  public If(String cond, Instruction t, Instruction e)
  {
     condition = cond;
     left = t;
     right = e;
  }

  public void reset(){ super.reset(); condEvaluated = false; }

  final public String toString(){ 
    return "if "+condition+" then "+left+" else "+right+" end"; 
  }

  final protected byte activation(Machine machine)
  {
    if (!condEvaluated){
      condEvaluated = true;
      value = External.parseBooleanExpression(condition);
    }
    return value ? left.activ(machine) : right.activ(machine);
  }
}