[nim-dev] Re: Code generation from AST with annotations in Nim like in C

  • From: Krux02 <arne.doering@xxxxxxx>
  • To: nim-dev@xxxxxxxxxxxxx
  • Date: Sun, 4 Dec 2016 16:09:05 +0000

I would really like to answer your question with yes, but I do not entirely 
understand your question.

Does your own answer mean you already answered the question yourself?

Just one hint from me who already wrote a few more macros.

instead of this:

```Nim
case definition.kind
of nnkProcDef:
  [...]
else:
  error([...])
```

you can do this: ``definition.expectKind(nnkProcDef)``

And for the AST generation I really recommend you to use ``quote do:`` as much 
as possible, even for smaller code sections. The advantage is not only that it 
is more readable, but also nodes generated with ``quote do:`` have positional 
information, meaning when you get an error in your macro you actually get to 
know where the error comes from. One thing that you should take care of though 
when you try this approach is that you need to assign expressions to 
identifiers first before you can insert them in the ast.

```Nim
let something = generateSomethingNode([...])
result = quote do:
  bananas = `something` * 3
```




Other related posts: