Kolja Strohm 6 жил өмнө
parent
commit
a97a0d274b

+ 3 - 9
src/bk/BlockCalc.java

@@ -8,6 +8,7 @@ import animation.CodeLine;
 import animation.ControlFlow;
 import animation.Memory;
 import animation.PseudoCodeNode;
+import codelines.FunctionDefinition;
 import graph.LayeredGraphNode;
 import lib.TextLayoutHelper;
 
@@ -20,15 +21,8 @@ public class BlockCalc implements AlgorithmStage {
 
     @Override
     public PseudoCodeNode createPseudocodeTree( JTree tree ) {
-        String[] vars = { "graph", "L", "v", "r", "neighbors" };
-        PseudoCodeNode root = new PseudoCodeNode( TextLayoutHelper.setupPseudoCode( "calculateBlockGraph( graph )", vars ), tree, new CodeLine() {
-        	
-			@Override
-			public ControlFlow runForward(Memory m) {
-				return new ControlFlow( ControlFlow.STEP_INTO );
-			}
-        	
-        } );
+        String[] vars = { "graph", "L", "v", "r", "neighbors", "layout" };
+        PseudoCodeNode root = new PseudoCodeNode( TextLayoutHelper.setupPseudoCode( "calculateBlockGraph( graph, layout )", vars ), tree, new FunctionDefinition( new String[]{ "graph", "layout" } ) );
         return root;
     }
 }

+ 59 - 0
src/codelines/AbstractForLoop.java

@@ -0,0 +1,59 @@
+package codelines;
+
+import animation.CodeLine;
+import animation.ControlFlow;
+import animation.Memory;
+import animation.StackFrame;
+import animation.Memory.MemoryType;
+import animation.StackFrame.FrameType;
+
+public abstract class AbstractForLoop<T> extends CodeLine {
+
+    String loopVar;
+
+    public AbstractForLoop( String varName )
+    {
+        this.loopVar = varName;
+    }
+    
+    @Override
+    public ControlFlow runForward(Memory m) {
+        if( !m.isDefined( loopVar, MemoryType.LOCAL ) )
+        { // first loop step
+            m.addFrame( new StackFrame( FrameType.LOOP ) );
+            m.declare( loopVar, begin( m ), MemoryType.LOCAL ); // set loop variable
+            if( !condition( m ) ) // prove if the loop has finished
+            {
+                m.removeFrame();
+                actions.push( (Memory mem) -> {} );
+                return new ControlFlow( ControlFlow.STEP_OVER ); // don't execute the loop body
+            }
+            actions.push( (Memory mem) -> {
+                mem.removeFrame();
+            } );
+        }
+        BackwardAction reverseChange = change( m );
+        if( !condition( m ) ) // prove if loop was finished
+        {
+            StackFrame sf = m.removeFrame(); // remove loop stack
+            actions.add( (Memory mem) -> {
+                mem.addFrame( sf ); // restore last loop stack
+                reverseChange.backward( mem );
+            });
+            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 ) );
+        actions.push( (Memory mem) -> {
+            mem.removeFrame();
+            mem.addFrame( old );
+        });
+        return new ControlFlow( ControlFlow.STEP_INTO );
+    }
+    
+    abstract protected T begin( Memory m );
+    
+    abstract protected BackwardAction change( Memory m );
+    
+    abstract protected boolean condition( Memory m );
+}