-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpycompiler.vim
946 lines (840 loc) · 27 KB
/
pycompiler.vim
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
call extend(s:, vimlparser#import())
" Ignore undefined variable errors of vint
" vint: -ProhibitUsingUndeclaredVariable
let s:StringReader = s:StringReader
let s:VimLParser = s:VimLParser
let s:ExprParser = s:ExprParser
let s:NIL = s:NIL
" vint: +ProhibitUsingUndeclaredVariable
let s:opprec = {}
" vint: -ProhibitUsingUndeclaredVariable
let s:opprec[s:NODE_TERNARY] = 1
let s:opprec[s:NODE_OR] = 2
let s:opprec[s:NODE_AND] = 3
let s:opprec[s:NODE_EQUAL] = 4
let s:opprec[s:NODE_EQUALCI] = 4
let s:opprec[s:NODE_EQUALCS] = 4
let s:opprec[s:NODE_NEQUAL] = 4
let s:opprec[s:NODE_NEQUALCI] = 4
let s:opprec[s:NODE_NEQUALCS] = 4
let s:opprec[s:NODE_GREATER] = 4
let s:opprec[s:NODE_GREATERCI] = 4
let s:opprec[s:NODE_GREATERCS] = 4
let s:opprec[s:NODE_GEQUAL] = 4
let s:opprec[s:NODE_GEQUALCI] = 4
let s:opprec[s:NODE_GEQUALCS] = 4
let s:opprec[s:NODE_SMALLER] = 4
let s:opprec[s:NODE_SMALLERCI] = 4
let s:opprec[s:NODE_SMALLERCS] = 4
let s:opprec[s:NODE_SEQUAL] = 4
let s:opprec[s:NODE_SEQUALCI] = 4
let s:opprec[s:NODE_SEQUALCS] = 4
let s:opprec[s:NODE_MATCH] = 4
let s:opprec[s:NODE_MATCHCI] = 4
let s:opprec[s:NODE_MATCHCS] = 4
let s:opprec[s:NODE_NOMATCH] = 4
let s:opprec[s:NODE_NOMATCHCI] = 4
let s:opprec[s:NODE_NOMATCHCS] = 4
let s:opprec[s:NODE_IS] = 4
let s:opprec[s:NODE_ISCI] = 4
let s:opprec[s:NODE_ISCS] = 4
let s:opprec[s:NODE_ISNOT] = 4
let s:opprec[s:NODE_ISNOTCI] = 4
let s:opprec[s:NODE_ISNOTCS] = 4
let s:opprec[s:NODE_ADD] = 5
let s:opprec[s:NODE_SUBTRACT] = 5
let s:opprec[s:NODE_CONCAT] = 5
let s:opprec[s:NODE_MULTIPLY] = 6
let s:opprec[s:NODE_DIVIDE] = 6
let s:opprec[s:NODE_REMAINDER] = 6
let s:opprec[s:NODE_NOT] = 7
let s:opprec[s:NODE_MINUS] = 7
let s:opprec[s:NODE_PLUS] = 7
let s:opprec[s:NODE_SUBSCRIPT] = 8
let s:opprec[s:NODE_SLICE] = 8
let s:opprec[s:NODE_CALL] = 8
let s:opprec[s:NODE_DOT] = 8
let s:opprec[s:NODE_NUMBER] = 9
let s:opprec[s:NODE_BLOB] = 9
let s:opprec[s:NODE_STRING] = 9
let s:opprec[s:NODE_LIST] = 9
let s:opprec[s:NODE_DICT] = 9
let s:opprec[s:NODE_OPTION] = 9
let s:opprec[s:NODE_IDENTIFIER] = 9
let s:opprec[s:NODE_CURLYNAME] = 9
let s:opprec[s:NODE_ENV] = 9
let s:opprec[s:NODE_REG] = 9
" vint: +ProhibitUsingUndeclaredVariable
" Reserved Python keywords (dict for faster lookup).
let s:reserved_keywords = {
\ 'False': 1,
\ 'None': 1,
\ 'True': 1,
\ 'and': 1,
\ 'as': 1,
\ 'assert': 1,
\ 'break': 1,
\ 'class': 1,
\ 'continue': 1,
\ 'def': 1,
\ 'del': 1,
\ 'elif': 1,
\ 'else': 1,
\ 'except': 1,
\ 'finally': 1,
\ 'for': 1,
\ 'from': 1,
\ 'global': 1,
\ 'if': 1,
\ 'import': 1,
\ 'in': 1,
\ 'is': 1,
\ 'lambda': 1,
\ 'nonlocal': 1,
\ 'not': 1,
\ 'or': 1,
\ 'pass': 1,
\ 'raise': 1,
\ 'return': 1,
\ 'try': 1,
\ 'while': 1,
\ 'with': 1,
\ 'yield': 1}
let s:PythonCompiler = {}
function s:PythonCompiler.new(...)
let obj = copy(self)
call call(obj.__init__, a:000, obj)
return obj
endfunction
function s:PythonCompiler.__init__()
let self.indent = ['']
let self.lines = []
let self.in_class = 0
endfunction
function s:PythonCompiler.out(...)
if len(a:000) ==# 1
if a:000[0] =~# '^)\+$'
let self.lines[-1] .= a:000[0]
else
call add(self.lines, self.indent[0] . a:000[0])
endif
else
call add(self.lines, self.indent[0] . call('printf', a:000))
endif
endfunction
function s:PythonCompiler.emptyline()
call add(self.lines, '')
endfunction
function s:PythonCompiler.incindent(s)
call insert(self.indent, self.indent[0] . a:s)
endfunction
function s:PythonCompiler.decindent()
call remove(self.indent, 0)
endfunction
" vint: -ProhibitUsingUndeclaredVariable
function s:PythonCompiler.compile(node)
if a:node.type ==# s:NODE_TOPLEVEL
return self.compile_toplevel(a:node)
elseif a:node.type ==# s:NODE_COMMENT
return self.compile_comment(a:node)
elseif a:node.type ==# s:NODE_EXCMD
return self.compile_excmd(a:node)
elseif a:node.type ==# s:NODE_FUNCTION
return self.compile_function(a:node)
elseif a:node.type ==# s:NODE_DELFUNCTION
return self.compile_delfunction(a:node)
elseif a:node.type ==# s:NODE_RETURN
return self.compile_return(a:node)
elseif a:node.type ==# s:NODE_EXCALL
return self.compile_excall(a:node)
elseif a:node.type ==# s:NODE_LET
return self.compile_let(a:node)
elseif a:node.type ==# s:NODE_UNLET
return self.compile_unlet(a:node)
elseif a:node.type ==# s:NODE_LOCKVAR
return self.compile_lockvar(a:node)
elseif a:node.type ==# s:NODE_UNLOCKVAR
return self.compile_unlockvar(a:node)
elseif a:node.type ==# s:NODE_IF
return self.compile_if(a:node)
elseif a:node.type ==# s:NODE_WHILE
return self.compile_while(a:node)
elseif a:node.type ==# s:NODE_FOR
return self.compile_for(a:node)
elseif a:node.type ==# s:NODE_CONTINUE
return self.compile_continue(a:node)
elseif a:node.type ==# s:NODE_BREAK
return self.compile_break(a:node)
elseif a:node.type ==# s:NODE_TRY
return self.compile_try(a:node)
elseif a:node.type ==# s:NODE_THROW
return self.compile_throw(a:node)
elseif a:node.type ==# s:NODE_ECHO
return self.compile_echo(a:node)
elseif a:node.type ==# s:NODE_ECHON
return self.compile_echon(a:node)
elseif a:node.type ==# s:NODE_ECHOHL
return self.compile_echohl(a:node)
elseif a:node.type ==# s:NODE_ECHOMSG
return self.compile_echomsg(a:node)
elseif a:node.type ==# s:NODE_ECHOERR
return self.compile_echoerr(a:node)
elseif a:node.type ==# s:NODE_EXECUTE
return self.compile_execute(a:node)
elseif a:node.type ==# s:NODE_TERNARY
return self.compile_ternary(a:node)
elseif a:node.type ==# s:NODE_OR
return self.compile_or(a:node)
elseif a:node.type ==# s:NODE_AND
return self.compile_and(a:node)
elseif a:node.type ==# s:NODE_EQUAL
return self.compile_equal(a:node)
elseif a:node.type ==# s:NODE_EQUALCI
return self.compile_equalci(a:node)
elseif a:node.type ==# s:NODE_EQUALCS
return self.compile_equalcs(a:node)
elseif a:node.type ==# s:NODE_NEQUAL
return self.compile_nequal(a:node)
elseif a:node.type ==# s:NODE_NEQUALCI
return self.compile_nequalci(a:node)
elseif a:node.type ==# s:NODE_NEQUALCS
return self.compile_nequalcs(a:node)
elseif a:node.type ==# s:NODE_GREATER
return self.compile_greater(a:node)
elseif a:node.type ==# s:NODE_GREATERCI
return self.compile_greaterci(a:node)
elseif a:node.type ==# s:NODE_GREATERCS
return self.compile_greatercs(a:node)
elseif a:node.type ==# s:NODE_GEQUAL
return self.compile_gequal(a:node)
elseif a:node.type ==# s:NODE_GEQUALCI
return self.compile_gequalci(a:node)
elseif a:node.type ==# s:NODE_GEQUALCS
return self.compile_gequalcs(a:node)
elseif a:node.type ==# s:NODE_SMALLER
return self.compile_smaller(a:node)
elseif a:node.type ==# s:NODE_SMALLERCI
return self.compile_smallerci(a:node)
elseif a:node.type ==# s:NODE_SMALLERCS
return self.compile_smallercs(a:node)
elseif a:node.type ==# s:NODE_SEQUAL
return self.compile_sequal(a:node)
elseif a:node.type ==# s:NODE_SEQUALCI
return self.compile_sequalci(a:node)
elseif a:node.type ==# s:NODE_SEQUALCS
return self.compile_sequalcs(a:node)
elseif a:node.type ==# s:NODE_MATCH
return self.compile_match(a:node)
elseif a:node.type ==# s:NODE_MATCHCI
return self.compile_matchci(a:node)
elseif a:node.type ==# s:NODE_MATCHCS
return self.compile_matchcs(a:node)
elseif a:node.type ==# s:NODE_NOMATCH
return self.compile_nomatch(a:node)
elseif a:node.type ==# s:NODE_NOMATCHCI
return self.compile_nomatchci(a:node)
elseif a:node.type ==# s:NODE_NOMATCHCS
return self.compile_nomatchcs(a:node)
elseif a:node.type ==# s:NODE_IS
return self.compile_is(a:node)
elseif a:node.type ==# s:NODE_ISCI
return self.compile_isci(a:node)
elseif a:node.type ==# s:NODE_ISCS
return self.compile_iscs(a:node)
elseif a:node.type ==# s:NODE_ISNOT
return self.compile_isnot(a:node)
elseif a:node.type ==# s:NODE_ISNOTCI
return self.compile_isnotci(a:node)
elseif a:node.type ==# s:NODE_ISNOTCS
return self.compile_isnotcs(a:node)
elseif a:node.type ==# s:NODE_ADD
return self.compile_add(a:node)
elseif a:node.type ==# s:NODE_SUBTRACT
return self.compile_subtract(a:node)
elseif a:node.type ==# s:NODE_CONCAT
return self.compile_concat(a:node)
elseif a:node.type ==# s:NODE_MULTIPLY
return self.compile_multiply(a:node)
elseif a:node.type ==# s:NODE_DIVIDE
return self.compile_divide(a:node)
elseif a:node.type ==# s:NODE_REMAINDER
return self.compile_remainder(a:node)
elseif a:node.type ==# s:NODE_NOT
return self.compile_not(a:node)
elseif a:node.type ==# s:NODE_PLUS
return self.compile_plus(a:node)
elseif a:node.type ==# s:NODE_MINUS
return self.compile_minus(a:node)
elseif a:node.type ==# s:NODE_SUBSCRIPT
return self.compile_subscript(a:node)
elseif a:node.type ==# s:NODE_SLICE
return self.compile_slice(a:node)
elseif a:node.type ==# s:NODE_DOT
return self.compile_dot(a:node)
elseif a:node.type ==# s:NODE_CALL
return self.compile_call(a:node)
elseif a:node.type ==# s:NODE_NUMBER
return self.compile_number(a:node)
elseif a:node.type ==# s:NODE_BLOB
return self.compile_blob(a:node)
elseif a:node.type ==# s:NODE_STRING
return self.compile_string(a:node)
elseif a:node.type ==# s:NODE_LIST
return self.compile_list(a:node)
elseif a:node.type ==# s:NODE_DICT
return self.compile_dict(a:node)
elseif a:node.type ==# s:NODE_OPTION
return self.compile_option(a:node)
elseif a:node.type ==# s:NODE_IDENTIFIER
return self.compile_identifier(a:node)
elseif a:node.type ==# s:NODE_CURLYNAME
return self.compile_curlyname(a:node)
elseif a:node.type ==# s:NODE_ENV
return self.compile_env(a:node)
elseif a:node.type ==# s:NODE_REG
return self.compile_reg(a:node)
else
throw self.err('Compiler: unknown node: %s', string(a:node))
endif
endfunction
" vint: +ProhibitUsingUndeclaredVariable
function s:PythonCompiler.compile_body(body)
let empty = 1
for node in a:body
call self.compile(node)
" vint: -ProhibitUsingUndeclaredVariable
if node.type != s:NODE_COMMENT
" vint: +ProhibitUsingUndeclaredVariable
let empty = 0
endif
endfor
if empty
call self.out('pass')
endif
endfunction
function s:PythonCompiler.compile_toplevel(node)
call self.compile_body(a:node.body)
return self.lines
endfunction
function s:PythonCompiler.compile_comment(node)
call self.out('#%s', a:node.str)
endfunction
function s:PythonCompiler.compile_excmd(node)
throw 'NotImplemented: excmd'
endfunction
function s:PythonCompiler.insert_empty_lines_before_comment(count)
" Find start of preceding comment (block).
let comment_start = 0
let len_lines = len(self.lines)
if len_lines
while 1
let line = get(self.lines, comment_start - 1, '')
if line !~# '^\s*#'
break
endif
let comment_start -= 1
" Adjust indentation to current level.
let self.lines[comment_start] = substitute(line, '^\s\+', self.indent[0], '')
endwhile
if comment_start != 0
let comment_start = len_lines + comment_start
endif
endif
if comment_start
for c in range(a:count)
call insert(self.lines, '', comment_start)
endfor
else
for c in range(a:count)
call self.emptyline()
endfor
endif
endfunction
function s:PythonCompiler.compile_function(node)
let left = self.compile(a:node.left)
let rlist = map(a:node.rlist, 'self.compile(v:val)')
if !empty(rlist) && rlist[-1] ==# '...'
let rlist[-1] = '*a000'
endif
if left =~# '^\(VimLParser\|ExprTokenizer\|ExprParser\|LvalueParser\|StringReader\|Compiler\|RegexpParser\)\.'
let left = matchstr(left, '\.\zs.*')
if left ==# 'new'
return
endif
call self.insert_empty_lines_before_comment(1)
call insert(rlist, 'self')
call self.out('def %s(%s):', left, join(rlist, ', '))
call self.incindent(' ')
call self.compile_body(a:node.body)
call self.decindent()
else
if self.in_class
let self.in_class = 0
call self.decindent()
endif
call self.insert_empty_lines_before_comment(2)
call self.out('def %s(%s):', left, join(rlist, ', '))
call self.incindent(' ')
call self.compile_body(a:node.body)
call self.decindent()
endif
endfunction
function s:PythonCompiler.compile_delfunction(node)
throw 'NotImplemented: delfunction'
endfunction
function s:PythonCompiler.compile_return(node)
if a:node.left is s:NIL
call self.out('return')
else
call self.out('return %s', self.compile(a:node.left))
endif
endfunction
function s:PythonCompiler.compile_excall(node)
call self.out('%s', self.compile(a:node.left))
endfunction
function s:PythonCompiler.compile_let(node)
let op = a:node.op
if op ==# '.='
let op = '+='
endif
let right = self.compile(a:node.right)
if a:node.left isnot s:NIL
let left = self.compile(a:node.left)
if left ==# 'LvalueParser'
let class_def = 'LvalueParser(ExprParser)'
elseif left =~# '^\(VimLParser\|ExprTokenizer\|ExprParser\|LvalueParser\|StringReader\|Compiler\|RegexpParser\)$'
let class_def = left
elseif left =~# '^\(VimLParser\|ExprTokenizer\|ExprParser\|LvalueParser\|StringReader\|Compiler\|RegexpParser\)\.'
let left = matchstr(left, '\.\zs.*')
call self.out('%s %s %s', left, op, right)
return
else
call self.out('%s %s %s', left, op, right)
return
endif
if self.in_class
call self.decindent()
endif
call self.insert_empty_lines_before_comment(2)
call self.out('class %s:', class_def)
let self.in_class = 1
call self.incindent(' ')
else
let list = map(a:node.list, 'self.compile(v:val)')
if a:node.rest isnot s:NIL
let rest = self.compile(a:node.rest)
call add(list, '*' . rest)
endif
let left = join(list, ', ')
call self.out('%s %s %s', left, op, right)
endif
endfunction
function s:PythonCompiler.compile_unlet(node)
let list = map(a:node.list, 'self.compile(v:val)')
call self.out('del %s', join(list, ', '))
endfunction
function s:PythonCompiler.compile_lockvar(node)
throw 'NotImplemented: lockvar'
endfunction
function s:PythonCompiler.compile_unlockvar(node)
throw 'NotImplemented: unlockvar'
endfunction
function s:PythonCompiler.compile_if(node)
call self.out('if %s:', self.compile(a:node.cond))
call self.incindent(' ')
call self.compile_body(a:node.body)
call self.decindent()
for node in a:node.elseif
call self.out('elif %s:', self.compile(node.cond))
call self.incindent(' ')
call self.compile_body(node.body)
call self.decindent()
endfor
if a:node.else isnot s:NIL
call self.out('else:')
call self.incindent(' ')
call self.compile_body(a:node.else.body)
call self.decindent()
endif
endfunction
function s:PythonCompiler.compile_while(node)
call self.out('while %s:', self.compile(a:node.cond))
call self.incindent(' ')
call self.compile_body(a:node.body)
call self.decindent()
endfunction
function s:PythonCompiler.compile_for(node)
if a:node.left isnot s:NIL
let left = self.compile(a:node.left)
else
let list = map(a:node.list, 'self.compile(v:val)')
if a:node.rest isnot s:NIL
let rest = self.compile(a:node.rest)
call add(list, '*' . rest)
endif
let left = join(list, ', ')
endif
let right = self.compile(a:node.right)
call self.out('for %s in %s:', left, right)
call self.incindent(' ')
call self.compile_body(a:node.body)
call self.decindent()
endfunction
function s:PythonCompiler.compile_continue(node)
call self.out('continue')
endfunction
function s:PythonCompiler.compile_break(node)
call self.out('break')
endfunction
function s:PythonCompiler.compile_try(node)
call self.out('try:')
call self.incindent(' ')
call self.compile_body(a:node.body)
call self.decindent()
for node in a:node.catch
if node.pattern isnot s:NIL
call self.out('except:')
call self.incindent(' ')
call self.compile_body(node.body)
call self.decindent()
else
call self.out('except:')
call self.incindent(' ')
call self.compile_body(node.body)
call self.decindent()
endif
endfor
if a:node.finally isnot s:NIL
call self.out('finally:')
call self.incindent(' ')
call self.compile_body(a:node.finally.body)
call self.decindent()
endif
endfunction
function s:PythonCompiler.compile_throw(node)
call self.out('raise VimLParserException(%s)', self.compile(a:node.left))
endfunction
function s:PythonCompiler.compile_echo(node)
let list = map(a:node.list, 'self.compile(v:val)')
call self.out('print(%s)', join(list, ', '))
endfunction
function s:PythonCompiler.compile_echon(node)
let list = map(a:node.list, 'self.compile(v:val)')
call self.out('print(%s)', join(list, ', '))
endfunction
function s:PythonCompiler.compile_echohl(node)
throw 'NotImplemented: echohl'
endfunction
function s:PythonCompiler.compile_echomsg(node)
let list = map(a:node.list, 'self.compile(v:val)')
call self.out('print(%s)', join(list, ', '))
endfunction
function s:PythonCompiler.compile_echoerr(node)
let list = map(a:node.list, 'self.compile(v:val)')
call self.out('raise VimLParserException([%s]))', join(list, ', '))
endfunction
function s:PythonCompiler.compile_execute(node)
throw 'NotImplemented: execute'
endfunction
function s:PythonCompiler.compile_ternary(node)
let cond = self.compile(a:node.cond)
if s:opprec[a:node.type] >= s:opprec[a:node.cond.type]
let cond = '(' . cond . ')'
endif
let left = self.compile(a:node.left)
if s:opprec[a:node.type] >= s:opprec[a:node.left.type]
let left = '(' . left . ')'
endif
let right = self.compile(a:node.right)
return printf('%s if %s else %s', left, cond, right)
endfunction
function s:PythonCompiler.compile_or(node)
return self.compile_op2(a:node, 'or')
endfunction
function s:PythonCompiler.compile_and(node)
return self.compile_op2(a:node, 'and')
endfunction
function s:PythonCompiler.compile_equal(node)
return self.compile_op2(a:node, '==')
endfunction
function s:PythonCompiler.compile_equalci(node)
return printf('viml_equalci(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_equalcs(node)
return self.compile_op2(a:node, '==')
endfunction
function s:PythonCompiler.compile_nequal(node)
return self.compile_op2(a:node, '!=')
endfunction
function s:PythonCompiler.compile_nequalci(node)
return printf('not viml_equalci(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_nequalcs(node)
return self.compile_op2(a:node, '!=')
endfunction
function s:PythonCompiler.compile_greater(node)
return self.compile_op2(a:node, '>')
endfunction
function s:PythonCompiler.compile_greaterci(node)
throw 'NotImplemented: >?'
endfunction
function s:PythonCompiler.compile_greatercs(node)
throw 'NotImplemented: >#'
endfunction
function s:PythonCompiler.compile_gequal(node)
return self.compile_op2(a:node, '>=')
endfunction
function s:PythonCompiler.compile_gequalci(node)
throw 'NotImplemented: >=?'
endfunction
function s:PythonCompiler.compile_gequalcs(node)
throw 'NotImplemented: >=#'
endfunction
function s:PythonCompiler.compile_smaller(node)
return self.compile_op2(a:node, '<')
endfunction
function s:PythonCompiler.compile_smallerci(node)
throw 'NotImplemented: <?'
endfunction
function s:PythonCompiler.compile_smallercs(node)
throw 'NotImplemented: <#'
endfunction
function s:PythonCompiler.compile_sequal(node)
return self.compile_op2(a:node, '<=')
endfunction
function s:PythonCompiler.compile_sequalci(node)
throw 'NotImplemented: <=?'
endfunction
function s:PythonCompiler.compile_sequalcs(node)
throw 'NotImplemented: <=#'
endfunction
function s:PythonCompiler.compile_match(node)
return printf('viml_eqreg(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_matchci(node)
return printf('viml_eqregq(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_matchcs(node)
return printf('viml_eqregh(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_nomatch(node)
return printf('not viml_eqreg(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_nomatchci(node)
return printf('not viml_eqregq(%s, %s, flags=re.IGNORECASE)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_nomatchcs(node)
return printf('not viml_eqregh(%s, %s)', self.compile(a:node.left), self.compile(a:node.right))
endfunction
function s:PythonCompiler.compile_is(node)
return self.compile_op2(a:node, 'is')
endfunction
function s:PythonCompiler.compile_isci(node)
throw 'NotImplemented: is?'
endfunction
function s:PythonCompiler.compile_iscs(node)
return self.compile_op2(a:node, 'is')
endfunction
function s:PythonCompiler.compile_isnot(node)
return self.compile_op2(a:node, 'is not')
endfunction
function s:PythonCompiler.compile_isnotci(node)
throw 'NotImplemented: isnot?'
endfunction
function s:PythonCompiler.compile_isnotcs(node)
return self.compile_op2(a:node, 'is not')
endfunction
function s:PythonCompiler.compile_add(node)
return self.compile_op2(a:node, '+')
endfunction
function s:PythonCompiler.compile_subtract(node)
return self.compile_op2(a:node, '-')
endfunction
function s:PythonCompiler.compile_concat(node)
return self.compile_op2(a:node, '+')
endfunction
function s:PythonCompiler.compile_multiply(node)
return self.compile_op2(a:node, '*')
endfunction
function s:PythonCompiler.compile_divide(node)
return self.compile_op2(a:node, '/')
endfunction
function s:PythonCompiler.compile_remainder(node)
return self.compile_op2(a:node, '%')
endfunction
function s:PythonCompiler.compile_not(node)
return self.compile_op1(a:node, 'not ')
endfunction
function s:PythonCompiler.compile_plus(node)
return self.compile_op1(a:node, '+')
endfunction
function s:PythonCompiler.compile_minus(node)
return self.compile_op1(a:node, '-')
endfunction
function s:PythonCompiler.compile_subscript(node)
let left = self.compile(a:node.left)
let right = self.compile(a:node.right)
if left ==# 'self'
return printf('getattr(%s, %s)', left, right)
else
return printf('%s[%s]', left, right)
endif
endfunction
function s:PythonCompiler.compile_slice(node)
throw 'NotImplemented: slice'
endfunction
function s:PythonCompiler.compile_dot(node)
let left = self.compile(a:node.left)
let right = self.compile(a:node.right)
return printf('%s.%s', left, right)
endfunction
function s:PythonCompiler.compile_call(node)
let rlist = map(a:node.rlist, 'self.compile(v:val)')
let left = self.compile(a:node.left)
if left ==# 'map'
let r = s:StringReader.new([eval(rlist[1])])
let p = s:ExprParser.new(r)
let n = p.parse()
return printf('[%s for vval in %s]', self.compile(n), rlist[0])
elseif left ==# 'call' && rlist[0][0] =~# '[''"]'
return printf('viml_%s(*%s)', rlist[0][1:-2], rlist[1])
endif
if left =~# '\.new$'
let left = matchstr(left, '.*\ze\.new$')
endif
if index(s:viml_builtin_functions, left) != -1
let left = printf('viml_%s', left)
endif
return printf('%s(%s)', left, join(rlist, ', '))
endfunction
function s:PythonCompiler.compile_number(node)
return a:node.value
endfunction
function s:PythonCompiler.compile_string(node)
if a:node.value[0] ==# "'"
let s = substitute(a:node.value[1:-2], "''", "'", 'g')
return '"' . escape(s, '\"') . '"'
else
return a:node.value
endif
endfunction
function s:PythonCompiler.compile_list(node)
let value = map(a:node.value, 'self.compile(v:val)')
if empty(value)
return '[]'
else
return printf('[%s]', join(value, ', '))
endif
endfunction
function s:PythonCompiler.compile_dict(node)
let value = map(a:node.value, 'self.compile(v:val[0]) . ": " . self.compile(v:val[1])')
if empty(value)
return 'AttributeDict({})'
else
return printf('AttributeDict({%s})', join(value, ', '))
endif
endfunction
function s:PythonCompiler.compile_option(node)
throw 'NotImplemented: option'
endfunction
function s:PythonCompiler.compile_identifier(node)
let name = a:node.value
if name ==# 'a:000'
let name = 'a000'
elseif name ==# 'v:val'
let name = 'vval'
elseif name =~# '^[sa]:'
let name = name[2:]
endif
if has_key(s:reserved_keywords, name)
let name .= '_'
endif
return name
endfunction
function s:PythonCompiler.compile_curlyname(node)
throw 'NotImplemented: curlyname'
endfunction
function s:PythonCompiler.compile_env(node)
throw 'NotImplemented: env'
endfunction
function s:PythonCompiler.compile_reg(node)
throw 'NotImplemented: reg'
endfunction
function s:PythonCompiler.compile_op1(node, op)
let left = self.compile(a:node.left)
if s:opprec[a:node.type] > s:opprec[a:node.left.type]
let left = '(' . left . ')'
endif
return printf('%s%s', a:op, left)
endfunction
function s:PythonCompiler.compile_op2(node, op)
let left = self.compile(a:node.left)
if s:opprec[a:node.type] > s:opprec[a:node.left.type]
let left = '(' . left . ')'
endif
let right = self.compile(a:node.right)
if s:opprec[a:node.type] > s:opprec[a:node.right.type]
let right = '(' . right . ')'
endif
return printf('%s %s %s', left, a:op, right)
endfunction
let s:viml_builtin_functions = map(copy(s:VimLParser.builtin_functions), 'v:val.name')
let s:script_dir = expand('<sfile>:h')
function! s:convert(in, out)
let vimlfunc = fnamemodify(s:script_dir . '/vimlfunc.py', ':p')
let head = readfile(vimlfunc) + ['', '']
try
let r = s:StringReader.new(readfile(a:in))
let p = s:VimLParser.new()
let c = s:PythonCompiler.new()
let lines = c.compile(p.parse(r))
unlet lines[0 : index(lines, 'NIL = []') - 1]
let tail = [
\ '',
\ '',
\ 'if __name__ == ''__main__'':',
\ ' main()',
\ ]
call writefile(head + lines + tail, a:out)
catch
echoerr substitute(v:throwpoint, '\.\.\zs\d\+', '\=s:numtoname(submatch(0))', 'g') . "\n" . v:exception
endtry
endfunction
function! s:numtoname(num)
let sig = printf("function('%s')", a:num)
for k in keys(s:)
if type(s:[k]) ==# type({})
for name in keys(s:[k])
if type(s:[k][name]) ==# type(function('tr')) && string(s:[k][name]) ==# sig
return printf('%s.%s', k, name)
endif
endfor
endif
endfor
return a:num
endfunction
function! s:parse_args() abort
let v = [
\ fnamemodify(s:script_dir . '/../autoload/vimlparser.vim', ':p'),
\ fnamemodify(s:script_dir . '/vimlparser.py', ':p')
\]
let args = argv()[1:]
%argdel
if len(args) != 0
if len(args) != 2
throw 'invalid argument: ' . string(args)
endif
let v = args
endif
return v
endfunction
function! s:main() abort
try
let args = s:parse_args()
call s:convert(args[0], args[1])
catch
call writefile([v:exception], has('win32') ? 'conout$' : '/dev/stderr')
cquit
endtry
endfunction
call s:main()