Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:advanced:cram-macros [2022/04/10 23:15] – luca | tutorials:advanced:cram-macros [2022/04/10 23:34] (current) – luca | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Defining new macros ===== | + | ====== Defining new macros====== |
- | Disclamer: | + | Disclaimer: |
A general rule of thumb is to try and not define new macros if they are not needed, or if they do not serve a specific purpose, since, while they do make the code more readable in some cases, they also make it way harder to debug the code. | A general rule of thumb is to try and not define new macros if they are not needed, or if they do not serve a specific purpose, since, while they do make the code more readable in some cases, they also make it way harder to debug the code. | ||
- | ====== Defining new macros====== | ||
===== Syntax ===== | ===== Syntax ===== | ||
Line 14: | Line 13: | ||
Macros are used to extend the syntax of standard LISP by generating/ | Macros are used to extend the syntax of standard LISP by generating/ | ||
- | One important thing to note is that the computation of the body is already done at compile time and that it is possible to even quote LISP expressions, | + | One important thing to note is that the computation of the body is already done at compile time and that it is possible to even quote LISP expressions, |
For example: | For example: | ||
Line 59: | Line 58: | ||
===== Example of an CRAM-macro ===== | ===== Example of an CRAM-macro ===== | ||
<code lisp> | <code lisp> | ||
- | (defmacro with-simulated-robot (&body body) | + | CL-USER> |
- | `(let ((results | + | `(let ((results |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
</ | </ | ||
Line 75: | Line 74: | ||
Here, " | Here, " | ||
- | So now lets go through what the macro actually does. The first thing that is done, is to signal to LISP that an backquoted expression is about to start. | + | So now let' |
<code lisp> | <code lisp> | ||
Line 84: | Line 83: | ||
Those two functions/ | Those two functions/ | ||
+ | After defining result, let's see roughly how the body of our macro will expand. Since non of the expressions have a comma (,) in front of them, every function/ | ||
+ | |||
+ | <code lisp> | ||
+ | '(car ' | ||
+ | </ | ||
+ | |||
+ | To conclude: without any prior knowledge of what the other functions/ |