From 4d91c9d887051892001ef5fdffe58d7a0f6148bd Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 1 Apr 2011 00:26:07 +0200 Subject: type constraints; tuple lifting --- lib/pure/parsexml.nim | 2 +- lib/system.nim | 69 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/pure/parsexml.nim b/lib/pure/parsexml.nim index 8551dda90..c49986087 100755 --- a/lib/pure/parsexml.nim +++ b/lib/pure/parsexml.nim @@ -586,11 +586,11 @@ proc next*(my: var TXmlParser) = of stateNormal: getTok(my) of stateStart: + my.state = stateNormal getTok(my) if my.kind == xmlPI and my.a == "xml": # just skip the first ```` processing instruction getTok(my) - my.state = stateNormal of stateAttr: # parse an attribute key-value pair: if my.buf[my.bufpos] == '>': diff --git a/lib/system.nim b/lib/system.nim index 09b85d32b..daf0c5423 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -765,7 +765,7 @@ proc add *[T](x: var seq[T], y: openArray[T]) {.noSideEffect.} = setLen(x, xl + y.len) for i in 0..high(y): x[xl+i] = y[i] -proc del* [T](x: var seq[T], i: int) {.noSideEffect.} = +proc del*[T](x: var seq[T], i: int) {.noSideEffect.} = ## deletes the item at index `i` by putting ``x[high(x)]`` into position `i`. ## This is an O(1) operation. var xl = x.len @@ -1111,15 +1111,6 @@ iterator items*(a: cstring): char {.inline.} = yield a[i] inc(i) -iterator enumerate*[TContainer, TItem](a: TContainer): tuple[ - index: int, item: TItem] {.inline.} = - ## iterates over each item of `a` via `items` and yields an additional - ## counter/index starting from 0. - var j = 0 - for it in items(a): - yield (j, a) - inc j - proc isNil*[T](x: seq[T]): bool {.noSideEffect, magic: "IsNil".} proc isNil*[T](x: ref T): bool {.noSideEffect, magic: "IsNil".} proc isNil*(x: string): bool {.noSideEffect, magic: "IsNil".} @@ -1170,7 +1161,7 @@ when not defined(NimrodVM): proc find*[T, S: typeDesc](a: T, item: S): int {.inline.}= ## Returns the first index of `item` in `a` or -1 if not found. This requires - ## appropriate `items` and `==` procs to work. + ## appropriate `items` and `==` operations to work. for i in items(a): if i == item: return inc(result) @@ -1200,6 +1191,62 @@ proc each*[T](data: var openArray[T], op: proc (x: var T)) = ## `op` to every item in `data`. for i in 0..data.len-1: op(data[i]) +iterator fields*(x: tuple[]): expr {.magic: "Fields", noSideEffect.} + ## iterates over every field of `x`. Warning: This is really transforms + ## the 'for' and unrolls the loop. The current implementation also has a bug + ## that affects symbol binding in the loop body. +iterator fields*(x, y: tuple[]): tuple[a, b: expr] {. + magic: "Fields", noSideEffect.} + ## iterates over every field of `x` and `y`. + ## Warning: This is really transforms the 'for' and unrolls the loop. + ## The current implementation also has a bug that affects symbol binding + ## in the loop body. +iterator fieldPairs*(x: tuple[]): expr {.magic: "FieldPairs", noSideEffect.} + ## iterates over every field of `x`. Warning: This is really transforms + ## the 'for' and unrolls the loop. The current implementation also has a bug + ## that affects symbol binding in the loop body. +iterator fieldPairs*(x, y: tuple[]): tuple[a, b: expr] {. + magic: "FieldPairs", noSideEffect.} + ## iterates over every field of `x` and `y`. + ## Warning: This is really transforms the 'for' and unrolls the loop. + ## The current implementation also has a bug that affects symbol binding + ## in the loop body. + +proc `==`*[T: tuple](x, y: T): bool = + ## generic ``==`` operator that is lifted from the components + ## of `x` and `y`. + for a, b in fields(x, y): + if a != b: return false + return true + +proc `<=`*[T: tuple](x, y: T): bool = + ## generic ``<=`` operator that is lifted from the components + ## of `x` and `y`. This implementation uses `cmp`. + for a, b in fields(x, y): + var c = cmp(a, b) + if c < 0: return true + if c > 0: return false + return true + +proc `<`*[T: tuple](x, y: T): bool = + ## generic ``<`` operator that is lifted from the components + ## of `x` and `y`. This implementation uses `cmp`. + for a, b in fields(x, y): + var c = cmp(a, b) + if c < 0: return true + if c > 0: return false + return false + +proc `$`*[T: tuple](x: T): string = + ## generic ``$`` operator that is lifted from the components of `x`. + result = "(" + for name, value in fieldPairs(x): + if result.len > 1: result.add(", ") + result.add(name) + result.add(": ") + result.add($value) + result.add(")") + # ----------------- GC interface --------------------------------------------- proc GC_disable*() {.rtl, inl.} -- cgit 1.4.1-2-gfad0 p;id=023a2622f98da1b5b0b59696db33939d7f358e87'>^
fc719e4 ^
74af57b ^




fc719e4 ^

6811143 ^


fc719e4 ^










588a6c7 ^


fc719e4 ^










50f25e7 ^



fc719e4 ^
acfe7d7 ^








fc719e4 ^


50f25e7 ^
fc719e4 ^

8be4c57 ^


fc719e4 ^


50f25e7 ^












030f390 ^





50f25e7 ^









d22a914 ^


fc719e4 ^



50f25e7 ^
fc719e4 ^







50f25e7 ^
fc719e4 ^


50f25e7 ^
fc719e4 ^



ce475e4 ^

1bb1a80 ^
ce475e4 ^
28fc9fa ^



e56ceb0 ^
28fc9fa ^

ccf5c02 ^


e56ceb0 ^

fc719e4 ^







0b14d92 ^







fc719e4 ^

7446a17 ^
fc719e4 ^





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