-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
macros: add
stamp
for unquoting a template body (#1426)
## Summary Add `stamp` to `std/macros` , which takes a template body and applies it immediately. This is the same behaviour as the previous `quote` that was changed after #1393. ## Details After #1393 we have a hole in the stdlib for `quote` -like interpolation but with automatic binding of symbols to reduce boilerplate. Various libraries within the ecosystem such as `criterion` and `npeg` have found the feature useful and extensively employ it. Reintroduces old `quote` behavior as `stamp` , without support for custom operators and explicitly document the template-like nature of `stamp` 's body as well as its pitfalls. This macro was originally written and shared to Matrix by @zerbina. --------- Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com> Co-authored-by: Saem Ghani <saemghani+github@gmail.com>
- Loading branch information
1 parent
5178f0a
commit 89b45fc
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
discard """ | ||
description: Tests for macros.stamp | ||
""" | ||
|
||
import std/macros | ||
|
||
block binder: | ||
## Test automatic binding behavior | ||
macro simple() = | ||
let ast = stamp(echo "hello") | ||
doAssert ast[0] == bindSym"echo" | ||
|
||
simple() | ||
|
||
macro noResult() = | ||
let ast = stamp(result) | ||
doAssert ast.kind == nnkIdent | ||
doAssert eqIdent(ast, "result") | ||
|
||
noResult() |