public void Executor_Function_ReturnsAValue()
        {
            var tree = new ParseTree();
            tree.Root.AddChild(GetRandNode());

            var result = _executor.Execute(tree);

            Assert.IsTrue(result.Success, "Should be able to process simple funcs such as rand");
        }
        public void Executor_ValueOnly_ReturnsValue()
        {
            var valNode = new ValueNode("simple", Token.Word);

            var tree = new ParseTree();
            tree.Root.AddChild(valNode);

            var result = _executor.Execute(tree);

            Assert.AreEqual(valNode.Value, result.Response.First(), "When only given a value node, then the value should simple be returned as the result");
        }
        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");
        }
        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;
        }
        public ExecutionResult Execute(ParseTree tree)
        {
            var result = SolveNode(tree.Root);

            return ExecutionResult.Ok(new string[] { result.DisplayMessage });
        }