Esempio n. 1
0
 public void Print(AdditionExpression ae, StringBuilder sb)
 {
     sb.Append("(");
     Print((dynamic)ae.Left, sb);
     sb.Append("+");
     Print((dynamic)ae.Right, sb);
     sb.Append(")");
 }
Esempio n. 2
0
 public override void Visit(AdditionExpression ae)
 {
     sb.Append("(");
     ae.LHS.Accept(this);
     sb.Append("+");
     ae.RHS.Accept(this);
     sb.Append(")");
 }
Esempio n. 3
0
        public void SimpleAddition()
        {
            var simple = new AdditionExpression(new Value(2), new Value(3));
            var ep     = new ExpressionPrinter();

            ep.Visit(simple);
            Assert.That(ep.ToString(), Is.EqualTo("(2+3)"));
        }
Esempio n. 4
0
 public void Visit(AdditionExpression ae)
 {
     sb.Append("(");
     ae.left.Accept(this);
     sb.Append("+");
     ae.right.Accept(this);
     sb.Append(")");
 }
Esempio n. 5
0
        static void Main(string[] args)
        {
            var e = new AdditionExpression(
                left: new DoubleExpression(1),
                right: new AdditionExpression(
                    left: new DoubleExpression(2),
                    right: new DoubleExpression(3)
                    )
                );

            // INTRUSIVE EXPRESSION PRINTING - NOT VISITOR PATTERN

            //var sb = new StringBuilder();

            //e.Print(sb);

            //Console.WriteLine(sb);

            // what is more likely: new type or new operation

            // REFLECTION BASED PRINTING - STILL NOT USING VISITOR PATTERN

            //var sb = new StringBuilder();

            //ExpressionPrinter.Print2(e, sb);
            //Console.WriteLine(sb);

            // CLASSIC VISITOR

            //var ep = new ExpressionPrinter();
            //ep.Visit(e);

            //Console.WriteLine(ep.ToString());

            //var calc = new ExpressionCalculator();
            //calc.Visit(e);

            //Console.WriteLine($"{ep} = {calc.Result}");

            // DYNAMIC VISITOR

            var ep = new ExpressionPrinter();
            var sb = new StringBuilder();

            ep.Print((dynamic)e, sb);

            Console.WriteLine(sb);

            // disadvantages:

            // 1) Performance penalty
            // 2) Runtime error on missing visitor
            // 3) Problematic w.r.t. inheritance

            Console.ReadLine();
        }
Esempio n. 6
0
        public void Visit(AdditionExpression ae)
        {
            ae.left.Accept(this);
            var a = Result;

            ae.right.Accept(this);
            var b = Result;

            Result = a + b;
        }
Esempio n. 7
0
        void Main(string[] args)
        {
            Expression e = new AdditionExpression(
                new DoubleExpression(1),
                new AdditionExpression(
                    new DoubleExpression(2),
                    new DoubleExpression(3)));

            var ep = new ExpressionPrinter();
            var sb = new StringBuilder();

            ep.Print((dynamic)e, sb);
            Console.WriteLine(sb);
        }
Esempio n. 8
0
 public virtual void Visit(AdditionExpression ex)
 {
 }
Esempio n. 9
0
        public static void Main(string[] args)
        {
            var e = new AdditionExpression(
                new DoubleExpression(1),
                new AdditionExpression(
                    new DoubleExpression(2),
                    new DoubleExpression(3)));

            var ep = new ExpressionPrinter();

            ep.Visit(e);
            Console.WriteLine(ep);

            var calc = new ExpressionCalculator();

            calc.Visit(e);
            Console.WriteLine($"{ep} = {calc.Result}");

            Console.WriteLine("Taxi Visitor!");


            CustomerStructure o = new CustomerStructure();

            o.Attach(new TaxiCustomer("Andrzej"));
            o.Attach(new TaxiCustomer("Seba"));
            o.Attach(new TaxiCustomer("Pudzian"));
            o.Attach(new RichCustomer("Musk"));
            o.Attach(new PoorCustomer("Żul"));

            Taxi t1 = new Taxi1();
            Taxi t2 = new Taxi2();

            o.Accept(t1);

            Console.WriteLine();

            o.Accept(t2);


            Console.WriteLine("Acyclic visitor");


            var expression = new AcyclicVisitor.AdditionExpression(
                new AcyclicVisitor.DoubleExpression(1),
                new AcyclicVisitor.AdditionExpression(
                    new AcyclicVisitor.DoubleExpression(2),
                    new AcyclicVisitor.DoubleExpression(3)));

            var acyclic_ep = new AcyclicVisitor.ExpressionPrinter();

            acyclic_ep.Visit(expression);
            Console.WriteLine(acyclic_ep.ToString());


            Console.WriteLine();

            Console.WriteLine("Code exercise");

            var simple      = new Coding.Exercise.AdditionExpression(new Coding.Exercise.Value(2), new Coding.Exercise.Value(3));
            var exercise_ep = new Coding.Exercise.ExpressionPrinter();

            exercise_ep.Visit(simple);

            Console.WriteLine(exercise_ep.ToString());


            Console.ReadLine();
        }