Re: Macro for creating ‘hooks’?

2019-11-13 Thread mratsim
You can PR that in ;)

Re: Macro for creating ‘hooks’?

2019-11-13 Thread antoinets
Awesome, I’ll be watching the repo! For posterity, I also made a version that supports any number of hooks registered under one name, based on the compile-time table version. import tables, macros var HOOKS {.compileTime.}: Table[string, seq[NimNode]] macro add_hoo

Re: Macro for creating ‘hooks’?

2019-11-12 Thread mratsim
I created a cookbook repo to store some recipes, I had (I have plenty sprayed over my repos!). Yours is the first one: [https://github.com/status-im/nim-cookbook/blob/264cc90d/macros_register_hooks.nim](https://github.com/status-im/nim-cookbook/blob/264cc90d/macros_register_hooks.nim)

Re: Macro for creating ‘hooks’?

2019-11-06 Thread mratsim
the identifiers doesn't need to be valid/existing but it prevents passing a whole statement or parenthesis or brackets. It's really optional, just an extra check.

Re: Macro for creating ‘hooks’?

2019-11-06 Thread Hlaaftana
For some reason the "parameter constraints" section of what was once in the Nim manual was moved to the experimental features manual along with term rewriting macros, even though it is an independent feature from them. [Here](https://nim-lang.org/docs/manual_experimental.html#term-rewriting-macr

Re: Macro for creating ‘hooks’?

2019-11-05 Thread antoinets
Thanks mratsim, the use of `{.compileTime.}` was what I was missing :) One follow-up question: what does the `untyped{ident}` syntax mean? Is it restricting the argument to valid identifiers? Thanks

Re: Macro for creating ‘hooks’?

2019-11-04 Thread mratsim
And second version, with run_hook as pragma import tables, macros var hooks {.compileTime.}: Table[string, NimNode] macro registerHook(name: untyped{ident}, body: untyped): untyped = result = newStmtList() result.add newProc( name = name,

Re: Macro for creating ‘hooks’?

2019-11-04 Thread mratsim
Version 1, exactly what you asked. import tables, macros var hooks {.compileTime.}: Table[string, NimNode] macro registerHook(name: untyped{ident}, body: untyped): untyped = result = newStmtList() result.add newProc( name = name, body = bo

Macro for creating ‘hooks’?

2019-11-04 Thread antoinets
Hi there, I want to write macros that takes code written in one place and inserts it elsewhere. In use this would look something like: registerHook post_foo: echo “foo done. x=“, x ... proc foo = let x = 10 runHooks post_foo Run My