summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xdoc/manual.txt8
-rwxr-xr-xrod/pnimsyn.nim43
-rwxr-xr-xtests/accept/run/spec.csv1
-rw-r--r--tests/accept/run/toprprec.nim12
4 files changed, 38 insertions, 26 deletions
diff --git a/doc/manual.txt b/doc/manual.txt
index 4aed263cc..299cf5b0f 100755
--- a/doc/manual.txt
+++ b/doc/manual.txt
@@ -368,19 +368,20 @@ indentation tokens is already described in the `Lexical Analysis`_ section.
 Nimrod allows user-definable operators.

 Binary operators have 8 different levels of precedence. For user-defined

 operators, the precedence depends on the first character the operator consists

-of. All binary operators are left-associative.

+of. All binary operators are left-associative, except binary operator starting

+with (or only consisting of) ``^``.

 

 ================  ==============================================  ==================  ===============

 Precedence level    Operators                                     First characters    Terminal symbol

 ================  ==============================================  ==================  ===============

-  7 (highest)                                                     ``$``               OP7

+  7 (highest)                                                     ``$  ^``            OP7

   6               ``*    /    div   mod   shl  shr  %``           ``* % \  /``        OP6

   5               ``+    -``                                      ``+  ~  |``         OP5

   4               ``&``                                           ``&``               OP4

   3               ``==  <= < >= > !=  in  not_in  is  isnot``     ``= <  > !``        OP3

   2               ``and``                                                             OP2

   1               ``or xor``                                                          OP1

-  0 (lowest)                                                      ``? @ ^ ` : .``     OP0

+  0 (lowest)                                                      ``? @  ` : .``      OP0

 ================  ==============================================  ==================  ===============

 

 

@@ -1530,6 +1531,7 @@ Example:
       close(f)

 

 

+

 The statements after the `try`:idx: are executed in sequential order unless

 an exception ``e`` is raised. If the exception type of ``e`` matches any

 of the list ``exceptlist`` the corresponding statements are executed.

diff --git a/rod/pnimsyn.nim b/rod/pnimsyn.nim
index 766e01671..fe80fe0c4 100755
--- a/rod/pnimsyn.nim
+++ b/rod/pnimsyn.nim
@@ -1,7 +1,7 @@
 #
 #
 #           The Nimrod Compiler
-#        (c) Copyright 2009 Andreas Rumpf
+#        (c) Copyright 2011 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
@@ -145,11 +145,14 @@ proc parseStmt(p: var TParser): PNode
 proc parseTypeDesc(p: var TParser): PNode
 proc parseParamList(p: var TParser): PNode
 
+proc IsLeftAssociative(tok: PToken): bool {.inline.} =
+  result = tok.tokType != tkOpr or tok.ident.s[0] != '^'
+
 proc getPrecedence(tok: PToken): int = 
   case tok.tokType
   of tkOpr: 
     case tok.ident.s[0]
-    of '$': result = 7
+    of '$', '^': result = 7
     of '*', '%', '/', '\\': result = 6
     of '+', '-', '~', '|': result = 5
     of '&': result = 4
@@ -159,7 +162,7 @@ proc getPrecedence(tok: PToken): int =
   of tkIn, tkNotIn, tkIs, tkIsNot: result = 3
   of tkAnd: result = 2
   of tkOr, tkXor: result = 1
-  else: result = - 1
+  else: result = - 10
   
 proc isOperator(tok: PToken): bool = 
   result = getPrecedence(tok) >= 0
@@ -386,14 +389,10 @@ proc parseAddr(p: var TParser): PNode =
 
 proc setBaseFlags(n: PNode, base: TNumericalBase) = 
   case base
-  of base10: 
-    nil
-  of base2: 
-    incl(n.flags, nfBase2)
-  of base8: 
-    incl(n.flags, nfBase8)
-  of base16: 
-    incl(n.flags, nfBase16)
+  of base10: nil
+  of base2: incl(n.flags, nfBase2)
+  of base8: incl(n.flags, nfBase8)
+  of base16: incl(n.flags, nfBase16)
   
 proc identOrLiteral(p: var TParser): PNode = 
   case p.tok.tokType
@@ -475,7 +474,7 @@ proc identOrLiteral(p: var TParser): PNode =
     result = parseAddr(p)
   else: 
     parMessage(p, errExprExpected, tokToStr(p.tok))
-    getTok(p)                 # we must consume a token here to prevend endless loops!
+    getTok(p)  # we must consume a token here to prevend endless loops!
     result = nil
 
 proc primary(p: var TParser): PNode = 
@@ -520,25 +519,23 @@ proc primary(p: var TParser): PNode =
     else: break 
   
 proc lowestExprAux(p: var TParser, v: var PNode, limit: int): PToken = 
-  var 
-    op, nextop: PToken
-    opPred: int
-    v2, node, opNode: PNode
   v = primary(p) # expand while operators have priorities higher than 'limit'
-  op = p.tok
-  opPred = getPrecedence(p.tok)
-  while (opPred > limit): 
-    node = newNodeP(nkInfix, p)
-    opNode = newIdentNodeP(op.ident, p) # skip operator:
+  var op = p.tok
+  var opPrec = getPrecedence(op)
+  while opPrec >= limit: 
+    var leftAssoc = ord(IsLeftAssociative(op))
+    var node = newNodeP(nkInfix, p)
+    var opNode = newIdentNodeP(op.ident, p) # skip operator:
     getTok(p)
     optInd(p, opNode)         # read sub-expression with higher priority
-    nextop = lowestExprAux(p, v2, opPred)
+    var v2: PNode
+    var nextop = lowestExprAux(p, v2, opPrec + leftAssoc)
     addSon(node, opNode)
     addSon(node, v)
     addSon(node, v2)
     v = node
     op = nextop
-    opPred = getPrecedence(nextop)
+    opPrec = getPrecedence(nextop)
   result = op                 # return first untreated operator
   
 proc lowestExpr(p: var TParser): PNode = 
diff --git a/tests/accept/run/spec.csv b/tests/accept/run/spec.csv
index 075e4e67e..980301bd0 100755
--- a/tests/accept/run/spec.csv
+++ b/tests/accept/run/spec.csv
@@ -49,6 +49,7 @@ tnestprc.nim;10
 toop1.nim;34[]o 5
 topenarrayrepr.nim;5 - [1]
 topenlen.nim;7
+toprprec.nim;done
 toverflw.nim;the computation overflowed
 toverflw2.nim;Error: unhandled exception: over- or underflow [EOverflow]
 toverl2.nim;true012
diff --git a/tests/accept/run/toprprec.nim b/tests/accept/run/toprprec.nim
new file mode 100644
index 000000000..4728b2e68
--- /dev/null
+++ b/tests/accept/run/toprprec.nim
@@ -0,0 +1,12 @@
+# Test operator precedence: 
+
+assert 3+5*5-2 == 28- -26-28
+
+proc `^-` (x, y: int): int =  
+  # now right-associative!
+  result = x - y
+  
+assert 34 ^- 6 ^- 2 == 30
+assert 34 - 6 - 2 == 26
+echo "done"
+
k.com> 2015-09-12 13:51:25 -0700 2183 - environment + external editor using tmux' href='/akkartik/mu/commit/sandbox/006-sandbox-edit.mu?h=main&id=fa94f4d92340f001560b16dd0c2e5681ca5db031'>fa94f4d9 ^
d31037ff ^
fa94f4d9 ^

aaf61a53 ^
fa94f4d9 ^




b0bf5321 ^
fa94f4d9 ^
be422222 ^
b0bf5321 ^

be422222 ^
fa94f4d9 ^

b0bf5321 ^
fa94f4d9 ^
be422222 ^
b0bf5321 ^
d31037ff ^
a01dd595 ^
1ead3562 ^
d31037ff ^
fa94f4d9 ^
d31037ff ^

b0bf5321 ^
d31037ff ^
fa94f4d9 ^
d31037ff ^


b0bf5321 ^
d31037ff ^

b0bf5321 ^
d31037ff ^












95425355 ^
d31037ff ^


fa94f4d9 ^


e00d4854 ^
fa94f4d9 ^

b0bf5321 ^

fa94f4d9 ^



b0bf5321 ^
fa94f4d9 ^



3151fb23 ^
d135851e ^
fa94f4d9 ^







fa94f4d9 ^





b0bf5321 ^
fa94f4d9 ^


d135851e ^
fa94f4d9 ^




e0d69d3b ^




b0bf5321 ^


e0d69d3b ^









b0bf5321 ^
e0d69d3b ^













b0bf5321 ^
e0d69d3b ^












95425355 ^




b0bf5321 ^


95425355 ^







b0bf5321 ^
95425355 ^















b0bf5321 ^
95425355 ^


















b0bf5321 ^
95425355 ^











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