Example #1
0
    private string ToStringEx(ListDictionary sqlVarNames)
    {
      StringBuffer sb;
      int n;

      if (this is Cut)
        return "!";
      else if (!hasValue)
      {
        if (sqlVarNames == null)
        {
          if (this is NamedVar)
            return ((NamedVar)this).Name;
          else
            return ("_" + varNo);
        }
        else
        {
          string colName = (string)sqlVarNames[varNo];
          if (colName == null) PrologIO.Error("Unbound variable not allowed in where-part of persistent predicate call");
          return (colName);
        }
      }
      else if (isUnified)
        return LinkEnd.ToStringEx(sqlVarNames);
      else if (IsString)
      {
        if (sqlVarNames == null)
          return "\"" + functor + "\"";
        else // SQL
          return "'" + functor + "'";
      }
      else if (this == NULLLIST)
        return "[]";
      else if (IsList && arity == 2)
      {
        Term t;
        sb = new StringBuffer("[" + args[0].ToStringEx(sqlVarNames));
        t = args[1];
        while (t.Arity == 2 && t.IsList)
        {
          sb.Append("," + t.Arg(0).ToStringEx(sqlVarNames));
          t = t.Arg(1);
        }

        if (t.IsList)
          sb.Append("]");
        else
          sb.Append("|" + t.ToStringEx(sqlVarNames) + "]");
        return sb.ToString();
      }
      else if (functor == Parser.CURL)
      {
        if (arity == 0)
          return "{}";
        else
        {
          Term t;
          sb = new StringBuffer("{" + args[0].ToStringEx(sqlVarNames));
          t = args[1];
          while (t.Arity == 2)
          {
            sb.Append("," + t.Arg(0).ToStringEx(sqlVarNames));
            t = t.Arg(1);
          }
          sb.Append("}");
        }
      }
      else if (functor == NUMVAR && arity == 1 && Arg(0).IsInteger && (n = (int)Arg(0).ExprValue.AsNumber) >= 0)
      {
        int m = n / 26;
        n = n % 26;
        sb = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ".Substring(n, 1) + ((m == 0) ? "" : m.ToString()));
      }
      else if (functor == Parser.COMMA && arity == 2)
        sb = new StringBuffer("(" + args[0].ToStringEx(sqlVarNames) + "," + args[1].ToStringEx(sqlVarNames) + ")");
      else
      {
        sb = new StringBuffer();

        if (arity == 0)
        {
          if (oDescr == null)
            sb.Append(functor);
          else // functors which are actually operators are parenthesized
            sb.Append("(" + functor + ")");
        }
        else
        {
          if (oType == OType.xfx || oType == OType.xfy || oType == OType.yfx)
          {
            if (precedence < args[0].Precedence ||
                 (precedence == args[0].Precedence && (oType == OType.xfx || oType == OType.yfx)))
              sb.AppendPackedTerm(args[0], sqlVarNames);
            else
              sb.AppendTerm(args[0], sqlVarNames);

            sb.Append(functor);

            if (precedence < args[1].Precedence ||
                 (precedence == args[1].Precedence && (oType == OType.xfx || oType == OType.yfx)))
              sb.AppendPackedTerm(args[1], sqlVarNames);
            else
              sb.AppendTerm(args[1], sqlVarNames);
          }
          else if (oType == OType.fx && arity <= 1)
          {
            sb.Append(functor);
            if (precedence <= args[0].Precedence)
              sb.AppendPackedTerm(args[0], sqlVarNames);
            else
              sb.AppendTerm(args[0], sqlVarNames);
          }
          else if (oType == OType.fy && arity <= 1)
          {
            sb.Append(functor);
            if (precedence < args[0].Precedence)
              sb.AppendPackedTerm(args[0], sqlVarNames);
            else
              sb.AppendTerm(args[0], sqlVarNames);
          }
          else if (oType == OType.xf)
          {
            if (precedence <= args[0].Precedence)
              sb.AppendPackedTerm(args[0], sqlVarNames);
            else
              sb.AppendTerm(args[0], sqlVarNames);
            sb.Append(functor);
          }
          else if (oType == OType.yf)
          {
            if (precedence < args[0].Precedence)
              sb.AppendPackedTerm(args[0], sqlVarNames);
            else
              sb.AppendTerm(args[0], sqlVarNames);
            sb.Append(functor);
          }
          else
          {
            sb.Append(functor);
            sb.Append("(");
            for (int i = 0; i < (arity - 1); i++)
              sb.Append(args[i].ToStringEx(sqlVarNames) + ",");
            sb.Append(args[arity - 1].ToStringEx(sqlVarNames));
            sb.Append(")");
          }
        }
      }
      return sb.ToString();
    }