示例#1
0
        public bool PrintSeq(ref string str, ExprOp op, ExprOpContext context)
        {
            bool found = false;

            if (op.Left == null)
            {
                throw new InvalidOperationException();
            }

            if (op.Left.Print(ref str, context))
            {
                found = true;
            }

            if (op.Right != null)
            {
                str += "; ";
                if (op.Right.Kind == OpKindEnum.O_SEQ)
                {
                    found = PrintSeq(ref str, op.Right, context);
                }
                else if (op.Right.Print(ref str, context))
                {
                    found = true;
                }
            }
            return(found);
        }
示例#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);
        }