Example #1
0
        private AstNode Expression()
        {
            // EXPRESSION = TERM (ADD | SUBTRACT TERM)* (COMPARISON TERM)

            var node = Term();

            var ops = new[] { Tokens.Add, Tokens.Subtract };

            while (ops.Contains(_currentToken.Type))
            {
                var token = _currentToken;
                Eat(token.Type);
                if (token.Type == Tokens.Add)
                {
                    node = new AddNode(node, Term());
                }
                else if (token.Type == Tokens.Subtract)
                {
                    node = new SubtractNode(node, Term());
                }
            }

            if (Tokens.IsComparison(_currentToken.Type))
            {
                var token = _currentToken;
                if (token.Type == Tokens.Equal)
                {
                    Eat(Tokens.Equal);
                    node = new EqualToNode(node, Term());
                }
                else if (token.Type == Tokens.NotEqual)
                {
                    Eat(Tokens.NotEqual);
                    node = new NotEqualToNode(node, Term());
                }
                else if (token.Type == Tokens.GreaterThan)
                {
                    Eat(Tokens.GreaterThan);
                    node = new GreaterThanNode(node, Term());
                }
                else if (token.Type == Tokens.GreaterThanOrEqualTo)
                {
                    Eat(Tokens.GreaterThanOrEqualTo);
                    node = new GreaterThanOrEqualToNode(node, Term());
                }
                else if (token.Type == Tokens.LessThan)
                {
                    Eat(Tokens.LessThan);
                    node = new LessThanNode(node, Term());
                }
                else if (token.Type == Tokens.LessThanOrEqualTo)
                {
                    Eat(Tokens.LessThanOrEqualTo);
                    node = new LessThanOrEqualToNode(node, Term());
                }
            }

            return(node);
        }
Example #2
0
        private bool Visit(GreaterThanOrEqualToNode node)
        {
            var left  = Visit(node.Left);
            var right = Visit(node.Right);

            if (left is int && right is int)
            {
                return((int)left >= (int)right);
            }
            else if (left is float && right is float)
            {
                return((float)left >= (float)right);
            }
            return(false);
        }