FunctionDefinition.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package codeline;
  2. import processor.ControlFlow;
  3. import processor.Memory;
  4. import processor.StackFrame;
  5. import processor.Memory.Visibility;
  6. import processor.StackFrame.FrameType;
  7. /**
  8. * a line of code that defines introduces the definition of a new function
  9. * @author kolja
  10. */
  11. public class FunctionDefinition extends CodeLine {
  12. private String[] params;
  13. /**
  14. * Erstellt eine neue Funktion
  15. * @param params Eine Liste mit Namen von Argumenten die automatisch beim Aufruf aus den Globalen Registern in das neue Stackframe geladen werden sollen
  16. */
  17. public FunctionDefinition( String[] params )
  18. {
  19. this.params = params;
  20. }
  21. @Override
  22. public ControlFlow runForward(Memory m) {
  23. if( !m.isDefined( "line_" + lineId + "_inside", Visibility.LOCAL ) )
  24. { // Funktion wurde noch nicht aufgerufen
  25. m.addFrame( new StackFrame( FrameType.FUNCTION ) ); // F�ge neues Stackframe hinzu
  26. m.declare( "line_" + lineId + "_inside", true, Visibility.LOCAL );
  27. int index = 1;
  28. Object[] olds = new Object[ params.length ];
  29. for( String p : params )
  30. { // Lade alle Parameter aus den Globalen Registern
  31. olds[ index - 1 ] = m.read( "param" + index, Visibility.GLOBAL );
  32. m.declare( p, olds[ index - 1 ], Visibility.LOCAL ); // Speichere sie im Stack
  33. m.undeclare( "param" + index, Visibility.GLOBAL );
  34. index++;
  35. }
  36. actions.push( (Memory mem) -> { // merkt sich die R�ckw�rtsaktion
  37. mem.removeFrame();
  38. int i = 1;
  39. for( @SuppressWarnings("unused") String p : params )
  40. {
  41. mem.declare( "param" + i, olds[ i - 1], Visibility.GLOBAL );
  42. i++;
  43. }
  44. } );
  45. return new ControlFlow( ControlFlow.STEP_INTO ); // springe in die Funktion
  46. }
  47. else
  48. { // Funktion ist bereits follst�ndig ausgef�hrt worden
  49. StackFrame frame = m.removeFrame();
  50. actions.push( (Memory mem) -> {
  51. mem.addFrame( frame );
  52. } );
  53. return new ControlFlow( ControlFlow.STEP_OVER ); // return
  54. }
  55. }
  56. }