I'm trying to determine the compile order in which to list functions in a text file. Why you ask? We use a business rules management language IDE at work that runs very slow on out remote VM's. I'm looking for a way to compile the code from a Java Application.
I already have a Java Application that reads in all Functions and other needed files (Code, Function names etc..) into Memory. I would like to be able to include in my Application a way to determine the order in w开发者_开发问答hich Functions need to be compiled.
For Example:
function B() {
//Do Stuff
}
Integer globalVariable = 0;
function A() {
globalVariable = 1337;
B();
}
Function B must be declared before Function A. I would like to just be able to scan through each function and see that 'Function A calls B' so B must be declared before A. Same thing for Global Variables (Yes the language we are using has Globals) so globalVariable must be declared before the function it is used in.
Thanks!
One way to solve this kind of problem is to represent it as a directed acyclic graph, where each function (or global variable) is a node of the graph, and the edges of the graph represent the dependencies. So, your example would have nodes "A", "B", and "globalVariable", and edges "A->B", and "A->globalVariable".
Then, your desired order can be computed by doing a Topological sort on the graph.
精彩评论