diff options
Diffstat (limited to 'tests/showoff')
-rw-r--r-- | tests/showoff/tdrdobbs_examples.nim | 12 | ||||
-rw-r--r-- | tests/showoff/tformatopt.nim | 8 | ||||
-rw-r--r-- | tests/showoff/tgenericmacrotypes.nim | 55 | ||||
-rw-r--r-- | tests/showoff/thello2.nim | 2 | ||||
-rw-r--r-- | tests/showoff/thtml1.nim | 2 | ||||
-rw-r--r-- | tests/showoff/thtml2.nim | 10 | ||||
-rw-r--r-- | tests/showoff/tonce.nim | 2 | ||||
-rw-r--r-- | tests/showoff/tquasiquote.nim | 4 |
8 files changed, 75 insertions, 20 deletions
diff --git a/tests/showoff/tdrdobbs_examples.nim b/tests/showoff/tdrdobbs_examples.nim index 78f711325..c61e177dc 100644 --- a/tests/showoff/tdrdobbs_examples.nim +++ b/tests/showoff/tdrdobbs_examples.nim @@ -40,14 +40,14 @@ const msb3999 = mostSignificantBit(3999) echo msb3999, " ", mostSignificantBit(0), " ", square(44) -proc filter[T](a: openarray[T], predicate: proc (x: T): bool): seq[T] = +proc filter[T](a: openArray[T], predicate: proc (x: T): bool): seq[T] = result = @[] # @[] constructs the empty seq for x in a: if predicate(x): result.add(x) -proc map[T, S](a: openarray[T], fn: proc (x: T): S): seq[S] = +proc map[T, S](a: openArray[T], fn: proc (x: T): S): seq[S] = newSeq(result, a.len) - for i in 0 .. <a.len: result[i] = fn(a[i]) + for i in 0 ..< a.len: result[i] = fn(a[i]) type @@ -105,10 +105,10 @@ proc pat2kind(pattern: string): FormulaKind = import macros proc matchAgainst(n, pattern: NimNode): NimNode {.compileTime.} = - template `@`(current, field: expr): expr = + template `@`(current, field: untyped): untyped = newDotExpr(current, newIdentNode(astToStr(field))) - template `==@`(n, pattern: expr): expr = + template `==@`(n, pattern: untyped): untyped = newCall("==", n@kind, newIdentNode($pat2kind($pattern.ident))) case pattern.kind @@ -126,7 +126,7 @@ proc matchAgainst(n, pattern: NimNode): NimNode {.compileTime.} = else: error "invalid pattern" -macro `=~` (n: Formula, pattern: expr): bool = +macro `=~` (n: Formula, pattern: untyped): bool = result = matchAgainst(n, pattern) proc isPolyTerm2(n: Formula): bool = n =~ c * x^c diff --git a/tests/showoff/tformatopt.nim b/tests/showoff/tformatopt.nim index f33ed6921..420dd026b 100644 --- a/tests/showoff/tformatopt.nim +++ b/tests/showoff/tformatopt.nim @@ -10,7 +10,7 @@ import macros proc invalidFormatString() = echo "invalidFormatString" -template formatImpl(handleChar: expr) = +template formatImpl(handleChar: untyped) = var i = 0 while i < f.len: if f[i] == '$': @@ -29,15 +29,15 @@ template formatImpl(handleChar: expr) = i += 1 proc `%`*(f: string, a: openArray[string]): string = - template identity(x: expr): expr = x + template identity(x: untyped): untyped = x result = "" formatImpl(identity) -macro optFormat{`%`(f, a)}(f: string{lit}, a: openArray[string]): expr = +macro optFormat{`%`(f, a)}(f: string{lit}, a: openArray[string]): untyped = result = newNimNode(nnkBracket) let f = f.strVal formatImpl(newLit) - result = nestList(!"&", result) + result = nestList(newIdentNode("&"), result) template optAdd1{x = y; add(x, z)}(x, y, z: string) = x = y & z diff --git a/tests/showoff/tgenericmacrotypes.nim b/tests/showoff/tgenericmacrotypes.nim new file mode 100644 index 000000000..cc07f4355 --- /dev/null +++ b/tests/showoff/tgenericmacrotypes.nim @@ -0,0 +1,55 @@ +# issue #7974 + +import macros + +macro genTypeA(arg: typed): untyped = + if arg.typeKind != ntyTypeDesc: + error("expected typedesc", arg) + + result = arg.getTypeInst[1] + +macro genTypeB(arg: typed): untyped = + if arg.typeKind != ntyTypeDesc: + error("expected typedesc", arg) + + + let typeSym = arg.getTypeInst[1] + result = + nnkTupleTy.newTree( + nnkIdentDefs.newTree( + ident"a", typeSym, newEmptyNode() + ) + ) + +type + # this is the trivial case, MyTypeA[T] is basically just T, nothing else. But it works. + MyTypeA[T] = genTypeA(T) + # in this case I generate `tuple[a: T]`. This this is something the compiler does not want + MyTypeB[T] = genTypeB(T) + +# these are just alias types for int32 and float32, nothing really happens, but it works +var a1: MyTypeA[int32] +doAssert a1 is MyTypeA[int32] +doAssert a1 is int32 +a1 = 0'i32 +var a2: MyTypeA[float32] +doAssert a2 is MyTypeA[float32] +doAssert a2 is float32 +a2 = 0'f32 +var a3: MyTypeA[float32] +doAssert a3 is MyTypeA[float32] +doAssert a3 is float32 +a3 = 0'f32 + +var b1: MyTypeB[int32] # cannot generate VM code fur tuple[a: int32] +doAssert b1 is MyTypeB[int32] +doAssert b1 is tuple[a: int32] +b1 = (a: 0'i32) +var b2: MyTypeB[float32] +doAssert b2 is MyTypeB[float32] +doAssert b2 is tuple[a: float32] +b2 = (a: 0'f32) +var b3: MyTypeB[float32] +doAssert b3 is MyTypeB[float32] +doAssert b3 is tuple[a: float32] +b3 = (a: 0'f32) diff --git a/tests/showoff/thello2.nim b/tests/showoff/thello2.nim index d2e2f6227..3ccb4e3be 100644 --- a/tests/showoff/thello2.nim +++ b/tests/showoff/thello2.nim @@ -1,5 +1,5 @@ discard """ - output: '''(a: 3, b: 4, s: abc)''' + output: '''(a: 3, b: 4, s: "abc")''' """ type diff --git a/tests/showoff/thtml1.nim b/tests/showoff/thtml1.nim index c7a083c21..fe0cd3b1e 100644 --- a/tests/showoff/thtml1.nim +++ b/tests/showoff/thtml1.nim @@ -2,7 +2,7 @@ discard """ output: "<br>" """ -template htmlTag(tag: expr) {.immediate.} = +template htmlTag(tag: untyped) = proc tag(): string = "<" & astToStr(tag) & ">" htmlTag(br) diff --git a/tests/showoff/thtml2.nim b/tests/showoff/thtml2.nim index faeb4e50d..dcf6534a5 100644 --- a/tests/showoff/thtml2.nim +++ b/tests/showoff/thtml2.nim @@ -4,20 +4,20 @@ discard """ import strutils -template html(name: expr, matter: stmt) {.immediate.} = +template html(name, matter: untyped) = proc name(): string = result = "<html>" matter result.add("</html>") -template nestedTag(tag: expr) {.immediate.} = - template tag(matter: stmt) {.immediate.} = +template nestedTag(tag: untyped) = + template tag(matter: untyped) = result.add("<" & astToStr(tag) & ">") matter result.add("</" & astToStr(tag) & ">") -template simpleTag(tag: expr) {.immediate.} = - template tag(matter: expr) {.immediate.} = +template simpleTag(tag: untyped) = + template tag(matter: untyped) = result.add("<$1>$2</$1>" % [astToStr(tag), matter]) nestedTag body diff --git a/tests/showoff/tonce.nim b/tests/showoff/tonce.nim index 6fc372e87..ed2684dcf 100644 --- a/tests/showoff/tonce.nim +++ b/tests/showoff/tonce.nim @@ -5,7 +5,7 @@ new instantiation some call of p''' """ -template once(body: stmt) = +template once(body) = var x {.global.} = false if not x: x = true diff --git a/tests/showoff/tquasiquote.nim b/tests/showoff/tquasiquote.nim index df7fccc33..404712a02 100644 --- a/tests/showoff/tquasiquote.nim +++ b/tests/showoff/tquasiquote.nim @@ -1,10 +1,10 @@ discard """ - outputsub: '''tquasiquote.nim(14,8): Check failed: 1 > 2''' + outputsub: '''tquasiquote.nim(14, 8): Check failed: 1 > 2''' """ import macros -macro check(ex: expr): stmt = +macro check(ex: untyped): untyped = var info = ex.lineInfo var expString = ex.toStrLit result = quote do: |