Beispiel #1
0
        private bool AutoPrintBracesOrBracks(ILNode node, Precedence context)
        {
            var name = node.Name;

            if (name == S.Array && node.IsCall() && !HasPAttrs(node.Target))
            {
                PrintArgList(node.Args(), node.BaseStyle() == NodeStyle.StatementBlock, "[", ']', null, node.Target);
                return(true);
            }
            else if (name == S.Tuple && node.ArgCount() != 1 && node.IsCall() && !HasPAttrs(node.Target))
            {
                PrintArgList(node.Args(), node.BaseStyle() == NodeStyle.StatementBlock, "(", ')', "; ", node.Target);
                return(true);
            }
            else if (name == S.Braces && node.IsCall() && !HasPAttrs(node.Target))
            {
                if (context.Lo == StartStmt.Lo)
                {
                    _out.Dedent(PrinterIndentHint.Subexpression).Indent(PrinterIndentHint.NoIndent);
                }
                PrintArgList(node.Args(), node.BaseStyle() != NodeStyle.Expression, "{", '}', null, node.Target);
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        private bool AutoPrintBracesOrBracks(ILNode node)
        {
            var name = node.Name;

            if ((name == S.Array || name == S.Braces) && node.IsCall() && !HasPAttrs(node.Target))
            {
                if (name == S.Array)
                {
                    PrintArgList(node.Args(), node.BaseStyle() == NodeStyle.Statement, "[", ']', node.Target);
                    return(true);
                }
                else if (name == S.Braces)
                {
                    PrintArgList(node.Args(), node.BaseStyle() != NodeStyle.Expression, "{", '}', node.Target);
                    return(true);
                }
            }
            return(false);
        }
Beispiel #3
0
        void PrintPrefixNotation(ILNode node, Precedence context)
        {
            switch (node.Kind)
            {
            case LNodeKind.Id:
                PrintIdOrSymbol(node.Name, false); break;

            case LNodeKind.Literal:
                PrintLiteral(node); break;

            case LNodeKind.Call:
            default:
                Print(node.Target, LesPrecedence.Primary.LeftContext(context), "(");
                PrintArgList(node.Args(), node.BaseStyle() == NodeStyle.Statement, "", ')', null);
                break;
            }
        }
Beispiel #4
0
        internal void Print(ILNode node, Precedence context, string terminator = null)
        {
            bool old_isOneLiner = _isOneLiner;

            _isOneLiner |= (node.Style & NodeStyle.OneLiner) != 0;
            try {
                int parenCount = WriteAttrs(node, ref context);

                if (!node.IsCall() || node.BaseStyle() == NodeStyle.PrefixNotation)
                {
                    PrintPrefixNotation(node, context);
                }
                else
                {
                    do
                    {
                        if (AutoPrintBracesOrBracks(node))
                        {
                            break;
                        }
                        if (!LesPrecedence.Primary.CanAppearIn(context))
                        {
                            _out.Write("(@[] ", true);
                            parenCount++;
                            context = StartStmt;
                        }
                        int args = node.ArgCount();
                        if (args == 1 && AutoPrintPrefixOrSuffixOp(node, context))
                        {
                            break;
                        }
                        if (args == 2 && AutoPrintInfixOp(node, context))
                        {
                            break;
                        }
                        PrintPrefixNotation(node, context);
                    } while (false);
                }

                PrintSuffixTrivia(node, parenCount, terminator);
            } finally {
                _isOneLiner = old_isOneLiner;
            }
        }
Beispiel #5
0
        /// <summary>Top-level non-static printing method</summary>
        internal void Print(ILNode node, Precedence context, string terminator = null)
        {
            _out.BeginNode(node);
            int parenCount = WriteAttrs(node, ref context);

            _out.FlushIndent().Indent(PrinterIndentHint.Subexpression);

            if (!node.IsCall() || node.BaseStyle() == NodeStyle.PrefixNotation)
            {
                PrintPrefixNotation(node, context);
            }
            else
            {
                do
                {
                    if (AutoPrintBracesOrBracks(node, context))
                    {
                        break;
                    }
                    if (!LesPrecedence.Primary.CanAppearIn(context))
                    {
                        _out.WriteOpening("(@[] ");
                        parenCount++;
                        context = StartSubexpr;
                    }
                    int args = node.ArgCount();
                    if (args == 1 && AutoPrintPrefixOrSuffixOp(node, context))
                    {
                        break;
                    }
                    if (args == 2 && AutoPrintInfixOp(node, context))
                    {
                        break;
                    }
                    PrintPrefixNotation(node, context);
                } while (false);
            }

            _out.Dedent();
            PrintSuffixTrivia(node, parenCount, terminator);
            _out.EndNode();
        }
Beispiel #6
0
        private Precedence?GetPrecedenceIfOperator(ILNode node, Symbol opName, OperatorShape shape, Precedence context)
        {
            int ac = node.ArgCount();

            if ((ac == (int)shape || ac == -(int)shape) && HasTargetIdWithoutPAttrs(node))
            {
                var  bs        = node.BaseStyle();
                bool naturalOp = Les2PrecedenceMap.IsNaturalOperator(opName.Name);
                if ((naturalOp && bs != NodeStyle.PrefixNotation) ||
                    (bs == NodeStyle.Operator && node.Name != null))
                {
                    var result = _prec.Find(shape, opName);
                    if (!result.CanAppearIn(context) || !result.CanMixWith(context))
                    {
                        return(null);
                    }
                    return(result);
                }
            }
            return(null);
        }