Macros are essentially a way of writing code that writes other code, which is also known as meta programming.
The best way to understand macros and how they work is to create one.
So let's create a greatest common denominator macro.
And to start off we will just say macro rules and it will give us a template for creating a macro.
And now we will give our macro a name.
And in this case it will be GCD, which for us to call our macro, we just say GCD with the exclamation
point.
Now we are going to give two arguments for our macro and an argument is faced with a dollar sign.
So we'll call our argument one, we'll just call it A, and it's going to be a token of type expr,
which stands for expression.
And then we'll do the same thing for our second expression.
And now in here, we can start.
Performing our logic for our greatest common denominator.
So we'll say let mute m equals to.
Our B parameter noticed I did money B and then we'll say let mute n is equal to money a.
And now we'll execute our loop.
So while M does not equal to zero.
We'll say if M is less than NW, let t equals m m equal nw and then n equals t, and now we do m equals
m mod n.
And then after that loop is done executing, we just return n.
And that's a basic algorithm for computing the GCD.
So let's give our macro a test run and I want to print it out.
And so we know that the GCD of 14 and 15 is going to be one.
So by calling GCD macro here, what this does is.
Exxon 14 and 15 here respectively.
So a will be 14 and B will be 15.
And then.
It essentially just executes this code for us.
So remember when I said it is a way of writing code that writes other code.
So when we call GCD or compile this macro, I should say, and then run it when we compile it, it's
going to write this code into the binary for us.
Which is, as I said, meta programming.
So if we run this, we expect one to be printed out, which is exactly what we got.
And now if we just change it to 16 now we should have two.
So it looks like everything is working correctly.
And so that's really all there is to it, to a macro.
It's basically just creating a.
Code that's going to write other code for us and allows us to reuse it in a much simpler manner, such
as just saying GCD with an exclamation point.
So I hope you enjoyed that quick little example on how to create a declarative macro.