浏览代码

Forwärts und Rückwärts funktioniert alles

Kolja Strohm 6 年之前
父节点
当前提交
eccbab88e3

+ 4 - 5
src/animation/CodeLine.java

@@ -6,7 +6,7 @@ public abstract class CodeLine {
 
 	protected interface BackwardAction
 	{
-		public ControlFlow backward( Memory m );
+		public void backward( Memory m );
 	}
 	
 	protected Stack<BackwardAction> actions;
@@ -23,10 +23,9 @@ public abstract class CodeLine {
 	
     public abstract ControlFlow runForward( Memory m );
     
-    public ControlFlow runBackward( Memory m )
+    public void runBackward( Memory m )
     {
-    	if( actions.size() == 0 )
-    		return new ControlFlow( ControlFlow.STEP_OVER );
-    	return actions.pop().backward( m );
+    	if( actions.size() != 0 )
+    	    actions.pop().backward( m );
     }
 }

+ 37 - 2
src/animation/ControlFlow.java

@@ -1,5 +1,7 @@
 package animation;
 
+import animation.CodeLine.BackwardAction;
+
 public class ControlFlow {
     public static final int STEP_INTO = 0;
     public static final int STEP_OVER = 1;
@@ -7,24 +9,57 @@ public class ControlFlow {
     
     private int status;
     private PseudoCodeNode function;
+    private PseudoCodeNode jumpBack;
+    private BackwardAction reverse;
     
     public ControlFlow( int status )
     {
         this.status = status;
+        function = null;
+        jumpBack = null;
+        reverse = null;
     }
     
     public ControlFlow( PseudoCodeNode functionNode )
     {
     	status = CALL;
     	function = functionNode;
+        jumpBack = null;
+        reverse = null;
+    }
+    
+    /*
+     * Package Private from here on
+     * This is wanted because the individual algorithms should not have access to these functions
+     */
+    
+    void setJumpBack( PseudoCodeNode jB )
+    {
+        jumpBack = jB;
+    }
+    
+    void setBackwardAction( BackwardAction bka )
+    {
+        reverse = bka;
+    }
+    
+    void backward( Memory m )
+    {
+        if( reverse != null )
+            reverse.backward( m );
+    }
+    
+    PseudoCodeNode getJumpBack()
+    {
+        return jumpBack;
     }
     
-    public PseudoCodeNode getFunction()
+    PseudoCodeNode getFunction()
     {
     	return function;
     }
     
-    public int getStatus()
+    int getStatus()
     {
         return status;
     }

+ 13 - 3
src/animation/PseudoCodeNode.java

@@ -171,12 +171,22 @@ public class PseudoCodeNode extends DefaultMutableTreeNode {
     
     public ControlFlow forwardStep( Memory m )
     {
-        return code.runForward( m );
+        ControlFlow cf = code.runForward( m );
+        cf.setJumpBack( this );
+        return cf;
     }
     
-    public ControlFlow backwardStep( Memory m )
+    public ControlFlow emptyForwardStep( Memory m )
     {
-        return code.runBackward( m );
+        code.actions.push( (Memory mem) -> {} ); // add empty reverse function
+        ControlFlow cf = new ControlFlow( ControlFlow.STEP_OVER );
+        cf.setJumpBack( this );
+        return cf;
+    }
+    
+    public void backwardStep( Memory m )
+    {
+        code.runBackward( m );
     }
     
     public String getDebugOutput( Memory m )

+ 39 - 40
src/animation/PseudoCodeProcessor.java

@@ -1,5 +1,7 @@
 package animation;
 
+import java.util.Stack;
+
 import animation.Memory.MemoryType;
 import animation.StackFrame.FrameType;
 
@@ -15,6 +17,7 @@ public class PseudoCodeProcessor {
     private String currentDebugOutput;
     private Memory mem;
     private PseudoCodeNode programPointer;
+    private Stack<ControlFlow> controlStack;
     
     public PseudoCodeProcessor( PseudoCodeNode tree )
     {
@@ -22,6 +25,7 @@ public class PseudoCodeProcessor {
         mem.addFrame( new StackFrame( FrameType.FUNCTION ) );
         programPointer = tree;
         currentDebugOutput = "";
+        controlStack = new Stack<>();
     }
     
     private CodeStatus selectNextNode( PseudoCodeNode next, PseudoCodeNode last )
@@ -46,15 +50,30 @@ public class PseudoCodeProcessor {
             return CodeStatus.FINISHED;
         StackFrame before = mem.removeFrame();
         mem.addFrame( before );
-        ControlFlow cf = programPointer.forwardStep( mem );
+        ControlFlow cf = null;
+        if( mem.isDefined( "_call" + programPointer.getId(), MemoryType.LOCAL ) )
+        {
+            mem.undeclare( "_call" + programPointer.getId(), MemoryType.LOCAL );
+            cf = programPointer.emptyForwardStep( mem );
+            cf.setBackwardAction( (Memory m) -> {
+                mem.declare( "_call" + programPointer.getId(), true, MemoryType.LOCAL );
+            });
+        }
+        else
+            cf = programPointer.forwardStep( mem );
+        controlStack.push( cf );
         currentDebugOutput = programPointer.getDebugOutput( mem );
         switch( cf.getStatus() )
         {
         case ControlFlow.STEP_INTO:
             if( mem.isDefined( "_returnTo" + programPointer.getId(), MemoryType.GLOBAL ) )
             {
-                mem.declare( "_returnTo" + programPointer, mem.read( "_returnTo" + programPointer.getId(), MemoryType.GLOBAL ), MemoryType.LOCAL );
+                mem.declare( "_returnTo" + programPointer.getId(), mem.read( "_returnTo" + programPointer.getId(), MemoryType.GLOBAL ), MemoryType.LOCAL );
                 mem.undeclare( "_returnTo" + programPointer.getId(), MemoryType.GLOBAL );
+                cf.setBackwardAction( (Memory m) -> {
+                    mem.declare( "_returnTo" + programPointer.getId(), mem.read( "_returnTo" + programPointer.getId(), MemoryType.LOCAL ), MemoryType.GLOBAL );
+                    mem.undeclare( "_returnTo" + programPointer.getId(), MemoryType.LOCAL );
+                });
             }
             if( programPointer.children() == null )
                 throw new IllegalStateException( "A Codeline without sublines tryd to make a STEP_INTO." );
@@ -67,6 +86,9 @@ public class PseudoCodeProcessor {
             {
                 PseudoCodeNode nextPC = before.<PseudoCodeNode>get( "_returnTo" + programPointer.getId() );
                 before.undeclare( "_returnTo" + programPointer.getId() );
+                cf.setBackwardAction( (Memory m) -> {
+                    before.declare( "_returnTo" + programPointer.getId(), nextPC );
+                });
                 return selectNextNode( nextPC, programPointer );
             }
             PseudoCodeNode nextPC = (PseudoCodeNode) ((PseudoCodeNode)programPointer.getParent()).getChildAfter( programPointer );
@@ -75,8 +97,14 @@ public class PseudoCodeProcessor {
             else
                 return selectNextNode( nextPC, programPointer );
         case ControlFlow.CALL:
-            mem.declare( "_returnTo" + cf.getFunction().getId(), programPointer, MemoryType.GLOBAL );
-            return selectNextNode( cf.getFunction(), programPointer );
+            PseudoCodeNode f = cf.getFunction();
+            mem.declare( "_call" + programPointer.getId(), true, MemoryType.LOCAL );
+            mem.declare( "_returnTo" + f.getId(), cf.getJumpBack(), MemoryType.GLOBAL );
+            cf.setBackwardAction( (Memory m) -> {
+                m.undeclare( "_returnTo" + f.getId(), MemoryType.GLOBAL );
+                m.undeclare( "_call" + programPointer.getId(), MemoryType.LOCAL );
+            });
+            return selectNextNode( f, programPointer );
         }
         throw new IllegalStateException( "Unbekannte ControlFlow Aktion" );
     }
@@ -123,43 +151,14 @@ public class PseudoCodeProcessor {
     
     public CodeStatus backwardStep()
     {
-        if( programPointer == null )
+        if( programPointer == null || controlStack.isEmpty() )
             return CodeStatus.FINISHED;
-        StackFrame before = mem.removeFrame();
-        mem.addFrame( before );
-        ControlFlow cf = programPointer.backwardStep( mem );
-        currentDebugOutput = programPointer.getDebugOutput( mem );
-        switch( cf.getStatus() )
-        {
-        case ControlFlow.STEP_INTO:
-            if( mem.isDefined( "_returnTo" + programPointer.getId(), MemoryType.GLOBAL ) )
-            {
-                mem.declare( "_returnTo" + programPointer, mem.read( "_returnTo" + programPointer.getId(), MemoryType.GLOBAL ), MemoryType.LOCAL );
-                mem.undeclare( "_returnTo" + programPointer.getId(), MemoryType.GLOBAL );
-            }
-            if( programPointer.children() == null )
-                throw new IllegalStateException( "A Codeline without sublines tryd to make a STEP_INTO." );
-            else
-                return selectBeforeNode( (PseudoCodeNode)programPointer.getLastChild(), programPointer );
-        case ControlFlow.STEP_OVER:
-            if( programPointer.getParent() == null )
-                return CodeStatus.FINISHED;
-            if( before.isDefined( "_returnTo" + programPointer.getId() ) )
-            {
-                PseudoCodeNode nextPC = before.<PseudoCodeNode>get( "_returnTo" + programPointer.getId() );
-                before.undeclare( "_returnTo" + programPointer.getId() );
-                return selectBeforeNode( nextPC, programPointer );
-            }
-            PseudoCodeNode nextPC = (PseudoCodeNode) ((PseudoCodeNode)programPointer.getParent()).getChildBefore( programPointer );
-            if( nextPC == null )
-                return selectBeforeNode( (PseudoCodeNode) programPointer.getParent(), programPointer );
-            else
-                return selectBeforeNode( nextPC, programPointer );
-        case ControlFlow.CALL:
-            mem.declare( "_returnTo" + cf.getFunction().getId(), programPointer, MemoryType.GLOBAL );
-            return selectBeforeNode( cf.getFunction(), programPointer );
-        }
-        throw new IllegalStateException( "Unbekannte ControlFlow Aktion" );
+        ControlFlow cf = controlStack.pop();
+        PseudoCodeNode nextPC = cf.getJumpBack();
+        cf.backward( mem );
+        nextPC.backwardStep( mem );
+        currentDebugOutput = nextPC.getDebugOutput( mem );
+        return selectBeforeNode( nextPC, programPointer );
     }
     
     public CodeStatus backwardStepOver()

+ 3 - 2
src/bk/BKNodePlacement.java

@@ -63,13 +63,14 @@ public class BKNodePlacement extends AnimatedAlgorithm {
 				if( m.isDefined( "Called", MemoryType.GLOBAL ) )
 				{
 					actions.push( (Memory mem) -> {
-						return new ControlFlow( mainFunction );
+					    m.undeclare( "param1", MemoryType.GLOBAL );
 					} );
 					return new ControlFlow( ControlFlow.STEP_OVER );
 				}
 				m.declare( "Called", true, MemoryType.GLOBAL );
 				actions.push( (Memory mem) -> {
-					return new ControlFlow( ControlFlow.STEP_OVER );
+                    m.undeclare( "param1", MemoryType.GLOBAL );
+	                m.undeclare( "Called", MemoryType.GLOBAL );
 				} );
 				return new ControlFlow( mainFunction );
 			}

+ 0 - 3
src/bk/ConflictDetection.java

@@ -133,7 +133,6 @@ public class ConflictDetection implements AlgorithmStage {
 				m.declare( "L", m.<LayeredGraphNode>read( "graph", MemoryType.LOCAL ).getContainedLayers(), MemoryType.LOCAL );
 				actions.push( (Memory mem) -> {
 					mem.undeclare( "L", MemoryType.LOCAL );
-					return new ControlFlow( ControlFlow.STEP_OVER );
 				} );
 				return new ControlFlow( ControlFlow.STEP_OVER );
 			}
@@ -158,7 +157,6 @@ public class ConflictDetection implements AlgorithmStage {
 				actions.push( (Memory mem) -> {
 					mem.undeclare( "k0", MemoryType.LOCAL );
 					mem.undeclare( "l", MemoryType.LOCAL );
-					return new ControlFlow( ControlFlow.STEP_OVER );
 				} );
 				return new ControlFlow( ControlFlow.STEP_OVER );
 			}
@@ -236,7 +234,6 @@ public class ConflictDetection implements AlgorithmStage {
 				e.setConflicted( true, null );
 				actions.add( (Memory mem) -> {
 					e.setConflicted( old, null );
-					return new ControlFlow( ControlFlow.STEP_OVER );
 				});
 				return new ControlFlow( ControlFlow.STEP_OVER );
 			}

+ 1 - 6
src/codelines/BackwardForEachLoop.java

@@ -31,9 +31,7 @@ public class BackwardForEachLoop <T> extends CodeLine {
 		if( m.<Integer>read( "line_" + lineId + "_index", MemoryType.LOCAL ) < 0 ) // prove if the loop has finished
 		{
 			m.undeclare( "line_" + lineId + "_index", MemoryType.LOCAL );
-			actions.push( (Memory mem) -> {
-				return new ControlFlow( ControlFlow.STEP_OVER ); // loop was not called so nothing to reverse
-			});
+			actions.push( (Memory mem) -> {});
 			return new ControlFlow( ControlFlow.STEP_OVER ); // don't execute the loop body
 		}
 		if( declared )
@@ -43,7 +41,6 @@ public class BackwardForEachLoop <T> extends CodeLine {
 			actions.push( (Memory mem) -> {
 				mem.removeFrame();
 				mem.undeclare( "line_" + lineId + "_index", MemoryType.LOCAL );
-				return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
 			} );
 		}
 		else
@@ -60,7 +57,6 @@ public class BackwardForEachLoop <T> extends CodeLine {
 	                m.declare( loopVar, old, MemoryType.LOCAL );
 					mem.write( "line_" + lineId + "_index", oldIndex, MemoryType.LOCAL );
 					mem.addFrame( sf ); // restore last loop stack
-					return new ControlFlow( ControlFlow.STEP_INTO ); // step into the loop body
 				});
 				return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
 			}
@@ -71,7 +67,6 @@ public class BackwardForEachLoop <T> extends CodeLine {
 				mem.removeFrame();
 				mem.addFrame( old );
 				mem.write( "line_" + lineId + "_index", oldIndex, MemoryType.LOCAL );
-				return new ControlFlow( ControlFlow.STEP_INTO );
 			});
 		}
 		return new ControlFlow( ControlFlow.STEP_INTO );

+ 0 - 1
src/codelines/DeclareVariable.java

@@ -26,7 +26,6 @@ public abstract class DeclareVariable <T> extends CodeLine {
 				mem.undeclare( name, MemoryType.LOCAL );
 			else
 				mem.declare( name, oldVal, MemoryType.LOCAL ); // TODO declare in correct stack frame
-			return new ControlFlow( ControlFlow.STEP_OVER );
 		});
 		return new ControlFlow( ControlFlow.STEP_OVER );
 	}

+ 3 - 11
src/codelines/ForEachLoop.java

@@ -29,19 +29,16 @@ public abstract class ForEachLoop <T> extends CodeLine {
 		if( m.<Integer>read( "line_" + lineId + "_index", MemoryType.LOCAL ) > list( m ).size() - 1 ) // prove if the loop has finished
 		{
 			m.undeclare( "line_" + lineId + "_index", MemoryType.LOCAL );
-			actions.push( (Memory mem) -> {
-				return new ControlFlow( ControlFlow.STEP_OVER ); // loop was not called so nothing to reverse
-			});
+			actions.push( (Memory mem) -> {});
 			return new ControlFlow( ControlFlow.STEP_OVER ); // don't execute the loop body
 		}
 		if( declared )
 		{
-			m.declare( loopVar, list( m ).get( m.read( "line_" + lineId + "_index", MemoryType.LOCAL ) ), MemoryType.LOCAL ); // set loop variable
 			m.addFrame( new StackFrame( FrameType.LOOP ) );
+            m.declare( loopVar, list( m ).get( m.read( "line_" + lineId + "_index", MemoryType.LOCAL ) ), MemoryType.LOCAL ); // set loop variable
 			actions.push( (Memory mem) -> {
 				mem.removeFrame();
 				mem.undeclare( "line_" + lineId + "_index", MemoryType.LOCAL );
-				return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
 			} );
 		}
 		else
@@ -52,24 +49,19 @@ public abstract class ForEachLoop <T> extends CodeLine {
 			{
 				StackFrame sf = m.removeFrame(); // remove loop stack
 				m.undeclare( "line_" + lineId + "_index", MemoryType.LOCAL );
-				T old = m.read( loopVar, MemoryType.LOCAL );
-				m.undeclare( loopVar, MemoryType.LOCAL );
 				actions.push( (Memory mem) -> {
-				    mem.declare( loopVar, old, MemoryType.LOCAL );
 					mem.write( "line_" + lineId + "_index", oldIndex, MemoryType.LOCAL );
 					mem.addFrame( sf ); // restore last loop stack
-					return new ControlFlow( ControlFlow.STEP_INTO ); // step into the loop body
 				});
 				return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
 			}
 			StackFrame old = m.removeFrame(); // fresh stack frame for loop body
 			m.addFrame( new StackFrame( FrameType.LOOP ) );
-			m.write( loopVar, list( m ).get( m.read( "line_" + lineId + "_index", MemoryType.LOCAL ) ), MemoryType.LOCAL ); // update loop variable
+			m.declare( loopVar, list( m ).get( m.read( "line_" + lineId + "_index", MemoryType.LOCAL ) ), MemoryType.LOCAL ); // update loop variable
 			actions.push( (Memory mem) -> {
 				mem.removeFrame();
 				mem.addFrame( old );
 				mem.write( "line_" + lineId + "_index", oldIndex, MemoryType.LOCAL );
-				return new ControlFlow( ControlFlow.STEP_INTO );
 			});
 		}
 		return new ControlFlow( ControlFlow.STEP_INTO );

+ 1 - 6
src/codelines/ForLoop.java

@@ -27,9 +27,7 @@ public abstract class ForLoop extends CodeLine {
 		if( m.<Integer>read( "line_" + lineId + "_index", MemoryType.LOCAL ) > maximum( m ) ) // prove if the loop has finished
 		{
 			m.undeclare( "line_" + lineId + "_index", MemoryType.LOCAL );
-			actions.push( (Memory mem) -> {
-				return new ControlFlow( ControlFlow.STEP_OVER ); // loop was not called so nothing to reverse
-			});
+			actions.push( (Memory mem) -> {});
 			return new ControlFlow( ControlFlow.STEP_OVER ); // don't execute the loop body
 		}
 		if( declared )
@@ -39,7 +37,6 @@ public abstract class ForLoop extends CodeLine {
 			actions.push( (Memory mem) -> {
 				mem.removeFrame();
 				mem.undeclare( "line_" + lineId + "_index", MemoryType.LOCAL );
-				return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
 			} );
 		}
 		else
@@ -56,7 +53,6 @@ public abstract class ForLoop extends CodeLine {
 	                //m.declare( loopVar, old, false );
 					mem.write( "line_" + lineId + "_index", oldIndex, MemoryType.LOCAL );
 					mem.addFrame( sf ); // restore last loop stack
-					return new ControlFlow( ControlFlow.STEP_INTO ); // step into the loop body
 				});
 				return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
 			}
@@ -67,7 +63,6 @@ public abstract class ForLoop extends CodeLine {
 				mem.removeFrame();
 				mem.addFrame( old );
 				mem.write( "line_" + lineId + "_index", oldIndex, MemoryType.LOCAL );
-				return new ControlFlow( ControlFlow.STEP_INTO );
 			});
 		}
 		return new ControlFlow( ControlFlow.STEP_INTO );

+ 0 - 1
src/codelines/FunctionCall.java

@@ -32,7 +32,6 @@ public class FunctionCall extends CodeLine {
 				m.undeclare( "param" + ind, MemoryType.GLOBAL );
 				ind++;
 			}
-			return new ControlFlow( target );
 		} );
 		return new ControlFlow( target );
 	}

+ 0 - 2
src/codelines/FunctionDefinition.java

@@ -39,7 +39,6 @@ public class FunctionDefinition extends CodeLine {
 					mem.declare( "param" + i, olds[ i - 1], MemoryType.GLOBAL );
 					i++;
 				}
-				return new ControlFlow( ControlFlow.STEP_OVER );
 			} );
 			return new ControlFlow( ControlFlow.STEP_INTO );
 		}
@@ -48,7 +47,6 @@ public class FunctionDefinition extends CodeLine {
 			StackFrame frame = m.removeFrame();
 			actions.push( (Memory mem) -> {
 				mem.addFrame( frame );
-				return new ControlFlow( ControlFlow.STEP_INTO ); // call function backwards
 			} );
 			return new ControlFlow( ControlFlow.STEP_OVER ); // return
 		}

+ 1 - 5
src/codelines/IfLoop.java

@@ -21,7 +21,6 @@ public abstract class IfLoop extends CodeLine {
 			StackFrame old = m.removeFrame();
 			actions.push( (Memory mem) -> {
 				mem.addFrame( old );
-            	return new ControlFlow( ControlFlow.STEP_INTO );
 			} );
             return new ControlFlow( ControlFlow.STEP_OVER );
 		}
@@ -32,13 +31,10 @@ public abstract class IfLoop extends CodeLine {
             	m.declare( "line_" + lineId + "_inside", true, MemoryType.LOCAL );
             	actions.push( (Memory mem) -> {
             		mem.removeFrame();
-            		return new ControlFlow( ControlFlow.STEP_OVER );
             	} );
             	return new ControlFlow( ControlFlow.STEP_INTO );
             } else {
-            	actions.push( (Memory mem) -> {
-	                return new ControlFlow( ControlFlow.STEP_OVER );
-            	} );
+            	actions.push( (Memory mem) -> {} );
                 return new ControlFlow( ControlFlow.STEP_OVER );
             }
 		}

+ 0 - 1
src/codelines/SetVariable.java

@@ -26,7 +26,6 @@ public abstract class SetVariable <T> extends CodeLine {
                 mem.undeclare( name, MemoryType.LOCAL );
             else
                 mem.write( name, oldVal, MemoryType.LOCAL ); // TODO write in correct stack frame
-            return new ControlFlow( ControlFlow.STEP_OVER );
         });
         return new ControlFlow( ControlFlow.STEP_OVER );
     }

+ 1 - 6
src/codelines/WhileLoop.java

@@ -23,14 +23,11 @@ public abstract class WhileLoop extends CodeLine {
                 StackFrame sf = m.removeFrame(); // remove loop stack
                 actions.push( (Memory mem) -> {
                     mem.addFrame( sf ); // restore last loop stack
-                    return new ControlFlow( ControlFlow.STEP_INTO ); // step into the loop body
                 });
             }
             else
             {
-                actions.push( (Memory mem) -> {
-                    return new ControlFlow( ControlFlow.STEP_INTO ); // step into the loop body
-                });
+                actions.push( (Memory mem) -> {});
             }
             return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
         }
@@ -40,7 +37,6 @@ public abstract class WhileLoop extends CodeLine {
             m.declare( "line_" + lineId + "_index", 0, MemoryType.LOCAL );
 			actions.push( (Memory mem) -> {
 				mem.removeFrame();
-				return new ControlFlow( ControlFlow.STEP_OVER ); // step out of the loop
 			} );
 		}
 		else
@@ -51,7 +47,6 @@ public abstract class WhileLoop extends CodeLine {
 			actions.push( (Memory mem) -> {
 				mem.removeFrame();
 				mem.addFrame( old );
-				return new ControlFlow( ControlFlow.STEP_INTO );
 			});
 		}
 		return new ControlFlow( ControlFlow.STEP_INTO );