|
@@ -1,11 +1,19 @@
|
|
|
package processor;
|
|
|
|
|
|
+import java.awt.event.ComponentEvent;
|
|
|
+import java.awt.event.ComponentListener;
|
|
|
import java.util.Stack;
|
|
|
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.SwingUtilities;
|
|
|
+
|
|
|
+import algorithm.Action;
|
|
|
+import algorithm.AnimationController;
|
|
|
+import graph.LayeredGraphNode;
|
|
|
import processor.Memory.Visibility;
|
|
|
import processor.StackFrame.FrameType;
|
|
|
|
|
|
-public class PseudoCodeProcessor {
|
|
|
+public class PseudoCodeProcessor extends Thread {
|
|
|
|
|
|
public static enum CodeStatus
|
|
|
{
|
|
@@ -13,20 +21,33 @@ public class PseudoCodeProcessor {
|
|
|
BREAKPOINT,
|
|
|
FINISHED
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ private LayeredGraphNode graph;
|
|
|
private String currentDebugOutput;
|
|
|
private Memory mem;
|
|
|
private PseudoCodeNode programPointer;
|
|
|
private Stack<ControlFlow> controlStack;
|
|
|
private boolean skip = false;
|
|
|
+ private boolean renderImage = true;
|
|
|
+ private JFrame view;
|
|
|
+ private AnimationController controller;
|
|
|
|
|
|
- public PseudoCodeProcessor( PseudoCodeNode start )
|
|
|
+ public PseudoCodeProcessor( PseudoCodeNode start, LayeredGraphNode graph, JFrame view )
|
|
|
{
|
|
|
mem = new Memory();
|
|
|
mem.addFrame( new StackFrame( FrameType.FUNCTION ) );
|
|
|
+ mem.declare( "graph", graph, Visibility.GLOBAL );
|
|
|
programPointer = start;
|
|
|
currentDebugOutput = "";
|
|
|
controlStack = new Stack<>();
|
|
|
+ this.graph = graph;
|
|
|
+ controller = new AnimationController();
|
|
|
+ this.view = view;
|
|
|
+ }
|
|
|
+
|
|
|
+ public AnimationController getController()
|
|
|
+ {
|
|
|
+ return controller;
|
|
|
}
|
|
|
|
|
|
private CodeStatus selectNextNode( PseudoCodeNode next, PseudoCodeNode last )
|
|
@@ -127,7 +148,7 @@ public class PseudoCodeProcessor {
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
- public CodeStatus forwardStepOver()
|
|
|
+ private CodeStatus forwardStepOver()
|
|
|
{
|
|
|
if( programPointer == null )
|
|
|
return CodeStatus.FINISHED;
|
|
@@ -139,7 +160,7 @@ public class PseudoCodeProcessor {
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
- public CodeStatus forwardStepOut()
|
|
|
+ private CodeStatus forwardStepOut()
|
|
|
{
|
|
|
if( programPointer == null )
|
|
|
return CodeStatus.FINISHED;
|
|
@@ -168,7 +189,7 @@ public class PseudoCodeProcessor {
|
|
|
return CodeStatus.UNFINISHED;
|
|
|
}
|
|
|
|
|
|
- public CodeStatus backwardStep()
|
|
|
+ private CodeStatus backwardStep()
|
|
|
{
|
|
|
if( programPointer == null || controlStack.isEmpty() )
|
|
|
return CodeStatus.FINISHED;
|
|
@@ -180,7 +201,7 @@ public class PseudoCodeProcessor {
|
|
|
return selectBeforeNode( nextPC, programPointer );
|
|
|
}
|
|
|
|
|
|
- public CodeStatus backwardStepOverUntilNotSkip()
|
|
|
+ private CodeStatus backwardStepOverUntilNotSkip()
|
|
|
{
|
|
|
skip = true;
|
|
|
if( programPointer == null )
|
|
@@ -193,7 +214,7 @@ public class PseudoCodeProcessor {
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
- public CodeStatus backwardStepOver()
|
|
|
+ private CodeStatus backwardStepOver()
|
|
|
{
|
|
|
if( programPointer == null )
|
|
|
return CodeStatus.FINISHED;
|
|
@@ -205,7 +226,7 @@ public class PseudoCodeProcessor {
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
- public CodeStatus backwardStepOut()
|
|
|
+ private CodeStatus backwardStepOut()
|
|
|
{
|
|
|
if( programPointer == null )
|
|
|
return CodeStatus.FINISHED;
|
|
@@ -216,6 +237,76 @@ public class PseudoCodeProcessor {
|
|
|
} while( mem.getSize() >= stackSize && status == CodeStatus.UNFINISHED );
|
|
|
return status;
|
|
|
}
|
|
|
+
|
|
|
+ private synchronized void update()
|
|
|
+ {
|
|
|
+ if( renderImage )
|
|
|
+ {
|
|
|
+ renderImage = false;
|
|
|
+ SwingUtilities.invokeLater(new Runnable() {
|
|
|
+ public void run() {
|
|
|
+ for( ComponentListener l : view.getComponentListeners() )
|
|
|
+ {
|
|
|
+ l.componentResized( new ComponentEvent(view, 0) );
|
|
|
+ synchronized( this )
|
|
|
+ {
|
|
|
+ renderImage = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run()
|
|
|
+ {
|
|
|
+ while( true ) // if this loop would end we could not undo steps any more
|
|
|
+ {
|
|
|
+ CodeStatus status = null;
|
|
|
+ try {
|
|
|
+ Action action = controller.getNextAction();
|
|
|
+ graph.unselectGraph();
|
|
|
+ switch( action )
|
|
|
+ {
|
|
|
+ case FORWARD:
|
|
|
+ status = forwardStep();
|
|
|
+ break;
|
|
|
+ case FORWARD_OUT:
|
|
|
+ status = forwardStepOut();
|
|
|
+ graph.unselectGraph();
|
|
|
+ break;
|
|
|
+ case FORWARD_OVER:
|
|
|
+ status = forwardStepOver();
|
|
|
+ graph.unselectGraph();
|
|
|
+ break;
|
|
|
+ case BACKWARD:
|
|
|
+ status = backwardStep();
|
|
|
+ break;
|
|
|
+ case BACKWARD_OUT:
|
|
|
+ status = backwardStepOut();
|
|
|
+ graph.unselectGraph();
|
|
|
+ break;
|
|
|
+ case BACKWARD_OVER:
|
|
|
+ status = backwardStepOver();
|
|
|
+ graph.unselectGraph();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ assert false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ update();
|
|
|
+ if( status == CodeStatus.FINISHED )
|
|
|
+ {
|
|
|
+ controller.setContinuous( false );
|
|
|
+ controller.setNextAction( null );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public Memory getMemory()
|
|
|
{
|