IfLoop.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package codelines;
  2. import animation.CodeLine;
  3. import animation.ControlFlow;
  4. import animation.Memory;
  5. import animation.StackFrame;
  6. import animation.Memory.MemoryType;
  7. import animation.StackFrame.FrameType;
  8. /**
  9. * Yey, IF is a LOOP.
  10. * @author kolja
  11. *
  12. */
  13. public abstract class IfLoop extends CodeLine {
  14. @Override
  15. public ControlFlow runForward(Memory m) {
  16. if( m.isDefined( "line_" + lineId + "_inside", MemoryType.LOCAL ) )
  17. {
  18. StackFrame old = m.removeFrame();
  19. actions.push( (Memory mem) -> {
  20. mem.addFrame( old );
  21. } );
  22. return new ControlFlow( ControlFlow.STEP_OVER );
  23. }
  24. else
  25. {
  26. if( condition( m ) ) {
  27. m.addFrame( new StackFrame( FrameType.LOOP ) );
  28. m.declare( "line_" + lineId + "_inside", true, MemoryType.LOCAL );
  29. actions.push( (Memory mem) -> {
  30. mem.removeFrame();
  31. } );
  32. return new ControlFlow( ControlFlow.STEP_INTO );
  33. } else {
  34. actions.push( (Memory mem) -> {} );
  35. return new ControlFlow( ControlFlow.STEP_OVER );
  36. }
  37. }
  38. }
  39. abstract protected boolean condition( Memory m );
  40. }