Ejemplo n.º 1
0
        public static void WriteTo(this BoundNode node, IndentedTextWriter writer)
        {
            switch (node.Kind)
            {
            case BoundNodeKind.BlockStatement:
                WriteBlockStatement((BoundBlockStatemnet)node, writer);
                break;

            case BoundNodeKind.VariableDeclaration:
                WriteVariableDeclaration((BoundVeriableDeclaration)node, writer);
                break;

            case BoundNodeKind.IfStatement:
                WriteIfStatement((BoundIfStatement)node, writer);
                break;

            case BoundNodeKind.WhileStatement:
                WriteWhileStatement((BoundWhileStatement)node, writer);
                break;

            // case BoundNodeKind.DoWhileStatement:
            //     WriteDoWhileStatement((BoundDoWhileStatement)node, writer);
            // break;
            case BoundNodeKind.ForStatement:
                WriteForStatement((BoundForStatement)node, writer);
                break;

            case BoundNodeKind.LabelStatement:
                WriteLabelStatement((BoundLabelStatement)node, writer);
                break;

            case BoundNodeKind.GotoStatment:
                WriteGotoStatement((BoundGotoStatment)node, writer);
                break;

            case BoundNodeKind.ConditionalGotoStatment:
                WriteConditionalGotoStatement((BoundConditionalGotoStatment)node, writer);
                break;

            case BoundNodeKind.ExpressionStatement:
                WriteExpressionStatement((BoundExpressionStatemnet)node, writer);
                break;

            case BoundNodeKind.ErrorExpression:
                WriteErrorExpression((BoundErrorExpression)node, writer);
                break;

            case BoundNodeKind.LiteralExpression:
                WriteLiteralExpression((BoundLiteralExpression)node, writer);
                break;

            case BoundNodeKind.VariableExpression:
                WriteVariableExpression((BoundVariableExpression)node, writer);
                break;

            case BoundNodeKind.AssignmentExpression:
                WriteAssignmentExpression((BoundAssignmentExpression)node, writer);
                break;

            case BoundNodeKind.UnaryExpression:
                WriteUnaryExpression((BoundUnaryExpression)node, writer);
                break;

            case BoundNodeKind.BinaryExpression:
                WriteBinaryExpression((BoundBinaryExpression)node, writer);
                break;

            case BoundNodeKind.CallExpression:
                WriteCallExpression((BoundCallExpression)node, writer);
                break;

            case BoundNodeKind.ConversionExpression:
                WriteConversionExpression((BoundConversionExpression)node, writer);
                break;

            case BoundNodeKind.ReturnStatement:
                WriteReturnStatement((BoundReturnStatement)node, writer);
                break;

            default:
                throw new Exception($"Unexpected node {node.Kind}");
            }
        }
Ejemplo n.º 2
0
        private static void PrettyPrint(TextWriter writer, BoundNode node, string indent = "", bool isLast = true)
        {
            var isToConsole = writer == Console.Out;

            var marker = isLast ? "└──" : "├──";

            if (isToConsole)
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
            }

            writer.Write(indent);
            writer.Write(marker);

            if (isToConsole)
            {
                Console.ForegroundColor = GetColor(node);
            }

            var text = GetText(node);

            writer.Write(text);

            var isFirstProperty = true;

            foreach (var p in node.GetProperties())
            {
                if (isFirstProperty)
                {
                    isFirstProperty = false;
                }
                else
                {
                    if (isToConsole)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                    }
                    writer.Write(",");
                }
                writer.Write(" ");
                if (isToConsole)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }
                writer.Write(p.Name);

                if (isToConsole)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                }
                writer.Write(" = ");

                if (isToConsole)
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                }
                writer.Write(p.Value);
            }

            if (isToConsole)
            {
                Console.ResetColor();
            }

            writer.WriteLine();

            indent += isLast ? "   " : "│   ";

            var lastChild = node.GetChildren().LastOrDefault();

            foreach (var child in node.GetChildren())
            {
                PrettyPrint(writer, child, indent, child == lastChild);
            }
        }