munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.88k stars 1.04k forks source link

Module system in Lox #1042

Closed bichanna closed 2 years ago

bichanna commented 2 years ago

Thank you for your amazing book :D

I have currently learned to write my interpreter following the book. But I don't know how to implement a module system like Python. I couldn't find any tutorials on the internet.

I appreciate it if you give me a couple of hints.

GalaxianMonster commented 2 years ago

Probably just import the module via filename, search from the std and installed modules directory to the user script directory, check if it does exist and return an error, define module map/class, get all globals from the module then add the globals to the module map/class. Might be wrong though.

bichanna commented 2 years ago

Should I do it in the parser or the interpreter? I tried it in the parser, but it gets nasty (errors thrown, and I don't know how to lead it to the imported file) when there are runtime errors in the source code.

GalaxianMonster commented 2 years ago

You may do it on runtime.

bichanna commented 2 years ago

So, parsing the imported file is done in runtime? I don't know how to do that.

nocturn9x commented 2 years ago

What do you mean? Just call the compiler from inside the VM. How you implement the import system however is more complicated. Do you go down the C route and just copy-paste like header files (in which case there's no runtime support needed but it's a horrible idea) or do you create namespaces like Python does?

bichanna commented 2 years ago

Thanks!