package nxm.sys.intr;

import nxm.sys.lib.Intrinsic;

/** Handle a block IF control statement (see also {@link Elseif}, {@link Else},
    {@link Endif}). <br>
    <br>
    The {@link If} intrinsic handles selection of the proper branch in an
    IF..ELSEIF..ELSE..ENDIF block and branches the code as appropriate.

    @author  Jeff Schoen
 */
public class If extends Intrinsic {

  /** default constructor */
  public If () {}

  @Override
  public int process () {
    boolean test = MA.logicalTest();

    if (M.macro == null) {
      M.info("Logical test = " + test);
    }

    // step through block-if until true
    for (int index=nextIndex; index>0 && !test; ) {
      M.macro.nextIndex = index+1;
      // the logical test is performed in the Args class
      test = M.macro.lines[index].logicalTest();
      // want to see all tests in the block if verify is on
      if (test) M.macro.lines[index].cmd.verify();
      // jump to the next ELSE/ELSEIF/ENDIF command
      index = ((Intrinsic)M.macro.lines[index].cmd).nextIndex;
    }
    return FINISH;
  }
}
