This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
compiler.rb
1758 lines (1567 loc) · 49.5 KB
/
compiler.rb
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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
module Prism
class ParserCompiler < Compiler
attr_reader :parser, :builder, :source_buffer, :offset_cache
attr_reader :locals, :in_destructure, :in_pattern
def initialize(parser, offset_cache, locals: nil, in_destructure: false, in_pattern: false)
@parser = parser
@builder = parser.builder
@source_buffer = parser.source_buffer
@offset_cache = offset_cache
@locals = locals
@in_destructure = in_destructure
@in_pattern = in_pattern
end
# alias foo bar
# ^^^^^^^^^^^^^
def visit_alias_method_node(node)
builder.alias(token(node.keyword_loc), visit(node.new_name), visit(node.old_name))
end
# alias $foo $bar
# ^^^^^^^^^^^^^^^
def visit_alias_global_variable_node(node)
builder.alias(token(node.keyword_loc), visit(node.new_name), visit(node.old_name))
end
# foo => bar | baz
# ^^^^^^^^^
def visit_alternation_pattern_node(node)
builder.match_alt(visit(node.left), token(node.operator_loc), visit(node.right))
end
# a and b
# ^^^^^^^
def visit_and_node(node)
builder.logical_op(:and, visit(node.left), token(node.operator_loc), visit(node.right))
end
# []
# ^^
def visit_array_node(node)
builder.array(token(node.opening_loc), visit_all(node.elements), token(node.closing_loc))
end
# foo => [bar]
# ^^^^^
def visit_array_pattern_node(node)
if node.constant
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.array_pattern(nil, visit_all([*node.requireds, *node.rest, *node.posts]), nil), token(node.closing_loc))
else
builder.array_pattern(token(node.opening_loc), visit_all([*node.requireds, *node.rest, *node.posts]), token(node.closing_loc))
end
end
# foo(bar)
# ^^^
def visit_arguments_node(node)
visit_all(node.arguments)
end
# { a: 1 }
# ^^^^
def visit_assoc_node(node)
if node.value.is_a?(ImplicitNode)
builder.pair_label([node.key.slice.chomp(":"), srange(node.key.location)])
elsif in_pattern && node.value.nil?
if node.key.is_a?(SymbolNode)
builder.match_hash_var([node.key.unescaped, srange(node.key.location)])
else
builder.match_hash_var_from_str(token(node.key.opening_loc), visit_all(node.key.parts), token(node.key.closing_loc))
end
elsif node.operator_loc
builder.pair(visit(node.key), token(node.operator_loc), visit(node.value))
elsif node.key.is_a?(SymbolNode) && node.key.opening_loc.nil?
builder.pair_keyword([node.key.unescaped, srange(node.key.location)], visit(node.value))
else
parts =
if node.key.is_a?(SymbolNode)
[builder.string_internal([node.key.unescaped, srange(node.key.value_loc)])]
else
visit_all(node.key.parts)
end
builder.pair_quoted(token(node.key.opening_loc), parts, token(node.key.closing_loc), visit(node.value))
end
end
# def foo(**); bar(**); end
# ^^
#
# { **foo }
# ^^^^^
def visit_assoc_splat_node(node)
if node.value.nil? && locals.include?(:**)
builder.forwarded_kwrestarg(token(node.operator_loc))
else
builder.kwsplat(token(node.operator_loc), visit(node.value))
end
end
# $+
# ^^
def visit_back_reference_read_node(node)
builder.back_ref(token(node.location))
end
# begin end
# ^^^^^^^^^
def visit_begin_node(node)
rescue_bodies = []
if (rescue_clause = node.rescue_clause)
begin
find_start_offset = (rescue_clause.reference&.location || rescue_clause.exceptions.last&.location || rescue_clause.keyword_loc).end_offset
find_end_offset = (rescue_clause.statements&.location&.start_offset || rescue_clause.consequent&.location&.start_offset || (find_start_offset + 1))
rescue_bodies << builder.rescue_body(
token(rescue_clause.keyword_loc),
rescue_clause.exceptions.any? ? builder.array(nil, visit_all(rescue_clause.exceptions), nil) : nil,
token(rescue_clause.operator_loc),
visit(rescue_clause.reference),
srange_find(find_start_offset, find_end_offset, [";"]),
visit(rescue_clause.statements)
)
end until (rescue_clause = rescue_clause.consequent).nil?
end
begin_body =
builder.begin_body(
visit(node.statements),
rescue_bodies,
token(node.else_clause&.else_keyword_loc),
visit(node.else_clause),
token(node.ensure_clause&.ensure_keyword_loc),
visit(node.ensure_clause&.statements)
)
if node.begin_keyword_loc
builder.begin_keyword(token(node.begin_keyword_loc), begin_body, token(node.end_keyword_loc))
else
begin_body
end
end
# foo(&bar)
# ^^^^
def visit_block_argument_node(node)
builder.block_pass(token(node.operator_loc), visit(node.expression))
end
# foo { |; bar| }
# ^^^
def visit_block_local_variable_node(node)
builder.shadowarg(token(node.location))
end
# A block on a keyword or method call.
def visit_block_node(node)
raise "Cannot directly compile block nodes"
end
# def foo(&bar); end
# ^^^^
def visit_block_parameter_node(node)
builder.blockarg(token(node.operator_loc), token(node.name_loc))
end
# A block's parameters.
def visit_block_parameters_node(node)
[*visit(node.parameters)].concat(visit_all(node.locals))
end
# break
# ^^^^^
#
# break foo
# ^^^^^^^^^
def visit_break_node(node)
builder.keyword_cmd(:break, token(node.keyword_loc), nil, visit(node.arguments) || [], nil)
end
# foo
# ^^^
#
# foo.bar
# ^^^^^^^
#
# foo.bar() {}
# ^^^^^^^^^^^^
def visit_call_node(node)
name = node.name
arguments = node.arguments&.arguments || []
block = node.block
if block.is_a?(BlockArgumentNode)
arguments = [*arguments, block]
block = nil
end
visit_block(
if name == :!
builder.not_op(
token(node.message_loc),
token(node.opening_loc),
visit(node.receiver),
token(node.closing_loc)
)
elsif name == :[]
builder.index(
visit(node.receiver),
token(node.opening_loc),
visit_all(arguments),
token(node.closing_loc)
)
elsif name == :[]= && node.message != "[]=" && node.arguments && block.nil?
builder.assign(
builder.index_asgn(
visit(node.receiver),
token(node.opening_loc),
visit_all(node.arguments.arguments[...-1]),
token(node.closing_loc),
),
srange_find(node.message_loc.end_offset, node.arguments.arguments.last.location.start_offset, ["="]),
visit(node.arguments.arguments.last)
)
else
message_loc = node.message_loc
call_operator_loc = node.call_operator_loc
call_operator = [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)] if call_operator_loc
if name.end_with?("=") && !message_loc.slice.end_with?("=") && node.arguments && block.nil?
builder.assign(
builder.attr_asgn(visit(node.receiver), call_operator, token(message_loc)),
srange_find(message_loc.end_offset, node.arguments.location.start_offset, ["="]),
visit(node.arguments.arguments.last)
)
else
builder.call_method(
visit(node.receiver),
call_operator,
message_loc ? [node.name, srange(message_loc)] : nil,
token(node.opening_loc),
visit_all(arguments),
token(node.closing_loc)
)
end
end,
block
)
end
# foo.bar += baz
# ^^^^^^^^^^^^^^^
def visit_call_operator_write_node(node)
call_operator_loc = node.call_operator_loc
builder.op_assign(
builder.call_method(
visit(node.receiver),
call_operator_loc.nil? ? nil : [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)],
node.message_loc ? [node.read_name, srange(node.message_loc)] : nil,
nil,
[],
nil
),
[node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
visit(node.value)
)
end
# foo.bar &&= baz
# ^^^^^^^^^^^^^^^
alias visit_call_and_write_node visit_call_operator_write_node
# foo.bar ||= baz
# ^^^^^^^^^^^^^^^
alias visit_call_or_write_node visit_call_operator_write_node
# foo.bar, = 1
# ^^^^^^^
def visit_call_target_node(node)
call_operator_loc = node.call_operator_loc
builder.attr_asgn(
visit(node.receiver),
call_operator_loc.nil? ? nil : [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)],
token(node.message_loc)
)
end
# foo => bar => baz
# ^^^^^^^^^^
def visit_capture_pattern_node(node)
builder.match_as(visit(node.value), token(node.operator_loc), visit(node.target))
end
# case foo; when bar; end
# ^^^^^^^^^^^^^^^^^^^^^^^
def visit_case_node(node)
builder.case(
token(node.case_keyword_loc),
visit(node.predicate),
visit_all(node.conditions),
token(node.consequent&.else_keyword_loc),
visit(node.consequent),
token(node.end_keyword_loc)
)
end
# case foo; in bar; end
# ^^^^^^^^^^^^^^^^^^^^^
def visit_case_match_node(node)
builder.case_match(
token(node.case_keyword_loc),
visit(node.predicate),
visit_all(node.conditions),
token(node.consequent&.else_keyword_loc),
visit(node.consequent),
token(node.end_keyword_loc)
)
end
# class Foo; end
# ^^^^^^^^^^^^^^
def visit_class_node(node)
builder.def_class(
token(node.class_keyword_loc),
visit(node.constant_path),
token(node.inheritance_operator_loc),
visit(node.superclass),
node.body&.accept(copy_compiler(locals: node.locals)),
token(node.end_keyword_loc)
)
end
# @@foo
# ^^^^^
def visit_class_variable_read_node(node)
builder.cvar(token(node.location))
end
# @@foo = 1
# ^^^^^^^^^
#
# @@foo, @@bar = 1
# ^^^^^ ^^^^^
def visit_class_variable_write_node(node)
builder.assign(
builder.assignable(builder.cvar(token(node.name_loc))),
token(node.operator_loc),
visit(node.value)
)
end
# @@foo += bar
# ^^^^^^^^^^^^
def visit_class_variable_operator_write_node(node)
builder.op_assign(
builder.assignable(builder.cvar(token(node.name_loc))),
[node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
visit(node.value)
)
end
# @@foo &&= bar
# ^^^^^^^^^^^^^
alias visit_class_variable_and_write_node visit_class_variable_operator_write_node
# @@foo ||= bar
# ^^^^^^^^^^^^^
alias visit_class_variable_or_write_node visit_class_variable_operator_write_node
# @@foo, = bar
# ^^^^^
def visit_class_variable_target_node(node)
builder.assignable(builder.cvar(token(node.location)))
end
# Foo
# ^^^
def visit_constant_read_node(node)
builder.const([node.name, srange(node.location)])
end
# Foo = 1
# ^^^^^^^
#
# Foo, Bar = 1
# ^^^ ^^^
def visit_constant_write_node(node)
builder.assign(builder.assignable(builder.const([node.name, srange(node.name_loc)])), token(node.operator_loc), visit(node.value))
end
# Foo += bar
# ^^^^^^^^^^^
def visit_constant_operator_write_node(node)
builder.op_assign(
builder.assignable(builder.const([node.name, srange(node.name_loc)])),
[node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
visit(node.value)
)
end
# Foo &&= bar
# ^^^^^^^^^^^^
alias visit_constant_and_write_node visit_constant_operator_write_node
# Foo ||= bar
# ^^^^^^^^^^^^
alias visit_constant_or_write_node visit_constant_operator_write_node
# Foo, = bar
# ^^^
def visit_constant_target_node(node)
builder.assignable(builder.const([node.name, srange(node.location)]))
end
# Foo::Bar
# ^^^^^^^^
def visit_constant_path_node(node)
if node.parent.nil?
builder.const_global(
token(node.delimiter_loc),
[node.child.name, srange(node.child.location)]
)
else
builder.const_fetch(
visit(node.parent),
token(node.delimiter_loc),
[node.child.name, srange(node.child.location)]
)
end
end
# Foo::Bar = 1
# ^^^^^^^^^^^^
#
# Foo::Foo, Bar::Bar = 1
# ^^^^^^^^ ^^^^^^^^
def visit_constant_path_write_node(node)
builder.assign(
builder.assignable(visit(node.target)),
token(node.operator_loc),
visit(node.value)
)
end
# Foo::Bar += baz
# ^^^^^^^^^^^^^^^
def visit_constant_path_operator_write_node(node)
builder.op_assign(
builder.assignable(visit(node.target)),
[node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
visit(node.value)
)
end
# Foo::Bar &&= baz
# ^^^^^^^^^^^^^^^^
alias visit_constant_path_and_write_node visit_constant_path_operator_write_node
# Foo::Bar ||= baz
# ^^^^^^^^^^^^^^^^
alias visit_constant_path_or_write_node visit_constant_path_operator_write_node
# Foo::Bar, = baz
# ^^^^^^^^
def visit_constant_path_target_node(node)
builder.assignable(visit_constant_path_node(node))
end
# def foo; end
# ^^^^^^^^^^^^
#
# def self.foo; end
# ^^^^^^^^^^^^^^^^^
def visit_def_node(node)
if node.equal_loc
if node.receiver
builder.def_endless_singleton(
token(node.def_keyword_loc),
visit(node.receiver.is_a?(ParenthesesNode) ? node.receiver.body : node.receiver),
token(node.operator_loc),
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
token(node.equal_loc),
node.body&.accept(copy_compiler(locals: node.locals))
)
else
builder.def_endless_method(
token(node.def_keyword_loc),
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
token(node.equal_loc),
node.body&.accept(copy_compiler(locals: node.locals))
)
end
elsif node.receiver
builder.def_singleton(
token(node.def_keyword_loc),
visit(node.receiver.is_a?(ParenthesesNode) ? node.receiver.body : node.receiver),
token(node.operator_loc),
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
node.body&.accept(copy_compiler(locals: node.locals)),
token(node.end_keyword_loc)
)
else
builder.def_method(
token(node.def_keyword_loc),
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
node.body&.accept(copy_compiler(locals: node.locals)),
token(node.end_keyword_loc)
)
end
end
# defined? a
# ^^^^^^^^^^
#
# defined?(a)
# ^^^^^^^^^^^
def visit_defined_node(node)
builder.keyword_cmd(
:defined?,
token(node.keyword_loc),
token(node.lparen_loc),
[visit(node.value)],
token(node.rparen_loc)
)
end
# if foo then bar else baz end
# ^^^^^^^^^^^^
def visit_else_node(node)
visit(node.statements)
end
# "foo #{bar}"
# ^^^^^^
def visit_embedded_statements_node(node)
builder.begin(
token(node.opening_loc),
visit(node.statements),
token(node.closing_loc)
)
end
# "foo #@bar"
# ^^^^^
def visit_embedded_variable_node(node)
visit(node.variable)
end
# begin; foo; ensure; bar; end
# ^^^^^^^^^^^^
def visit_ensure_node(node)
raise "Cannot directly compile ensure nodes"
end
# false
# ^^^^^
def visit_false_node(node)
builder.false(token(node.location))
end
# foo => [*, bar, *]
# ^^^^^^^^^^^
def visit_find_pattern_node(node)
if node.constant
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.find_pattern(nil, visit_all([node.left, *node.requireds, node.right]), nil), token(node.closing_loc))
else
builder.find_pattern(token(node.opening_loc), visit_all([node.left, *node.requireds, node.right]), token(node.closing_loc))
end
end
# 1.0
# ^^^
def visit_float_node(node)
visit_numeric(node, builder.float([node.value, srange(node.location)]))
end
# for foo in bar do end
# ^^^^^^^^^^^^^^^^^^^^^
def visit_for_node(node)
builder.for(
token(node.for_keyword_loc),
visit(node.index),
token(node.in_keyword_loc),
visit(node.collection),
if node.do_keyword_loc
token(node.do_keyword_loc)
else
srange_find(node.collection.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset, [";"])
end,
visit(node.statements),
token(node.end_keyword_loc)
)
end
# def foo(...); bar(...); end
# ^^^
def visit_forwarding_arguments_node(node)
builder.forwarded_args(token(node.location))
end
# def foo(...); end
# ^^^
def visit_forwarding_parameter_node(node)
builder.forward_arg(token(node.location))
end
# super
# ^^^^^
#
# super {}
# ^^^^^^^^
def visit_forwarding_super_node(node)
visit_block(
builder.keyword_cmd(
:zsuper,
["super", srange_offsets(node.location.start_offset, node.location.start_offset + 5)]
),
node.block
)
end
# $foo
# ^^^^
def visit_global_variable_read_node(node)
builder.gvar(token(node.location))
end
# $foo = 1
# ^^^^^^^^
#
# $foo, $bar = 1
# ^^^^ ^^^^
def visit_global_variable_write_node(node)
builder.assign(
builder.assignable(builder.gvar(token(node.name_loc))),
token(node.operator_loc),
visit(node.value)
)
end
# $foo += bar
# ^^^^^^^^^^^
def visit_global_variable_operator_write_node(node)
builder.op_assign(
builder.assignable(builder.gvar(token(node.name_loc))),
[node.operator, srange(node.operator_loc)],
visit(node.value)
)
end
# $foo &&= bar
# ^^^^^^^^^^^^
alias visit_global_variable_and_write_node visit_global_variable_operator_write_node
# $foo ||= bar
# ^^^^^^^^^^^^
alias visit_global_variable_or_write_node visit_global_variable_operator_write_node
# $foo, = bar
# ^^^^
def visit_global_variable_target_node(node)
builder.assignable(builder.gvar([node.slice, srange(node.location)]))
end
# {}
# ^^
def visit_hash_node(node)
builder.associate(
token(node.opening_loc),
visit_all(node.elements),
token(node.closing_loc)
)
end
# foo => {}
# ^^
def visit_hash_pattern_node(node)
if node.constant
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.hash_pattern(nil, visit_all(node.elements), nil), token(node.closing_loc))
else
builder.hash_pattern(token(node.opening_loc), visit_all(node.elements), token(node.closing_loc))
end
end
# if foo then bar end
# ^^^^^^^^^^^^^^^^^^^
#
# bar if foo
# ^^^^^^^^^^
#
# foo ? bar : baz
# ^^^^^^^^^^^^^^^
def visit_if_node(node)
if !node.if_keyword_loc
builder.ternary(
visit(node.predicate),
token(node.then_keyword_loc),
visit(node.statements),
token(node.consequent.else_keyword_loc),
visit(node.consequent)
)
elsif node.if_keyword_loc.start_offset == node.location.start_offset
builder.condition(
token(node.if_keyword_loc),
visit(node.predicate),
if node.then_keyword_loc
token(node.then_keyword_loc)
else
srange_find(node.predicate.location.end_offset, (node.statements&.location || node.consequent&.location || node.end_keyword_loc).start_offset, [";"])
end,
visit(node.statements),
case node.consequent
when IfNode
token(node.consequent.if_keyword_loc)
when ElseNode
token(node.consequent.else_keyword_loc)
end,
visit(node.consequent),
if node.if_keyword != "elsif"
token(node.end_keyword_loc)
end
)
else
builder.condition_mod(
visit(node.statements),
visit(node.consequent),
token(node.if_keyword_loc),
visit(node.predicate)
)
end
end
# 1i
def visit_imaginary_node(node)
visit_numeric(node, builder.complex([node.value, srange(node.location)]))
end
# { foo: }
# ^^^^
def visit_implicit_node(node)
raise "Cannot directly compile implicit nodes"
end
# foo { |bar,| }
# ^
def visit_implicit_rest_node(node)
raise "Cannot directly compile implicit rest nodes"
end
# case foo; in bar; end
# ^^^^^^^^^^^^^^^^^^^^^
def visit_in_node(node)
pattern = nil
guard = nil
case node.pattern
when IfNode
pattern = within_pattern { |compiler| node.pattern.statements.accept(compiler) }
guard = builder.if_guard(token(node.pattern.if_keyword_loc), visit(node.pattern.predicate))
when UnlessNode
pattern = within_pattern { |compiler| node.pattern.statements.accept(compiler) }
guard = builder.unless_guard(token(node.pattern.keyword_loc), visit(node.pattern.predicate))
else
pattern = within_pattern { |compiler| node.pattern.accept(compiler) }
end
builder.in_pattern(
token(node.in_loc),
pattern,
guard,
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset || node.location.end_offset, [";", "then"]),
visit(node.statements)
)
end
# foo[bar] += baz
# ^^^^^^^^^^^^^^^
def visit_index_operator_write_node(node)
arguments = node.arguments&.arguments || []
arguments << node.block if node.block
builder.op_assign(
builder.index(
visit(node.receiver),
token(node.opening_loc),
visit_all(arguments),
token(node.closing_loc)
),
[node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
visit(node.value)
)
end
# foo[bar] &&= baz
# ^^^^^^^^^^^^^^^^
alias visit_index_and_write_node visit_index_operator_write_node
# foo[bar] ||= baz
# ^^^^^^^^^^^^^^^^
alias visit_index_or_write_node visit_index_operator_write_node
# foo[bar], = 1
# ^^^^^^^^
def visit_index_target_node(node)
builder.index_asgn(
visit(node.receiver),
token(node.opening_loc),
visit_all(node.arguments.arguments),
token(node.closing_loc),
)
end
# @foo
# ^^^^
def visit_instance_variable_read_node(node)
builder.ivar(token(node.location))
end
# @foo = 1
# ^^^^^^^^
#
# @foo, @bar = 1
# ^^^^ ^^^^
def visit_instance_variable_write_node(node)
builder.assign(
builder.assignable(builder.ivar(token(node.name_loc))),
token(node.operator_loc),
visit(node.value)
)
end
# @foo += bar
# ^^^^^^^^^^^
def visit_instance_variable_operator_write_node(node)
builder.op_assign(
builder.assignable(builder.ivar(token(node.name_loc))),
[node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
visit(node.value)
)
end
# @foo &&= bar
# ^^^^^^^^^^^^
alias visit_instance_variable_and_write_node visit_instance_variable_operator_write_node
# @foo ||= bar
# ^^^^^^^^^^^^
alias visit_instance_variable_or_write_node visit_instance_variable_operator_write_node
# @foo, = bar
# ^^^^
def visit_instance_variable_target_node(node)
builder.assignable(builder.ivar(token(node.location)))
end
# 1
# ^
def visit_integer_node(node)
visit_numeric(node, builder.integer([node.value, srange(node.location)]))
end
# /foo #{bar}/
# ^^^^^^^^^^^^
def visit_interpolated_regular_expression_node(node)
builder.regexp_compose(
token(node.opening_loc),
visit_all(node.parts),
[node.closing[0], srange_offsets(node.closing_loc.start_offset, node.closing_loc.start_offset + 1)],
builder.regexp_options([node.closing[1..], srange_offsets(node.closing_loc.start_offset + 1, node.closing_loc.end_offset)])
)
end
# if /foo #{bar}/ then end
# ^^^^^^^^^^^^
alias visit_interpolated_match_last_line_node visit_interpolated_regular_expression_node
# "foo #{bar}"
# ^^^^^^^^^^^^
def visit_interpolated_string_node(node)
if node.opening&.start_with?("<<")
children, closing = visit_heredoc(node)
builder.string_compose(token(node.opening_loc), children, closing)
else
builder.string_compose(
token(node.opening_loc),
visit_all(node.parts),
token(node.closing_loc)
)
end
end
# :"foo #{bar}"
# ^^^^^^^^^^^^^
def visit_interpolated_symbol_node(node)
builder.symbol_compose(
token(node.opening_loc),
visit_all(node.parts),
token(node.closing_loc)
)
end
# `foo #{bar}`
# ^^^^^^^^^^^^
def visit_interpolated_x_string_node(node)
if node.opening.start_with?("<<")
children, closing = visit_heredoc(node)
builder.xstring_compose(token(node.opening_loc), children, closing)
else
builder.xstring_compose(
token(node.opening_loc),
visit_all(node.parts),
token(node.closing_loc)
)
end
end
# foo(bar: baz)
# ^^^^^^^^
def visit_keyword_hash_node(node)
builder.associate(nil, visit_all(node.elements), nil)
end
# def foo(**bar); end
# ^^^^^
#
# def foo(**); end
# ^^
def visit_keyword_rest_parameter_node(node)
builder.kwrestarg(
token(node.operator_loc),
node.name ? [node.name, srange(node.name_loc)] : nil
)
end
# -> {}
def visit_lambda_node(node)
builder.block(
builder.call_lambda(token(node.operator_loc)),
[node.opening, srange(node.opening_loc)],
if node.parameters
if node.parameters.is_a?(NumberedParametersNode)
visit(node.parameters)
else
builder.args(
token(node.parameters.opening_loc),
visit(node.parameters),
token(node.parameters.closing_loc),
false
)
end
else
builder.args(nil, [], nil, false)
end,
node.body&.accept(copy_compiler(locals: node.locals)),
[node.closing, srange(node.closing_loc)]
)
end
# foo
# ^^^
def visit_local_variable_read_node(node)
builder.ident([node.name, srange(node.location)]).updated(:lvar)
end
# foo = 1
# ^^^^^^^
#
# foo, bar = 1
# ^^^ ^^^
def visit_local_variable_write_node(node)
builder.assign(
builder.assignable(builder.ident(token(node.name_loc))),
token(node.operator_loc),
visit(node.value)
)
end
# foo += bar
# ^^^^^^^^^^
def visit_local_variable_operator_write_node(node)
builder.op_assign(
builder.assignable(builder.ident(token(node.name_loc))),
[node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
visit(node.value)
)
end
# foo &&= bar
# ^^^^^^^^^^^