public void Executor_NestedFunctions_One_Success()
        {
            var tree = new ParseTree();
            var addNode = new FunctionNode("add");
            addNode.AddChild(GetRandNode());
            addNode.AddChild(new ValueNode("3", Token.Int));
            tree.Root.AddChild(addNode);

            var result = _executor.Execute(tree);

            Assert.IsTrue(result.Success, "Should be able to process nested functions");
        }
Exemplo n.º 2
0
        private ResultNode SolveFunctionNode(FunctionNode node)
        {
            if (!node.Children.All(x => x is ResultNode))
                throw new ExecutorException("Should not be asked to solve a function node where it has any non result node children");

            var funcClass = _funcFactory.Build(node.Value);

            var result = funcClass.Invoke(node.Children.Cast<ResultNode>());
            if (result.IsSuccess)
                return new ResultNode(result.Value.ToString(), result.Token, result.Message);

            throw new ExecutorException("Funcation call failure");
        }
Exemplo n.º 3
0
        private ParseTree ProcessMatches(IEnumerable<Match> matches)
        {
            var result = new ParseTree();

            var currentPos = result.Root;
            var expectFunc = false;
            var bracketCount = 0;

            foreach (var match in matches)
            {
                if (match.Token == Token.OpenBracket)
                {
                    if (expectFunc)
                        throw new ParserException("Expected function name after open bracket");

                    expectFunc = true;
                    bracketCount++;
                }
                else if (match.Token == Token.Word && expectFunc)
                {
                    var newNode = new FunctionNode(match.Value);
                    currentPos.AddChild(newNode);
                    currentPos = newNode;
                    expectFunc = false;
                }
                else if (match.Token == Token.CloseBracket)
                {
                    currentPos = currentPos.Parent;
                    expectFunc = false;
                    bracketCount--;

                    if (bracketCount < 0)
                        throw new ParserException("Inconsistent brackets");
                }
                else
                {
                    currentPos.AddChild(new ValueNode(match.Value, match.Token));
                    expectFunc = false;
                }
            }

            if (0 < bracketCount)
                throw new ParserException("Missing close brackets");

            return result;
        }
        private FunctionNode GetRandNode()
        {
            var funcNode = new FunctionNode("rand");
            funcNode.AddChild(new ValueNode("5", Token.Int));

            return funcNode;
        }