IfLoop.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. return new ControlFlow( ControlFlow.STEP_INTO );
  22. } );
  23. return new ControlFlow( ControlFlow.STEP_OVER );
  24. }
  25. else
  26. {
  27. if( condition( m ) ) {
  28. m.addFrame( new StackFrame( FrameType.LOOP ) );
  29. m.declare( "line_" + lineId + "_inside", true, MemoryType.LOCAL );
  30. actions.push( (Memory mem) -> {
  31. mem.removeFrame();
  32. return new ControlFlow( ControlFlow.STEP_OVER );
  33. } );
  34. return new ControlFlow( ControlFlow.STEP_INTO );
  35. } else {
  36. actions.push( (Memory mem) -> {
  37. return new ControlFlow( ControlFlow.STEP_OVER );
  38. } );
  39. return new ControlFlow( ControlFlow.STEP_OVER );
  40. }
  41. }
  42. }
  43. abstract protected boolean condition( Memory m );
  44. }