/// <summary>Replaces a subtree of this Program with a new object.</summary> /// <param name="inIndex">The index of the subtree to replace.</param> /// <param name="inReplacement">The replacement for the subtree</param> /// <returns>True if a replacement was made (the index was valid).</returns> public bool ReplaceSubtree(int inIndex, object inReplacement) { if (_size == 0) { this.Push(inReplacement); return(true); } if (inIndex < _size) { this[inIndex] = Cloneforprogram(inReplacement); return(true); } else { int startIndex = _size; for (int n = 0; n < _size; n++) { object o = this[n]; if (o is Psh.Program) { Psh.Program sub = (Psh.Program)o; int length = sub.ProgramSize(); if (inIndex - startIndex < length) { return(sub.ReplaceSubtree(inIndex - startIndex, inReplacement)); } startIndex += length; } } } return(false); }
/// <summary>Returns a subtree of the program.</summary> /// <param name="inIndex">The index of the requested subtree.</param> /// <returns>The program subtree.</returns> public object Subtree(int inIndex) { if (inIndex < _size) { return(this[inIndex]); } else { int startIndex = _size; for (int n = 0; n < _size; n++) { object o = this[n]; if (o is Psh.Program) { Psh.Program sub = (Psh.Program)o; int length = sub.ProgramSize(); if (inIndex - startIndex < length) { return(sub.Subtree(inIndex - startIndex)); } startIndex += length; } } } return(null); }
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. Psh.Program replacement = new Psh.Program(this); Clear(); for (int i = 0; i < replacement._size; i++) { if (inIndex == i) { if (replacement[i] is Psh.Program) { Psh.Program p = (Psh.Program)replacement[i]; for (int j = 0; j < p._size; j++) { this.Push(p[j]); } } else { this.Push(replacement[i]); } } else { this.Push(replacement[i]); } } } else { int startIndex = _size; for (int n = 0; n < _size; n++) { object o = this[n]; if (o is Psh.Program) { Psh.Program sub = (Psh.Program)o; int length = sub.ProgramSize(); if (inIndex - startIndex < length) { sub.Flatten(inIndex - startIndex); break; } startIndex += length; } } } }