package nxm.sys.intr;

import nxm.sys.lib.Command;
import nxm.sys.lib.Intrinsic;

/**
  Handle a Break statement in a DO, WHILE, or FOREACH loop.

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

  int level, index;
  Command cmd;

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

  public int process() {

    level = MA.getL("LEVELS");

    // step through block-if until true
    index = M.macro.nextIndex;
    while (index<M.macro.lastIndex && level>0) {
      cmd = M.macro.lines[index++].cmd;
           if (cmd instanceof Do) level++;
      else if (cmd instanceof Enddo) level--;
      else if (cmd instanceof While) level++;
      else if (cmd instanceof Endwhile) level--;
      else if (cmd instanceof Foreach) level++;
      else if (cmd instanceof Endfor) level--;
    }
    M.macro.nextIndex = index;

    return FINISH;
  }

}
