CodeLine.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package codeline;
  2. import java.util.Stack;
  3. import processor.ControlFlow;
  4. import processor.Memory;
  5. /**
  6. * A line of pseudo code that can be executed.
  7. * @author kolja
  8. *
  9. */
  10. public abstract class CodeLine {
  11. /**
  12. * an action that undoes what the {@link CodeLine} did
  13. * @author kolja
  14. *
  15. */
  16. public interface BackwardAction
  17. {
  18. public void backward( Memory m );
  19. }
  20. protected Stack<BackwardAction> actions;
  21. protected int lineId;
  22. private static int nextLineId = 0;
  23. /**
  24. * creates a new line of code
  25. */
  26. public CodeLine()
  27. {
  28. synchronized( CodeLine.class ) {
  29. lineId = nextLineId++;
  30. }
  31. actions = new Stack<BackwardAction>();
  32. }
  33. /**
  34. * executes the line of code
  35. * @param m the memory in which the variables are
  36. * @return what should be executed next
  37. */
  38. public abstract ControlFlow runForward( Memory m );
  39. /**
  40. * undo a call of {@code runForward}
  41. * @param m the memory in which the variables are
  42. */
  43. public void runBackward( Memory m )
  44. {
  45. if( actions.size() != 0 )
  46. actions.pop().backward( m ); // Arbeite den Stack von Rückwärts Aktionen ab
  47. }
  48. /**
  49. * creates an empty backwards action and pushes it to the stack
  50. */
  51. public void createEmptyBackwardsAction() {
  52. actions.push( (Memory mem) -> {} ); // add empty reverse function
  53. }
  54. }