Ejemplo n.º 1
0
        private static void TestRpnPrinter()
        {
            // (1 + 2) * (4 - 3) => 1 2 + 4 3 - *
            Expr expression =
                new Expr.Binary(
                    new Expr.Grouping(
                        new Expr.Binary(
                            new Expr.Literal(1),
                            new Token(TokenType.PLUS, "+", null, 1),
                            new Expr.Literal(2)
                            )
                        ),
                    new Token(TokenType.STAR, "*", null, 1),
                    new Expr.Grouping(
                        new Expr.Binary(
                            new Expr.Literal(4),
                            new Token(TokenType.MINUS, "-", null, 1),
                            new Expr.Literal(3)
                            )
                        )
                    );

            var printer = new RpnPrinter();

            Console.WriteLine(expression.Accept(printer));
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private static void TestAstPrinter()
        {
            // -123 * (45.67) => (* (- 123) (group 45.67))
            Expr expression =
                new Expr.Binary(
                    new Expr.Unary(
                        new Token(TokenType.MINUS, "-", null, 1),
                        new Expr.Literal(123)
                        ),
                    new Token(TokenType.STAR, "*", null, 1),
                    new Expr.Grouping(
                        new Expr.Literal(45.67)
                        )
                    );

            var printer = new AstPrinter();

            Console.WriteLine(expression.Accept(printer));
            Console.ReadLine();
        }