Пример #1
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("::");
     writer.Write(Name);
     writer.Write("::");
     writer.WriteLine();
 }
Пример #2
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("(");
     WriteUnaryOp(Type, writer);
     (Expression as Node).Write(writer);
     writer.Write(")");
 }
Пример #3
0
        public override void Write(IndentAwareTextWriter writer)
        {
            if (ForceTruncateReturnValues)
            {
                writer.Write("(");
            }

            if (Function is TableAccess && Arguments.Count > 0)
            {
                var tf = ((TableAccess)Function);

                if (tf.Table == Arguments[0] && tf.Index is StringLiteral && ((StringLiteral)tf.Index).Value.IsIdentifier())
                {
                    WriteMethodStyle(writer, tf.Table, ((StringLiteral)tf.Index).Value);
                }
                else
                {
                    WriteGenericStyle(writer);
                }
            }
            else
            {
                WriteGenericStyle(writer);
            }

            if (ForceTruncateReturnValues)
            {
                writer.Write(")");
            }
        }
Пример #4
0
        public void Write(IndentAwareTextWriter writer, bool alone)
        {
            if (TopLevel && alone)
            {
                alone = false;
            }

            if (alone)
            {
                writer.Write("do");
                writer.IncreaseIndent();
                writer.WriteLine();
            }
            for (var i = 0; i < Statements.Count; i++)
            {
                var stat = Statements[i];

                stat.Write(writer);
                //if (writer.ForceOneLine && stat.AmbiguousTermination && i != Statements.Count - 1) {
                //    writer.Write(";");
                //}
                if (i < Statements.Count - 1)
                {
                    writer.WriteLine();
                }
            }
            if (alone)
            {
                writer.DecreaseIndent();
                writer.WriteLine();
                writer.Write("end");
            }
        }
Пример #5
0
 public void WriteGenericStyle(IndentAwareTextWriter writer)
 {
     writer.Write("[");
     Key.Write(writer);
     writer.Write("]");
     writer.Write(" = ");
     Value.Write(writer);
 }
Пример #6
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("(");
     (Left as Node).Write(writer);
     writer.Write(" ");
     WriteBinaryOp(Type, writer);
     writer.Write(" ");
     (Right as Node).Write(writer);
     writer.Write(")");
 }
Пример #7
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("repeat");
     writer.IncreaseIndent();
     writer.WriteLine();
     Block.Write(writer, false);
     writer.DecreaseIndent();
     writer.WriteLine();
     writer.Write("until ");
     Condition.Write(writer);
 }
Пример #8
0
        public static void WriteUnaryOp(OpType type, IndentAwareTextWriter writer)
        {
            switch (type)
            {
            case OpType.Negate: writer.Write("-"); break;

            case OpType.Invert: writer.Write("not "); break;

            case OpType.Length: writer.Write("#"); break;
            }
        }
Пример #9
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("while ");
     Condition.Write(writer);
     writer.Write(" do");
     writer.IncreaseIndent();
     writer.WriteLine();
     Block.Write(writer, false);
     writer.DecreaseIndent();
     writer.WriteLine();
     writer.Write("end");
 }
Пример #10
0
 public override void Write(IndentAwareTextWriter writer)
 {
     if (HexFormat)
     {
         writer.Write("0x");
         writer.Write(((long)Value).ToString("X"));
     }
     else
     {
         writer.Write(Value);
     }
 }
Пример #11
0
 public override void Write(IndentAwareTextWriter writer)
 {
     if (HexFormat)
     {
         writer.Write("0x");
         writer.Write(Value.ToString("X4"));
     }
     else
     {
         writer.Write(Value);
     }
     writer.Write("LL");
 }
Пример #12
0
 public void WriteDotStyle(IndentAwareTextWriter writer, string index)
 {
     if (Table is StringLiteral)
     {
         writer.Write("(");
     }
     Table.Write(writer);
     if (Table is StringLiteral)
     {
         writer.Write(")");
     }
     writer.Write(".");
     writer.Write(index);
 }
Пример #13
0
 public void WriteGenericStyle(IndentAwareTextWriter writer)
 {
     if (Table is StringLiteral)
     {
         writer.Write("(");
     }
     Table.Write(writer);
     if (Table is StringLiteral)
     {
         writer.Write(")");
     }
     writer.Write("[");
     Index.Write(writer);
     writer.Write("]");
 }
Пример #14
0
        public override void Write(IndentAwareTextWriter writer)
        {
            writer.Write("for ");
            for (var i = 0; i < VariableNames.Count; i++)
            {
                writer.Write(VariableNames[i]);
                if (i < VariableNames.Count - 1)
                {
                    writer.Write(", ");
                }
            }
            writer.Write(" in ");

            for (int i = 0; i < Iterator.Count; i++)
            {
                Iterator[i].Write(writer);
                if (i != Iterator.Count - 1)
                {
                    writer.Write(", ");
                }
            }

            writer.Write(" do");
            writer.IncreaseIndent();
            writer.WriteLine();
            Block.Write(writer, false);
            writer.DecreaseIndent();
            writer.WriteLine();
            writer.Write("end");
        }
Пример #15
0
        public override void Write(IndentAwareTextWriter writer)
        {
            writer.Write("return");
            if (Expressions.Count > 0)
            {
                writer.Write(" ");
            }
            for (var i = 0; i < Expressions.Count; i++)
            {
                var expr = Expressions[i];

                expr.Write(writer);
                if (i < Expressions.Count - 1)
                {
                    writer.Write(", ");
                }
            }
        }
Пример #16
0
        public void Write(IndentAwareTextWriter writer, bool from_named)
        {
            if (!from_named)
            {
                writer.Write("function");
            }
            writer.Write("(");

            var arg_start_idx = 0;

            if (ImplicitSelf && from_named)
            {
                arg_start_idx += 1;
            }
            // Skips the self for method defs

            for (var i = arg_start_idx; i < ArgumentNames.Count; i++)
            {
                var arg = ArgumentNames[i];
                writer.Write(arg);
                if (i < ArgumentNames.Count - 1)
                {
                    writer.Write(", ");
                }
            }
            if (AcceptsVarargs)
            {
                if (ArgumentNames.Count > 0)
                {
                    writer.Write(", ");
                }
                writer.Write("...");
            }
            writer.Write(")");
            if (Block.IsEmpty)
            {
                writer.Write(" ");
            }
            else
            {
                writer.IncreaseIndent();
                writer.WriteLine();
                Block.Write(writer, false);
                writer.DecreaseIndent();
                writer.WriteLine();
            }
            writer.Write("end");
        }
Пример #17
0
 public override void Write(IndentAwareTextWriter writer)
 {
     MainIf.Write(writer);
     for (var i = 0; i < ElseIfs.Count; i++)
     {
         writer.Write("else");
         ElseIfs[i].Write(writer);
     }
     if (Else != null)
     {
         writer.Write("else");
         writer.IncreaseIndent();
         writer.WriteLine();
         Else.Write(writer, false);
         writer.DecreaseIndent();
         writer.WriteLine();
     }
     writer.Write("end");
 }
Пример #18
0
 public void WriteGenericStyle(IndentAwareTextWriter writer)
 {
     if (Function is FunctionDefinition)
     {
         writer.Write("(");
     }
     Function.Write(writer);
     if (Function is FunctionDefinition)
     {
         writer.Write(")");
     }
     writer.Write("(");
     for (var i = 0; i < Arguments.Count; i++)
     {
         Arguments[i].Write(writer);
         if (i < Arguments.Count - 1)
         {
             writer.Write(", ");
         }
     }
     writer.Write(")");
 }
Пример #19
0
        public void WriteGenericStyle(IndentAwareTextWriter writer)
        {
            // note: local declaration is never named function style (because
            // that automatically implies there's a value assigned)

            for (var i = 0; i < Targets.Count; i++)
            {
                var target = Targets[i] as Node;
                target.Write(writer);
                if (i != Targets.Count - 1)
                {
                    writer.Write(", ");
                }
            }

            if (IsLocalDeclaration)
            {
                return;
            }

            writer.Write(" = ");
            for (var i = 0; i < Values.Count; i++)
            {
                var value = Values[i] as Node;
                value.Write(writer);
                if (i != Values.Count - 1)
                {
                    writer.Write(", ");
                }
            }

            if (ForceExplicitLocalNil && Values.Count < Targets.Count)
            {
                // match with nils if ForceExplicitLocalNil is set
                if (Values.Count > 0)
                {
                    writer.Write(", ");
                }
                var fill_in_count = (Targets.Count - Values.Count);
                for (var i = 0; i < fill_in_count; i++)
                {
                    writer.Write("nil");
                    if (i < fill_in_count - 1)
                    {
                        writer.Write(", ");
                    }
                }
            }
        }
Пример #20
0
        public override void Write(IndentAwareTextWriter writer)
        {
            if (IsLocal)
            {
                writer.Write("local ");
            }

            if (Targets.Count == 1 && Values.Count == 1 && Values[0] is FunctionDefinition)
            {
                string funcname = null;

                if (Targets[0] is Variable && ((Variable)Targets[0]).Name.IsIdentifier())
                {
                    funcname = ((Variable)Targets[0]).Name;
                    WriteNamedFunctionStyle(writer, funcname, Values[0] as FunctionDefinition);
                }
                else if (Targets[0] is TableAccess)
                {
                    var func = Values[0] as FunctionDefinition;
                    funcname = ((TableAccess)Targets[0]).GetIdentifierAccessChain(is_method_access: func.ImplicitSelf);
                    if (funcname != null)
                    {
                        WriteNamedFunctionStyle(writer, funcname, func);
                    }
                    else
                    {
                        WriteGenericStyle(writer);
                    }
                }
                else
                {
                    WriteGenericStyle(writer);
                }
            }
            else
            {
                WriteGenericStyle(writer);
            }
        }
Пример #21
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("for ");
     writer.Write(VariableName);
     writer.Write(" = ");
     StartPoint.Write(writer);
     writer.Write(", ");
     EndPoint.Write(writer);
     if (Step != null && !(Step is NumberLiteral && ((NumberLiteral)Step).Value == 1))
     {
         writer.Write(", ");
         Step.Write(writer);
     }
     writer.Write(" do");
     writer.IncreaseIndent();
     writer.WriteLine();
     Block.Write(writer, false);
     writer.DecreaseIndent();
     writer.WriteLine();
     writer.Write("end");
 }
Пример #22
0
        public override void Write(IndentAwareTextWriter writer)
        {
            if (Entries.Count == 0)
            {
                writer.Write("{}");
                return;
            }

            if (Entries.Count == 1)
            {
                writer.Write("{ ");
                var ent = Entries[0];
                ent.Write(writer, skip_key: ent.Key is NumberLiteral && ((NumberLiteral)ent.Key).Value == 1 && !ent.ExplicitKey);
                writer.Write(" }");
                return;
            }

            var seq_idx = 1;

            writer.Write("{");
            writer.IncreaseIndent();
            for (var i = 0; i < Entries.Count; i++)
            {
                writer.WriteLine();

                var ent = Entries[i];

                var is_sequential = false;
                if (ent.Key is NumberLiteral && ((NumberLiteral)ent.Key).Value == seq_idx && !ent.ExplicitKey)
                {
                    is_sequential = true;
                    seq_idx      += 1;
                }

                Entries[i].Write(writer, skip_key: is_sequential);
                if (i < Entries.Count - 1)
                {
                    writer.Write(",");
                }
            }
            writer.DecreaseIndent();
            writer.WriteLine();
            writer.Write("}");
        }
Пример #23
0
 public void WriteMethodStyle(IndentAwareTextWriter writer, IExpression obj, string method_name)
 {
     if (obj is FunctionDefinition)
     {
         writer.Write("(");
     }
     obj.Write(writer);
     if (obj is FunctionDefinition)
     {
         writer.Write(")");
     }
     writer.Write(":");
     writer.Write(method_name);
     writer.Write("(");
     for (var i = 1; i < Arguments.Count; i++)
     {
         Arguments[i].Write(writer);
         if (i < Arguments.Count - 1)
         {
             writer.Write(", ");
         }
     }
     writer.Write(")");
 }
Пример #24
0
        public static void WriteBinaryOp(OpType type, IndentAwareTextWriter writer)
        {
            switch (type)
            {
            case OpType.Add: writer.Write("+"); break;

            case OpType.Subtract: writer.Write("-"); break;

            case OpType.Multiply: writer.Write("*"); break;

            case OpType.Divide: writer.Write("/"); break;

            case OpType.Power: writer.Write("^"); break;

            case OpType.Modulo: writer.Write("%"); break;

            case OpType.Concat: writer.Write(".."); break;

            case OpType.GreaterThan: writer.Write(">"); break;

            case OpType.GreaterOrEqual: writer.Write(">="); break;

            case OpType.LessThan: writer.Write("<"); break;

            case OpType.LessOrEqual: writer.Write("<="); break;

            case OpType.Equal: writer.Write("=="); break;

            case OpType.NotEqual: writer.Write("~="); break;

            case OpType.And: writer.Write("and"); break;

            case OpType.Or: writer.Write("or"); break;
            }
        }
Пример #25
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write(Value ? "true" : "false");
 }
Пример #26
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("break");
 }
Пример #27
0
 public override void Write(IndentAwareTextWriter writer)
 {
     writer.Write("goto ");
     writer.Write(GoToLabel);
 }
Пример #28
0
 public void WriteIdentifierStyle(IndentAwareTextWriter writer, string index)
 {
     writer.Write(index);
     writer.Write(" = ");
     Value.Write(writer);
 }
Пример #29
0
 public void WriteNamedFunctionStyle(IndentAwareTextWriter writer, string name, FunctionDefinition func)
 {
     writer.Write("function ");
     writer.Write(name);
     func.Write(writer, from_named: true);
 }
Пример #30
0
 public static void Quote(IndentAwareTextWriter s, string str)
 {
     s.Write('"');
     for (var i = 0; i < str.Length; i++)
     {
         var c = str[i];
         if (c == '\n')
         {
             s.Write("\\n");
         }
         else if (c == '\t')
         {
             s.Write("\\t");
         }
         else if (c == '\r')
         {
             s.Write("\\r");
         }
         else if (c == '\a')
         {
             s.Write("\\a");
         }
         else if (c == '\b')
         {
             s.Write("\\b");
         }
         else if (c == '\f')
         {
             s.Write("\\f");
         }
         else if (c == '\v')
         {
             s.Write("\\v");
         }
         else if (c == '\\')
         {
             s.Write("\\\\");
         }
         else if (c == '"')
         {
             s.Write("\\\"");
         }
         else if (!c.IsASCIIPrintable())
         {
             s.Write($"\\{((int)c).ToString("D3")}");
         }
         else
         {
             s.Write(c);
         }
     }
     s.Write('"');
 }