r/ProgrammingLanguages • • 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.

6 Upvotes

15 comments sorted by

View all comments

2

u/cxzuk Oct 28 '26

Hi Dekai,

Yes, absolutely a good idea and doable. You're going to need to create multiple dependency graphs. This is a graph where nodes represent structs, and directed edges (A -> B) represent the A depends on B.

You're going to need to start with a list of all the structs in the source file, provided by your AST/Symbol Table.

Doing a first initial pass to get all these and added to the dependency graph as nodes first simplifies adding the edges. The end points are already in place.

Now add the edges that the struct defines. Note - Primitive types do not create a dependency edge. A pointer is a primitive type.

You can now use a depth first search and compute the Post Order - creating a topological sort of each the graphs. You can detect cycles here and report an error etc.

https://godbolt.org/z/9K7o4zMMY is some code to demonstrate the basics. Feel free to add a cycle to test.

The result is a sorted list, to propagate information upwards (e.g. calculate the size of a struct) work from left to right. To propagate downward, right to left.

M ✌