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 22:32] – [Syntax] 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 57: | Line 56: | ||
If we need to remove the outermost parenthesis, | If we need to remove the outermost parenthesis, | ||
+ | ===== Example of an CRAM-macro ===== | ||
+ | <code lisp> | ||
+ | CL-USER> (defmacro with-simulated-robot (&body body) | ||
+ | `(let ((results | ||
+ | | ||
+ | | ||
+ | , | ||
+ | (car (cram-projection:: | ||
+ | </ | ||
+ | |||
+ | When analyzing this macro the first thing we can notice is that we only have (&body body) as our parameter list. This means, that this macro will be used as a prefix to code that we want to run on our robot. For example: | ||
+ | |||
+ | <code lisp> | ||
+ | CL-USER> (with-simulated-robot | ||
+ | code that does awesome stuff) | ||
+ | </ | ||
+ | |||
+ | Here, " | ||
+ | So now let's go through what the macro actually does. The first thing that is done, is to signal to LISP that a backquoted expression is about to start. Here, a variable " | ||
+ | |||
+ | <code lisp> | ||
+ | ' | ||
+ | ' | ||
+ | evaluation of our code that does awesome stuff)) | ||
+ | </ | ||
+ | |||
+ | 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/ |