public override string ToString() { StringBuilder sb = new StringBuilder(); bool first = true; int indent = 2; TermNode tn = this; BaseTerm t; while (tn != null) { if ((t = tn.term) is TryOpenTerm) { if (!first) { sb.Append(','); } sb.AppendFormat("{0}{1}TRY{0}{1}(", Environment.NewLine, Spaces(2 * indent++)); first = true; } else if (t is CatchOpenTerm) { CatchOpenTerm co = (CatchOpenTerm)t; string msgVar = (co.MsgVar is AnonymousVariable) ? null : co.MsgVar.Name; string comma = (co.ExceptionClass == null || msgVar == null) ? null : ", "; sb.AppendFormat("{0}{1}){0}{1}CATCH {2}{3}{4}{0}{1}(", Environment.NewLine, Spaces(2 * (indent - 1)), co.ExceptionClass, comma, msgVar); first = true; } else if (t == TC_CLOSE) { sb.AppendFormat("{0}{1})", Environment.NewLine, Spaces(2 * --indent)); first = false; } else { if (first) { first = false; } else { sb.Append(','); } sb.AppendFormat("{0}{1}{2}", Environment.NewLine, Spaces(2 * indent), t); } tn = tn.nextNode; } return(sb.ToString()); }
BaseTerm CopyEx(int newVerNo, bool mustBeNamed) { if (IsUnified) { return(ChainEnd().CopyEx(newVerNo, mustBeNamed)); } // A neater solution would be to use overrides for each term subtype. if (this is Variable) { Variable v = (Variable)this; if (newVerNo == v.verNo) { return(v.newVar); } v.verNo = newVerNo; return(v.newVar = (mustBeNamed && this is NamedVariable) ? new NamedVariable(((NamedVariable)v).Name) : new Variable()); } else if (this is CatchOpenTerm) { CatchOpenTerm c = (CatchOpenTerm)this; return(new CatchOpenTerm(c.Id, c.ExceptionClass, c.MsgVar.CopyEx(newVerNo, mustBeNamed), c.SeqNo, c.SaveStackSize)); } else { if (arity == 0) { return(this); } BaseTerm t = null; BaseTerm[] a = new BaseTerm[arity]; for (int i = 0; i < arity; i++) { if (args[i] != null) // may be null for a GapTerm { a[i] = args[i].CopyEx(newVerNo, mustBeNamed); // recursively refresh arguments } } if (this is ListPatternTerm) { t = new ListPatternTerm(a); } else if (this is AltListTerm) { AltListTerm alt = (AltListTerm)this; t = new AltListTerm(alt.LeftBracket, alt.RightBracket, a[0], a[1]); } else if (this is ListTerm) { if (((ListTerm)this).CharCodeString == null) { t = new ListTerm(a[0], a[1]); } else // it's an ISO-style string { t = new ListTerm(((ListTerm)this).CharCodeString); } } else if (this is OperatorTerm) { t = new OperatorTerm(((OperatorTerm)this).od, a); } else if (this is DcgTerm) { t = new DcgTerm(functor, a); } else if (this is WrapperTerm) { t = new WrapperTerm((WrapperTerm)this, a); } else if (this is IntRangeTerm) { t = new IntRangeTerm((IntRangeTerm)this); } else if (this is ListPatternElem) { t = new ListPatternElem(a, ((ListPatternElem)this).downRepFactor, ((ListPatternElem)this).IsNegSearch); } else if (this is CompoundTerm) { t = new CompoundTerm(functor, a); } else { IO.Error("CopyEx(): type '{0}' not handled explicitly", this.GetType()); } return(t); } }