Example #1
0
        private static void Main(string[] args)
        {
            /* The forum and its posts and their posts and so on is our syntax tree.
             * Syntax tree consists of expressions which are used to interpret data in certain context.
             * Forum and Post are all implementing the IExpression.
             * Context is the context at which each expression is interpreted.
             * Its depth property is needed for the proper interpretation of each post node.
             * The interpreted result is also saved in the output property of context.
             * Here the syntax tree is constructed manually by the client.
             * In real world it is constructed with the help of a parser.
             */
            var forum = new Forum();

            var post1 = new Post("Post 1 message.");
            var post11 = new Post("Post 1 reply 1 message");
            var post12 = new Post("Post 1 reply 2 message");
            var post121 = new Post("Post 1 reply 2 repy 1 message");
            post12.AddReply(post121);
            post1.AddReply(post11);
            post1.AddReply(post12);

            forum.AddPost(post1);

            var post2 = new Post("Post 2 message.");
            var post21 = new Post("Post 2 reply 1 message.");
            post2.AddReply(post21);

            forum.AddPost(post2);

            var ctx = new Context();
            forum.Interpret(ctx);

            Console.WriteLine(ctx.Output);
        }
Example #2
0
        public void Interpret(Context ctx)
        {
            ctx.Output = "<--Forum start-->\n";

            foreach (var post in this.posts)
            {
                post.Interpret(ctx);
            }

            ctx.Output += "<--Forum end-->";
        }
Example #3
0
        public void Interpret(Context ctx)
        {
            ctx.Output += this.Msg + "\n";

            ctx.Depth++;
            foreach (var post in posts)
            {
                ctx.Output += new String(' ', ctx.Depth * 4);
                post.Interpret(ctx);
            }
            ctx.Depth--;
        }
        public static void Main(string[] args)
        {
            Context context = new Context();

            ArrayList list = new ArrayList();

            list.Add(new TerminalExpression());
            list.Add(new NonTerminalExpression());
            list.Add(new TerminalExpression());
            list.Add(new TerminalExpression());

            foreach(AbstractExpression exp in list)
            {
                exp.Interpret(context);
            }

            Console.ReadLine();
        }