Exemple #1
0
        protected override void BuildBlock(CodeBlock block)
        {
            if (expressions == null || expressions.Count() == 0)
            {
                if (args != null)
                {
                    block.Append($"new {type}({args})");
                }
                else
                {
                    block.Append($"new {type}()");
                }

                return;
            }

            if (args != null)
            {
                block.Append($"new {type}({args})");
            }
            else
            {
                block.Append($"new {type}");
            }

            OutputExpressions(block);
        }
Exemple #2
0
        protected override void BuildBlock(CodeBlock block)
        {
            base.BuildBlock(block);

            string _comment = string.Empty;

            if (Comment != null)
            {
                _comment = Comment.ToString();

                if (Comment.Alignment == Alignment.Top)
                {
                    block.Append(_comment);
                    _comment = string.Empty;
                }
            }

            if (UserValue != null)
            {
                block.AppendLine($"{Signature} = {UserValue};{_comment}");
            }
            else if (value != null)
            {
                block.AppendLine();
                block.Append($"{Signature} = ");
                block.Append(value);
                block.Append(";");
            }
            else
            {
                block.AppendLine($"{Signature};{_comment}");
            }
        }
Exemple #3
0
        internal void BuildCode(CodeBlock block)
        {
            if (value is Value)
            {
                (value as Value).BuildCode(block);
            }
            else if (value is Array)                        // new Foo[] { new Foo {...}, new Foo {...}, ...}
            {
                var A = value as Array;
                if (Type == TypeInfo.Anonymous)
                {
                    Type = new TypeInfo {
                        Type = A.GetType()
                    }
                }
                ;

                block.Append($"new {Type}");
                WriteArrayValue(block, A, 10);
            }
            else if (value is Dictionary <string, Value> )    // new Foo { A = 1, B = true }
            {
                block.Append($"new {Type}");
                WriteDictionary(block, value as Dictionary <string, Value>);
            }
            else if (value is Dictionary <object, object> )   // new Dictionary<T1,T2> { [t1] = new T2 {...}, ... }
            {
                block.Append($"new {Type}");
                WriteDictionary(block, value as Dictionary <object, object>);
            }
            else
            {
                block.Append(ToPrimitive(value));
            }
        }
Exemple #4
0
        private void Lambda(CodeBlock block, string opr, Statement statement)
        {
            if (opr.EndsWith("get"))
            {
                if (statement.Count == 1)
                {
                    block.Append($" => {statement}");
                    return;
                }

                throw new Exception("cannot generate complicated get lambda expression");
            }

            block.Append($"{opr} => ").AddWithBeginEnd(statement);
        }
Exemple #5
0
        protected override void BuildBlock(CodeBlock block)
        {
            if (IsExpressionBodied)
            {
                block.Append(signature);

                //print in next line
                if (NextLine)
                {
                    block.AppendLine();
                }

                block.Append($"=> {Body}");

                return;
            }

            base.BuildBlock(block);
        }
Exemple #6
0
        private void WriteDictionary(CodeBlock block, Dictionary <string, Value> A)
        {
            switch (format)
            {
            case ValueOutputFormat.SingleLine:
                block.Append("{");
                A.ForEach(
                    kvp =>
                {
                    block.Append($"{kvp.Key} = ");
                    kvp.Value.BuildCode(block);
                },
                    _ => block.Append(",")
                    );

                block.Append("}");
                break;

            default:
                block.Begin();

                A.ForEach(
                    kvp =>
                {
                    block.AppendLine();
                    block.Append($"{kvp.Key} = ");
                    kvp.Value.BuildCode(block);
                },
                    _ => block.Append(",")
                    );

                block.End();
                break;
            }
        }
Exemple #7
0
        private void OutputExpressions(CodeBlock block)
        {
            switch (Format)
            {
            case ValueOutputFormat.SingleLine:
                block.Append(" { ");
                expressions.ForEach(
                    expr =>
                {
                    block.Append(expr);
                },
                    _ => block.Append(", ")
                    );

                block.Append(" }");
                break;

            default:
                block.Begin();
                expressions.ForEach(
                    expr =>
                {
                    block.AppendLine();
                    block.Append(expr);
                },
                    _ => block.Append(",")
                    );

                block.End();
                break;
            }
        }
Exemple #8
0
        private void WriteDictionary(CodeBlock block, Dictionary <object, object> dict)
        {
            switch (Format)
            {
            case ValueOutputFormat.SingleLine:
                block.Append("{");
                dict.ForEach(
                    kvp =>
                {
                    block.Append($"[{kvp.Key}] = ");
                    NewValue(kvp.Value).BuildBlock(block);
                },
                    _ => block.Append(",")
                    );

                block.Append("}");
                break;

            default:
                block.Begin();

                dict.ForEach(
                    kvp =>
                {
                    block.AppendLine();
                    block.Append($"[{kvp.Key}] = ");
                    NewValue(kvp.Value).BuildBlock(block);
                },
                    _ => block.Append(",")
                    );

                block.End();
                break;
            }
        }
Exemple #9
0
        protected override void BuildBlock(CodeBlock block)
        {
            base.BuildBlock(block);

            switch (value)
            {
            case Value x:
                x.BuildBlock(block);
                break;

            case New instance:
                block.Add(instance);
                break;

            case Array A:
                if (Type == TypeInfo.Anonymous)
                {
                    Type = new TypeInfo {
                        Type = A.GetType()
                    }
                }
                ;
                block.Append($"new {Type}");
                WriteArrayValue(block, A, 10);
                break;

            case Dictionary <object, object> dict:
                block.Append($"new {Type}");
                WriteDictionary(block, dict);
                break;

            default:
                block.Append(Primitive.ToPrimitive(value));
                break;
            }
        }
Exemple #10
0
        public Expression(TypeInfo type, Arguments args, IEnumerable <Expression> expressions)
        {
            CodeBlock codeBlock = new CodeBlock();

            if (expressions == null || expressions.Count() == 0)
            {
                if (args != null)
                {
                    codeBlock.Append($"new {type}({args})");
                }
                else
                {
                    codeBlock.Append($"new {type}()");
                }

                expr = codeBlock.ToString();
                return;
            }

            if (args != null)
            {
                codeBlock.Append($"new {type}({args})");
            }
            else
            {
                codeBlock.Append($"new {type}");
            }

            codeBlock.Append(" { ");
            expressions.ForEach(
                assign => codeBlock.Append($"{assign}"),
                assign => codeBlock.Append(", ")
                );
            codeBlock.Append(" }");

            expr = codeBlock.ToString();
        }
Exemple #11
0
        private void WriteArrayValue(CodeBlock block, Array A, int columnNumber)
        {
            Type ty = Type.GetElementType();

            if (ty != null && ty.IsPrimitive)
            {
                if (A.Length < 30)
                {
                    format = ValueOutputFormat.SingleLine;
                }
                else if (A.Length < 100)
                {
                    format       = ValueOutputFormat.Wrap;
                    columnNumber = 10;
                }
                else
                {
                    format       = ValueOutputFormat.Wrap;
                    columnNumber = 20;
                }
            }

            switch (format)
            {
            case ValueOutputFormat.SingleLine:
                block.Append("{");
                A.OfType <object>().ForEach(
                    x =>
                {
                    NewValue(x).BuildCode(block);
                },
                    _ => block.Append(",")
                    );

                block.Append("}");
                break;



            case ValueOutputFormat.Wrap:
                block.Begin();
                for (int i = 0; i < A.Length; i++)
                {
                    if (i % columnNumber == 0)
                    {
                        block.AppendLine();
                    }

                    if (i != 0 && i % (columnNumber * 10) == 0)         //add empty line every 10 lines
                    {
                        block.AppendLine();
                    }

                    Value item = NewValue(A.GetValue(i));
                    item.BuildCode(block);

                    if (i != A.Length - 1)
                    {
                        block.Append(",");
                    }
                }

                block.End();
                break;

            default:
                block.Begin();

                for (int i = 0; i < A.Length; i++)
                {
                    if (i != 0 && i % columnNumber == 0)
                    {
                        block.AppendLine();
                    }

                    block.AppendLine();
                    Value item = NewValue(A.GetValue(i));
                    item.BuildCode(block);

                    if (i != A.Length - 1)
                    {
                        block.Append(",");
                    }
                }

                block.End();
                break;
            }
        }