-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
test.nim
446 lines (353 loc) · 8.84 KB
/
test.nim
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# Nim Sample file
# Obtained form: https://nim-by-example.github.io/
# Comment ALERT NOTE FIXME
#[ Multi-line
comment ]#
## Documentation comment
##[ Multi-line
documentation comment ]##
import strformat
type
Person = object
name: string
age: Natural # Ensures the age is positive
let people = [
Person(name: "John", age: 45),
Person(name: "Kate", age: 30)
]
for person in people:
# Type-safe string interpolation,
# evaluated at compile time.
echo(fmt"{person.name} is {person.age} years old")
# Thanks to Nim's 'iterator' and 'yield' constructs,
# iterators are as easy to write as ordinary
# functions. They are compiled to inline loops.
iterator oddNumbers[Idx, T](a: array[Idx, T]): T =
for x in a:
if x mod 2 == 1:
yield x
for odd in oddNumbers([3, 6, 9, 12, 15, 18]):
echo odd
# Use Nim's macro system to transform a dense
# data-centric description of x86 instructions
# into lookup tables that are used by
# assemblers and JITs.
import macros, strutils
macro toLookupTable(data: static[string]): untyped =
result = newTree(nnkBracket)
for w in data.split(';'):
result.add newLit(w)
const
data = "mov;btc;cli;xor"
opcodes = toLookupTable(data)
for o in opcodes:
echo o
# Variables
proc getAlphabet(): string =
var accm = ""
for letter in 'a'..'z': # see iterators
accm.add(letter)
return accm
# Computed at compilation time
const alphabet = getAlphabet()
# Mutable variables
var
a = "foo"
b = 0
# Works fine, initialized to 0
c: int
# Immutable variables
let
d = "foo"
e = 5
# Compile-time error, must be initialized at creation
f: float
# Works fine, `a` is mutable
a.add("bar")
b += 1
c = 3
# Compile-time error, const cannot be modified at run-time
alphabet = "abc"
# Compile-time error, `d` and `e` are immutable
d.add("bar")
e += 1
# Const
STRING_LITERAL(TMP129, "abcdefghijklmnopqrstuvwxyz", 26);
# Loops
import strutils, random
randomize()
let answer = random(10) + 1
while true:
echo "I have a number from 1 to 10, what is it? "
let guess = parseInt(stdin.readLine)
if guess < answer:
echo "Too low, try again"
elif guess > answer:
echo "Too high, try again"
else:
echo "Correct!"
break
block busyloops:
while true:
while true:
break busyloops
# Case Statements
case "charlie":
of "alfa":
echo "A"
of "bravo":
echo "B"
of "charlie":
echo "C"
else:
echo "Unrecognized letter"
case 'h':
of 'a', 'e', 'i', 'o', 'u':
echo "Vowel"
of '\127'..'\255':
echo "Unknown"
else:
echo "Consonant"
proc positiveOrNegative(num: int): string =
result = case num:
of low(int).. -1:
"negative"
of 0:
"zero"
of 1..high(int):
"positive"
else:
"impossible"
echo positiveOrNegative(-1)
# items and pairs
type
CustomRange = object
low: int
high: int
iterator items(range: CustomRange): int =
var i = range.low
while i <= range.high:
yield i
inc i
iterator pairs(range: CustomRange): tuple[a: int, b: char] =
for i in range: # uses CustomRange.items
yield (i, char(i + ord('a')))
for i, c in CustomRange(low: 1, high: 3):
echo c
# Operators
iterator `...`*[T](a: T, b: T): T =
var res: T = T(a)
while res <= b:
yield res
inc res
for i in 0...5:
echo i
# Inline Iterators
iterator countTo(n: int): int =
var i = 0
while i <= n:
yield i
inc i
for i in countTo(5):
echo i
# Closure Iterators
proc countTo(n: int): iterator(): int =
return iterator(): int =
var i = 0
while i <= n:
yield i
inc i
let countTo20 = countTo(20)
echo countTo20()
var output = ""
# Raw iterator usage:
while true:
# 1. grab an element
let next = countTo20()
# 2. Is the element bogus? It's the end of the loop, discard it
if finished(countTo20):
break
# 3. Loop body goes here:
output.add($next & " ")
echo output
output = ""
let countTo9 = countTo(9)
for i in countTo9():
output.add($i)
echo output
# Procs
proc fibonacci(n: int): int =
if n < 2:
result = n
else:
result = fibonacci(n - 1) + (n - 2).fibonacci
# Operators
proc `$`(a: array[2, array[2, int]]): string =
result = ""
for v in a:
for vx in v:
result.add($vx & ", ")
result.add("\n")
echo([[1, 2], [3, 4]]) # See varargs for
# how echo works
proc `^&*^@%`(a, b: string): string =
## A confusingly named useless operator
result = a[0] & b[high(b)]
assert("foo" ^&*^@% "bar" == "fr")
# Generic Functions
# Not really good idea for obvious reasons
let zero = ""
proc `+`(a, b: string): string =
a & b
proc `*`[T](a: T, b: int): T =
result = zero
for i in 0..b-1:
result = result + a # calls `+` from line 3
assert("a" * 10 == "aaaaaaaaaa")
# Blocks
block outer:
for i in 0..2000:
for j in 0..2000:
if i+j == 3145:
echo i, ", ", j
break outer
let b = 3
block:
let b = "3" # shadowing is probably a dumb idea
# Primitive types
let
a: int8 = 0x7F # Works
b: uint8 = 0b1111_1111 # Works
d = 0xFF # type is int
c: uint8 = 256 # Compile time error
let
a: int = 2
b: int = 4
echo 4/2
# Types Aliases
type
MyInteger* = int
let a: int = 2
discard a + MyInteger(4)
# Objects
type
Animal* = object
name*, species*: string
age: int
proc sleep*(a: var Animal) =
a.age += 1
proc dead*(a: Animal): bool =
result = a.age > 20
var carl: Animal
carl = Animal(name : "Carl",
species : "L. glama",
age : 12)
let joe = Animal(name : "Joe",
species : "H. sapiens",
age : 23)
assert(not carl.dead)
for i in 0..10:
carl.sleep()
assert carl.dead
# Enums
type
CompassDirections = enum
cdNorth, cdEast, cdSouth, cdWest
Colors {.pure.} = enum
Red = "FF0000", Green = (1, "00FF00"), Blue = "0000FF"
Signals = enum
sigQuit = 3, sigAbort = 6, sigKill = 9
# Distinct Types
type
Dollars* = distinct float
var a = 20.Dollars
a = 25 # Doesn't compile
a = 25.Dollars # Works fine
# Strings
echo "words words words ⚑"
echo """
<html>
<head>
</head>\n\n
<body>
</body>
</html> """
proc re(s: string): string = s
echo r"."".\s\" # Raw string
echo re"\b[a-z]++\b" # Regular expression
echo function"text" # Tagged string
# Arrays
type
ThreeStringAddress = array[3, string]
let names: ThreeStringAddress = ["Jasmine", "Ktisztina", "Kristof"]
let addresses: ThreeStringAddress = ["101 Betburweg", "66 Bellion Drive", "194 Laarderweg"]
type
Matrix[W, H: static[int]] =
array[1..W, array[1..H, int]]
let mat1: Matrix[2, 2] = [[1, 0],
[0, 1]]
let mat2: Matrix[2, 2] = [[0, 1],
[1, 0]]
proc `+`[W, H](a, b: Matrix[W, H]):
Matrix[W, H] =
for i in 1..high(a):
for j in 1..high(a[0]):
result[i][j] = a[i][j] + b[i][j]
# Seqs
var
a = @[1, 2, 3]
b = newSeq[int](3)
for i, v in a:
b[i] = v*v
for i in 4..100:
b.add(i * i)
b.delete(0) # takes O(n) time
b = a[0] & b # Same as original b
# JSON
import json
let element = "Hydrogen"
let atomicNumber = 1
let jsonObject = %* {"element": element, "atomicNumber": atomicNumber}
# This will print {"element":"Hydrogen", "atomicNumber": 1}
echo $jsonObject
# We start with a string representation of a JSON object
let jsonObject = """{"name": "Sky", "age": 32}"""
let jsonArray = """[7, 8, 9]"""
let parsedObject = parseJson(jsonObject)
let name = parsedObject["name"].getStr()
# This will print Sky
echo name
let parsedArray = parseJson(jsonArray)
let eight = parsedArray[1].getInt()
# This will print 8
echo eight
# First we'll define our types
type
Element = object
name: string
atomicNumber: int
# Let's say this is the JSON we want to convert
let jsonObject = parseJson("""{"name": "Carbon", "atomicNumber": 6}""")
let element = to(jsonObject, Element)
# This will print Carbon
echo element.name
# This will print 6
echo element.atomicNumber
# Object Oriented Programming
type Animal = ref object of RootObj
name: string
age: int
method vocalize(this: Animal): string {.base.} = "..."
method ageHumanYrs(this: Animal): int {.base.} = this.age
type Dog = ref object of Animal
method vocalize(this: Dog): string = "woof"
method ageHumanYrs(this: Dog): int = this.age * 7
type Cat = ref object of Animal
method vocalize(this: Cat): string = "meow"
var animals: seq[Animal] = @[]
animals.add(Dog(name: "Sparky", age: 10))
animals.add(Cat(name: "Mitten", age: 10))
for a in animals:
echo a.vocalize()
echo a.ageHumanYrs()
let slash = "\\"