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

Merged
merged 4 commits into from Jun 7, 2018

Conversation

Projects
None yet
2 participants
@ghost
Copy link

ghost commented Jun 3, 2018

No description provided.

notTito added some commits Jun 3, 2018

notTito
notTito
@dom96
Copy link
Member

dom96 left a comment

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"

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

Perhaps "Creating a simple macro" would be better


```nim
template adder(graph, src, dest): untyped =
graph.addEdge(initEdge(initNode(src), initNode(dest)))

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

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.

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

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)

This comment has been minimized.

@ghost

ghost Jun 5, 2018

This is hard for me. Can you provide the html?

This comment has been minimized.

@ghost

```nim
proc buildCityGraph(): Digraph =
result = initGraph()

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

Wouldn't it be better to generate this in the macro?

This comment has been minimized.

@ghost

ghost Jun 5, 2018

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

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

Huh? This doesn't explain why that is not a correct solution at all. It looks perfectly correct to me.

This comment has been minimized.

@ghost

ghost Jun 5, 2018

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.

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

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

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

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]))

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

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)

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

I don't see why this code is better than the version above.

title: "Create a simple macro"
author: Antonis Geralis
---

This comment has been minimized.

@dom96

dom96 Jun 5, 2018

Member

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.

This comment has been minimized.

@Araq

Araq Jun 5, 2018

Member

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.)"

This comment has been minimized.

@ghost

ghost Jun 5, 2018

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

This comment has been minimized.

@Araq

Araq Jun 5, 2018

Member

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 .

notTito
@ghost

This comment has been minimized.

Copy link

ghost commented Jun 5, 2018

@dom96 done.

notTito
Fix

@dom96 dom96 merged commit 4da3f25 into nim-lang:master Jun 7, 2018

dom96 added a commit that referenced this pull request Jun 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment