summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xcompiler/parser.nim9
-rwxr-xr-xcompiler/semtempl.nim52
-rwxr-xr-xdoc/grammar.txt2
-rw-r--r--tests/run/tstempl.nim24
-rwxr-xr-xtodo.txt3
-rwxr-xr-xweb/news.txt3
6 files changed, 61 insertions, 32 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim
index 7f11ddb2b..88edcd967 100755
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -1412,13 +1412,12 @@ proc parseBind(p: var TParser): PNode =
   result = newNodeP(nkBindStmt, p)
   getTok(p)
   optInd(p, result)
-  while p.tok.tokType == tkSymbol: 
-    var a = newIdentNodeP(p.tok.ident, p)
-    getTok(p)
+  while true:
+    var a = qualifiedIdent(p)
     addSon(result, a)
-    if p.tok.tokType != tkComma: break 
+    if p.tok.tokType != tkComma: break
     getTok(p)
-    optInd(p, a)
+    optInd(p, a)  
   expectNl(p)
   
 proc parseStmtPragma(p: var TParser): PNode =
diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim
index b09f50306..98c4aff53 100755
--- a/compiler/semtempl.nim
+++ b/compiler/semtempl.nim
@@ -13,12 +13,12 @@ proc symChoice(c: PContext, n: PNode, s: PSym): PNode =
   var 
     a: PSym
     o: TOverloadIter
-    i: int
-  i = 0
+  var i = 0
   a = initOverloadIter(o, c, n)
   while a != nil: 
     a = nextOverloadIter(o, c, n)
     inc(i)
+    if i > 1: break
   if i <= 1: 
     result = newSymNode(s)
     result.info = n.info
@@ -36,41 +36,45 @@ proc symChoice(c: PContext, n: PNode, s: PSym): PNode =
 proc semBindStmt(c: PContext, n: PNode, toBind: var TIntSet): PNode =
   for i in 0 .. < n.len:
     var a = n.sons[i]
-    if a.kind == nkIdent:
-      var s = SymtabGet(c.Tab, a.ident)
-      if s != nil:
-        toBind.incl(s.name.id)
-      else:
-        localError(a.info, errUndeclaredIdentifier, a.ident.s)
-    else: 
+    # If 'a' is an overloaded symbol, we use the first symbol as a 'witness'
+    # and use the fact that subsequent lookups will yield the same symbol!
+    # This is currently the case due to the hash table's implementation...
+    let s = QualifiedLookUp(c, a)
+    if s != nil:
+      toBind.incl(s.id)
+    else:
       illFormedAst(a)
   result = newNodeI(nkEmpty, n.info)
 
-proc resolveTemplateParams(c: PContext, n: PNode, withinBind: bool, 
+proc resolveTemplateParams(c: PContext, n: PNode, owner: PSym, 
                            toBind: var TIntSet): PNode = 
   var s: PSym
   case n.kind
-  of nkIdent: 
-    if not withinBind and not Contains(toBind, n.ident.id): 
-      s = SymTabLocalGet(c.Tab, n.ident)
-      if s != nil: 
+  of nkIdent, nkAccQuoted:
+    result = n
+    let s = QualifiedLookUp(c, n, {})
+    if s != nil:
+      if s.owner == owner and s.kind == skParam:
         result = newSymNode(s)
         result.info = n.info
-      else: 
-        result = n
-    else: 
-      Incl(toBind, n.ident.id)
-      result = symChoice(c, n, lookup(c, n))
+      elif Contains(toBind, s.id):
+        result = symChoice(c, n, s)
   of nkEmpty, nkSym..nkNilLit:         # atom
     result = n
-  of nkBind: 
-    result = resolveTemplateParams(c, n.sons[0], true, toBind)
+  of nkBind:
+    result = resolveTemplateParams(c, n.sons[0], owner, toBind)
   of nkBindStmt:
     result = semBindStmt(c, n, toBind)
-  else: 
+  else:
+    # dotExpr is ambiguous: note that we explicitely allow 'x.TemplateParam',
+    # so we use the generic code for nkDotExpr too
+    if n.kind == nkDotExpr:
+      let s = QualifiedLookUp(c, n, {})
+      if s != nil and Contains(toBind, s.id):
+        return symChoice(c, n, s)
     result = n
     for i in countup(0, sonsLen(n) - 1): 
-      result.sons[i] = resolveTemplateParams(c, n.sons[i], withinBind, toBind)
+      result.sons[i] = resolveTemplateParams(c, n.sons[i], owner, toBind)
   
 proc transformToExpr(n: PNode): PNode = 
   var realStmt: int
@@ -122,7 +126,7 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
       s.typ.sons[0] = newTypeS(tyStmt, c)
       s.typ.n.sons[0] = newNodeIT(nkType, n.info, s.typ.sons[0])
   var toBind = initIntSet()
-  n.sons[bodyPos] = resolveTemplateParams(c, n.sons[bodyPos], false, toBind)
+  n.sons[bodyPos] = resolveTemplateParams(c, n.sons[bodyPos], s, toBind)
   if s.typ.sons[0].kind notin {tyStmt, tyTypeDesc}:
     n.sons[bodyPos] = transformToExpr(n.sons[bodyPos]) 
     # only parameters are resolved, no type checking is performed
diff --git a/doc/grammar.txt b/doc/grammar.txt
index f378cd140..1e54d1116 100755
--- a/doc/grammar.txt
+++ b/doc/grammar.txt
@@ -137,7 +137,7 @@ staticStmt ::= 'static' ':' stmt
 filename ::= symbol | STR_LIT | RSTR_LIT | TRIPLESTR_LIT
 importStmt ::= 'import' filename (comma filename)*
 includeStmt ::= 'include' filename (comma filename)*
-bindStmt ::= 'bind' IDENT (comma IDENT)*
+bindStmt ::= 'bind' qualifiedIdent (comma qualifiedIdent)*
 fromStmt ::= 'from' filename 'import' symbol (comma symbol)*
 
 pragma ::= '{.' optInd (colonExpr [comma])* optPar ('.}' | '}')
diff --git a/tests/run/tstempl.nim b/tests/run/tstempl.nim
new file mode 100644
index 000000000..2b4a8baa0
--- /dev/null
+++ b/tests/run/tstempl.nim
@@ -0,0 +1,24 @@
+discard """
+  output: '''global = levB, arg = levA, test = false
+levB'''
+"""
+
+# tstempl.nim
+import strutils
+
+type 
+  TLev = enum
+    levA,
+    levB
+
+var abclev = levB
+
+template tstLev(abclev: TLev) =
+  bind tstempl.abclev, `%`
+  writeln(stdout, "global = $1, arg = $2, test = $3" % [
+    $tstempl.abclev, $abclev, $(tstempl.abclev == abclev)])
+  # evaluates to true, but must be false
+
+
+tstLev(levA)
+writeln(stdout, $abclev)
diff --git a/todo.txt b/todo.txt
index 15a26f75d..34cd89ae5 100755
--- a/todo.txt
+++ b/todo.txt
@@ -2,7 +2,6 @@ version 0.9.0
 =============
 
 - fix DLLs
-- fix 'bind' for templates
 - implicit deref for parameter matching
 - deprecate ``var x, y = 0`` as it's confusing for tuple consistency
 
@@ -35,7 +34,7 @@ Bugs
   but this can lead to compilation errors
 - bug: the parser is not strict enough with newlines: 'echo "a" echo "b"' 
   compiles
-
+- bug: blocks can "export" an identifier but the CCG generates {} for them ...
 
 version 0.9.XX
 ==============
diff --git a/web/news.txt b/web/news.txt
index df81b952c..fc879f474 100755
--- a/web/news.txt
+++ b/web/news.txt
@@ -95,6 +95,8 @@ Changes affecting backwards compatibility
   conversions from ``int`` to ``int32`` are now forbidden.
 - ``system.byte`` is now an alias for ``uint8``; it used to be an alias 
   to ``int8``.
+- ``bind`` expressions in templates are not properly supported anymore. Use
+  the declarative ``bind`` statement instead.
 
 
 Compiler Additions
@@ -136,6 +138,7 @@ Language Additions
   readability: ``proc divmod(a, b: int; resA, resB: var int)``.
 - A semicolon can now be used to have multiple simple statements on a single
   line: ``inc i; inc j``.
+- ``bind`` supports overloaded symbols and operators.
 
 
 2012-02-09 Version 0.8.14 released
to <nincsnevem662@gmail.com> 2023-08-19 10:02:54 +0200 committer bptato <nincsnevem662@gmail.com> 2023-08-20 15:39:07 +0200 javascript: update Events, misc fixes & additions' href='/ahoang/chawan/commit/src/html/event.nim?id=65ad7f9fc8b69140c050006fbe9ea1644bc283d8'>65ad7f9f ^
cc7eacf7 ^
65ad7f9f ^

b46f8e9c ^


65ad7f9f ^
3c5aa064 ^
65ad7f9f ^
3c5aa064 ^

65ad7f9f ^


bc6f921a ^








65ad7f9f ^
66b9574b ^

65ad7f9f ^


bc6f921a ^


66b9574b ^
65ad7f9f ^

66b9574b ^
9026c578 ^
65ad7f9f ^
79ef0154 ^
65ad7f9f ^


66b9574b ^
3a7d189d ^


860893ab ^
3a7d189d ^

66b9574b ^

65ad7f9f ^





66b9574b ^
65ad7f9f ^
66b9574b ^
65ad7f9f ^

01027f17 ^


65ad7f9f ^






66b9574b ^
65ad7f9f ^

66b9574b ^
65ad7f9f ^
66b9574b ^
65ad7f9f ^



66b9574b ^
65ad7f9f ^

66b9574b ^

65ad7f9f ^

66b9574b ^
65ad7f9f ^
66b9574b ^
65ad7f9f ^



66b9574b ^
65ad7f9f ^

66b9574b ^
65ad7f9f ^

66b9574b ^
65ad7f9f ^

66b9574b ^
79ef0154 ^
65ad7f9f ^
79ef0154 ^
bc6f921a ^
65ad7f9f ^


66b9574b ^
79ef0154 ^
65ad7f9f ^
66b9574b ^


65ad7f9f ^
65ad7f9f ^





b46f8e9c ^
65ad7f9f ^



b46f8e9c ^

65ad7f9f ^





b46f8e9c ^






97037835 ^
b46f8e9c ^
97037835 ^

b46f8e9c ^








97037835 ^
b46f8e9c ^



65ad7f9f ^
b46f8e9c ^
65ad7f9f ^
b46f8e9c ^
65ad7f9f ^

b46f8e9c ^

65ad7f9f ^
b46f8e9c ^
65ad7f9f ^

b46f8e9c ^




65ad7f9f ^

66b9574b ^
65ad7f9f ^






66b9574b ^
65ad7f9f ^


cc7eacf7 ^
65ad7f9f ^





cc7eacf7 ^
65ad7f9f ^
97037835 ^





65ad7f9f ^
cc7eacf7 ^
65ad7f9f ^

b46f8e9c ^




65ad7f9f ^

3a7d189d ^
65ad7f9f ^


b46f8e9c ^
65ad7f9f ^

b46f8e9c ^
65ad7f9f ^
66b9574b ^

65ad7f9f ^



b46f8e9c ^
65ad7f9f ^
3c5aa064 ^
01027f17 ^
65ad7f9f ^
01027f17 ^
3c5aa064 ^
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