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

  • From: komerdoor <komerdoor@xxxxxxxxx>
  • To: nim-dev@xxxxxxxxxxxxx
  • Date: Sun, 27 Nov 2016 13:49:59 +0000

I use annotation macros in C to generate new C code from the AST.

For example in C, where the X macro uses the GNU annotate attribute, I do the 
following:

```c
// Type definition (can access fields from script)
X(script) typedef struct {
  int value;
} something_t; // yes I know it violates POSIX to use _t

// Forward declaration (only the interface is needed to generate script 
bindings)
X(script) X(inc_op) something_t something_inc(const something_t something);
```

Then I wrote a code generator using libclang that reads the AST and generates 
script bindings in C for every AST node that is annotated with "script".

I also use annotations to automatically generate transparent networking code, 
save/load functionality etc.

The generated code may contain new structures and functions based on the 
annotated ones which can be used from other C code afterwards.

Is something like this possible in Nim as well?









Other related posts: