@@ -204,6 +204,63 @@ let s:TOKEN_DOTDOTDOT = 63
204
204
let s: TOKEN_SHARP = 64
205
205
let s: TOKEN_ARROW = 65
206
206
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
+
207
264
let s: MAX_FUNC_ARGS = 20
208
265
209
266
function ! s: isalpha (c )
@@ -5125,171 +5182,177 @@ function! s:Printer.print_execute(node)
5125
5182
endfunction
5126
5183
5127
5184
function ! 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 )
5129
5192
endfunction
5130
5193
5131
5194
function ! 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, ' || ' )
5133
5196
endfunction
5134
5197
5135
5198
function ! 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, ' && ' )
5137
5200
endfunction
5138
5201
5139
5202
function ! 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, ' == ' )
5141
5204
endfunction
5142
5205
5143
5206
function ! 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, ' ==? ' )
5145
5208
endfunction
5146
5209
5147
5210
function ! 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, ' ==# ' )
5149
5212
endfunction
5150
5213
5151
5214
function ! 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, ' != ' )
5153
5216
endfunction
5154
5217
5155
5218
function ! 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, ' !=? ' )
5157
5220
endfunction
5158
5221
5159
5222
function ! 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, ' !=# ' )
5161
5224
endfunction
5162
5225
5163
5226
function ! 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, ' > ' )
5165
5228
endfunction
5166
5229
5167
5230
function ! 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, ' >? ' )
5169
5232
endfunction
5170
5233
5171
5234
function ! 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, ' ># ' )
5173
5236
endfunction
5174
5237
5175
5238
function ! 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, ' >= ' )
5177
5240
endfunction
5178
5241
5179
5242
function ! 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, ' >=? ' )
5181
5244
endfunction
5182
5245
5183
5246
function ! 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, ' >=# ' )
5185
5248
endfunction
5186
5249
5187
5250
function ! 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, ' < ' )
5189
5252
endfunction
5190
5253
5191
5254
function ! 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, ' <? ' )
5193
5256
endfunction
5194
5257
5195
5258
function ! 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, ' <# ' )
5197
5260
endfunction
5198
5261
5199
5262
function ! 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, ' <= ' )
5201
5264
endfunction
5202
5265
5203
5266
function ! 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, ' <=? ' )
5205
5268
endfunction
5206
5269
5207
5270
function ! 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, ' <=# ' )
5209
5272
endfunction
5210
5273
5211
5274
function ! 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, ' =~ ' )
5213
5276
endfunction
5214
5277
5215
5278
function ! 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, ' =~? ' )
5217
5280
endfunction
5218
5281
5219
5282
function ! 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, ' =~# ' )
5221
5284
endfunction
5222
5285
5223
5286
function ! 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, ' !~ ' )
5225
5288
endfunction
5226
5289
5227
5290
function ! 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, ' !~? ' )
5229
5292
endfunction
5230
5293
5231
5294
function ! 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, ' !~# ' )
5233
5296
endfunction
5234
5297
5235
5298
function ! 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 ' )
5237
5300
endfunction
5238
5301
5239
5302
function ! 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? ' )
5241
5304
endfunction
5242
5305
5243
5306
function ! 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# ' )
5245
5308
endfunction
5246
5309
5247
5310
function ! 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 ' )
5249
5312
endfunction
5250
5313
5251
5314
function ! 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? ' )
5253
5316
endfunction
5254
5317
5255
5318
function ! 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# ' )
5257
5320
endfunction
5258
5321
5259
5322
function ! 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, ' + ' )
5261
5324
endfunction
5262
5325
5263
5326
function ! 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, ' - ' )
5265
5328
endfunction
5266
5329
5267
5330
function ! 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, ' . ' )
5269
5332
endfunction
5270
5333
5271
5334
function ! 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, ' * ' )
5273
5336
endfunction
5274
5337
5275
5338
function ! 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, ' / ' )
5277
5340
endfunction
5278
5341
5279
5342
function ! 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, ' % ' )
5281
5344
endfunction
5282
5345
5283
5346
function ! s: Printer .print_not (node)
5284
- return printf ( ' (!(%s)) ' , self .print (a: node. left ) )
5347
+ return self .print_op1 (a: node, ' ! ' )
5285
5348
endfunction
5286
5349
5287
5350
function ! s: Printer .print_plus (node)
5288
- return printf ( ' (+(%s)) ' , self .print (a: node. left ) )
5351
+ return self .print_op1 (a: node, ' + ' )
5289
5352
endfunction
5290
5353
5291
5354
function ! s: Printer .print_minus (node)
5292
- return printf ( ' (-(%s)) ' , self .print (a: node. left ) )
5355
+ return self .print_op1 (a: node, ' - ' )
5293
5356
endfunction
5294
5357
5295
5358
function ! s: Printer .print_subscript (node)
@@ -5403,6 +5466,26 @@ function! s:Printer.print_modifiers(node)
5403
5466
call self .out (' %s ' , join (modlist, ' ' ))
5404
5467
endfunction
5405
5468
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
+
5406
5489
" TODO: under construction
5407
5490
let s: RegexpParser = {}
5408
5491
0 commit comments