1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
discard """
nimout: '''
Infix
Ident "=>"
Call
Ident "name"
Ident "a"
ExprColonExpr
Ident "b"
Ident "cint"
NilLit
macrocache ok
'''
output: '''
x = 10
x + y = 30
proc foo[T, N: static[int]]()
proc foo[T; N: static[int]]()
a[0]: 42
a[1]: 45
x: some string
([("key", "val"), ("keyB", "2")], [("val", "key"), ("2", "keyB")])
([("key", "val"), ("keyB", "2")], [("val", "key"), ("2", "keyB")])
0
0
0
'''
"""
import macros, sugar, macrocache
block tdump:
let
x = 10
y = 20
dump x
dump(x + y)
block texprcolonexpr:
macro def(x): untyped =
echo treeRepr(x)
def name(a, b:cint) => nil
block tgenericparams:
macro test():string =
let expr0 = "proc foo[T, N: static[int]]()"
let expr1 = "proc foo[T; N: static[int]]()"
newLit($toStrLit(parseExpr(expr0)) & "\n" & $toStrLit(parseExpr(expr1)))
echo test()
block tidgen:
# Test compile-time state in same module
var gid {.compileTime.} = 3
macro genId(): int =
result = newIntLitNode(gid)
inc gid
proc Id1(): int {.compileTime.} = return genId()
proc Id2(): int {.compileTime.} = return genId()
doAssert Id1() == 3
doAssert Id2() == 4
block tlexerex:
macro match(s: cstring|string; pos: int; sections: varargs[untyped]): untyped =
for sec in sections:
expectKind sec, nnkOfBranch
expectLen sec, 2
result = newStmtList()
var input = "the input"
var pos = 0
match input, pos:
of r"[a-zA-Z_]\w+": echo "an identifier"
of r"\d+": echo "an integer"
of r".": echo "something else"
block tcopylineinfo:
# issue #5617, feature request
type Test = object
macro mixer(n: typed): untyped =
let x = newIdentNode("echo")
x.copyLineInfo(n)
result = newLit(x.lineInfo == n.lineInfo)
var z = mixer(Test)
doAssert z
block tsetgetlineinfo:
# issue #21098, feature request
type Test = object
macro mixer1(n: typed): untyped =
let x = newIdentNode("echo")
var lineInfo = n.lineInfoObj
x.setLineInfo lineInfo
result = newLit(x.lineInfo == n.lineInfo)
macro mixer2(n: typed): untyped =
let x = newIdentNode("echo")
var lineInfo = n.lineInfoObj
lineInfo.line += 1
x.setLineInfo lineInfo
result = newLit(x.lineInfo != n.lineInfo)
doAssert mixer1(Test)
doAssert mixer2(Test)
block tdebugstmt:
macro debug(n: varargs[untyped]): untyped =
result = newNimNode(nnkStmtList, n)
for i in 0..n.len-1:
add(result, newCall("write", newIdentNode("stdout"), toStrLit(n[i])))
add(result, newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
add(result, newCall("writeLine", newIdentNode("stdout"), n[i]))
var
a: array[0..10, int]
x = "some string"
a[0] = 42
a[1] = 45
debug(a[0], a[1], x)
const
pairs = {"key": "val", "keyB": "2"}
macro bilookups(arg: static[openArray[(string, string)]]): untyped =
var a = newTree(nnkBracket)
var b = newTree(nnkBracket)
for (k, v) in items(arg):
a.add(newTree(nnkTupleConstr, newLit k, newLit v))
b.add(newTree(nnkTupleConstr, newLit v, newLit k))
result = newTree(nnkTupleConstr, a, b)
macro bilookups2(arg: untyped): untyped =
var a = newTree(nnkBracket)
var b = newTree(nnkBracket)
arg.expectKind(nnkTableConstr)
for x in items(arg):
x.expectKind(nnkExprColonExpr)
a.add(newTree(nnkTupleConstr, x[0], x[1]))
b.add(newTree(nnkTupleConstr, x[1], x[0]))
result = newTree(nnkTupleConstr, a, b)
const cnst1 = bilookups(pairs)
echo cnst1
const cnst2 = bilookups2({"key": "val", "keyB": "2"})
echo cnst2
# macrocache #11404
const
mcTable = CacheTable"nimTest"
mcSeq = CacheSeq"nimTest"
mcCounter = CacheCounter"nimTest"
static:
doAssert(mcCounter.value == 0) # CacheCounter.value
mcCounter.inc # CacheCounter.inc
doAssert(mcCounter.value == 1) # CacheCounter.value
let a = newLit(1)
let b = newLit(2)
let c = newLit(3)
let d = newLit(4)
mcSeq.add a # CacheSeq.add
mcSeq.add b # CacheSeq.add
mcSeq.add c # CacheSeq.add
doAssert(mcSeq.len == 3) # CacheSeq.len
#doAssert(c in mcSeq) # CacheSeq.contains
#doAssert(d notin mcSeq) # CacheSeq.contains
mcSeq.incl d # CacheSeq.incl
doAssert(mcSeq.len == 4) # CacheSeq.len
mcSeq.incl c # CacheSeq.incl
doAssert(mcSeq.len == 4) # CacheSeq.len
doAssert(mcSeq[3] == d) # CacheSeq.[]
#doAssert(mcSeq.pop() == d)# CacheSeq.pop
#doAssert(mcSeq.len == 3) # CacheSeq.len
doAssert(mcTable.len == 0) # CacheTable.len
mcTable["a"] = a # CacheTable.[]=
doAssert(mcTable.len == 1) # CacheTable.len
doAssert(mcTable["a"] == a) # CacheTable.[]
#doAssert("a" in mcTable) # CacheTable.contains
#doAssert(mcTable.hasKey("a"))# CacheTable.hasKey
for k, v in mcTable: # CacheTable.items
doAssert(k == "a")
doAssert(v == a)
echo "macrocache ok"
block tupleNewLitTests:
macro t(): untyped =
result = newLit (1, "foo", (), (1,), (a1: 'x', a2: @["ba"]))
doAssert $t() == """(1, "foo", (), (1,), (a1: 'x', a2: @["ba"]))"""
# this `$` test is needed because tuple equality doesn't distinguish
# between named vs unnamed tuples
doAssert t() == (1, "foo", (), (1, ), (a1: 'x', a2: @["ba"]))
from strutils import contains
block getImplTransformed:
macro bar(a: typed): string =
# newLit a.getImpl.repr # this would be before code transformation
let b = a.getImplTransformed
newLit b.repr
template toExpand() =
for ai in 0..2: echo ai
proc baz(a=1): int =
defer: discard
toExpand()
12
const code = bar(baz)
# sanity check:
doAssert "finally" in code # `defer` is lowered to try/finally
doAssert "while" in code # `for` is lowered to `while`
doAssert "toExpand" notin code
# template is expanded (but that would already be the case with
# `a.getImpl.repr`, unlike the other transformations mentioned above
# test macro resemming
macro makeVar(): untyped =
quote:
var tensorY {.inject.}: int
macro noop(a: typed): untyped =
a
noop:
makeVar
echo tensorY
macro xbenchmark(body: typed): untyped =
result = body
xbenchmark:
proc fastSHA(inputtest: string) =
discard inputtest
fastSHA("hey")
block: # issue #4547
macro lazy(stmtList : typed) : untyped =
let decl = stmtList[0]
decl.expectKind nnkLetSection
let name = decl[0][0].strVal
let call = decl[0][2].copy
call.expectKind nnkCall
let ident = newIdentNode("get" & name)
result = quote do:
var value : type(`call`)
proc `ident`() : type(`call`) =
if value.isNil:
value = `call`
value
type MyObject = object
a,b: int
# this part, the macro call and it's result (written in the comment below) is important
lazy:
let y = new(MyObject)
#[
var value: type(new(MyObject))
proc gety(): type(new(MyObject)) =
if value.isNil:
value = new(MyObject)
value
]#
doAssert gety().a == 0 # works and should work
doAssert gety().b == 0 # works and should work
doAssert not declared(y)
doAssert not compiles(y.a) # identifier y should not exist anymore
doAssert not compiles(y.b) # identifier y should not exist anymore
block: # bug #13511
type
Builder = ref object
components: seq[Component]
Component = object
proc add(builder: var Builder, component: Component) {.compileTime.} =
builder.components.add(component)
macro debugAst(arg: typed): untyped =
## just for debugging purpose.
discard arg.treeRepr
return arg
static:
var component = Component()
var builder = Builder()
template foo(): untyped =
## WAS: this doc comment causes compilation failure.
builder
debugAst:
add(foo(), component)
block: # bug #15118
macro flop(name: static string) =
let id = genSym(nskType, "env")
let r =
nnkStmtList.newTree(
nnkTypeSection.newTree(
nnkTypeDef.newTree(
id,
newEmptyNode(),
nnkRefTy.newTree(
nnkObjectTy.newTree(
newEmptyNode(),
newEmptyNode(),
nnkRecList.newTree(
nnkIdentDefs.newTree(
newIdentNode(name),
newIdentNode("int"),
newEmptyNode()
)
)
)
)
)
),
# var f: T
nnkVarSection.newTree(
nnkIdentDefs.newTree(
newIdentNode("f"),
id,
newEmptyNode()
)
),
# echo f.a
nnkCommand.newTree(
newIdentNode("new"),
newIdentNode("f")
),
nnkCommand.newTree(
newIdentNode("echo"),
nnkDotExpr.newTree(
newIdentNode("f"),
newIdentNode(name)
)
)
)
r
block:
flop("a")
block:
flop("b")
static:
block:
const containsTable = CacheTable"containsTable"
doAssert "foo" notin containsTable
containsTable["foo"] = newLit 42
doAssert "foo" in containsTable
|