boo-lang / boo

The Boo Programming Language.
BSD 3-Clause "New" or "Revised" License
856 stars 144 forks source link

Importing sourcefiles from booi ? #192

Closed Guevara-chan closed 5 years ago

Guevara-chan commented 5 years ago

Recently, while writing some service Boo scripts, I found myself missing good old require from Node.JS to better organize functionality and split some commonly used functions without compiling them.

Is there any way to implement such functionality with macro and interpreter control interface ?

masonwheeler commented 5 years ago

Possibly. What specific functionality are you missing that import can't do?

Guevara-chan commented 5 years ago

Import is for compiled binaries. And booi is all about interpreting (look, Boo can be a script lang), so I want direct sourcecode importing.

masonwheeler commented 5 years ago

Hmm...

That's tricky. The pipelined nature of the compiler means you can't do it as a macro, because all the parsing is finished well before macro expansion begins. (The compiler does have a sort of a way around this for internal macros, but it's weird and complicated and full of gotchas, so I'd prefer to avoid it.)

Boo.Lang.Useful.IO does have a text preprocessor that could be run before the parser starts, that could be extended to recognize a require statement and add a file to CompilerContext.Parameters.Input, but it's not set up as a compiler step.

If you really wanted to, you could build this and submit a PR, though...

Guevara-chan commented 5 years ago

Yes, I want it. Whole presence of booi was my reason to still cling with Boo with other options easiliy available. Yet, unfortunately, it's cannot my used for multifile projects easily.

masonwheeler commented 5 years ago

All right. Well as I said, feel free to implement it and submit a PR. Have a look at BooTemplate for an example of inserting a preprocessing step before the parsing step.

Guevara-chan commented 5 years ago

Seriously ? I guess it's something in Boo.Lang.Interpreter, like Eval(File(module).ReadAllText()) ?

masonwheeler commented 5 years ago

No, what you want to do is have require add a new file to the list of compiler inputs, so it will be compiled together with the file you're currently compiling. (Make sure to have a loop in there to then preprocess that file, and a check that you don't pull in any file that's already part of the build, in case two files both require the same third file.)

You want to put this in a compiler step, and insert it at the beginning of the pipeline, before Parsing. BooTemplate does something similar, so I suggested it as an example.

Guevara-chan commented 5 years ago

compiling booi

Ahem... How about no ?

masonwheeler commented 5 years ago

Booi is powered by the same compiler as booc. The only real difference is the last step; it executes the compiled code in-memory rather than saving it to disc.