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:29] – [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. | ||
- | ==== Syntax ==== | + | ===== Syntax |
<code lisp> | <code lisp> | ||
Line 13: | 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 30: | Line 30: | ||
Now (forty-two) is equivalent to (* 2 21), which is evaluated to 42 during runtime. | Now (forty-two) is equivalent to (* 2 21), which is evaluated to 42 during runtime. | ||
- | === Parameter List === | + | ===== Parameter List ===== |
- | Similarly to functions, you can use & | + | Similarly to functions, you can use & |
- | === Backquote/ | + | ===== Backquote/ |
To make it easier to read/write complex expressions, | To make it easier to read/write complex expressions, | ||
Line 53: | Line 53: | ||
Note that in this example, we want the function calculate-time to be quoted in our code, since we told the macro to evaluate ccase during compile-time but want to evaluate calculate-time during runtime. | Note that in this example, we want the function calculate-time to be quoted in our code, since we told the macro to evaluate ccase during compile-time but want to evaluate calculate-time during runtime. | ||
- | == Splicing == | + | ==== Splicing |
- | If we need to remove the outermost | + | If we need to remove the outermost |
+ | ===== 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/ |