FunctionDefinition.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package codelines;
  2. import animation.CodeLine;
  3. import animation.ControlFlow;
  4. import animation.Memory;
  5. import animation.Memory.MemoryType;
  6. import animation.StackFrame;
  7. import animation.StackFrame.FrameType;
  8. public class FunctionDefinition extends CodeLine {
  9. private String[] params;
  10. public FunctionDefinition( String[] params )
  11. {
  12. this.params = params;
  13. }
  14. @Override
  15. public ControlFlow runForward(Memory m) {
  16. if( !m.isDefined( "line_" + lineId + "_inside", MemoryType.CURRENT ) )
  17. {
  18. m.addFrame( new StackFrame( FrameType.FUNCTION ) );
  19. m.declare( "line_" + lineId + "_inside", true, MemoryType.LOCAL );
  20. int index = 1;
  21. Object olds[] = new Object[ params.length ];
  22. for( String p : params )
  23. {
  24. olds[ index - 1 ] = m.read( "param" + index, MemoryType.GLOBAL );
  25. m.declare( p, olds[ index - 1 ], MemoryType.LOCAL );
  26. m.undeclare( "param" + index, MemoryType.GLOBAL );
  27. index++;
  28. }
  29. actions.push( (Memory mem) -> {
  30. mem.removeFrame();
  31. int i = 1;
  32. for( @SuppressWarnings("unused") String p : params )
  33. {
  34. mem.declare( "param" + i, olds[ i - 1], MemoryType.GLOBAL );
  35. i++;
  36. }
  37. return new ControlFlow( ControlFlow.STEP_OVER );
  38. } );
  39. return new ControlFlow( ControlFlow.STEP_INTO );
  40. }
  41. else
  42. {
  43. StackFrame frame = m.removeFrame();
  44. actions.push( (Memory mem) -> {
  45. mem.addFrame( frame );
  46. return new ControlFlow( ControlFlow.STEP_INTO ); // call function backwards
  47. } );
  48. return new ControlFlow( ControlFlow.STEP_OVER ); // return
  49. }
  50. }
  51. }