2 ms·
It can mean a few different things, in this case it's about programs that manipulate the program source and produce new programs (or program segments). Think of
by Jtsummers 4y ago
It can mean a few different things, in this case it's about programs that manipulate the program source and produce new programs (or program segments). Think of a code generator that can take a DSL and turn it into some valid (for execution) code:
for i in 1 to 10 do printf("%d\n", i)
Becomes (through a preprocessor in this hypothetical system) valid C as:
for(int i = 1; i <= 10; i++) { printf("%d\n", i); }
Now that's not too interesting, but it is a bit (subjective) nicer looping syntax than C's straight syntax. In the Lisp case, macros allow you to take an arbitrary set of values (the parameters to the macro) and produce some other value or values in their place. This could be just executing the code to get a kind of compile time evaluation:
(hypotehtical-expression-evaluator-macro (complex-computation-that-gets-reused-100-times-but-takes-10-minutes-each-time))
Or it could be more transformative, like Lisp's loop macro that lets you write an Algol-ish loop instead of a lisp-y loop (or using recursion or other approaches):
(loop for i from 1 to 10 do (print i))
Which gets transformed into some other valid Lisp code (not on my laptop so not in a position to actually show the result).
David Moon's "Moon's Mark Down" is actually a pretty neat case of Lisp meta programming. He defines a macro defdot which will take a name for a particular directive for his markdown syntax and generate all the necessary functions so that things "just work". And when parsing, when a . is found at the start of a line, the correct handler is calculated and called. To add a new directive, you add a new defdot and that macro will create the relevant functions and register the new handler.
http://users.rcn.com/david-moon/MMD/HTML/index.html http://users.rcn.com/david-moon/MMD/HTML/index.html
http://users.rcn.com/david-moon/MMD/MMD.lisp http://users.rcn.com/david-moon/MMD/MMD.lisp