New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add guest post. #90
Add guest post. #90
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for writing this, it's great to get more learning resources for this topic :)
@@ -0,0 +1,120 @@ | |||
|
|||
--- | |||
title: "Create a simple macro" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps "Creating a simple macro" would be better
|
||
```nim | ||
template adder(graph, src, dest): untyped = | ||
graph.addEdge(initEdge(initNode(src), initNode(dest))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be consistent with your indentation, this has 3 spaces :)
this: `` `->`(result, "Boston", "Providence") ``. A macro, however can be used | ||
to create a simpler and more appealling syntax. | ||
|
||
> Macros can be used to implement domain specific languages. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will look a bit odd as a blockquote. Might be better to recreate the admonition HTML generated in this post: https://nim-lang.org/blog/2017/10/02/documenting-profiling-and-debugging-nim-code.html
(It uses adoc for this, but you should be able to just write that HTML into the document)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hard for me. Can you provide the html?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm
|
||
```nim | ||
proc buildCityGraph(): Digraph = | ||
result = initGraph() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to generate this in the macro?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean the init function? Well I thought (also sections like edges: ...) so but I just wanted something really simple.
``` | ||
|
||
What is important to understand now is that, while we are done, procedurally | ||
modifying an AST is not the correct solution. It is a tree after all. Also it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh? This doesn't explain why that is not a correct solution at all. It looks perfectly correct to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopping someone can provide better reasoning here but i stand by this opinion.
From making this simple macro we can take away how to structure procs that | ||
transform the Nim AST. | ||
|
||
> **Bonus excersise** In the ``buildCityGraph`` proc we see there is an undirected edge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would write this like so:
"There is an undirected edge in the buildCityGraph
proc. Can you ..."
It reads better because it's more concise.
Hello, as you might know Nim is a powerful programming language that supports | ||
metaprogramming using macros. Though a lot of Nim programmers are unaware about | ||
the merits of them due to lack of learning resources. In the first part of | ||
this series we will discuss the use of macros to simplify the creation of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend going through and removing any use of "we" in sentences. This sentence can be rewritten to:
"The first part of this series will discuss the ..."
It makes sentences more concise and thus easier to read.
if n.kind == nnkInfix and $n[0] == "->": | ||
# we pass the template to getAst to avoid constructing | ||
# the AST manualy | ||
result.add getAst(adder(head, n[1], n[2])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great use of getAst
.
# a parent node is created with the correct kind. | ||
result = copyNimNode(body) | ||
for n in body: | ||
result.add graphDslImpl(head, n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why this code is better than the version above.
title: "Create a simple macro" | ||
author: Antonis Geralis | ||
--- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a "Guest Post" div, like done here:
https://nim-lang.org/blog/2018/01/22/yes-command-in-nim.html
Look in the underlying .md to see how it's done.
the possibility of using an operator with a nice syntax. Although operators can | ||
have more than two parameters, we are limited into calling them like | ||
this: `` `->`(result, "Boston", "Providence") ``. A macro, however can be used | ||
to create a simpler and more appealling syntax. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually a template would work for this too. I understand that you want to show off macros here, so add maybe "(A template could do so too here, but I want to show how macros work.)"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, you mean, result is hardcoded in the template?
result.add getAst(adder(head, n[1], n[2])) | ||
``` | ||
|
||
What is important to understand now is that, while we are done, procedurally |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this paragraph to "This macro is incomplete however, it doesn't replace nested usages of ->
. We would like to replace ->
anywhere in the passed body
, so let's use recursion with the help of a helper called graphDslImpl
.
@dom96 done. |
No description provided.