tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.03k stars 48 forks source link

Add macro, which returns the function of a macro #132

Closed porky11 closed 7 years ago

porky11 commented 7 years ago

I tried to implement a simple macro, which tries to instantiate a concept macro, that should define a function with the same name as the macro, and if this function exists, it should expand to this macro. But instantiate cannot be used with variables, only as macro, so the macro will need to expand to some macros which do this. But then the function wouldn't be defined in the namespace, whre the macro is expanded, but inside a function definition (because + is only used inside of functions), and so only accessible inside the function, and would have to be redefined inside every function, (in case functions could be defined inside functions, now it just doesn't work). Generate the macro inside the macro, and evaluate it seems like the most intuitive way, but this would require eval-expression be implemented for (p DNode) too. A simpler approach may be adding a way to extract the function of a macro, (would be in introspection module, I think, and I didn't find it). This macro for doing this would require a list of N types and optional rest as last argument, and return the matching function, as a pointer casted to a function with return type (p Dnode) and a argument-list of a (p MContext) and N (p DNodes), which is optional variadic. Then (macro-function instantiate-fn \ rest) (analog to (# fn …) may return something, that can be called at macro expansion time to instantiate a fn.

porky11 commented 7 years ago

I had some misconception. Getting the macro-function doesn't help anything. Evaluating macros at compiletime also does not work, because the function would have to be defined in a specific namespace anyway. So I'll open another issue for this.