public TypeNode VisitFunctionCall(FunctionCallExpression funcCallExpNode, List <TypeNode> parameterTypes)
        {
            TypeNode res;

            if (IsLocalReferenceAMatch(funcCallExpNode, parameterTypes))
            {
                funcCallExpNode.GlobalReferences = new List <int>();
                FunctionTypeNode funcDeclType = (FunctionTypeNode)parameterTypes[funcCallExpNode.LocalReference];
                res = funcDeclType.ReturnType;
            }
            else
            {
                List <int> matchingRefs = funcCallExpNode.GlobalReferences;
                CheckMatches(funcCallExpNode.Children, matchingRefs, parameterTypes);
                if (matchingRefs.Count > 1)
                {
                    throw new OverloadException(funcCallExpNode, GetFunctions(matchingRefs));
                }
                else if (matchingRefs.Count == 0)
                {
                    throw new NoMatchingFunctionFoundException(funcCallExpNode);
                }

                funcCallExpNode.LocalReference   = FunctionCallExpression.NO_LOCAL_REF;
                funcCallExpNode.GlobalReferences = matchingRefs;
                res = _functions[matchingRefs.First()].FunctionType.ReturnType;
            }

            return(res);
        }
        private FunctionNode CreateFunctionNode(ASTNode himeDeclNode)
        {
            ASTNode          himeFuncNode = GetHimeFuncNode(himeDeclNode);
            FunctionTypeNode type         = _expressionHelper.CreateFunctionTypeNode(himeFuncNode.Children[_conf.FUNCTIONTYPE_POS]);

            List <string> parameterIdentifiers = GetParameterIdentifiers(himeFuncNode);
            string        typeID     = GetTypeID(himeFuncNode);
            string        functionID = GetFunctionID(himeFuncNode);

            if (typeID != functionID)
            {
                throw new FunctionIdentifierMatchException(functionID, typeID);
            }

            TextPosition position = himeFuncNode.Children[0].Position;

            if (IsConditional(himeDeclNode))
            {
                List <ConditionNode> conditions = new List <ConditionNode>();
                VisitConditions(GetFunctionContent(himeDeclNode),
                                conditions,
                                false);
                return(new FunctionNode(conditions, typeID, parameterIdentifiers, type,
                                        position.Line, position.Column));
            }
            else
            {
                ConditionNode condition = GetInsertedConditionNode(himeDeclNode);
                return(new FunctionNode(typeID, condition, parameterIdentifiers, type,
                                        position.Line, position.Column));
            }
        }
        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 FunctionNode(List <ConditionNode> conditions, string identifier, List <string> parameterIdentifiers,
                     FunctionTypeNode functionType, int line, int letter) : base(line, letter)
 {
     Conditions           = conditions;
     ParameterIdentifiers = parameterIdentifiers;
     FunctionType         = functionType;
     Identifier           = identifier;
 }
        public static FunctionNode GetFunctionNode(string id)
        {
            IntegerLiteralExpression intLitExpr       = new IntegerLiteralExpression(0, 0, 0);
            ConditionNode            conditionNode    = GetConditionNode(intLitExpr);
            FunctionTypeNode         functionTypeNode = GetFunctionTypeNode();
            List <string>            paramIDs         = new List <string>();

            return(new FunctionNode(id, conditionNode, paramIDs, functionTypeNode, 0, 0));
        }
 public FunctionNode(string identifier, ConditionNode condition, List <string> parameterIdentifiers,
                     FunctionTypeNode functionType, int line, int letter) : base(line, letter)
 {
     Conditions = new List <ConditionNode> {
         condition
     };
     ParameterIdentifiers = parameterIdentifiers;
     FunctionType         = functionType;
     Identifier           = identifier;
 }
        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);
        }
 private void CheckEdgeFunction(FunctionTypeNode funcTypeNode, string name, GraphExpression parent)
 {
     if (funcTypeNode.Type != TypeEnum.Function)
     {
         throw new UnmatchableTypesException(parent, funcTypeNode.Type, TypeEnum.Function);
     }
     if (funcTypeNode.ParameterTypes.Count != 1)
     {
         throw new Exception($"{name} function must take a single parameter");
     }
     if (funcTypeNode.ParameterTypes[0].Type != TypeEnum.Element)
     {
         throw new UnmatchableTypesException(parent, funcTypeNode.ParameterTypes[0].Type, TypeEnum.Element);
     }
     if (funcTypeNode.ReturnType.Type != TypeEnum.Element)
     {
         throw new UnmatchableTypesException(parent, funcTypeNode.ReturnType.Type, TypeEnum.Element);
     }
 }
        private bool IsLocalReferenceAMatch(FunctionCallExpression funcCallExpNode, List <TypeNode> parameterTypes)
        {
            if (funcCallExpNode.LocalReference == FunctionCallExpression.NO_LOCAL_REF)
            {
                return(false);
            }

            FunctionTypeNode funcDeclType = (FunctionTypeNode)parameterTypes[funcCallExpNode.LocalReference];

            for (int i = 0; i < funcCallExpNode.Children.Count; i++)
            {
                TypeNode child    = _getType(funcCallExpNode.Children[i], parameterTypes);
                TypeNode expected = funcDeclType.ParameterTypes[i];
                if (!AreTypesEqual(child, expected))
                {
                    return(false);
                }
            }
            return(true);
        }
        public TypeNode VisitGraph(GraphExpression node, List <TypeNode> parameterTypes)
        {
            TypeNode         vertexSet = GetType(node.Children[0], parameterTypes);
            TypeNode         edgeSet   = GetType(node.Children[1], parameterTypes);
            FunctionTypeNode src       = (FunctionTypeNode)GetType(node.Children[2], parameterTypes);
            FunctionTypeNode dst       = (FunctionTypeNode)GetType(node.Children[3], parameterTypes);

            if (vertexSet.Type != TypeEnum.Set)
            {
                throw new UnmatchableTypesException(node, vertexSet.Type, TypeEnum.Set);
            }
            if (edgeSet.Type != TypeEnum.Set)
            {
                throw new UnmatchableTypesException(node, edgeSet.Type, TypeEnum.Set);
            }
            CheckEdgeFunction(src, nameof(src), node);
            CheckEdgeFunction(dst, nameof(dst), node);

            return(new TypeNode(TypeEnum.Graph, 0, 0));
        }
        private bool IsFunctionTypesEqual(FunctionTypeNode a, FunctionTypeNode b)
        {
            if (!AreTypesEqual(a.ReturnType, b.ReturnType))
            {
                return(false);
            }
            if (a.ParameterTypes.Count != b.ParameterTypes.Count)
            {
                return(false);
            }

            for (int i = 0; i < a.ParameterTypes.Count; i++)
            {
                if (!AreTypesEqual(a.ParameterTypes[i], b.ParameterTypes[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
        public TypeNode VisitAnonymousFunction(AnonymousFunctionExpression node, List <TypeNode> parameterTypes)
        {
            List <TypeNode> newScope = new List <TypeNode>();

            newScope.AddRange(parameterTypes);
            newScope.AddRange(node.Types);

            TypeNode returnType = _getType(node.ReturnValue, newScope);

            FunctionTypeNode functionType = new FunctionTypeNode(returnType, newScope, 0, 0);

            int           line      = node.LineNumber;
            int           letter    = node.LineNumber;
            ConditionNode condition = new ConditionNode(node.ReturnValue, line, letter);
            FunctionNode  function  = new FunctionNode(functionType.ToString(), condition,
                                                       node.Identifiers, functionType, line, letter);

            node.Reference = _functions.Count;
            _functions.Add(function);
            return(new FunctionTypeNode(returnType, node.Types, 0, 0));
        }
Beispiel #13
0
        public static FunctionNode GetFunctionNodeWith(List <string> paramIDs, TypeNode returnType, List <TypeNode> paramTypes)
        {
            FunctionTypeNode fType = new FunctionTypeNode(returnType, paramTypes, 0, 0);

            return(new FunctionNode("f", null, paramIDs, fType, 1, 1));
        }
 internal static FunctionNode GetFunction(FunctionTypeNode funcType)
 {
     return(new FunctionNode("", null, null, funcType, 0, 0));
 }
        internal static FunctionNode GetFunctionNode(string id, ConditionNode condition)
        {
            FunctionTypeNode functionTypeNode = GetFunctionTypeNode();

            return(new FunctionNode(id, condition, new List <string>(), functionTypeNode, 0, 0));
        }
        public static FunctionNode GetFunctionNode(string id, List <string> parameters, ConditionNode condition)
        {
            FunctionTypeNode functionTypeNode = GetFunctionTypeNode(parameters.Count, TypeEnum.Function);

            return(new FunctionNode(id, condition, parameters, functionTypeNode, 0, 0));
        }
        internal static FunctionNode GetFunctionNode(string id, string elementId, ConditionNode condition)
        {
            FunctionTypeNode functionTypeNode = GetFunctionTypeNode(1, TypeEnum.Element);

            return(new FunctionNode(id, condition, new List <string>(), functionTypeNode, 0, 0));
        }