Beispiel #1
0
        /// <summary>
        /// Ported from expr_t::op_t::dump
        /// </summary>
        public string Dump(int depth = 0)
        {
            StringBuilder sb = new StringBuilder(new string(' ', depth));

            switch (Kind)
            {
            case OpKindEnum.VALUE:
                sb.AppendFormat("VALUE: {0}", AsValue.Dump());
                break;

            case OpKindEnum.IDENT:
                sb.AppendFormat("IDENT: {0}", AsIdent);
                break;

            case OpKindEnum.SCOPE:
                sb.AppendFormat("SCOPE: {0}", IsScopeUnset ? "null" : AsScope.Description);
                break;

            case OpKindEnum.LAST:
                throw new InvalidOperationException("assert(false)");

            default:
                sb.Append(Kind.ToString());
                break;
            }

            sb.AppendLine(String.Format(" ({0})", 0 /* DM - no refc */));

            // An identifier is a special non-terminal, in that its left() can
            // hold the compiled definition of the identifier.
            if (Kind > OpKindEnum.TERMINALS || IsScope || IsIdent)
            {
                if (Left != null)
                {
                    sb.Append(Left.Dump(depth + 1));
                    if (Kind > OpKindEnum.UNARY_OPERATORS && HasRight)
                    {
                        sb.Append(Right.Dump(depth + 1));
                    }
                }
                else if (Kind > OpKindEnum.UNARY_OPERATORS)
                {
                    if (HasRight)
                    {
                        throw new InvalidOperationException("assert(! has_right())");
                    }
                }
            }

            return(sb.ToString());
        }
Beispiel #2
0
        public bool Print(ref string str, ExprOpContext context = null)
        {
            str     = str ?? String.Empty;
            context = context ?? new ExprOpContext();

            bool found = false;

            if (context.StartPos.HasValue && this == context.OpToFind)
            {
                context.StartPos = str.Length - 1;
                found            = true;
            }

            string symbol = null;

            if (Kind > OpKindEnum.TERMINALS && (Kind != OpKindEnum.O_CALL && Kind != OpKindEnum.O_DEFINE))
            {
                str += '(';
            }

            switch (Kind)
            {
            case OpKindEnum.VALUE:
                str += AsValue.Dump(context.Relaxed);
                break;

            case OpKindEnum.IDENT:
                str += AsIdent;
                break;

            case OpKindEnum.FUNCTION:
                str += "<FUNCTION>";
                break;

            case OpKindEnum.SCOPE:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_NOT:
                str += "!";
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_NEG:
                str += "- ";
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_ADD:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " + ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_SUB:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " - ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_MUL:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " * ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_DIV:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " / ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_EQ:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " == ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_LT:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " < ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_LTE:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " <= ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_GT:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " > ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_GTE:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " >= ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_AND:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " & ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_OR:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " | ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_QUERY:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " ? ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_COLON:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " : ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_CONS:
                found = PrintCons(ref str, this, context);
                break;

            case OpKindEnum.O_SEQ:
                found = PrintSeq(ref str, this, context);
                break;

            case OpKindEnum.O_DEFINE:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " = ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_LOOKUP:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += ".";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_LAMBDA:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " -> ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            case OpKindEnum.O_CALL:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                if (Right != null)
                {
                    if (Right.Kind == OpKindEnum.O_CONS)
                    {
                        if (Right != null && Right.Print(ref str, context))
                        {
                            found = true;
                        }
                    }
                    else
                    {
                        str += "(";
                        if (Right != null && Right.Print(ref str, context))
                        {
                            found = true;
                        }
                        str += ")";
                    }
                }
                else
                {
                    str += "()";
                }
                break;

            case OpKindEnum.O_MATCH:
                if (Left != null && Left.Print(ref str, context))
                {
                    found = true;
                }
                str += " =~ ";
                if (Right != null && Right.Print(ref str, context))
                {
                    found = true;
                }
                break;

            default:
                throw new InvalidOperationException();
            }

            if (Kind > OpKindEnum.TERMINALS && (Kind != OpKindEnum.O_CALL && Kind != OpKindEnum.O_DEFINE))
            {
                str += ')';
            }

            if (!String.IsNullOrEmpty(symbol))
            {
                if (CommodityPool.Current.Find(symbol) != null)
                {
                    str += "@";
                }
                str += symbol;
            }

            if (context.EndPos.HasValue && this == context.OpToFind)
            {
                context.EndPos = str.Length - 1;
            }

            return(found);
        }