123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- discard """
- output: '''
- @[]
- 5
- 0
- a
- hi
- Hello, World!
- (e: 42)
- hey
- foo
- foo
- foo
- false
- true
- '''
- """
- import macros, json
- block t2057:
- proc mpf_get_d(x: int): float = float(x)
- proc mpf_cmp_d(a: int; b: float): int = 0
- template toFloatHelper(result, tooSmall, tooLarge: untyped) =
- result = mpf_get_d(a)
- if result == 0.0 and mpf_cmp_d(a,0.0) != 0:
- tooSmall
- if result == Inf:
- tooLarge
- proc toFloat(a: int): float =
- toFloatHelper(result) do:
- raise newException(ValueError, "number too small")
- do:
- raise newException(ValueError, "number too large")
- doAssert toFloat(8) == 8.0
- import sequtils, os
- block t2629:
- template glob_rst(basedir: string = ""): untyped =
- if baseDir.len == 0:
- to_seq(walk_files("*.rst"))
- else:
- to_seq(walk_files(basedir/"*.rst"))
- let rst_files = concat(glob_rst(), glob_rst("docs"))
- when true: echo rst_files
- block t5417:
- macro genBody: untyped =
- let sbx = genSym(nskLabel, "test")
- when true:
- result = quote do:
- block `sbx`:
- break `sbx`
- else:
- template foo(s1, s2) =
- block s1:
- break s2
- result = getAst foo(sbx, sbx)
- proc test() =
- genBody()
- block t909:
- template baz() =
- proc bar() =
- var x = 5
- iterator foo(): int {.closure.} =
- echo x
- var y = foo
- discard y()
- macro test(): untyped =
- result = getAst(baz())
- test()
- bar()
- block t993:
- type PNode = ref object of RootObj
- template litNode(name, ty) =
- type name = ref object of PNode
- val: ty
- litNode PIntNode, int
- template withKey(j: JsonNode; key: string; varname,
- body: untyped): typed =
- if j.hasKey(key):
- let varname{.inject.}= j[key]
- block:
- body
- var j = parsejson("{\"zzz\":1}")
- withkey(j, "foo", x):
- echo(x)
- block t1337:
- template someIt(a, pred): untyped =
- var it {.inject.} = 0
- pred
- proc aProc(n: auto) =
- n.someIt(echo(it))
- aProc(89)
- import mlt
- block t4564:
- type Bar = ref object of RootObj
- proc foo(a: Bar): int = 0
- var a: Bar
- let b = a.foo() > 0
- block t8052:
- type
- UintImpl[N: static[int], T: SomeUnsignedInt] = object
- raw_data: array[N, T]
- template genLoHi(TypeImpl: untyped): untyped =
- template loImpl[N: static[int], T: SomeUnsignedInt](dst: TypeImpl[N div 2, T], src: TypeImpl[N, T]) =
- let halfSize = N div 2
- for i in 0 ..< halfSize:
- dst.raw_data[i] = src.raw_data[i]
- proc lo[N: static[int], T: SomeUnsignedInt](x: TypeImpl[N,T]): TypeImpl[N div 2, T] {.inline.}=
- loImpl(result, x)
- genLoHi(UintImpl)
- var a: UintImpl[4, uint32]
- a.raw_data = [1'u32, 2'u32, 3'u32, 4'u32]
- doAssert a.lo.raw_data.len == 2
- doAssert a.lo.raw_data[0] == 1
- doAssert a.lo.raw_data[1] == 2
- block t2585:
- type
- RenderPass = object
- state: ref int
- RenderData = object
- fb: int
- walls: seq[RenderPass]
- Mat2 = int
- Vector2[T] = T
- Pixels=int
- template use(fb: int, st: untyped): untyped =
- echo "a ", $fb
- st
- echo "a ", $fb
- proc render(rdat: var RenderData; passes: var openarray[RenderPass]; proj: Mat2;
- indexType = 1) =
- for i in 0 ..< len(passes):
- echo "blah ", repr(passes[i])
- proc render2(rdat: var RenderData; screenSz: Vector2[Pixels]; proj: Mat2) =
- use rdat.fb:
- render(rdat, rdat.walls, proj, 1)
- block t4292:
- template foo(s: string): string = s
- proc variadicProc(v: varargs[string, foo]) = echo v[0]
- variadicProc("a")
- block t2670:
- template testTemplate(b: bool): typed =
- when b:
- var a = "hi"
- else:
- var a = 5
- echo a
- testTemplate(true)
- block t4097:
- var i {.compileTime.} = 2
- template defineId(t: typedesc) =
- const id {.genSym.} = i
- static: inc(i)
- proc idFor(T: typedesc[t]): int {.inline, raises: [].} = id
- defineId(int8)
- defineId(int16)
- doAssert idFor(int8) == 2
- doAssert idFor(int16) == 3
- block t5235:
- template outer(body: untyped) =
- template test(val: string) =
- const SomeConst: string = val
- echo SomeConst
- body
- outer:
- test("Hello, World!")
- # bug #11941
- type X = object
- e: int
- proc works(T: type X, v: auto): T = T(e: v)
- template fails(T: type X, v: auto): T = T(e: v)
- var
- w = X.works(42)
- x = X.fails(42)
- echo x
- import mtempl5
- proc foo(): auto =
- trap "foo":
- echo "hey"
- discard foo()
- # bug #4722
- type
- IteratorF*[In] = iterator() : In {.closure.}
- template foof(In: untyped) : untyped =
- proc ggg*(arg: IteratorF[In]) =
- for i in arg():
- echo "foo"
- iterator hello() : int {.closure.} =
- for i in 1 .. 3:
- yield i
-
- foof(int)
- ggg(hello)
- # bug #2586
- var z = 10'u8
- echo z < 9 # Works
- echo z > 9 # Error: type mismatch
|