murarth / ketos

Lisp dialect scripting and extension language for Rust programs
Apache License 2.0
751 stars 45 forks source link

Define macro in module #16

Closed hauleth closed 8 years ago

hauleth commented 8 years ago

How to define macro in Rust module?

murarth commented 8 years ago

Macros are just lambda objects inserted into the GlobalScope macro namespace. There's no public interface for compiling a lambda from Rust. I can't imagine one that would be more convenient or useful than simply compiling a (macro (foo) ...) expression within the desired scope. The addition of a run method on ModuleBuilder would ease this. Would that be sufficient for your purposes?

Edit: On second thought, it wouldn't need to be added to ModuleBuilder, which currently has a nice no-error/Result interface. But some free function, e.g. run_code, could run code in a given scope.

hauleth commented 8 years ago

This will be answer for all of #15, #14 and this.

My use case is that I am in process of writing Capistrano-like deployment tool (TBA soon) that use Ketos as description language. Due that I need some functionalities (like macro and structure definitions) to be available through Rust-defined modules. So any solution that would allow me to provide such functionality would be really helpful.

That is also reason why I want do provide some kind of additional "core". It would be much easier for the user to not require to add (use core :all) at the beginning of each file.

murarth commented 8 years ago

I've added run_code_in_scope, which is re-exported at crate root.

This should solve this issue and #14, but #15 can be left open until I make some change to module loading/initialization API.