


package tmcm.xTurtle;

import java.util.Vector;
import java.awt.Color;

class TStack {

   double turtleX, turtleY, turtleHeading;
   boolean turtleIsVisible = true, turtleIsDrawing = true;
   int forkNumber;
   int programCounter;
   int stackStart, stackTop, stackRef;
   TStack next,prev;
   TStack parent;
   Vector children;
   Color color = Color.red;
   Object turtleRef = null;
   int recursionDepth;
   double[] stack;
   
   static int maxRecursionDepth = 2000;
   
   TStack() {
      this(0,null);
   }
   
   TStack(int start, TStack parent) {
      programCounter = start;
      next = this;
      prev = this;
      this.parent = parent;
      children = null;
      stack = new double[20];
   }
   
   void reinit(int globalCt) {
      stackTop = globalCt;
      next = this;
      prev = this;
   }
   
   void push(double x) {
      int loc = stackTop - stackStart;
      if (loc >= stack.length) {
         double[] temp = new double[stack.length + 100];
         System.arraycopy(stack,0,temp,0,stack.length);
         stack = temp;
      }
      stack[loc] = x;
      stackTop++;
   }
   
   double pop() {
      stackTop--;
      int loc = stackTop - stackStart;
      if (loc < 0)
         throw new TError("Internal xTurtle error! Attempt to pop past process start.",0);
      return stack[loc];
   }
   
   void store(int offset, double x) {
     if (offset <= 0)
       offset = -offset;
     else
       offset = stackRef + offset;
     TStack proc = this;
     while (offset < proc.stackStart)
       proc = proc.parent;
     int loc = offset - proc.stackStart;
     proc.stack[loc] = x;
   }
   
   double fetch(int offset) {
     if (offset <= 0)
       offset = -offset;
     else
       offset = stackRef + offset;
     TStack proc = this;
     while (offset < proc.stackStart)
       proc = proc.parent;
     int loc = offset - proc.stackStart;
     return proc.stack[loc];
   }
   
}


