package nxm.sys.intr;

import nxm.sys.lib.Intrinsic;

/** Handle a block ENDDO control statement.

    @author Jeff Schoen

    @see Do
    @see Endloop
 */
public class Enddo extends Intrinsic {
  private final String type; // Type of loop (DO or LOOP)

  /** The top-level do. */
  private nxm.sys.intr.Do top=null;

  /** Creates a new instance. */
  public Enddo () { this(Do.DO); }

  /** Creates a new instance, can be called from Endloop() */
  Enddo (String type) { this.type = type; }

  public int process() {
    // NTN: if we are not in a do loop, then display warning message
    //      and return otherwise we will get a NullPointerException
    if (top==null && M.macro == null) {
      M.warning("Found END"+type.toUpperCase()+" but not in a "+type.toUpperCase()+" loop.");
      return FINISH;
    }

    if (top==null) {
      top = (Do)M.macro.lines[nextIndex].cmd;
    }

    top.start += top.inc;

    if ((top.inc>0 && top.start<=top.end) ||
        (top.inc<0 && top.start>=top.end)) {

      M.macro.nextIndex = nextIndex+1;
      MR.put (top.label,top.start);
    }

    return FINISH;
  }
}
