protected override Expression VisitBlock(BlockExpression block)
            {
                _blocks.Push(block);

                var result = base.VisitBlock(block);

                _blocks.Pop();

                return(result);
            }
Exemple #2
0
        private static IEnumerable <string> GetBlockStatements(
            BlockExpression block,
            TranslationContext context)
        {
            var lines = GetBlockLines(block, context);

            var finalLineIndex = lines.Count - 1;

            for (var i = 0; i < lines.Count; i++)
            {
                var line           = lines[i];
                var isNotFirstLine = (i > 0);
                var previousLine   = isNotFirstLine ? lines[i - 1] : null;

                if (isNotFirstLine && LeaveBlankLineBefore(line, previousLine))
                {
                    yield return(string.Empty);
                }

                if (i != finalLineIndex)
                {
                    yield return(line);

                    if (LeaveBlankLineAfter(line, lines[i + 1]))
                    {
                        yield return(string.Empty);
                    }

                    continue;
                }

                if (DoNotAddReturnStatement(block, lines))
                {
                    yield return(line);

                    yield break;
                }

                if (isNotFirstLine && LeaveBlankLineBeforeReturn(line, previousLine))
                {
                    yield return(string.Empty);
                }

                if (CodeBlock.IsSingleStatement(line.SplitToLines()))
                {
                    yield return("return " + line);

                    yield break;
                }

                yield return(CodeBlock.InsertReturnKeyword(line));
            }
        }
Exemple #3
0
 private static IList <string> GetBlockLines(BlockExpression block, TranslationContext context)
 {
     return(block
            .Expressions
            .Filter(exp => (exp == block.Result) || Include(exp))
            .Project(exp => new
     {
         Expression = exp,
         Translation = GetTerminatedStatementOrNull(exp, context)
     })
            .Filter(d => d.Translation != null)
            .Project(d => d.Translation)
            .ToArray());
 }
Exemple #4
0
 private static IList <string> GetVariableDeclarations(
     BlockExpression block,
     TranslationContext context)
 {
     return(block
            .Variables
            .Except(context.JoinedAssignmentVariables)
            .GroupBy(v => v.Type)
            .Project(vGrp => new
     {
         TypeName = vGrp.Key.GetFriendlyName(),
         VariableNames = vGrp.Project(variable => ParameterExpressionTranslator.Translate(variable, context))
     })
            .Project(varData => $"{varData.TypeName} {varData.VariableNames.Join(", ")};")
            .ToArray());
 }
        private CodeBlock TranslateBlock(BlockExpression block)
        {
            if (block == null)
            {
                return(null);
            }

            if (block.Expressions.Count == 1)
            {
                return(TranslateSingle(block));
            }

            var blockString = Translate(block);
            var blockLines  = blockString.SplitToLines();

            return(new CodeBlock(block, blockLines));
        }
Exemple #6
0
 public static bool IsReturnable(this BlockExpression block)
 {
     return((block.Type != typeof(void)) && block.Result.IsReturnable());
 }
Exemple #7
0
 private static bool DoNotAddReturnStatement(BlockExpression block, ICollection <string> lines)
 {
     return((lines.Count <= 1) || !block.IsReturnable());
 }