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 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
discard """
output: '''
0
1
2
3
0
1
2
3
wth
3
2
1
0
(total: 6)
S1
5
abc
'''
"""
# bug #1915
import macros
# Test that parameters are properly gensym'ed finally:
template genNodeKind(kind, name: untyped) =
proc name*(children: varargs[NimNode]): NimNode {.compiletime.}=
result = newNimNode(kind)
for c in children:
result.add(c)
genNodeKind(nnkNone, None)
# Test that generics in templates still work (regression to fix #1915)
# bug #2004
type Something = object
proc testA(x: Something) = discard
template def(name: untyped) =
proc testB[T](reallyUniqueName: T) =
`test name`(reallyUniqueName)
def A
var x: Something
testB(x)
# bug #2215
# Test that templates in generics still work (regression to fix the
# regression...)
template forStatic(index, slice, predicate: untyped) =
const a = slice.a
const b = slice.b
when a <= b:
template iteration(i: int) =
block:
const index = i
predicate
template iterateStartingFrom(i: int) =
when i <= b:
iteration i
iterateStartingFrom i + 1
iterateStartingFrom a
proc concreteProc(x: int) =
forStatic i, 0..3:
echo i
proc genericProc(x: auto) =
forStatic i, 0..3:
echo i
concreteProc(7) # This works
genericProc(7) # This doesn't compile
import tables
# bug #9476
proc getTypeInfo*(T: typedesc): pointer =
var dummy: T
getTypeInfo(dummy)
macro implementUnary(op: untyped): untyped =
result = newStmtList()
template defineTable(tableSymbol) =
var tableSymbol = initTable[pointer, pointer]()
let tableSymbol = genSym(nskVar, "registeredProcs")
result.add(getAst(defineTable(tableSymbol)))
template defineRegisterInstantiation(tableSym, regTemplSym, instSym, op) =
template regTemplSym*(T: typedesc) =
let ti = getTypeInfo(T)
proc instSym(xOrig: int): int {.gensym, cdecl.} =
let x {.inject.} = xOrig
op
tableSym[ti] = cast[pointer](instSym)
let regTemplSymbol = ident("registerInstantiation")
let instSymbol = ident("instantiation")
result.add(getAst(defineRegisterInstantiation(
tableSymbol, regTemplSymbol, instSymbol, op
)))
echo result.repr
implementUnary(): x*x
registerInstantiation(int)
registerInstantiation(float)
# bug #10192
template nest(body) {.dirty.} =
template p1(b1: untyped) {.dirty, used.} =
template implp1: untyped {.dirty.} = b1
template p2(b2: untyped) {.dirty, used.} =
template implp2: untyped {.dirty.} = b2
body
implp1
implp2
template test() =
nest:
p1:
var foo = "bar"
p2:
doAssert(foo.len == 3)
test()
# regression found in PMunch's parser generator
proc namedcall(arg: string) =
discard
macro m(): untyped =
result = quote do:
(proc (arg: string) =
namedcall(arg = arg)
echo arg)
let meh = m()
meh("wth")
macro foo(body: untyped): untyped =
result = body
template baz(): untyped =
foo:
proc bar2(b: int): int =
echo b
if b > 0: b + bar2(b = b - 1)
else: 0
echo (total: bar2(3))
baz()
# bug #12121
macro state_machine_enum(states: varargs[untyped]) =
result = nnkTypeSection.newTree(
nnkTypeDef.newTree(
nnkPragmaExpr.newTree(ident("State"), nnkPragma.newTree(ident("pure"))),
newEmptyNode(),
nnkEnumTy.newTree(newEmptyNode())
)
)
for s in states:
expectKind(s, nnkIdent)
result[0][2].add s
template mystate_machine(body: untyped) =
state_machine_enum(S1, S2, S3)
var state_stack: seq[State]
template state_current(): State {.inject, used.} =
state_stack[^1]
template state_push(state_name) {.inject, used.} =
state_stack.add State.state_name
template state_pop(n = 1) {.inject, used.} =
state_stack.setLen(state_stack.len - n)
body
mystate_machine:
state_push(S1)
echo state_current()
state_pop()
# bug #15075
block: #Doesn't work
template genGenTempl: untyped =
proc loop(locals: int)
proc loop(locals: int) = discard
genGenTempl()
let pool = loop
block: #Doesn't work
macro genGenMacro: untyped =
quote do:
proc loop(locals: int)
proc loop(locals: int) = discard
genGenMacro()
let pool = loop
block: #This works
proc loop(locals: int)
proc loop(locals: int) = discard
let pool = loop
#Now somewhat recursive:
type Cont = ref object of RootObj
fn*: proc(c: Cont): Cont {.nimcall.}
block: #Doesn't work
template genGenTempl(): untyped =
proc loop(locals: Cont): Cont
proc loop(locals: Cont): Cont =
return Cont(fn: loop)
proc doServer(): Cont =
return Cont(fn: loop)
genGenTempl()
discard doServer()
block: #Doesn't work
macro genGenMacro(): untyped =
quote:
proc loop(locals: Cont): Cont
proc loop(locals: Cont): Cont =
return Cont(fn: loop)
proc doServer(): Cont =
return Cont(fn: loop)
genGenMacro()
discard doServer()
block: #This works
proc loop(locals: Cont): Cont
proc loop(locals: Cont): Cont =
return Cont(fn: loop)
proc doServer(): Cont =
return Cont(fn: loop)
discard doServer()
#And fully recursive:
block: #Doesn't work
template genGenTempl: untyped =
proc loop(locals: int)
proc loop(locals: int) = loop(locals)
genGenTempl()
let pool = loop
block: #Doesn't work
macro genGenMacro: untyped =
quote do:
proc loop(locals: int)
proc loop(locals: int) = loop(locals)
genGenMacro()
let pool = loop
block: #This works
proc loop(locals: int)
proc loop(locals: int) = loop(locals)
let pool = loop
block:
template genAndCallLoop: untyped =
proc loop() {.gensym.}
proc loop() {.gensym.} =
discard
loop()
genAndCallLoop
block: #Fully recursive and gensymmed:
template genGenTempl: untyped =
proc loop(locals: int) {.gensym.}
proc loop(locals: int) {.gensym.} = loop(locals)
let pool = loop
genGenTempl()
block: #Make sure gensymmed symbol doesn't overwrite the forward decl
proc loop()
proc loop() = discard
template genAndCallLoop: untyped =
proc loop() {.gensym.} =
discard
loop()
genAndCallLoop()
template genLoopDecl: untyped =
proc loop()
template genLoopDef: untyped =
proc loop() = discard
block:
genLoopDecl
genLoopDef
loop()
block:
proc loop()
genLoopDef
loop()
block:
genLoopDecl
proc loop() = discard
loop()
block: #Gensymmed sym sharing forward decl
macro genGenMacro: untyped =
let sym = genSym(nskProc, "loop")
nnkStmtList.newTree(
newProc(sym, body = newEmptyNode()),
newCall(sym),
newProc(sym, body = newStmtList()),
)
genGenMacro
# inject pragma on params
template test(procname, body: untyped): untyped =
proc procname(data {.inject.}: var int = 0) =
body
test(hello):
echo data
data = 3
var data = 5
hello(data)
# bug #5691
template bar(x: typed) = discard
macro barry(x: typed) = discard
var a = 0
bar:
var a = 10
barry:
var a = 20
bar:
var b = 10
barry:
var b = 20
var b = 30
# template bar(x: static int) = discard
#You may think that this should work:
# bar((var c = 1; echo "hey"; c))
# echo c
#But it must not! Since this would be incorrect:
# bar((var b = 3; const c = 1; echo "hey"; c))
# echo b # <- b wouldn't exist
discard not (let xx = 1; true)
discard xx
template barrel(a: typed): untyped = a
barrel:
var aa* = 1
var bb = 3
export bb
# Test declaredInScope within params
template test1: untyped =
when not declaredInScope(thing):
var thing {.inject.}: int
proc chunkedReadLoop =
test1
test1
template test2: untyped =
when not not not declaredInScope(thing):
var thing {.inject.}: int
proc chunkedReadLoop2 =
test2
test2
test1(); test2()
block: # bug #22846
template foo2(x: proc (y: string)) =
let f = x
f("abc")
foo2(proc (y: string) = echo y)
|