Arc Forumnew | comments | leaders | submitlogin
1 point by rincewind 5876 days ago | link | parent

how do you know that a variable is free at macro expansion time?


1 point by stefano 5876 days ago | link

You would need help from the compiler. Basically a macro would be called with a list of bound variables as an extra argument. For a compiler, this would be quite easy. Now a macro would look like:

  (mac my-mac (non-free-var-list my-arg1 ...)
    ...)
instead of:

  (mac my-mac (my-arg1 ...)
    ...)

-----

2 points by almkglor 5875 days ago | link

Why not just mod 'mac to something like (docstring extraction and name redefinition elided):

  (mac mac (name args . body)
    `(set ,name
       (annotate 'mac
         (fn ,(cons 'expansion-context args)
           ,@body))))
Then insert stuff in Scheme-side 'ac to use (apply macro the-free-list-or-whatever (cdr ex)) instead of (apply macro (cdr ex))

That way macros that need it can get it, while macros which don't need it can ignore it.

We can potentially define 'expansion-context as a table or function, and potentially we could allow an implementation to have expansion-context!line, say provide line number etc. information so that the macro can give decent error messages. Of course if we have a dorky backend (like mzscheme) then we can just have it return expansion-context!line as nil, because it can't somehow extract line numbers, but for implementations that can, then good.

edit: Oh and ye: the 'ac compiler on scheme-side really does keep a list of bound variables ^^

-----

1 point by rincewind 5876 days ago | link

This is a solution, but I consider this cheating, because it is nearly as tedious as manually inserting symeval around global functions.

w/var:do in my mexpr.arc is an example of a macro/dsl which can not take a free-var list as 1st arg (it could be changed, but that would make it useless)

and what about list in

  (let list (list 1 2 3) list)

-----