]> git.lizzy.rs Git - metalua.git/blob - doc/manual/dollar-ref.tex
0f65ae5ab10bf3d0f677eef3b8326002cc777768
[metalua.git] / doc / manual / dollar-ref.tex
1 \section[Dollar extension]{{\tt dollar}: generic syntax for macros}\r
2 When you write a short-lived macro which takes reasonably short arguments, you\r
3 generally don't want to write a supporting syntax for it. The dollar library\r
4 allows you to call it in a generic way: if you store your macro in the table\r
5 {\tt mlp.macros}, say as function {\tt mlp.macros.foobar}, then you can call it\r
6 in your code as {\tt\$foobar(arg1, arg2...)}: it will receive as parameters the\r
7 ASTs of the pseudo-call's arguments.\r
8 \r
9 \paragraph{Example}~\r
10 \begin{verbatim}\r
11 -{ block:\r
12    require 'dollar'\r
13    function doller.LOG(id)\r
14       match id with `Id{ id_name } -> \r
15          return +{ printf("%s = %s", id_name, \r
16                           table.tostring(-{id})) }\r
17       end\r
18    end }\r
19 \r
20 local x = { 1, 2, 3 }\r
21 $LOG(x) -- prints "x = { 1, 2, 3 }" when executed\r
22 \end{verbatim}\r