Syntax Issues
Yes, you can have Lisp-like macros in an imperative language, because Lisp supports compulsory programming. The main difference between macros from C and Lisp is how easy it is to manipulate the source tree:
In C, there are declarations, declarations, statements, expressions, blocks, several different control structures, labels, etc. New syntax constructs may require changes to the parser. Macros will need to build these data structures.
In Lisp, only s-expressions exist. New syntax constructs do not require changes to the parser. Only one data structure means that the API for constructing the syntax tree is very simple and easy to remember.
There are several languages ββwith more complex syntax (for example, C), but with powerful macro objects (for example, Lisp). For example, Haskell. However, the interface for writing macros in Haskell is somewhat more complicated, because you need functions to create and use type constructors, expressions, declarations, expressions, etc., and not just for one constructor for lists.
A template in a macro in Haskell has an annotated type:
[e| ... |] -- expression [d| ... |] -- declaration [t| ... |] -- type [p| ... |] -- pattern
For comparison, these letters e , d , t and p not needed in Lisp macros. They are not needed in Haskell because Haskell is strongly typed, but because annotations put the parser in the correct state so that it can parse the contents with the appropriate context. Again, Lisp syntax has only one context.
Interpreted and compiled
Most languages ββcan be interpreted, compiled, or both at the same time. C may be one or both. Lisp can be one or both. Macros require the compiler to execute code at compile time, which can be done either by interpreting the macro or compiling the macro and then executing it. Thus, interpreted-versus-compiled is really not a problem (this is not a problem in almost every discussion of languages).
Dietrich epp
source share