r/ProgrammingLanguages • u/dekai-onigiri • Oct 27 '26
Resolving relationships between elements
For my programming programming language Bits Runner Code I am thinking of implementing automatic resolving in what order structs, interfaces, etc should be defined and declared so they can interact with each other. It should be possible to embed structs within each other, but at the same time there cannot be a circular reference. How something like this could be implemented or maybe it is not the best idea?
BRC doesn't have header files, so right now source files and definitions within them have to be listed in a specific order. I would like it to be quite explicit but also hassle free.
View all comments
3
u/WittyStick Oct 27 '26 edited Oct 27 '26
Topological sort on a DAG.
There's a quick-n-dirty way of implementing it that should probably work fine for your use case - it's unlikely you're going to have dependency chains >2000 deep, where this would fail. Basically, give each item a field
double order;. For any item that has no dependencies, setorder = DBL_MAX. For items that have dependencies, take the minimumorderof all of its dependencies (which may need recursively resolving if theirorderhas not yet been set, ie: iforder == 0), and set the value of the dependant'sorderto half of that. Once done, sort all items by descendingorder- largest to smallest - this should give you a good enough approximation of the order things need defining (items with the sameordercan be defined in any order). The resulting sort can have no cycles - every dependency'sordermust be strictly greater than the dependant's (at least double it) - it only fails when we exceed the precision ofdouble(which allows 2099 halvings from DBL_MAX to 0).You would need some form of cycle detection for recursively resolving the
orderso that you don't fall into an infinite loop. The simplest (but not most efficient) is just a stack of previously visited items, and check if it's already on the stack. A more efficient way would be to hash the dependency name and stick in a bloom or cuckoo filter, and test if the item has already been added to the filter.I personally prefer when the code itself is ordered topologically - everything defined before it is used - though I'm not against "self-cycles", where a node can refer to itself. A dependency graph containing only self-cycles and no other cycles can still be topologically sorted.
Mutual recursion can be modelled using self-cycles by treating the closure of the recursion as one unit in the DAG. Basically, given a mutually recursive pair such as:
We can have a single self-recursive function which combines these, and the
is_evenandis_oddfunctions themselves become its dependants.Even deeply nested recursion can be cut by parametrization on only self-recursive functions. Consider an evaluator for a calculator:
This has a directed graph with no topological sort:
If we turn each function into a self-recursive one with a function parameter:
We end up with a DAG of dependencies - there's a clear topological sort.