Exemplo n.º 1
0
        public override void Print(Output output)
        {
            if (m_assign != null)
            {
                var target = m_assign.GetFirstTarget();

                if (target != null)
                {
                    new Assignment(target, GetValue()).Print(output);
                }
                else
                {
                    output.Print("-- unhandled set block");
                    output.PrintLine();
                }
            }
        }
Exemplo n.º 2
0
        public static void PrintSequence(Output output, List <Expression> exprs, bool lineBreak, bool multiple)
        {
            var n = exprs.Count;
            var i = 1;

            foreach (var expr in exprs)
            {
                bool last = (i == n);

                if (expr.IsMultiple)
                {
                    last = true;
                }

                if (last)
                {
                    if (multiple)
                    {
                        expr.PrintMultiple(output);
                    }
                    else
                    {
                        expr.Print(output);
                    }

                    break;
                }
                else
                {
                    expr.Print(output);
                    output.Print(",");

                    if (lineBreak)
                    {
                        output.PrintLine();
                    }
                    else
                    {
                        output.Print(" ");
                    }
                }

                i++;
            }
        }
Exemplo n.º 3
0
        public override void Print(Output output)
        {
            m_entries.Sort();
            m_listLength = 1;

            if (m_entries.Count == 0)
            {
                output.Print("{}");
            }
            else
            {
                var lineBreak = (m_isList && m_entries.Count > 5) ||
                                (m_isObject && m_entries.Count > 2) ||
                                (!m_isObject);

                if (!lineBreak)
                {
                    foreach (var entry in m_entries)
                    {
                        var value = entry.Value;

                        if (!value.IsBrief)
                        {
                            lineBreak = true;
                            break;
                        }
                    }
                }

                output.Print("{");

                if (lineBreak)
                {
                    output.PrintLine();
                    output.IncreaseIndent();
                }

                PrintEntry(0, output);

                if (!m_entries[0].Value.IsMultiple)
                {
                    for (int i = 1; i < m_entries.Count; i++)
                    {
                        output.Print(",");

                        if (lineBreak)
                        {
                            output.PrintLine();
                        }
                        else
                        {
                            output.Print(" ");
                        }

                        PrintEntry(i, output);

                        if (m_entries[i].Value.IsMultiple)
                        {
                            break;
                        }
                    }
                }

                if (lineBreak)
                {
                    output.PrintLine();
                    output.DecreaseIndent();
                }

                output.Print("}");
            }
        }
Exemplo n.º 4
0
        public void Print(Output output)
        {
            switch (m_type)
            {
            case CONST_NIL:
                output.Print("nil");
                break;

            case CONST_BOOL:
                output.Print(m_bool ? "true" : "false");
                break;

            case CONST_NUMBER:
                output.Print(m_number.ToString());
                break;

            case CONST_STRING:
            {
                var newLines     = 0;
                var unprinttable = 0;

                foreach (char c in m_string)
                {
                    if (c == '\n')
                    {
                        newLines++;
                    }
                    else if ((c <= 31 && c != '\t' || c >= 127))
                    {
                        unprinttable++;
                    }
                }

                if (unprinttable == 0 && !m_string.Contains("[[") &&
                    (newLines > 1 || newLines == 1 && m_string.IndexOf('\n') != m_string.Length - 1))
                {
                    var pipe       = 0;
                    var pipeString = "]]";

                    while (m_string.IndexOf(pipeString) >= 0)
                    {
                        pipe++;
                        pipeString = "]";

                        var i = pipe;

                        while (i-- > 0)
                        {
                            pipeString += "=";
                        }

                        pipeString += "]";
                    }

                    output.Print("[");

                    while (pipe-- > 0)
                    {
                        output.Print("=");
                    }

                    output.Print("[");

                    var indent = output.IndentationLevel;

                    output.IndentationLevel = 0;

                    output.PrintLine();
                    output.Print(m_string);
                    output.Print(pipeString);

                    output.IndentationLevel = indent;
                }
                else
                {
                    output.Print("\"");

                    var chars = new[] {
                        "\\a",
                        "\\b",
                        "\\t",
                        "\\n",
                        "\\v",
                        "\\f",
                        "\\r",
                    };

                    foreach (char c in m_string)
                    {
                        if (c <= 31 || c >= 127)
                        {
                            //if (c == 7)
                            //    output.Print("\\a");
                            //else if (c == 8)
                            //    output.Print("\\b");
                            //else if (c == 12)
                            //    output.Print("\\f");
                            //else if (c == 10)
                            //    output.Print("\\n");
                            //else if (c == 13)
                            //    output.Print("\\r");
                            //else if (c == 9)
                            //    output.Print("\\t");
                            //else if (c == 11)
                            //    output.Print("\\v");

                            var cx = ((int)c);

                            if (cx >= 7 && cx <= 13)
                            {
                                output.Print(chars[cx - 7]);
                            }
                            else
                            {
                                var dec = cx.ToString();
                                var len = dec.Length;

                                output.Print("\\");

                                while (len++ < 3)
                                {
                                    output.Print("0");
                                }

                                output.Print(dec);
                            }
                        }
                        else if (c == 34)
                        {
                            output.Print("\\\"");
                        }
                        else if (c == 92)
                        {
                            output.Print("\\\\");
                        }
                        else
                        {
                            output.Print(c.ToString());
                        }
                    }

                    output.Print("\"");
                }
            } break;

            default:
                throw new InvalidOperationException();
            }
        }