I'm writing my own compiler and I'm struggling to implement a module system. Can someone guide me, how should this be done? how other languages tackle this? Also I'm trying to avoid what c and c++ do (header files). I do like the module system in Go/Golang though.
I don't know if this is relevant, but I'm using LLVM (maybe there's a magic way to import symbols).
my initial approach:
- read and parse the entry point source file ie. main.mylang.
- go through the imports of main.mylang
- for each import: read, parse and resolve it's imports ...
this leads to a tree structure:
- main.mylang: import1.mylang, import2.mylang, import3.mylang
- import1.mylang: import4.mylang, import5.mylang
- import2.mylang: import6.mylang
... etc.
then I would traverse each node and copy it's symbols (functions, global variables, etc.) to the parent node's symbol table. if a parent node is null, it's an entry point file and the compiler can start output object files.
why do I think that this is bad?
- it's very slow, even when compiling 3-5 source files
- it's easy to cause name collisions
- you have to import the entire symbol table, because the imported file's exported symbols depend on the internal ones. for example: imagine an exported function that modifies an internal global variable
Thanks in advance