@@ -204,6 +204,63 @@ let s:TOKEN_DOTDOTDOT = 63
204204let s: TOKEN_SHARP = 64
205205let s: TOKEN_ARROW = 65
206206
207+ let s: opprec = {}
208+ let s: opprec [s: NODE_TERNARY ] = 1
209+ let s: opprec [s: NODE_OR ] = 2
210+ let s: opprec [s: NODE_AND ] = 3
211+ let s: opprec [s: NODE_EQUAL ] = 4
212+ let s: opprec [s: NODE_EQUALCI ] = 4
213+ let s: opprec [s: NODE_EQUALCS ] = 4
214+ let s: opprec [s: NODE_NEQUAL ] = 4
215+ let s: opprec [s: NODE_NEQUALCI ] = 4
216+ let s: opprec [s: NODE_NEQUALCS ] = 4
217+ let s: opprec [s: NODE_GREATER ] = 4
218+ let s: opprec [s: NODE_GREATERCI ] = 4
219+ let s: opprec [s: NODE_GREATERCS ] = 4
220+ let s: opprec [s: NODE_GEQUAL ] = 4
221+ let s: opprec [s: NODE_GEQUALCI ] = 4
222+ let s: opprec [s: NODE_GEQUALCS ] = 4
223+ let s: opprec [s: NODE_SMALLER ] = 4
224+ let s: opprec [s: NODE_SMALLERCI ] = 4
225+ let s: opprec [s: NODE_SMALLERCS ] = 4
226+ let s: opprec [s: NODE_SEQUAL ] = 4
227+ let s: opprec [s: NODE_SEQUALCI ] = 4
228+ let s: opprec [s: NODE_SEQUALCS ] = 4
229+ let s: opprec [s: NODE_MATCH ] = 4
230+ let s: opprec [s: NODE_MATCHCI ] = 4
231+ let s: opprec [s: NODE_MATCHCS ] = 4
232+ let s: opprec [s: NODE_NOMATCH ] = 4
233+ let s: opprec [s: NODE_NOMATCHCI ] = 4
234+ let s: opprec [s: NODE_NOMATCHCS ] = 4
235+ let s: opprec [s: NODE_IS ] = 4
236+ let s: opprec [s: NODE_ISCI ] = 4
237+ let s: opprec [s: NODE_ISCS ] = 4
238+ let s: opprec [s: NODE_ISNOT ] = 4
239+ let s: opprec [s: NODE_ISNOTCI ] = 4
240+ let s: opprec [s: NODE_ISNOTCS ] = 4
241+ let s: opprec [s: NODE_ADD ] = 5
242+ let s: opprec [s: NODE_SUBTRACT ] = 5
243+ let s: opprec [s: NODE_CONCAT ] = 5
244+ let s: opprec [s: NODE_MULTIPLY ] = 6
245+ let s: opprec [s: NODE_DIVIDE ] = 6
246+ let s: opprec [s: NODE_REMAINDER ] = 6
247+ let s: opprec [s: NODE_NOT ] = 7
248+ let s: opprec [s: NODE_MINUS ] = 7
249+ let s: opprec [s: NODE_PLUS ] = 7
250+ let s: opprec [s: NODE_SUBSCRIPT ] = 8
251+ let s: opprec [s: NODE_SLICE ] = 8
252+ let s: opprec [s: NODE_CALL ] = 8
253+ let s: opprec [s: NODE_DOT ] = 8
254+ let s: opprec [s: NODE_NUMBER ] = 9
255+ let s: opprec [s: NODE_STRING ] = 9
256+ let s: opprec [s: NODE_LIST ] = 9
257+ let s: opprec [s: NODE_DICT ] = 9
258+ let s: opprec [s: NODE_OPTION ] = 9
259+ let s: opprec [s: NODE_IDENTIFIER ] = 9
260+ let s: opprec [s: NODE_CURLYNAME ] = 9
261+ let s: opprec [s: NODE_ENV ] = 9
262+ let s: opprec [s: NODE_REG ] = 9
263+
207264let s: MAX_FUNC_ARGS = 20
208265
209266function ! s: isalpha (c )
@@ -5125,171 +5182,177 @@ function! s:Printer.print_execute(node)
51255182endfunction
51265183
51275184function ! s: Printer .print_ternary (node)
5128- return printf (' (%s ? %s : %s)' , self .print (a: node .cond), self .print (a: node .left ), self .print (a: node .right ))
5185+ let cond = self .print (a: node .cond)
5186+ if s: opprec [a: node .type ] >= s: opprec [a: node .cond.type ]
5187+ let cond = ' (' . cond . ' )'
5188+ endif
5189+ let left = self .print (a: node .left )
5190+ let right = self .print (a: node .right )
5191+ return printf (' %s ? %s : %s' , cond, left , right )
51295192endfunction
51305193
51315194function ! s: Printer .print_or (node)
5132- return printf ( ' (%s || %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5195+ return self .print_op2 (a: node, ' || ' )
51335196endfunction
51345197
51355198function ! s: Printer .print_and (node)
5136- return printf ( ' (%s && %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5199+ return self .print_op2 (a: node, ' && ' )
51375200endfunction
51385201
51395202function ! s: Printer .print_equal (node)
5140- return printf ( ' (%s == %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5203+ return self .print_op2 (a: node, ' == ' )
51415204endfunction
51425205
51435206function ! s: Printer .print_equalci (node)
5144- return printf ( ' (%s ==? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5207+ return self .print_op2 (a: node, ' ==? ' )
51455208endfunction
51465209
51475210function ! s: Printer .print_equalcs (node)
5148- return printf ( ' (%s ==# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5211+ return self .print_op2 (a: node, ' ==# ' )
51495212endfunction
51505213
51515214function ! s: Printer .print_nequal (node)
5152- return printf ( ' (%s != %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5215+ return self .print_op2 (a: node, ' != ' )
51535216endfunction
51545217
51555218function ! s: Printer .print_nequalci (node)
5156- return printf ( ' (%s !=? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5219+ return self .print_op2 (a: node, ' !=? ' )
51575220endfunction
51585221
51595222function ! s: Printer .print_nequalcs (node)
5160- return printf ( ' (%s !=# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5223+ return self .print_op2 (a: node, ' !=# ' )
51615224endfunction
51625225
51635226function ! s: Printer .print_greater (node)
5164- return printf ( ' (%s > %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5227+ return self .print_op2 (a: node, ' > ' )
51655228endfunction
51665229
51675230function ! s: Printer .print_greaterci (node)
5168- return printf ( ' (%s >? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5231+ return self .print_op2 (a: node, ' >? ' )
51695232endfunction
51705233
51715234function ! s: Printer .print_greatercs (node)
5172- return printf ( ' (%s ># %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5235+ return self .print_op2 (a: node, ' ># ' )
51735236endfunction
51745237
51755238function ! s: Printer .print_gequal (node)
5176- return printf ( ' (%s >= %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5239+ return self .print_op2 (a: node, ' >= ' )
51775240endfunction
51785241
51795242function ! s: Printer .print_gequalci (node)
5180- return printf ( ' (%s >=? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5243+ return self .print_op2 (a: node, ' >=? ' )
51815244endfunction
51825245
51835246function ! s: Printer .print_gequalcs (node)
5184- return printf ( ' (%s >=# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5247+ return self .print_op2 (a: node, ' >=# ' )
51855248endfunction
51865249
51875250function ! s: Printer .print_smaller (node)
5188- return printf ( ' (%s < %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5251+ return self .print_op2 (a: node, ' < ' )
51895252endfunction
51905253
51915254function ! s: Printer .print_smallerci (node)
5192- return printf ( ' (%s <? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5255+ return self .print_op2 (a: node, ' <? ' )
51935256endfunction
51945257
51955258function ! s: Printer .print_smallercs (node)
5196- return printf ( ' (%s <# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5259+ return self .print_op2 (a: node, ' <# ' )
51975260endfunction
51985261
51995262function ! s: Printer .print_sequal (node)
5200- return printf ( ' (%s <= %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5263+ return self .print_op2 (a: node, ' <= ' )
52015264endfunction
52025265
52035266function ! s: Printer .print_sequalci (node)
5204- return printf ( ' (%s <=? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5267+ return self .print_op2 (a: node, ' <=? ' )
52055268endfunction
52065269
52075270function ! s: Printer .print_sequalcs (node)
5208- return printf ( ' (%s <=# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5271+ return self .print_op2 (a: node, ' <=# ' )
52095272endfunction
52105273
52115274function ! s: Printer .print_match (node)
5212- return printf ( ' (%s =~ %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5275+ return self .print_op2 (a: node, ' =~ ' )
52135276endfunction
52145277
52155278function ! s: Printer .print_matchci (node)
5216- return printf ( ' (%s =~? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5279+ return self .print_op2 (a: node, ' =~? ' )
52175280endfunction
52185281
52195282function ! s: Printer .print_matchcs (node)
5220- return printf ( ' (%s =~# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5283+ return self .print_op2 (a: node, ' =~# ' )
52215284endfunction
52225285
52235286function ! s: Printer .print_nomatch (node)
5224- return printf ( ' (%s !~ %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5287+ return self .print_op2 (a: node, ' !~ ' )
52255288endfunction
52265289
52275290function ! s: Printer .print_nomatchci (node)
5228- return printf ( ' (%s !~? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5291+ return self .print_op2 (a: node, ' !~? ' )
52295292endfunction
52305293
52315294function ! s: Printer .print_nomatchcs (node)
5232- return printf ( ' (%s !~# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5295+ return self .print_op2 (a: node, ' !~# ' )
52335296endfunction
52345297
52355298function ! s: Printer .print_is (node)
5236- return printf ( ' (%s is %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5299+ return self .print_op2 (a: node, ' is ' )
52375300endfunction
52385301
52395302function ! s: Printer .print_isci (node)
5240- return printf ( ' (%s is? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5303+ return self .print_op2 (a: node, ' is? ' )
52415304endfunction
52425305
52435306function ! s: Printer .print_iscs (node)
5244- return printf ( ' (%s is# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5307+ return self .print_op2 (a: node, ' is# ' )
52455308endfunction
52465309
52475310function ! s: Printer .print_isnot (node)
5248- return printf ( ' (%s isnot %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5311+ return self .print_op2 (a: node, ' isnot ' )
52495312endfunction
52505313
52515314function ! s: Printer .print_isnotci (node)
5252- return printf ( ' (%s isnot? %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5315+ return self .print_op2 (a: node, ' isnot? ' )
52535316endfunction
52545317
52555318function ! s: Printer .print_isnotcs (node)
5256- return printf ( ' (%s isnot# %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5319+ return self .print_op2 (a: node, ' isnot# ' )
52575320endfunction
52585321
52595322function ! s: Printer .print_add (node)
5260- return printf ( ' (%s + %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5323+ return self .print_op2 (a: node, ' + ' )
52615324endfunction
52625325
52635326function ! s: Printer .print_subtract (node)
5264- return printf ( ' (%s - %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5327+ return self .print_op2 (a: node, ' - ' )
52655328endfunction
52665329
52675330function ! s: Printer .print_concat (node)
5268- return printf ( ' (%s . %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5331+ return self .print_op2 (a: node, ' . ' )
52695332endfunction
52705333
52715334function ! s: Printer .print_multiply (node)
5272- return printf ( ' (%s * %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5335+ return self .print_op2 (a: node, ' * ' )
52735336endfunction
52745337
52755338function ! s: Printer .print_divide (node)
5276- return printf ( ' (%s / %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5339+ return self .print_op2 (a: node, ' / ' )
52775340endfunction
52785341
52795342function ! s: Printer .print_remainder (node)
5280- return printf ( ' (%s %% %s) ' , self .print (a: node. left ), self . print ( a: node . right ) )
5343+ return self .print_op2 (a: node, ' % ' )
52815344endfunction
52825345
52835346function ! s: Printer .print_not (node)
5284- return printf ( ' (!(%s)) ' , self .print (a: node. left ) )
5347+ return self .print_op1 (a: node, ' ! ' )
52855348endfunction
52865349
52875350function ! s: Printer .print_plus (node)
5288- return printf ( ' (+(%s)) ' , self .print (a: node. left ) )
5351+ return self .print_op1 (a: node, ' + ' )
52895352endfunction
52905353
52915354function ! s: Printer .print_minus (node)
5292- return printf ( ' (-(%s)) ' , self .print (a: node. left ) )
5355+ return self .print_op1 (a: node, ' - ' )
52935356endfunction
52945357
52955358function ! s: Printer .print_subscript (node)
@@ -5403,6 +5466,26 @@ function! s:Printer.print_modifiers(node)
54035466 call self .out (' %s ' , join (modlist, ' ' ))
54045467endfunction
54055468
5469+ function s: Printer .print_op1 (node, op )
5470+ let left = self .print (a: node .left )
5471+ if s: opprec [a: node .type ] > s: opprec [a: node .left .type ]
5472+ let left = ' (' . left . ' )'
5473+ endif
5474+ return printf (' %s%s' , a: op , left )
5475+ endfunction
5476+
5477+ function s: Printer .print_op2 (node, op )
5478+ let left = self .print (a: node .left )
5479+ if s: opprec [a: node .type ] > s: opprec [a: node .left .type ]
5480+ let left = ' (' . left . ' )'
5481+ endif
5482+ let right = self .print (a: node .right )
5483+ if s: opprec [a: node .type ] > s: opprec [a: node .right .type ]
5484+ let right = ' (' . right . ' )'
5485+ endif
5486+ return printf (' %s %s %s' , left , a: op , right )
5487+ endfunction
5488+
54065489" TODO: under construction
54075490let s: RegexpParser = {}
54085491
0 commit comments