Example #1
0
        public static IExpression GetMarriedWomanExpression()
        {
            IExpression julie   = new TerminalExpression("Julie");
            IExpression married = new TerminalExpression("Married");

            return(new AndExpression(julie, married));
        }
Example #2
0
        public static IExpression Build(this string expression)
        {
            Stack <IExpression> expressionStack = new Stack <IExpression>();

            foreach (string value in expression.Split(" "))
            {
                IExpression resultExpression;

                if (int.TryParse(value, out int number))
                {
                    resultExpression = new TerminalExpression(number);
                }
                else
                {
                    IExpression secondExpression = expressionStack.Pop();
                    IExpression firstExpression  = expressionStack.Pop();

                    resultExpression = value switch
                    {
                        "+" => new AddExpression(firstExpression, secondExpression),
                        "-" => new SubtractExpression(firstExpression, secondExpression),
                        "*" => new MultiplyExpression(firstExpression, secondExpression),
                        "/" => new DivideExpression(firstExpression, secondExpression),
                        _ => throw new Exception("Invalid operation")
                    };
                }

                expressionStack.Push(resultExpression);
            }

            return(expressionStack.Pop());
        }
    }
Example #3
0
        public static IExpression GetMaleExpression()
        {
            IExpression robert = new TerminalExpression("Robert");
            IExpression john   = new TerminalExpression("John");

            return(new OrExpression(robert, john));
        }
Example #4
0
        public static Expression getMarriedWomanExpression()
        {
            Expression Mary    = new TerminalExpression("Mary");
            Expression Married = new TerminalExpression("Married");

            return(new AndExpression(Married, Mary));
        }
Example #5
0
        public static Expression GetMaleExpression()
        {
            Expression Zac  = new TerminalExpression("Zac");
            Expression Jean = new TerminalExpression("Jean");

            return(new OrExpression(Zac, Jean));
        }
Example #6
0
        //builds the interpreter tree and defines the rule "Site1 and (Bonus1 or (User1 or User2))"
        static Expression BuildInterpreterTree()
        {
            // Literal
            Expression terminal1 = new TerminalExpression("Bonus1");
            Expression terminal2 = new TerminalExpression("User1");
            Expression terminal3 = new TerminalExpression("User2");
            Expression terminal4 = new TerminalExpression("Site1");

            Expression alternation1 = new OrExpression(terminal2, terminal3);
            Expression alternation2 = new OrExpression(terminal1, alternation1);

            return(new AndExpression(terminal4, alternation2));
        }
Example #7
0
        public static IExpression GetMarriedWomanExpression()
        {
            IExpression e1 = new TerminalExpression()
            {
                Data = "C"
            };
            IExpression e2 = new TerminalExpression()
            {
                Data = "1"
            };

            return(new AndExpression()
            {
                Exp1 = e1, Exp2 = e2
            });
        }
Example #8
0
        public static IExpression GetMaleExpression()
        {
            IExpression e1 = new TerminalExpression()
            {
                Data = "A"
            };
            IExpression e2 = new TerminalExpression()
            {
                Data = "B"
            };

            return(new OrExpression()
            {
                Exp1 = e1, Exp2 = e2
            });
        }
Example #9
0
        private Expression buildTree(string expr)
        {
            Stack s = new Stack();

            for (int i = 0; i < expr.Length; i++)
            {
                string currChar = expr.Substring(i, 1);
                if (isOperator(currChar) == false)
                {
                    Expression e = new TerminalExpression(currChar);
                    s.Push(e);
                }
                else
                {
                    Expression r = (Expression)s.Pop();
                    Expression l = (Expression)s.Pop();
                    Expression n = getNonTerminalExpression(currChar, l, r);
                    s.Push(n);
                }
            }//for
            return((Expression)s.Pop());
        }