To understand C programs, study their data structures

Coming from writing python and coffeescript fluently, I've had lot of trouble at writing my virtual machine in C. It was so much trouble that I considered designing an another language so I could use that to write the virtual machine using that instead.

Switching to working on implementation of a second programming language would have easily meant doubled effort. It would have had to compile into C, or assembly. But I would prefer to do exactly such tasks in the language I'm working on, meaning that it should be the language I'm already developing. That's quite hard, so I will rather code my VM in C at first, so I would get something I can use, while I'm working on it.

Last friday I ended up collecting every struct I have in the program inside a single file. I did this for implementing garbage collection, as the author must be aware about the structure of every record in the program. I noticed it made the whole program easier to understand.

In a well-structured python program, you would expect to find the most important things about the program first in each file. Each file is a territory, containing code for certain functions required by the whole program. Lot of the complexity is maintained by visibility. Locations of the variables and where the values can be reached tells about the structure of the program.

In plain C programs, the files still group functions by some theme, but location in file does not mark importance of any kind. Overview of the program, and what the program is working on is visible in the header files, inside structs that describe the layout of the data in the program.

I ricocheted to reading zlib and sqlite source code. It seemed to be productive way to analyse the C source code. You're getting clear idea of what the program is working on, bit like those first lines in the well written python program. Then I tried the same thing on the triplane classic's source code. Simply doing grep -R "struct " splattered the whole thing open to my face.

It's lot of things to think about. This effect feels too useful to just let ignored. It might end up into improvement in my language.

Similar posts