예제 #1
0
        public static void WriteTo(this BoundNode node, IndentedTextWriter writer)
        {
            switch (node.Kind)
            {
            case BoundNodeKind.BlockStatement:
                WriteBlockStatement((BoundBlockStatement)node, writer);
                break;

            case BoundNodeKind.NopStatement:
                WriteNopStatement((BoundNopStatement)node, writer);
                break;

            case BoundNodeKind.VariableDeclaration:
                WriteVariableDeclaration((BoundVariableDeclaration)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.GotoStatement:
                WriteGotoStatement((BoundGotoStatement)node, writer);
                break;

            case BoundNodeKind.ConditionalGotoStatement:
                WriteConditionalGotoStatement((BoundConditionalGotoStatement)node, writer);
                break;

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

            case BoundNodeKind.ExpressionStatement:
                WriteExpressionStatement((BoundExpressionStatement)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.CompoundAssignmentExpression:
                WriteCompoundAssignmentExpression((BoundCompoundAssignmentExpression)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;

            default:
                throw new Exception($"Unexpected node {node.Kind}");
            }
        }
예제 #2
0
        private 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;

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

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

                    writer.Write(", ");
                }

                if (isToConsole)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }
                Console.Write(prop.Name);

                Console.Write(" = ");

                if (isToConsole)
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                }
                Console.Write(prop.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);
            }
        }
예제 #3
0
        public static void WriteTo(this BoundNode node, IndentedTextWriter writer)
        {
            switch (node.Kind)
            {
            case BoundNodeKind.AssignmentExpression:
                WriteAssignmentExpression(node as BoundAssignmentExpression, writer);
                break;

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

            case BoundNodeKind.BlockStatement:
                WriteBlockStatement(node as BoundBlockStatement, writer);
                break;

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

            case BoundNodeKind.ConditionalGotoStatement:
                WriteConditionalGotoStatement(node as BoundConditionalGotoStatement, writer);
                break;

            case BoundNodeKind.ConditionalStatement:
                WriteConditionalStatement(node as BoundConditionalStatement, writer);
                break;

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

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

            case BoundNodeKind.ErrorStatement:
                WriteErrorStatement(node as BoundErrorStatement, writer);
                break;

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

            case BoundNodeKind.ForToStatement:
                WriteForToStatement(node as BoundForToStatement, writer);
                break;

            case BoundNodeKind.GotoStatement:
                WriteGotoStatement(node as BoundGotoStatement, writer);
                break;

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

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

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

            case BoundNodeKind.Program:
                WriteProgram(node as BoundProgram, writer);
                break;

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

            case BoundNodeKind.VariableDeclarationStatement:
                WriteVariableDeclarationStatement(node as BoundVariableDeclarationStatement, writer);
                break;

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

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

            default:
                throw new Exception($"Unexpected node kind '{node.Kind}'");
            }
        }