molten icon indicating copy to clipboard operation
molten copied to clipboard

are modules supported?

Open naturallymitchell opened this issue 5 years ago • 1 comments

module: a collection of variables and functions contained in a single file

naturallymitchell avatar Jan 24 '19 07:01 naturallymitchell

Yes, modules are supported although I need to improve how they work currently.

At the moment, modules are implicit. Each source file is a module and all the functions and classes at the top level scope of the source file are exported (listed in the module's corresponding .dec file). A module run function is generated for each module that initializes and runs all top level expressions. By passing in the -l flag when compiling, it will not insert a "main" function, so all modules except the entry point should be compiled with -l. Using the "import" keyword, you can import the declarations from another module, and then you can later link the resulting files. There is a limitation atm where you must compile all files from the same directory, or there will be a link error. This is because I used a pretty hacky way of determining the module run function's name, using the import path. The core library, libcore, is a module and the molten script will compile it if necessary and link it to whatever you're trying to run.

I've partially added a means of controlling what symbols are exported, and I will eventually make a means of importing a module as an object to keep the functions scoped under one name, although I'm not yet sure what syntax and semantics I want for that. I will also eventually memoize the module object so that even if a module is "import"ed multiple times, it will only run the module's top level expressions once and return the module object on all subsequent imports.

transistorfet avatar Jan 24 '19 17:01 transistorfet