Example #1
0
  private int Parse(String inTokens[], int inStart) throws Exception {
    boolean first = (inStart == 0);

    for (int n = inStart; n < inTokens.length; n++) {
      String token = inTokens[n];

      if (!token.equals("")) {
        if (token.equals("(")) {

          // Found an open paren -- begin a recursive Parse, though
          // the very first
          // token in the list is a special case -- no need to create
          // a sub-program

          if (!first) {
            Program p = new Program(_interpreter);

            n = p.Parse(inTokens, n + 1);

            push(p);
          }
        } else if (token.equals(")")) {
          // End of the program -- return the advanced token index to
          // the caller

          return n;

        } else if (Character.isLetter(token.charAt(0))) {

          push(token);

          // This makes printing stacks very ugly. For now, will store
          // program instructions as strings, as was done before.
          /*
          Instruction i = _interpreter._instructions.get(token);
          if (i != null)
          	push(i);
          else
          	push(token);
          */

        } else {
          Object number;

          if (token.indexOf('.') != -1) number = Float.parseFloat(token);
          else number = Integer.parseInt(token);

          push(number);
        }

        first = false;
      }
    }

    // If we're here, there was no closing brace for one of the programs

    throw new Exception("no closing brace found for program");
  }
Example #2
0
  public void Flatten(int inIndex) {
    if (inIndex < _size) {
      // If here, the index to be flattened is in this program. So, push
      // the rest of the program onto a new program, and replace this with
      // that new program.

      Program replacement = new Program(this);
      clear();

      for (int i = 0; i < replacement._size; i++) {
        if (inIndex == i) {

          if (replacement._stack[i] instanceof Program) {
            Program p = (Program) replacement._stack[i];
            for (int j = 0; j < p._size; j++) this.push(p._stack[j]);
          } else {
            this.push(replacement._stack[i]);
          }
        } else {
          this.push(replacement._stack[i]);
        }
      }
    } else {
      int startIndex = _size;

      for (int n = 0; n < _size; n++) {
        Object o = _stack[n];

        if (o instanceof Program) {
          Program sub = (Program) o;
          int length = sub.programsize();

          if (inIndex - startIndex < length) {
            sub.Flatten(inIndex - startIndex);
            break;
          }

          startIndex += length;
        }
      }
    }
  }
Example #3
0
  /**
   * Returns a subtree of the program.
   *
   * @param inIndex The index of the requested subtree.
   * @return The program subtree.
   */
  public Object Subtree(int inIndex) {
    if (inIndex < _size) {
      return _stack[inIndex];
    } else {
      int startIndex = _size;

      for (int n = 0; n < _size; n++) {
        Object o = _stack[n];

        if (o instanceof Program) {
          Program sub = (Program) o;
          int length = sub.programsize();

          if (inIndex - startIndex < length) return sub.Subtree(inIndex - startIndex);

          startIndex += length;
        }
      }
    }

    return null;
  }
Example #4
0
  /**
   * Replaces a subtree of this Program with a new object.
   *
   * @param inIndex The index of the subtree to replace.
   * @param inReplacement The replacement for the subtree
   * @return True if a replacement was made (the index was valid).
   */
  public boolean ReplaceSubtree(int inIndex, Object inReplacement) {
    if (inIndex < _size) {
      _stack[inIndex] = cloneforprogram(inReplacement);
      return true;
    } else {
      int startIndex = _size;

      for (int n = 0; n < _size; n++) {
        Object o = _stack[n];

        if (o instanceof Program) {
          Program sub = (Program) o;
          int length = sub.programsize();

          if (inIndex - startIndex < length)
            return sub.ReplaceSubtree(inIndex - startIndex, inReplacement);

          startIndex += length;
        }
      }
    }

    return false;
  }
Example #5
0
 /**
  * Constructs a copy of an existing Program.
  *
  * @param inOther The Push program to copy.
  */
 public Program(Program inOther) {
   inOther.CopyTo(this);
   _interpreter = inOther._interpreter;
 }
Example #6
0
 /**
  * Copies this program to another.
  *
  * @param inOther The program to receive the copy of this program
  */
 public void CopyTo(Program inOther) {
   for (int n = 0; n < _size; n++) inOther.push(_stack[n]);
 }