summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-08-22 22:46:02 +0200
committerAraq <rumpf_a@web.de>2012-08-22 22:46:02 +0200
commitf8931798899647b65ebb7d7dcf28d6976da599ce (patch)
tree0fdbd7f0b5516279458d21bf99a698cb2cb89e37 /lib
parenta95e958046447d0ef88d93337171d4b8339348a4 (diff)
downloadNim-f8931798899647b65ebb7d7dcf28d6976da599ce.tar.gz
documented hygienic templates; made tests green; fixed system.clamp
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/collections/lists.nim15
-rw-r--r--lib/pure/collections/sequtils.nim5
-rwxr-xr-xlib/pure/collections/sets.nim14
-rwxr-xr-xlib/pure/collections/tables.nim17
-rw-r--r--lib/pure/unittest.nim25
-rwxr-xr-xlib/system.nim8
6 files changed, 48 insertions, 36 deletions
diff --git a/lib/pure/collections/lists.nim b/lib/pure/collections/lists.nim
index adfd9ceb4..ad8eca6a9 100755
--- a/lib/pure/collections/lists.nim
+++ b/lib/pure/collections/lists.nim
@@ -11,6 +11,9 @@
 ## to do so, the 'next' and 'prev' pointers are not hidden from you and can
 ## be manipulated directly for efficiency.
 
+when not defined(nimhygiene):
+  {.pragma: dirty.}
+
 type
   TDoublyLinkedNode* {.pure, 
       final.}[T] = object ## a node a doubly linked list consists of
@@ -62,13 +65,13 @@ proc newSinglyLinkedNode*[T](value: T): PSinglyLinkedNode[T] =
   new(result)
   result.value = value
 
-template itemsListImpl() =
+template itemsListImpl() {.dirty.} =
   var it = L.head
   while it != nil:
     yield it.value
     it = it.next
 
-template itemsRingImpl() =
+template itemsRingImpl() {.dirty.} =
   var it = L.head
   if it != nil:
     while true:
@@ -76,14 +79,14 @@ template itemsRingImpl() =
       it = it.next
       if it == L.head: break
 
-template nodesListImpl() =
+template nodesListImpl() {.dirty.} =
   var it = L.head
   while it != nil:
     var nxt = it.next
     yield it
     it = nxt
 
-template nodesRingImpl() =
+template nodesRingImpl() {.dirty.} =
   var it = L.head
   if it != nil:
     while true:
@@ -92,7 +95,7 @@ template nodesRingImpl() =
       it = nxt
       if it == L.head: break
 
-template findImpl() =
+template findImpl() {.dirty.} =
   for x in nodes(L):
     if x.value == value: return x
 
@@ -132,7 +135,7 @@ iterator nodes*[T](L: TDoublyLinkedRing[T]): PDoublyLinkedNode[T] =
   ## list during traversal is supported.
   nodesRingImpl()
 
-template dollarImpl() =
+template dollarImpl() {.dirty.} =
   result = "["
   for x in nodes(L):
     if result.len > 1: result.add(", ")
diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim
index 9e39bff62..82679bb7f 100644
--- a/lib/pure/collections/sequtils.nim
+++ b/lib/pure/collections/sequtils.nim
@@ -15,6 +15,9 @@
 ## **Note**: This interface will change as soon as the compiler supports
 ## closures and proper coroutines.
 
+when not defined(nimhygiene):
+  {.pragma: dirty.}
+
 proc concat*[T](seqs: varargs[seq[T]]): seq[T] =
   ## Takes several sequences' items and returns them inside of one sequence.
   var L = 0
@@ -50,7 +53,7 @@ proc filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): seq[T] =
   ## Returns all items in a sequence that fulfilled the predicate.
   accumulateResult(filter(seq1, pred))
 
-template filterIt*(seq1, pred: expr): expr {.immediate.} =
+template filterIt*(seq1, pred: expr): expr {.immediate, dirty.} =
   ## Finds a specific item in a sequence as long as the 
   ## predicate returns true. The predicate needs to be an expression
   ## containing ``it``: ``filterIt("abcxyz", it == 'x')``.
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim
index f65239b83..ff2ffec0e 100755
--- a/lib/pure/collections/sets.nim
+++ b/lib/pure/collections/sets.nim
@@ -16,6 +16,8 @@ import
   os, hashes, math
 
 {.pragma: myShallow.}
+when not defined(nimhygiene):
+  {.pragma: dirty.}
 
 type
   TSlotEnum = enum seEmpty, seFilled, seDeleted
@@ -48,7 +50,7 @@ proc mustRehash(length, counter: int): bool {.inline.} =
 proc nextTry(h, maxHash: THash): THash {.inline.} =
   result = ((5 * h) + 1) and maxHash
 
-template rawGetImpl() =
+template rawGetImpl() {.dirty.} =
   var h: THash = hash(key) and high(s.data) # start with real hash value
   while s.data[h].slot != seEmpty:
     if s.data[h].key == key and s.data[h].slot == seFilled:
@@ -56,7 +58,7 @@ template rawGetImpl() =
     h = nextTry(h, high(s.data))
   result = -1
 
-template rawInsertImpl() =
+template rawInsertImpl() {.dirty.} =
   var h: THash = hash(key) and high(data)
   while data[h].slot == seFilled:
     h = nextTry(h, high(data))
@@ -81,14 +83,14 @@ proc Enlarge[A](s: var TSet[A]) =
     if s.data[i].slot == seFilled: RawInsert(s, n, s.data[i].key)
   swap(s.data, n)
 
-template inclImpl() =
+template inclImpl() {.dirty.} =
   var index = RawGet(s, key)
   if index < 0:
     if mustRehash(len(s.data), s.counter): Enlarge(s)
     RawInsert(s, s.data, key)
     inc(s.counter)
 
-template containsOrInclImpl() =
+template containsOrInclImpl() {.dirty.} =
   var index = RawGet(s, key)
   if index >= 0:
     result = true
@@ -125,7 +127,7 @@ proc toSet*[A](keys: openarray[A]): TSet[A] =
   result = initSet[A](nextPowerOfTwo(keys.len+10))
   for key in items(keys): result.incl(key)
 
-template dollarImpl(): stmt =
+template dollarImpl(): stmt {.dirty.} =
   result = "{"
   for key in items(s):
     if result.len > 1: result.add(", ")
@@ -155,7 +157,7 @@ proc card*[A](s: TOrderedSet[A]): int {.inline.} =
   ## alias for `len`.
   result = s.counter
 
-template forAllOrderedPairs(yieldStmt: stmt) =
+template forAllOrderedPairs(yieldStmt: stmt) {.dirty.} =
   var h = s.first
   while h >= 0:
     var nxt = s.data[h].next
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index 81466bb15..3ae25d3ad 100755
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -26,6 +26,9 @@ type
     data: TKeyValuePairSeq[A, B]
     counter: int
 
+when not defined(nimhygiene):
+  {.pragma: dirty.}
+
 proc len*[A, B](t: TTable[A, B]): int =
   ## returns the number of keys in `t`.
   result = t.counter
@@ -66,7 +69,7 @@ proc mustRehash(length, counter: int): bool {.inline.} =
 proc nextTry(h, maxHash: THash): THash {.inline.} =
   result = ((5 * h) + 1) and maxHash
 
-template rawGetImpl() =
+template rawGetImpl() {.dirty.} =
   var h: THash = hash(key) and high(t.data) # start with real hash value
   while t.data[h].slot != seEmpty:
     if t.data[h].key == key and t.data[h].slot == seFilled:
@@ -74,7 +77,7 @@ template rawGetImpl() =
     h = nextTry(h, high(t.data))
   result = -1
 
-template rawInsertImpl() =
+template rawInsertImpl() {.dirty.} =
   var h: THash = hash(key) and high(data)
   while data[h].slot == seFilled:
     h = nextTry(h, high(data))
@@ -115,12 +118,12 @@ proc Enlarge[A, B](t: var TTable[A, B]) =
     if t.data[i].slot == seFilled: RawInsert(t, n, t.data[i].key, t.data[i].val)
   swap(t.data, n)
 
-template AddImpl() =
+template AddImpl() {.dirty.} =
   if mustRehash(len(t.data), t.counter): Enlarge(t)
   RawInsert(t, t.data, key, val)
   inc(t.counter)
 
-template PutImpl() =
+template PutImpl() {.dirty.} =
   var index = RawGet(t, key)
   if index >= 0:
     t.data[index].val = val
@@ -129,7 +132,7 @@ template PutImpl() =
 
 when false:
   # not yet used:
-  template HasKeyOrPutImpl() =
+  template HasKeyOrPutImpl() {.dirty.} =
     var index = RawGet(t, key)
     if index >= 0:
       t.data[index].val = val
@@ -168,7 +171,7 @@ proc toTable*[A, B](pairs: openarray[tuple[key: A,
   result = initTable[A, B](nextPowerOfTwo(pairs.len+10))
   for key, val in items(pairs): result[key] = val
 
-template dollarImpl(): stmt =
+template dollarImpl(): stmt {.dirty.} =
   if t.len == 0:
     result = "{:}"
   else:
@@ -199,7 +202,7 @@ proc len*[A, B](t: TOrderedTable[A, B]): int {.inline.} =
   ## returns the number of keys in `t`.
   result = t.counter
 
-template forAllOrderedPairs(yieldStmt: stmt) =
+template forAllOrderedPairs(yieldStmt: stmt) {.dirty.} =
   var h = t.first
   while h >= 0:
     var nxt = t.data[h].next
diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim
index 5220f53e0..3a52cb916 100644
--- a/lib/pure/unittest.nim
+++ b/lib/pure/unittest.nim
@@ -31,19 +31,19 @@ var
   

   checkpoints: seq[string] = @[]

 

-template TestSetupIMPL*: stmt = nil

-template TestTeardownIMPL*: stmt = nil

+template TestSetupIMPL*: stmt {.dirty.} = nil

+template TestTeardownIMPL*: stmt {.dirty.} = nil

 

 proc shouldRun(testName: string): bool =

   result = true

 

-template suite*(name: expr, body: stmt): stmt =

+template suite*(name: expr, body: stmt): stmt {.dirty.} =

   block:

-    template setup*(setupBody: stmt): stmt =

-      template TestSetupIMPL: stmt = setupBody

+    template setup*(setupBody: stmt): stmt {.dirty.} =

+      template TestSetupIMPL: stmt {.dirty.} = setupBody

 

-    template teardown*(teardownBody: stmt): stmt =

-      template TestTeardownIMPL: stmt = teardownBody

+    template teardown*(teardownBody: stmt): stmt {.dirty.} =

+      template TestTeardownIMPL: stmt {.dirty.} = teardownBody

 

     body

 

@@ -59,12 +59,12 @@ proc testDone(name: string, s: TTestStatus) =
     else:

       echo "[", $s, "] ", name, "\n"

   

-template test*(name: expr, body: stmt): stmt =

+template test*(name: expr, body: stmt): stmt {.dirty.} =

   bind shouldRun, checkpoints, testDone

 

   if shouldRun(name):

     checkpoints = @[]

-    var TestStatusIMPL = OK

+    var TestStatusIMPL {.inject.} = OK

     

     try:

       TestSetupIMPL()

@@ -146,13 +146,14 @@ macro check*(conditions: stmt): stmt =
     var ast = conditions.treeRepr

     error conditions.lineinfo & ": Malformed check statement:\n" & ast

 

-template require*(conditions: stmt): stmt =

+template require*(conditions: stmt): stmt {.dirty.} =

   block:

-    const AbortOnError = true    

+    const AbortOnError {.inject.} = true

     check conditions

 

 macro expect*(exp: stmt): stmt =

-  template expectBody(errorTypes, lineInfoLit: expr, body: stmt): PNimrodNode =

+  template expectBody(errorTypes, lineInfoLit: expr,
+                      body: stmt): PNimrodNode {.dirty.} =

     try:

       body

       checkpoint(lineInfoLit & ": Expect Failed, no exception was thrown.")

diff --git a/lib/system.nim b/lib/system.nim
index 6d1f14e2e..01b85a4d6 100755
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1259,8 +1259,8 @@ proc max*[T](x: varargs[T]): T =
 
 proc clamp*[T](x, a, b: T): T =
   ## limits the value ``x`` within the interval [a, b] 
-  if x > a: return a
-  if x < b: return b
+  if x < a: return a
+  if x > b: return b
   return x
 
 iterator items*[T](a: openarray[T]): T {.inline.} =
@@ -1657,9 +1657,9 @@ proc echo*[T](x: varargs[T, `$`]) {.magic: "Echo", noSideEffect.}
   ## side effects, so that it can be used for debugging routines marked as
   ## ``noSideEffect``.
 
-template newException*(exceptn: typeDesc, message: string): expr = 
+template newException*(exceptn: typeDesc, message: string): expr =
   ## creates an exception object of type ``exceptn`` and sets its ``msg`` field
-  ## to `message`. Returns the new exception object. 
+  ## to `message`. Returns the new exception object.
   # block: # open a new scope
   var
     e: ref exceptn
>
4690ce81 ^
db1f56c8 ^





b2566a84 ^
4690ce81 ^
b2566a84 ^





4690ce81 ^







76755b28 ^
db1f56c8 ^
4690ce81 ^




db1f56c8 ^



4690ce81 ^
db1f56c8 ^

4690ce81 ^


b2566a84 ^
db1f56c8 ^
4690ce81 ^


b2566a84 ^

db1f56c8 ^
b2566a84 ^
db1f56c8 ^

4690ce81 ^
dcc060c7 ^

4690ce81 ^
dcc060c7 ^




4690ce81 ^



b2566a84 ^
db1f56c8 ^



4690ce81 ^

db1f56c8 ^

4690ce81 ^
76755b28 ^
4690ce81 ^

b2566a84 ^
d2bd40bf ^

76755b28 ^

4690ce81 ^


32b8fac2 ^
4690ce81 ^
b2566a84 ^
76755b28 ^

32b8fac2 ^











4690ce81 ^
db1f56c8 ^


4690ce81 ^


76755b28 ^


4690ce81 ^


76755b28 ^


4690ce81 ^


b2566a84 ^


4690ce81 ^



dcc060c7 ^

d009e158 ^
4690ce81 ^
76755b28 ^


4690ce81 ^


dcc060c7 ^
4690ce81 ^
76755b28 ^




a654e4ec ^
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