Skip to content

Commit

Permalink
simplify: do:=>:
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jul 14, 2019
1 parent 78be6a2 commit e92090e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/core/macros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ macro genAst*(options: static set[GenAstOpt] = {}, args: varargs[untyped]): unty
let xignoredLocal = kfoo4
proc localExposed(): auto = kfoo4 # implicitly captured
let x3 = newLit kfoo4
result = genAst({}, s1=2, s2="asdf", x0=newLit x0, x1=x1, x2, x3) do:
result = genAst({}, s1=2, s2="asdf", x0=newLit x0, x1=x1, x2, x3):
# echo xignored # would give: Error: undeclared identifier
# echo s0 # would give: Error: internal error: expr: var not init s0_237159
(s1, s2, x0, x1, x2, x3, localExposed())
Expand All @@ -1381,7 +1381,7 @@ macro genAst*(options: static set[GenAstOpt] = {}, args: varargs[untyped]): unty
let s1 = "not captured!" ## does not override `s1=2`
let xignoredLocal = kfoo4
let x3 = newLit kfoo4
result = genAst({kNoExposeLocalInjects}, s1=2, s2="asdf", x0=newLit x0, x1=x1, x2, x3) do:
result = genAst({kNoExposeLocalInjects}, s1=2, s2="asdf", x0=newLit x0, x1=x1, x2, x3):
## only captures variables from `genAst` argument list
## uncaptured variables will be set from caller scope (Eg `s0`)
## `x2` is shortcut for the common `x2=x2`
Expand Down
14 changes: 7 additions & 7 deletions tests/macros/mgenast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ type Foo* = enum kfoo0, kfoo1, kfoo2, kfoo3, kfoo4
proc myLocalPriv(): auto = kfoo1
proc myLocalPriv2(): auto = kfoo1
macro bindme2*(): untyped =
genAst({}) do: myLocalPriv()
genAst({}): myLocalPriv()
macro bindme3*(): untyped =
## myLocalPriv must be captured explicitly
genAst({kNoExposeLocalInjects}, myLocalPriv) do: myLocalPriv()
genAst({kNoExposeLocalInjects}, myLocalPriv): myLocalPriv()

macro bindme4*(): untyped =
## calling this won't compile because `myLocalPriv` isn't captured
genAst({kNoExposeLocalInjects}) do: myLocalPriv()
genAst({kNoExposeLocalInjects}): myLocalPriv()

macro bindme5UseExpose*(): untyped =
genAst({}) do: myLocalPriv2()
genAst({}): myLocalPriv2()

macro bindme5UseExposeFalse*(): untyped =
genAst({kNoExposeLocalInjects}) do: myLocalPriv2()
genAst({kNoExposeLocalInjects}): myLocalPriv2()

## example from https://github.com/nim-lang/Nim/issues/7889
from std/streams import newStringStream, readData, writeData

macro bindme6UseExpose*(): untyped =
genAst({}) do:
genAst({}):
var tst = "sometext"
var ss = newStringStream("anothertext")
writeData(ss, tst[0].addr, 2)
Expand All @@ -36,7 +36,7 @@ macro bindme6UseExpose*(): untyped =
macro bindme6UseExposeFalse*(): untyped =
## without kexposeLocalInjects, requires passing all referenced symbols
## which can be tedious
genAst({kNoExposeLocalInjects}, newStringStream, writeData, readData) do:
genAst({kNoExposeLocalInjects}, newStringStream, writeData, readData):
var tst = "sometext"
var ss = newStringStream("anothertext")
writeData(ss, tst[0].addr, 2)
Expand Down
18 changes: 9 additions & 9 deletions tests/macros/tgenast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ block:
let s1 = "not captured!"
let xignoredLocal = kfoo4
let x3 = newLit kfoo4
result = genAst({kNoExposeLocalInjects}, s1=2, s2="asdf", x0=newLit x0, x1=x1, x2, x3) do:
result = genAst({kNoExposeLocalInjects}, s1=2, s2="asdf", x0=newLit x0, x1=x1, x2, x3):
doAssert not declared(xignored)
doAssert not declared(xignoredLocal)
(s1, s2, s0, x0, x1, x2, x3)
Expand Down Expand Up @@ -36,7 +36,7 @@ block:

macro m1(): untyped =
# result = quote do: # Error: undeclared identifier: 'a1'
result = genAst({}) do:
result = genAst({}):
template `a1=`(x: var Foo, val: int) =
x.a = val

Expand All @@ -51,7 +51,7 @@ block:
result = newStmtList()
macro foo(c: bool): untyped =
var b = false
result = genAst({}, b = newLit b, c) do:
result = genAst({}, b = newLit b, c):
fun(b, c)

foo(true)
Expand All @@ -61,15 +61,15 @@ block:
# since `==` works with genAst, the problem goes away
macro foo2(): untyped =
# result = quote do: # Error: '==' cannot be passed to a procvar
result = genAst({}) do:
result = genAst({}):
`==`(3,4)
doAssert not foo2()

block:
# fix https://github.com/nim-lang/Nim/issues/7726
macro foo(): untyped =
let a = @[1, 2, 3, 4, 5]
result = genAst({}, a, b = a.len) do: # shows 2 ways to get a.len
result = genAst({}, a, b = a.len): # shows 2 ways to get a.len
(a.len, b)
doAssert foo() == (5, 5)

Expand All @@ -82,15 +82,15 @@ block:
let info = args.lineInfoObj
let fun1 = bindSym"fun1" # optional; we can remove this and also the
# capture of fun1
result = genAst({}, info = newLit info, fun1) do:
result = genAst({}, info = newLit info, fun1):
(fun1(info), fun2(info.line))
doAssert bar2() == ("bar1", "bar2")

macro bar(args: varargs[untyped]): untyped =
let info = args.lineInfoObj
let fun1 = bindSym"fun1"
let fun2 = bindSym"fun2"
result = genAst({kNoExposeLocalInjects}, info = newLit info) do:
result = genAst({kNoExposeLocalInjects}, info = newLit info):
(fun1(info), fun2(info.line))
doAssert bar() == ("bar1", "bar2")

Expand Down Expand Up @@ -118,7 +118,7 @@ block:

proc funLocal(): auto = kfoo4

result = genAst({}, x1=newLit x1, x2, x3, x4 = newLit x4) do:
result = genAst({}, x1=newLit x1, x2, x3, x4 = newLit x4):
# local x1 overrides remote x1
when false:
# one advantage of using `kNoExposeLocalInjects` is that these would hold:
Expand Down Expand Up @@ -149,7 +149,7 @@ block:
# fix https://github.com/nim-lang/Nim/issues/8220
macro foo(): untyped =
# kNoExposeLocalInjects needed here
result = genAst({kNoExposeLocalInjects}) do:
result = genAst({kNoExposeLocalInjects}):
let bar = "Hello, World"
&"Let's interpolate {bar} in the string"
doAssert foo() == "Let's interpolate Hello, World in the string"

0 comments on commit e92090e

Please sign in to comment.