package codelines;

import animation.CodeLine;
import animation.ControlFlow;
import animation.Memory;
import animation.StackFrame;
import animation.Memory.MemoryType;
import animation.Memory.ReadOnlyMemory;
import animation.StackFrame.FrameType;

/**
 * Yey, IF is a LOOP.
 * @author kolja
 *
 */
public abstract class IfLoop extends CodeLine {

	@Override
	public ControlFlow runForward(Memory m) {
		if( m.isDefined( "line_" + lineId + "_inside", MemoryType.LOCAL ) )
		{
			StackFrame old = m.removeFrame();
			actions.push( (Memory mem) -> {
				mem.addFrame( old );
			} );
            return new ControlFlow( ControlFlow.STEP_OVER );
		}
		else
		{
            if( condition( m.createReadOnlyMemory() ) ) {
            	m.addFrame( new StackFrame( FrameType.LOOP ) );
            	m.declare( "line_" + lineId + "_inside", true, MemoryType.LOCAL );
            	actions.push( (Memory mem) -> {
            		mem.removeFrame();
            	} );
            	return new ControlFlow( ControlFlow.STEP_INTO );
            } else {
            	actions.push( (Memory mem) -> {} );
                return new ControlFlow( ControlFlow.STEP_OVER );
            }
		}
	}

	abstract protected boolean condition( ReadOnlyMemory m );
}
