I am building my own programming language and I came across a problem. All programming languages, I discovered, have # include, import, # import
function .I am trying to implement this same function in my own programming language, but I am not sure how to.
Could you explain how import functions operate, how compilers interpret them, and how I can do the same thing in my own programming language?
The project Project Link
#include
is rather simple actually, if you want to implement it yourself.
The way I usually implement it (when not using a "proper" preprocessor) is to treat the source files as a stack.
When you encounter a #include
directive, get the whole line, parse it and figure out what source file to "include". Then push it onto the stack.
The lexer will use the file at the top of the stack to read from. When it reached end of file the lexer pops the stack and continues reading from the new top.
When the stack is empty, you have finished with the original file.