src/NFun/Interpretation/RuntimeBuilderHelper.cs
/// <summary>
/// Gets order of calculating the functions, based on its co using.
/// </summary>
Gets order of calculating the functions, based on its co using.
Gets order of calculating the functions based on its co using .
public static UserFunctionDefinitionSyntaxNode[] FindFunctionSolvingOrderOrThrow(this SyntaxTree syntaxTree) {
        var userFunctions = syntaxTree.Children.OfType<UserFunctionDefinitionSyntaxNode>().ToArray();
        if (userFunctions.Length == 0)
            return userFunctions;
        var userFunctionsNames = new Dictionary<string, int>();
        int i = 0;
        foreach (var userFunction in userFunctions)
        {
            var alias = userFunction.GetFunAlias();
            if (userFunctionsNames.ContainsKey(alias))
                throw ErrorFactory.FunctionAlreadyExist(userFunction);
            userFunctionsNames.Add(alias, i);
            i++;
        }
        int[][] dependenciesGraph = new int[i][];
        int j = 0;
        foreach (var userFunction in userFunctions)
        {
            var visitor = new FindFunctionDependenciesVisitor(userFunction.GetFunAlias(), userFunctionsNames);
            if (!userFunction.ComeOver(visitor))
                throw new InvalidOperationException("User fun come over");
            userFunction.IsRecursive = visitor.HasSelfRecursion;
            dependenciesGraph[j] = visitor.GetFoundDependencies();
            j++;
        }
        var sortResults = GraphTools.SortCycledTopology(dependenciesGraph);
        var functionSolveOrder = new UserFunctionDefinitionSyntaxNode[sortResults.NodeNames.Length];
        for (int k = 0; k < sortResults.NodeNames.Length; k++)
        {
            var id = sortResults.NodeNames[k];
            functionSolveOrder[k] = userFunctions[id];
        }
        if (sortResults.HasCycle)
            throw ErrorFactory.ComplexRecursion(functionSolveOrder);
        return functionSolveOrder;
    }
public static UserFunctionDefinitionSyntaxNode [ ] FindFunctionSolvingOrderOrThrow ( this SyntaxTree syntaxTree ) { var userFunctions = syntaxTree . Children . OfType < UserFunctionDefinitionSyntaxNode > ( ) . ToArray ( ) ; if ( userFunctions . Length == 0 ) return userFunctions ; var userFunctionsNames = new Dictionary < string , int > ( ) ; int i = 0 ; foreach ( var userFunction in userFunctions ) { var alias = userFunction . GetFunAlias ( ) ; if ( userFunctionsNames . ContainsKey ( alias ) ) throw ErrorFactory . FunctionAlreadyExist ( userFunction ) ; userFunctionsNames . Add ( alias , i ) ; i ++ ; } int [ ] [ ] dependenciesGraph = new int [ i ] [ ] ; int j = 0 ; foreach ( var userFunction in userFunctions ) { var visitor = new FindFunctionDependenciesVisitor ( userFunction . GetFunAlias ( ) , userFunctionsNames ) ; if ( ! userFunction . ComeOver ( visitor ) ) throw new InvalidOperationException ( " " ) ; userFunction . IsRecursive = visitor . HasSelfRecursion ; dependenciesGraph [ j ] = visitor . GetFoundDependencies ( ) ; j ++ ; } var sortResults = GraphTools . SortCycledTopology ( dependenciesGraph ) ; var functionSolveOrder = new UserFunctionDefinitionSyntaxNode [ sortResults . NodeNames . Length ] ; for ( int k = 0 ; k < sortResults . NodeNames . Length ; k ++ ) { var id = sortResults . NodeNames [ k ] ; functionSolveOrder [ k ] = userFunctions [ id ] ; } if ( sortResults . HasCycle ) throw ErrorFactory . ComplexRecursion ( functionSolveOrder ) ; return functionSolveOrder ; }
Gets order of calculating the functions, based on its co using.
Gets order of calculating the functions based on its co using .
//if functions has cycle, then function solve order is cycled
SyntaxTree
this