Finite loops of the Repeat class execute their body a fixed number of times. Unlike infinite loops, an instantaneously terminating body is not a problem, as it does not prevent the loop to terminate. Therefore, there is no detection of instantaneously terminating bodies of Repeat instructions.Repeat contains two integer field: num which is the initial number of cycles, and counter which is the number of cycles performed so far.
public class Repeat extends UnaryInstruction { protected int num, counter; public Repeat(int num,Instruction body) { super.body = body; this.num = num; counter = num; } public void reset(){ super.reset(); counter = num; } final public boolean equals(Instruction inst){ return super.equals(inst) && num == ((Repeat)inst).num; } final public String toString(){ return "loop {" + num + "} times " + body + " end"; } final protected byte activation(Machine machine) { while (counter > 0){ byte res = body.activ(machine); if (res == TERM){ counter--; body.reset(); }else{ return res; } } return TERM; } }