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
|
discard """
output: '''
true012innertrue
m1
tup1
another number: 123
yay
helloa 1 b 2 x @[3, 4, 5] y 6 z 7
yay
12
ref ref T ptr S
dynamic: let
dynamic: var
static: const
static: literal
static: constant folding
static: static string
foo1
1
'''
"""
import strutils, sequtils
block overl2:
# Test new overloading resolution rules
proc toverl2(x: int): string = return $x
proc toverl2(x: bool): string = return $x
iterator toverl2(x: int): int =
var res = 0
while res < x:
yield res
inc(res)
var
pp: proc (x: bool): string {.nimcall.} = toverl2
stdout.write(pp(true))
for x in toverl2(3):
stdout.write(toverl2(x))
block:
proc toverl2(x: int): string = return "inner"
stdout.write(toverl2(5))
stdout.write(true)
stdout.write("\n")
#OUT true012innertrue
block overl3:
# Tests more specific generic match:
proc m[T](x: T) = echo "m2"
proc m[T](x: var ref T) = echo "m1"
proc tup[S, T](x: tuple[a: S, b: ref T]) = echo "tup1"
proc tup[S, T](x: tuple[a: S, b: T]) = echo "tup2"
var
obj: ref int
tu: tuple[a: int, b: ref bool]
m(obj)
tup(tu)
block toverprc:
# Test overloading of procs when used as function pointers
proc parseInt(x: float): int {.noSideEffect.} = discard
proc parseInt(x: bool): int {.noSideEffect.} = discard
proc parseInt(x: float32): int {.noSideEffect.} = discard
proc parseInt(x: int8): int {.noSideEffect.} = discard
proc parseInt(x: File): int {.noSideEffect.} = discard
proc parseInt(x: char): int {.noSideEffect.} = discard
proc parseInt(x: int16): int {.noSideEffect.} = discard
proc parseInt[T](x: T): int = echo x; 34
type
TParseInt = proc (x: string): int {.noSideEffect.}
var
q = TParseInt(parseInt)
p: TParseInt = parseInt
proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int =
result = x("123")
if false:
echo "Give a list of numbers (separated by spaces): "
var x = stdin.readline.split.map(parseInt).max
echo x, " is the maximum!"
echo "another number: ", takeParseInt(parseInt)
type
TFoo[a,b] = object
lorem: a
ipsum: b
proc bar[a,b](f: TFoo[a,b], x: a) = echo(x, " ", f.lorem, f.ipsum)
proc bar[a,b](f: TFoo[a,b], x: b) = echo(x, " ", f.lorem, f.ipsum)
discard parseInt[string]("yay")
block toverwr:
# Test the overloading resolution in connection with a qualifier
proc write(t: File, s: string) =
discard # a nop
system.write(stdout, "hello")
#OUT hello
block tparams_after_varargs:
proc test(a, b: int, x: varargs[int]; y, z: int) =
echo "a ", a, " b ", b, " x ", @x, " y ", y, " z ", z
test 1, 2, 3, 4, 5, 6, 7
# XXX maybe this should also work with ``varargs[untyped]``
template takesBlockA(a, b: untyped; x: varargs[typed]; blck: untyped): untyped =
blck
echo a, b
takesBlockA 1, 2, "some", 0.90, "random stuff":
echo "yay"
block tprefer_specialized_generic:
proc foo[T](x: T) =
echo "only T"
proc foo[T](x: ref T) =
echo "ref T"
proc foo[T, S](x: ref ref T; y: ptr S) =
echo "ref ref T ptr S"
proc foo[T, S](x: ref T; y: ptr S) =
echo "ref T ptr S"
proc foo[T](x: ref T; default = 0) =
echo "ref T; default"
var x: ref ref int
var y: ptr ptr int
foo(x, y)
block tstaticoverload:
proc foo(s: string) =
echo "dynamic: ", s
proc foo(s: static[string]) =
echo "static: ", s
let l = "let"
var v = "var"
const c = "const"
type staticString = static[string]
foo(l)
foo(v)
foo(c)
foo("literal")
foo("constant" & " " & "folding")
foo(staticString("static string"))
# bug #8568 (2)
proc goo(a: int): string = "int"
proc goo(a: static[int]): string = "static int"
proc goo(a: var int): string = "var int"
proc goo[T: int](a: T): string = "T: int"
#proc goo[T](a: T): string = "nur T"
const tmp1 = 1
let tmp2 = 1
var tmp3 = 1
doAssert goo(1) == "static int"
doAssert goo(tmp1) == "static int"
doAssert goo(tmp2) == "int"
doAssert goo(tmp3) == "var int"
doAssert goo[int](1) == "T: int"
doAssert goo[int](tmp1) == "T: int"
doAssert goo[int](tmp2) == "T: int"
doAssert goo[int](tmp3) == "T: int"
# bug #6076
type A[T] = object
proc regr(a: A[void]) = echo "foo1"
proc regr[T](a: A[T]) = doAssert(false)
regr(A[void]())
type Foo[T] = object
proc regr[T](p: Foo[T]): seq[T] =
discard
proc regr(p: Foo[void]): seq[int] =
discard
discard regr(Foo[int]())
discard regr(Foo[void]())
type
Sha2Context*[bits: static[int],
bsize: static[int],
T: uint32|uint64] = object
count: array[2, T]
state: array[8, T]
buffer: array[bsize, byte]
sha224* = Sha2Context[224, 64, uint32]
sha256* = Sha2Context[256, 64, uint32]
sha384* = Sha2Context[384, 128, uint64]
sha512* = Sha2Context[512, 128, uint64]
sha512_224* = Sha2Context[224, 128, uint64]
sha512_256* = Sha2Context[256, 128, uint64]
type
RipemdContext*[bits: static[int]] = object
count: array[2, uint32]
state: array[bits div 32, uint32]
buffer: array[64, byte]
ripemd128* = RipemdContext[128]
ripemd160* = RipemdContext[160]
ripemd256* = RipemdContext[256]
ripemd320* = RipemdContext[320]
const
MaxHmacBlockSize = 256
type
HMAC*[HashType] = object
mdctx: HashType
opadctx: HashType
template sizeBlock*(h: HMAC[Sha2Context]): uint = 1u
template sizeBlock*(h: HMAC[RipemdContext]): uint = 0u
proc init*[T](hmctx: HMAC[T], key: ptr byte, ulen: uint) =
const sizeBlock = hmctx.sizeBlock
echo sizeBlock
proc hmac*[A, B](HashType: typedesc, key: openarray[A],
data: openarray[B]) =
var ctx: HMAC[HashType]
ctx.init(nil, 0)
sha256.hmac("", "")
|