Exemplo n.º 1
0
        public object Process(Parser parser, SortedDictionary <string, object> parameters)
        {
            var _leftNode = (ExpressionNode)parameters["LeftNode"];

            if (parser.CurrenToken.Type == TokenType.OpTernary)
            {
                parser.NextToken();
                var _trueEp = (ExpressionNode) new AssignmentExpression().Process(parser, parameters);
                if (parser.CurrenToken.Type == TokenType.PmColon)
                {
                    parser.NextToken();
                    var _falseEp = (ExpressionNode) new AssignmentExpression().Process(parser, parameters);
                    var _result  = new TernaryNode
                    {
                        EvaluationExpression = _leftNode,
                        TrueExpression       = _trueEp,
                        FalseExpression      = _falseEp
                    };
                    return(_result);
                }
                else
                {
                    throw new ParserException("This was expected : in the conditional expression, Received: [" +
                                              parser.CurrenToken.LexemeVal + "], Row: " + parser.CurrenToken.Row
                                              + ", Column: " + parser.CurrenToken.Column);
                }
            }
            else
            {
                return(_leftNode);
            }
        }
Exemplo n.º 2
0
    public bool Contains(string s)
    {
        if (s == null || s == "")
        {
            throw new ArgumentException();
        }

        int         pos  = 0;
        TernaryNode node = m_root;

        while (node != null)
        {
            int cmp = s[pos] - node.m_char;
            if (s[pos] < node.m_char)
            {
                node = node.m_left;
            }
            else if (s[pos] > node.m_char)
            {
                node = node.m_right;
            }
            else
            {
                if (++pos == s.Length)
                {
                    return(node.m_wordEnd);
                }
                node = node.m_center;
            }
        }

        return(false);
    }
Exemplo n.º 3
0
        private IEnumerable <TernaryNode> Traversal(TernaryNode node)
        {
            var stack = new Stack <TernaryNode>();

            stack.Push(node);

            while (stack.Count > 0)
            {
                var n = stack.Pop();

                if (n.Left != null)
                {
                    stack.Push(n.Left);
                }

                if (n.Middle != null)
                {
                    stack.Push(n.Middle);
                }

                if (n.Right != null)
                {
                    stack.Push(n.Right);
                }

                yield return(n);
            }
        }
Exemplo n.º 4
0
    private void Add(string s, int pos, ref TernaryNode node)
    {
        if (node == null)
        {
            node = new TernaryNode(s[pos], false);
        }

        if (s[pos] < node.m_char)
        {
            Add(s, pos, ref node.m_left);
        }
        else if (s[pos] > node.m_char)
        {
            Add(s, pos, ref node.m_right);
        }
        else
        {
            if (pos + 1 == s.Length)
            {
                node.m_wordEnd = true;
            }
            else
            {
                Add(s, pos + 1, ref node.m_center);
            }
        }
    }
Exemplo n.º 5
0
 public virtual void Visit(TernaryNode node)
 {
     VisitNode(node.Condition);
     VisitNode(node.WhenTrue);
     if (node.WhenFalse != null)
     {
         VisitNode(node.WhenFalse);
     }
 }
Exemplo n.º 6
0
 public override void Visit(TernaryNode node)
 {
     ChallengeNode(node);
     if (Found)
     {
         return;
     }
     base.Visit(node);
 }
Exemplo n.º 7
0
        public virtual Node Mutate(TernaryNode node)
        {
            var condition = MutateNode(node.Condition);
            var whenTrue  = MutateNode(node.WhenTrue);
            var whenFalse = node.WhenFalse == null ? null : MutateNode(node.WhenFalse);

            return(condition == node.Condition && whenTrue == node.WhenTrue && whenFalse == node.WhenFalse
                ? node
                : new TernaryNode(condition, whenTrue, whenFalse));
        }
Exemplo n.º 8
0
 public override void Visit(TernaryNode node)
 {
     _stringBuilder.Append("( ");
     node.Condition.Accept(this);
     _stringBuilder.Append(" ? ");
     node.WhenTrue.Accept(this);
     _stringBuilder.Append(" : ");
     node.WhenFalse.Accept(this);
     _stringBuilder.Append(" )");
 }
Exemplo n.º 9
0
        public void VisitTernary(TernaryNode node)
        {
            UpdateLine(node);
            var label = asm.NewLabel();

            node.Condition.Accept(this);
            asm.Branch(label);
            node.False.Accept(this);
            var end = asm.NewLabel();

            asm.Jump(end);
            asm.MarkLabel(label);
            node.True.Accept(this);
            asm.MarkLabel(end);
        }
Exemplo n.º 10
0
        public void TernaryTest1()
        {
            string        expr = "a?(b?c:d):e";
            LexicalParser lp   = new LexicalParser();

            lp.SetParseContent(expr);
            List <Token <TokenType> > tokens = lp.Parse();

            SymanticParser sp = new SymanticParser();

            sp.SetParseContent(tokens);
            ExpressionLanguageAST ast = sp.Parse();

            TernaryNode tNode = ast.Root as TernaryNode;

            Assert.IsTrue(tNode.TrueValue is TernaryNode);
        }
Exemplo n.º 11
0
        public override Node Mutate(TernaryNode node)
        {
            var conditionMutated = MutateNode(node.Condition);
            var conditionCount   = _count;

            var whenTrueMutated = MutateNode(node.WhenTrue);
            var whenTrueCount   = _count;

            Node whenFalseMutated = null;

            if (node.WhenFalse != null)
            {
                whenFalseMutated = MutateNode(node.WhenFalse);
                if (whenTrueMutated.ToString() == whenFalseMutated.ToString())
                {
                    _count = 1;
                    return(whenTrueMutated);
                }
            }
            else
            {
                _count = 0;
            }
            var whenFalseCount = _count;


            if (conditionMutated is ConstantNode conditionConstant)
            {
                if (!(conditionConstant.Value > 0))
                {
                    return(whenFalseMutated);
                }

                _count = whenTrueCount;
                return(whenTrueMutated);
            }

            _count = conditionCount + whenTrueCount + whenFalseCount + 1;
            return(conditionMutated == node.Condition &&
                   whenTrueMutated == node.WhenTrue &&
                   whenFalseMutated == node.WhenFalse
                ? node
                : new TernaryNode(conditionMutated, whenTrueMutated, whenFalseMutated));
        }
Exemplo n.º 12
0
        public void SimpleTest8()
        {
            string        expr = ExpressionsForTest.Expr8;
            LexicalParser lp   = new LexicalParser();

            lp.SetParseContent(expr);
            List <Token <TokenType> > tokens = lp.Parse();

            SymanticParser sp = new SymanticParser();

            sp.SetParseContent(tokens);
            ExpressionLanguageAST ast = sp.Parse();

            Assert.IsTrue(ast.Root is TernaryNode);
            TernaryNode tNode = ast.Root as TernaryNode;

            Assert.IsTrue(tNode.Condition is OperatorNode);
            Assert.IsTrue(tNode.TrueValue is IndexerNode);
            Assert.IsTrue(tNode.FalseValue is SimpleNode);
        }
Exemplo n.º 13
0
        private ExpressionNode ParseExpression()
        {
            var ret = ParseConcat();

            while (More() && Accept(TokenType.QUESTION_MARK))
            {
                var pos = Position();

                Next();

                var @true = ParseExpression();

                Expect(TokenType.COLON);
                Next();

                var @false = ParseExpression();

                ret = new TernaryNode(pos, ret, @true, @false);
            }

            return(ret);
        }
Exemplo n.º 14
0
        private Node Value()
        {
            Node result         = Expr();
            Token <TokenType> t = PeekNextToken();
            bool loop           = true;

            while (!t.End)
            {
                switch (t.TokenType)
                {
                case TokenType.QuestionMark:
                    GetNextToken();    //Note:question token
                    Node trueNode = Expr();
                    Token <TokenType> colonToken = GetNextToken();
                    if (colonToken.TokenType != TokenType.Colon)
                    {
                        ExceptionHelper.ThrowExpectToken(TokenType.Colon, colonToken.Position);
                    }

                    Node falseNode = Expr();

                    result = new TernaryNode(result, trueNode, falseNode);
                    break;

                default:
                    loop = false;
                    break;
                }

                if (!loop)
                {
                    break;
                }
                t = PeekNextToken();
            }

            return(result);
        }
Exemplo n.º 15
0
        public override Node Mutate(TernaryNode node)
        {
            if (_random.NextDouble() <= CrossoverProbability)
            {
                return(Crossover());
            }

            if (node.WhenFalse != null && _random.NextDouble() <= ConditionInversionProbability)
            {
                return(new TernaryNode
                       (
                           MutateNode(node.Condition),
                           MutateNode(node.WhenFalse),
                           MutateNode(node.WhenTrue)
                       ));
            }

            if (_random.NextDouble() <= FullMutationProbability)
            {
                return(FullMutation());
            }

            return(base.Mutate(node));
        }
Exemplo n.º 16
0
        public void TernaryNodesAreConstantIfTheConditionTrueAndFalseAreConstant()
        {
            subject = new TernaryNode(new SourcePosition(2, 4), new BoolNode(new SourcePosition(2, 5), true), new IntNode(new SourcePosition(2, 6), 42), new IntNode(new SourcePosition(2, 6), 21));

            Assert.AreEqual(subject.IsConstant(), true);
        }
Exemplo n.º 17
0
 public TernaryNodeTests()
 {
     subject = new TernaryNode(new SourcePosition(2, 4), new IdentNode(new SourcePosition(2, 5), "foo"), new IntNode(new SourcePosition(2, 6), 42), new IntNode(new SourcePosition(2, 6), 21));
 }
Exemplo n.º 18
0
        public void Add(String key, TElement element)
        {
            if (_root == null)
            {
                _root = new TernaryNode();
            }

            if (String.IsNullOrEmpty(key))
            {
                _root.Grouping = (_root.Grouping ?? new Grouping());
                _root.Grouping.Add(element);

                return;
            }

            var index  = 0;
            var node   = _root;
            var length = key.Length - 1;

            while (true)
            {
                var @char = key[index];

                if (@char < node.Char)
                {
                    node.Left = (node.Left ?? new TernaryNode {
                        Char = @char
                    });
                    node      = node.Left;
                }
                else if (@char > node.Char)
                {
                    node.Right = (node.Right ?? new TernaryNode {
                        Char = @char
                    });
                    node       = node.Right;
                }
                else if (index < length)
                {
                    node.Middle = (node.Middle ?? new TernaryNode {
                        Char = key[index + 1]
                    });
                    node        = node.Middle;

                    index++;
                }
                else
                {
                    node.Leaf = true;

                    if (node.Grouping == null)
                    {
                        node.Grouping = new Grouping {
                            Key = key
                        };
                        _count++;
                    }

                    node.Grouping.Add(element);
                    return;
                }
            }
        }
Exemplo n.º 19
0
 public abstract object Visit(TernaryNode node);
Exemplo n.º 20
0
 public void Clear()
 {
     _root  = null;
     _count = 0;
 }
Exemplo n.º 21
0
 public override void Visit(TernaryNode node)
 {
     VisitNode(node.Condition);
     VisitNode(ComputedResult > 0 ? node.WhenTrue : node.WhenFalse);
 }
Exemplo n.º 22
0
 public object Visit(TernaryNode node)
 {
     return(Convert.ToBoolean(Visit((dynamic)node.TestExpr))
         ? Visit((dynamic)node.TrueExpr)
         : Visit((dynamic)node.FalseExpr));
 }
Exemplo n.º 23
0
 public void VisitTernary(TernaryNode node)
 {
     VisitTernaryHandler(node);
 }