Ejemplo n.º 1
0
        private T GetResult <T>(FunctionCallExpression node, List <object> parameters,
                                FunctionNode funcNode, List <object> scope)
        {
            List <object> funcParameters = scope;

            for (int i = 0; i < node.Children.Count; i++)
            {
                TypeEnum type = funcNode.FunctionType.ParameterTypes[i].Type;
                funcParameters.Add(_interpreter.Dispatch(node.Children[i], parameters, type));
            }
            return(_interpreter.Function <T>(funcNode, funcParameters));
        }
        public void FunctionCallFunction_UsingLocalReference_PassesCorrectFunctionNodeToFunctionFunction()
        {
            IntegerLiteralExpression functionLit = new IntegerLiteralExpression("1.0", 1, 1);
            List <ExpressionNode>    funcParams  = new List <ExpressionNode> {
                functionLit
            };
            FunctionCallExpression funcCallExpr = new FunctionCallExpression("test", funcParams, 1, 1);

            funcCallExpr.LocalReference   = 0;
            funcCallExpr.GlobalReferences = new List <int>();
            IInterpreterGeneric parent = Substitute.For <IInterpreterGeneric>();

            parent.Dispatch(funcParams[0], Arg.Any <List <object> >(), TypeEnum.Function).Returns((Object)1);
            GenericHelper   functionHelper = SetUpHelper(parent);
            List <TypeNode> typeNodes      = new List <TypeNode> {
                new TypeNode(TypeEnum.Function, 1, 1)
            };
            FunctionTypeNode funcTypeNode = new FunctionTypeNode(null, typeNodes, 1, 1);
            FunctionNode     funcNode     = new FunctionNode("", null, null, funcTypeNode, 1, 1);
            AST ast = new AST(new List <FunctionNode> {
                funcNode
            }, null, 1, 1);

            functionHelper.SetASTRoot(ast);
            FunctionNode res = null;

            parent.Function <Function>(Arg.Do <FunctionNode>(x => res = x), Arg.Any <List <object> >());

            functionHelper.FunctionCall <Function>(funcCallExpr, new List <object> {
                new Function(0)
            });

            res.Should().BeEquivalentTo(funcNode);
        }
        public void FunctionCallBoolean_GlobalRef_CorrectParameterTypes(int expectedElementCount, TypeEnum type)
        {
            int funcIndex  = 0;
            var parameters = new List <object>();

            var children = Utilities.GetIntLitExprNodes(expectedElementCount);
            var expr     = new FunctionCallExpression("", children, 0, 0);

            expr.GlobalReferences.Add(funcIndex);

            var funcType = Utilities.GetFunctionTypeNode(expectedElementCount, type);
            var func     = Utilities.GetFunction(funcType);
            AST ast      = Utilities.GetAst(func);

            IInterpreterGeneric parent = Substitute.For <IInterpreterGeneric>();
            var res = new List <object>();

            parent.Function <bool>(Arg.Any <FunctionNode>(), Arg.Do <List <object> >(x => res = x)).Returns(false);
            parent.Dispatch(Arg.Any <ExpressionNode>(),
                            Arg.Any <List <object> >(),
                            Arg.Is <TypeEnum>(x => x == type)).Returns(1);
            GenericHelper booleanHelper = Utilities.GetGenericHelper(parent, ast);

            booleanHelper.FunctionCall <bool>(expr, parameters);

            foreach (var elem in res)
            {
                Assert.IsNotNull(elem);
            }
        }
        public void ConditionFunction_ConditionNodeAndObjectList_ReturnsCorrectResult(int input, int expected)
        {
            IdentifierExpression id            = new IdentifierExpression("", 1, 1);
            ConditionNode        conditionNode = new ConditionNode(id, 1, 1);
            IInterpreterGeneric  parent        = Substitute.For <IInterpreterGeneric>();

            parent.Dispatch(id, Arg.Any <List <Object> >(), Arg.Any <TypeEnum>()).Returns(new Function(input));
            GenericHelper functionHelper = SetUpHelper(parent);

            int res = functionHelper.Condition <Function>(conditionNode, new List <Object>()).Element.Reference;

            Assert.AreEqual(expected, res);
        }
        public void FunctionCallFunction_f_f(Object[] numbers, TypeEnum[] types)
        {
            List <Object>         expected   = numbers.ToList();
            List <TypeEnum>       exTypes    = types.ToList();
            List <ExpressionNode> funcParams = new List <ExpressionNode>();
            IInterpreterGeneric   parent     = Substitute.For <IInterpreterGeneric>();
            List <TypeNode>       typeNodes  = new List <TypeNode>();

            for (int i = 0; i < expected.Count; i++)
            {
                switch (expected[i])
                {
                case int x:
                    funcParams.Add(new IntegerLiteralExpression(x.ToString(), 1, 1));
                    break;

                case double x:
                    funcParams.Add(new RealLiteralExpression(x.ToString(), 1, 1));
                    break;

                default:
                    throw new Exception("Unexpected shit");
                }
                parent.Dispatch(funcParams[i], Arg.Any <List <object> >(), exTypes[i]).Returns(expected[i]);
                typeNodes.Add(new TypeNode(exTypes[i], 1, 1));
            }



            FunctionCallExpression funcCallExpr = new FunctionCallExpression("test", funcParams, 1, 1);

            funcCallExpr.GlobalReferences = new List <int> {
                0
            };
            funcCallExpr.LocalReference = -1;
            GenericHelper    functionHelper = SetUpHelper(parent);
            FunctionTypeNode funcTypeNode   = new FunctionTypeNode(null, typeNodes, 1, 1);
            FunctionNode     funcNode       = new FunctionNode("", null, null, funcTypeNode, 1, 1);
            AST ast = new AST(new List <FunctionNode> {
                funcNode
            }, null, 1, 1);

            functionHelper.SetASTRoot(ast);
            List <object> res = new List <object>();

            parent.Function <Function>(Arg.Any <FunctionNode>(), Arg.Do <List <object> >(x => res = x));

            functionHelper.FunctionCall <Function>(funcCallExpr, new List <Object>());

            res.Should().BeEquivalentTo(expected);
        }
        public void ConditionFunction_ConditionNodeAndObjectList_ReturnsNull()
        {
            IdentifierExpression id            = new IdentifierExpression("", 1, 1);
            ConditionNode        conditionNode = new ConditionNode(id, id, 1, 1);
            IInterpreterGeneric  parent        = Substitute.For <IInterpreterGeneric>();

            parent.Dispatch(id, Arg.Any <List <Object> >(), Arg.Any <TypeEnum>()).Returns(false);
            GenericHelper functionHelper = SetUpHelper(parent);
            bool          expected       = false;

            bool res = functionHelper.Condition <Function>(conditionNode, new List <Object>()).IsCalculated;

            Assert.AreEqual(expected, res);
        }
        public void ConditionFunction_ConditionNodeAndObjectList_CorrectListPassed()
        {
            IdentifierExpression id            = new IdentifierExpression("", 1, 1);
            ConditionNode        conditionNode = new ConditionNode(id, 1, 1);
            IInterpreterGeneric  parent        = Substitute.For <IInterpreterGeneric>();
            List <Object>        res           = null;

            parent.Dispatch(id, Arg.Do <List <Object> >(x => res = x), Arg.Any <TypeEnum>()).Returns(new Function(0));
            List <Object> expected = new List <Object> {
                1, 1.3, ""
            };
            GenericHelper functionHelper = SetUpHelper(parent);

            functionHelper.Condition <Function>(conditionNode, expected);

            res.Should().BeEquivalentTo(expected);
        }