Class: SyntaxTree::YARV::Compiler

Inherits:
BasicVisitor show all
Defined in:
lib/syntax_tree/yarv/compiler.rb

Overview

This class is an experiment in transforming Syntax Tree nodes into their corresponding YARV instruction sequences. It attempts to mirror the behavior of RubyVM::InstructionSequence.compile.

You use this as with any other visitor. First you parse code into a tree, then you visit it with this compiler. Visiting the root node of the tree will return a SyntaxTree::Visitor::Compiler::InstructionSequence object. With that object you can call #to_a on it, which will return a serialized form of the instruction sequence as an array. This array should mirror the array given by RubyVM::InstructionSequence#to_a.

As an example, here is how you would compile a single expression:

program = SyntaxTree.parse("1 + 2")
program.accept(SyntaxTree::YARV::Compiler.new).to_a

[
  "YARVInstructionSequence/SimpleDataFormat",
  3,
  1,
  1,
  {:arg_size=>0, :local_size=>0, :stack_max=>2},
  "<compiled>",
  "<compiled>",
  "<compiled>",
  1,
  :top,
  [],
  {},
  [],
  [
    [:putobject_INT2FIX_1_],
    [:putobject, 2],
    [:opt_plus, {:mid=>:+, :flag=>16, :orig_argc=>1}],
    [:leave]
  ]
]

Note that this is the same output as calling:

RubyVM::InstructionSequence.compile("1 + 2").to_a

Defined Under Namespace

Classes: Options, RubyVisitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicVisitor

#visit, #visit_all, #visit_child_nodes, visit_method, visit_methods

Constructor Details

#initialize(options) ⇒ Compiler



288
289
290
291
292
# File 'lib/syntax_tree/yarv/compiler.rb', line 288

def initialize(options)
  @options = options
  @iseq = nil
  @last_statement = false
end

Instance Attribute Details

#iseqObject (readonly)

The current instruction sequence that is being compiled.



281
282
283
# File 'lib/syntax_tree/yarv/compiler.rb', line 281

def iseq
  @iseq
end

#last_statementObject (readonly)

A boolean to track if we’re currently compiling the last statement within a set of statements. This information is necessary to determine if we need to return the value of the last statement.



286
287
288
# File 'lib/syntax_tree/yarv/compiler.rb', line 286

def last_statement
  @last_statement
end

#optionsObject (readonly)

These options mirror the compilation options that we currently support that can be also passed to RubyVM::InstructionSequence.compile.



278
279
280
# File 'lib/syntax_tree/yarv/compiler.rb', line 278

def options
  @options
end

Instance Method Details

#visit_alias(node) ⇒ Object



334
335
336
337
338
339
340
# File 'lib/syntax_tree/yarv/compiler.rb', line 334

def visit_alias(node)
  iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
  iseq.putspecialobject(PutSpecialObject::OBJECT_CBASE)
  visit(node.left)
  visit(node.right)
  iseq.send(YARV.calldata(:"core#set_method_alias", 3))
end

#visit_aref(node) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/syntax_tree/yarv/compiler.rb', line 342

def visit_aref(node)
  calldata = YARV.calldata(:[], 1)
  visit(node.collection)

  if !options.frozen_string_literal? &&
       options.specialized_instruction? && (node.index.parts.length == 1)
    arg = node.index.parts.first

    if arg.is_a?(StringLiteral) && (arg.parts.length == 1)
      string_part = arg.parts.first

      if string_part.is_a?(TStringContent)
        iseq.opt_aref_with(string_part.value, calldata)
        return
      end
    end
  end

  visit(node.index)
  iseq.send(calldata)
end

#visit_arg_block(node) ⇒ Object



364
365
366
# File 'lib/syntax_tree/yarv/compiler.rb', line 364

def visit_arg_block(node)
  visit(node.value)
end

#visit_arg_paren(node) ⇒ Object



368
369
370
# File 'lib/syntax_tree/yarv/compiler.rb', line 368

def visit_arg_paren(node)
  visit(node.arguments)
end

#visit_arg_star(node) ⇒ Object



372
373
374
375
# File 'lib/syntax_tree/yarv/compiler.rb', line 372

def visit_arg_star(node)
  visit(node.value)
  iseq.splatarray(false)
end

#visit_args(node) ⇒ Object



377
378
379
# File 'lib/syntax_tree/yarv/compiler.rb', line 377

def visit_args(node)
  visit_all(node.parts)
end

#visit_array(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 381

def visit_array(node)
  if (compiled = RubyVisitor.compile(node))
    iseq.duparray(compiled)
  elsif node.contents && node.contents.parts.length == 1 &&
        node.contents.parts.first.is_a?(BareAssocHash) &&
        node.contents.parts.first.assocs.length == 1 &&
        node.contents.parts.first.assocs.first.is_a?(AssocSplat)
    iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
    iseq.newhash(0)
    visit(node.contents.parts.first)
    iseq.send(YARV.calldata(:"core#hash_merge_kwd", 2))
    iseq.newarraykwsplat(1)
  else
    length = 0

    node.contents.parts.each do |part|
      if part.is_a?(ArgStar)
        if length > 0
          iseq.newarray(length)
          length = 0
        end

        visit(part.value)
        iseq.concatarray
      else
        visit(part)
        length += 1
      end
    end

    iseq.newarray(length) if length > 0
    iseq.concatarray if length > 0 && length != node.contents.parts.length
  end
end

#visit_aryptn(node) ⇒ Object



416
417
# File 'lib/syntax_tree/yarv/compiler.rb', line 416

def visit_aryptn(node)
end

#visit_assign(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 419

def visit_assign(node)
  case node.target
  when ARefField
    calldata = YARV.calldata(:[]=, 2)

    if !options.frozen_string_literal? &&
         options.specialized_instruction? &&
         (node.target.index.parts.length == 1)
      arg = node.target.index.parts.first

      if arg.is_a?(StringLiteral) && (arg.parts.length == 1)
        string_part = arg.parts.first

        if string_part.is_a?(TStringContent)
          visit(node.target.collection)
          visit(node.value)
          iseq.swap
          iseq.topn(1)
          iseq.opt_aset_with(string_part.value, calldata)
          iseq.pop
          return
        end
      end
    end

    iseq.putnil
    visit(node.target.collection)
    visit(node.target.index)
    visit(node.value)
    iseq.setn(3)
    iseq.send(calldata)
    iseq.pop
  when ConstPathField
    names = constant_names(node.target)
    name = names.pop

    if RUBY_VERSION >= "3.2"
      iseq.opt_getconstant_path(names)
      visit(node.value)
      iseq.swap
      iseq.topn(1)
      iseq.swap
      iseq.setconstant(name)
    else
      visit(node.value)
      iseq.dup if last_statement?
      iseq.opt_getconstant_path(names)
      iseq.setconstant(name)
    end
  when Field
    iseq.putnil
    visit(node.target)
    visit(node.value)
    iseq.setn(2)
    iseq.send(YARV.calldata(:"#{node.target.name.value}=", 1))
    iseq.pop
  when TopConstField
    name = node.target.constant.value.to_sym

    if RUBY_VERSION >= "3.2"
      iseq.putobject(Object)
      visit(node.value)
      iseq.swap
      iseq.topn(1)
      iseq.swap
      iseq.setconstant(name)
    else
      visit(node.value)
      iseq.dup if last_statement?
      iseq.putobject(Object)
      iseq.setconstant(name)
    end
  when VarField
    visit(node.value)
    iseq.dup if last_statement?

    case node.target.value
    when Const
      iseq.putspecialobject(PutSpecialObject::OBJECT_CONST_BASE)
      iseq.setconstant(node.target.value.value.to_sym)
    when CVar
      iseq.setclassvariable(node.target.value.value.to_sym)
    when GVar
      iseq.setglobal(node.target.value.value.to_sym)
    when Ident
      lookup = visit(node.target)

      if lookup.local.is_a?(LocalTable::BlockLocal)
        iseq.setblockparam(lookup.index, lookup.level)
      else
        iseq.setlocal(lookup.index, lookup.level)
      end
    when IVar
      iseq.setinstancevariable(node.target.value.value.to_sym)
    end
  end
end

#visit_assoc(node) ⇒ Object



517
518
519
520
# File 'lib/syntax_tree/yarv/compiler.rb', line 517

def visit_assoc(node)
  visit(node.key)
  visit(node.value)
end

#visit_assoc_splat(node) ⇒ Object



522
523
524
# File 'lib/syntax_tree/yarv/compiler.rb', line 522

def visit_assoc_splat(node)
  visit(node.value)
end

#visit_backref(node) ⇒ Object



526
527
528
# File 'lib/syntax_tree/yarv/compiler.rb', line 526

def visit_backref(node)
  iseq.getspecial(GetSpecial::SVAR_BACKREF, node.value[1..].to_i << 1)
end

#visit_bare_assoc_hash(node) ⇒ Object



530
531
532
533
534
535
536
# File 'lib/syntax_tree/yarv/compiler.rb', line 530

def visit_bare_assoc_hash(node)
  if (compiled = RubyVisitor.compile(node))
    iseq.duphash(compiled)
  else
    visit_all(node.assocs)
  end
end

#visit_BEGIN(node) ⇒ Object



294
295
296
# File 'lib/syntax_tree/yarv/compiler.rb', line 294

def visit_BEGIN(node)
  visit(node.statements)
end

#visit_begin(node) ⇒ Object



538
539
# File 'lib/syntax_tree/yarv/compiler.rb', line 538

def visit_begin(node)
end

#visit_binary(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 541

def visit_binary(node)
  case node.operator
  when :"&&"
    done_label = iseq.label

    visit(node.left)
    iseq.dup
    iseq.branchunless(done_label)

    iseq.pop
    visit(node.right)
    iseq.push(done_label)
  when :"||"
    visit(node.left)
    iseq.dup

    skip_right_label = iseq.label
    iseq.branchif(skip_right_label)
    iseq.pop

    visit(node.right)
    iseq.push(skip_right_label)
  else
    visit(node.left)
    visit(node.right)
    iseq.send(YARV.calldata(node.operator, 1))
  end
end

#visit_block(node) ⇒ Object



570
571
572
573
574
575
576
577
578
# File 'lib/syntax_tree/yarv/compiler.rb', line 570

def visit_block(node)
  with_child_iseq(iseq.block_child_iseq(node.location.start_line)) do
    iseq.event(:RUBY_EVENT_B_CALL)
    visit(node.block_var)
    visit(node.bodystmt)
    iseq.event(:RUBY_EVENT_B_RETURN)
    iseq.leave
  end
end

#visit_block_var(node) ⇒ Object



580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/syntax_tree/yarv/compiler.rb', line 580

def visit_block_var(node)
  params = node.params

  if params.requireds.length == 1 && params.optionals.empty? &&
       !params.rest && params.posts.empty? && params.keywords.empty? &&
       !params.keyword_rest && !params.block
    iseq.argument_options[:ambiguous_param0] = true
  end

  visit(node.params)

  node.locals.each { |local| iseq.local_table.plain(local.value.to_sym) }
end

#visit_blockarg(node) ⇒ Object



594
595
596
597
598
# File 'lib/syntax_tree/yarv/compiler.rb', line 594

def visit_blockarg(node)
  iseq.argument_options[:block_start] = iseq.argument_size
  iseq.local_table.block(node.name.value.to_sym)
  iseq.argument_size += 1
end

#visit_bodystmt(node) ⇒ Object



600
601
602
# File 'lib/syntax_tree/yarv/compiler.rb', line 600

def visit_bodystmt(node)
  visit(node.statements)
end

#visit_break(node) ⇒ Object



604
605
# File 'lib/syntax_tree/yarv/compiler.rb', line 604

def visit_break(node)
end

#visit_call(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 607

def visit_call(node)
  if node.is_a?(CallNode)
    return(
      visit_call(
        CommandCall.new(
          receiver: node.receiver,
          operator: node.operator,
          message: node.message,
          arguments: node.arguments,
          block: nil,
          location: node.location
        )
      )
    )
  end

  # Track whether or not this is a method call on a block proxy receiver.
  # If it is, we can potentially do tailcall optimizations on it.
  block_receiver = false

  if node.receiver
    if node.receiver.is_a?(VarRef)
      lookup = iseq.local_variable(node.receiver.value.value.to_sym)

      if lookup.local.is_a?(LocalTable::BlockLocal)
        iseq.getblockparamproxy(lookup.index, lookup.level)
        block_receiver = true
      else
        visit(node.receiver)
      end
    else
      visit(node.receiver)
    end
  else
    iseq.putself
  end

  after_call_label = nil
  if node.operator&.value == "&."
    iseq.dup
    after_call_label = iseq.label
    iseq.branchnil(after_call_label)
  end

  arg_parts = argument_parts(node.arguments)
  argc = arg_parts.length
  flag = 0

  arg_parts.each do |arg_part|
    case arg_part
    when ArgBlock
      argc -= 1
      flag |= CallData::CALL_ARGS_BLOCKARG
      visit(arg_part)
    when ArgStar
      flag |= CallData::CALL_ARGS_SPLAT
      visit(arg_part)
    when ArgsForward
      flag |= CallData::CALL_TAILCALL if options.tailcall_optimization?

      flag |= CallData::CALL_ARGS_SPLAT
      lookup = iseq.local_table.find(:*)
      iseq.getlocal(lookup.index, lookup.level)
      iseq.splatarray(arg_parts.length != 1)

      flag |= CallData::CALL_ARGS_BLOCKARG
      lookup = iseq.local_table.find(:&)
      iseq.getblockparamproxy(lookup.index, lookup.level)
    when BareAssocHash
      flag |= CallData::CALL_KW_SPLAT
      visit(arg_part)
    else
      visit(arg_part)
    end
  end

  block_iseq = visit(node.block) if node.block

  # If there's no block and we don't already have any special flags set,
  # then we can safely call this simple arguments. Note that has to be the
  # first flag we set after looking at the arguments to get the flags
  # correct.
  flag |= CallData::CALL_ARGS_SIMPLE if block_iseq.nil? && flag == 0

  # If there's no receiver, then this is an "fcall".
  flag |= CallData::CALL_FCALL if node.receiver.nil?

  # If we're calling a method on the passed block object and we have
  # tailcall optimizations turned on, then we can set the tailcall flag.
  if block_receiver && options.tailcall_optimization?
    flag |= CallData::CALL_TAILCALL
  end

  iseq.send(
    YARV.calldata(node.message.value.to_sym, argc, flag),
    block_iseq
  )
  iseq.event(after_call_label) if after_call_label
end

#visit_case(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 707

def visit_case(node)
  visit(node.value) if node.value

  clauses = []
  else_clause = nil
  current = node.consequent

  while current
    clauses << current

    if (current = current.consequent).is_a?(Else)
      else_clause = current
      break
    end
  end

  branches =
    clauses.map do |clause|
      visit(clause.arguments)
      iseq.topn(1)
      iseq.send(
        YARV.calldata(
          :===,
          1,
          CallData::CALL_FCALL | CallData::CALL_ARGS_SIMPLE
        )
      )

      label = iseq.label
      iseq.branchif(label)
      [clause, label]
    end

  iseq.pop
  else_clause ? visit(else_clause) : iseq.putnil
  iseq.leave

  branches.each_with_index do |(clause, label), index|
    iseq.leave if index != 0
    iseq.push(label)
    iseq.pop
    visit(clause)
  end
end

#visit_CHAR(node) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/syntax_tree/yarv/compiler.rb', line 298

def visit_CHAR(node)
  if options.frozen_string_literal?
    iseq.putobject(node.value[1..])
  else
    iseq.putstring(node.value[1..])
  end
end

#visit_class(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 752

def visit_class(node)
  name = node.constant.constant.value.to_sym
  class_iseq =
    with_child_iseq(
      iseq.class_child_iseq(name, node.location.start_line)
    ) do
      iseq.event(:RUBY_EVENT_CLASS)
      visit(node.bodystmt)
      iseq.event(:RUBY_EVENT_END)
      iseq.leave
    end

  flags = DefineClass::TYPE_CLASS

  case node.constant
  when ConstPathRef
    flags |= DefineClass::FLAG_SCOPED
    visit(node.constant.parent)
  when ConstRef
    iseq.putspecialobject(PutSpecialObject::OBJECT_CONST_BASE)
  when TopConstRef
    flags |= DefineClass::FLAG_SCOPED
    iseq.putobject(Object)
  end

  if node.superclass
    flags |= DefineClass::FLAG_HAS_SUPERCLASS
    visit(node.superclass)
  else
    iseq.putnil
  end

  iseq.defineclass(name, class_iseq, flags)
end

#visit_command(node) ⇒ Object



787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/syntax_tree/yarv/compiler.rb', line 787

def visit_command(node)
  visit_call(
    CommandCall.new(
      receiver: nil,
      operator: nil,
      message: node.message,
      arguments: node.arguments,
      block: node.block,
      location: node.location
    )
  )
end

#visit_command_call(node) ⇒ Object



800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/syntax_tree/yarv/compiler.rb', line 800

def visit_command_call(node)
  visit_call(
    CommandCall.new(
      receiver: node.receiver,
      operator: node.operator,
      message: node.message,
      arguments: node.arguments,
      block: node.block,
      location: node.location
    )
  )
end

#visit_const_path_field(node) ⇒ Object



813
814
815
# File 'lib/syntax_tree/yarv/compiler.rb', line 813

def visit_const_path_field(node)
  visit(node.parent)
end

#visit_const_path_ref(node) ⇒ Object



817
818
819
820
# File 'lib/syntax_tree/yarv/compiler.rb', line 817

def visit_const_path_ref(node)
  names = constant_names(node)
  iseq.opt_getconstant_path(names)
end

#visit_def(node) ⇒ Object



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/syntax_tree/yarv/compiler.rb', line 822

def visit_def(node)
  name = node.name.value.to_sym
  method_iseq =
    iseq.method_child_iseq(name.to_s, node.location.start_line)

  with_child_iseq(method_iseq) do
    visit(node.params) if node.params
    iseq.event(:RUBY_EVENT_CALL)
    visit(node.bodystmt)
    iseq.event(:RUBY_EVENT_RETURN)
    iseq.leave
  end

  if node.target
    visit(node.target)
    iseq.definesmethod(name, method_iseq)
  else
    iseq.definemethod(name, method_iseq)
  end

  iseq.putobject(name)
end

#visit_defined(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 845

def visit_defined(node)
  case node.value
  when Assign
    # If we're assigning to a local variable, then we need to make sure
    # that we put it into the local table.
    if node.value.target.is_a?(VarField) &&
         node.value.target.value.is_a?(Ident)
      iseq.local_table.plain(node.value.target.value.value.to_sym)
    end

    iseq.putobject("assignment")
  when VarRef
    value = node.value.value
    name = value.value.to_sym

    case value
    when Const
      iseq.putnil
      iseq.defined(Defined::TYPE_CONST, name, "constant")
    when CVar
      iseq.putnil
      iseq.defined(Defined::TYPE_CVAR, name, "class variable")
    when GVar
      iseq.putnil
      iseq.defined(Defined::TYPE_GVAR, name, "global-variable")
    when Ident
      iseq.putobject("local-variable")
    when IVar
      iseq.putnil
      iseq.defined(Defined::TYPE_IVAR, name, "instance-variable")
    when Kw
      case name
      when :false
        iseq.putobject("false")
      when :nil
        iseq.putobject("nil")
      when :self
        iseq.putobject("self")
      when :true
        iseq.putobject("true")
      end
    end
  when VCall
    iseq.putself

    name = node.value.value.value.to_sym
    iseq.defined(Defined::TYPE_FUNC, name, "method")
  when YieldNode
    iseq.putnil
    iseq.defined(Defined::TYPE_YIELD, false, "yield")
  when ZSuper
    iseq.putnil
    iseq.defined(Defined::TYPE_ZSUPER, false, "super")
  else
    iseq.putobject("expression")
  end
end

#visit_dyna_symbol(node) ⇒ Object



903
904
905
906
907
# File 'lib/syntax_tree/yarv/compiler.rb', line 903

def visit_dyna_symbol(node)
  if node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
    iseq.putobject(node.parts.first.value.to_sym)
  end
end

#visit_else(node) ⇒ Object



909
910
911
912
# File 'lib/syntax_tree/yarv/compiler.rb', line 909

def visit_else(node)
  visit(node.statements)
  iseq.pop unless last_statement?
end

#visit_elsif(node) ⇒ Object



914
915
916
917
918
919
920
921
922
923
# File 'lib/syntax_tree/yarv/compiler.rb', line 914

def visit_elsif(node)
  visit_if(
    IfNode.new(
      predicate: node.predicate,
      statements: node.statements,
      consequent: node.consequent,
      location: node.location
    )
  )
end

#visit_END(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 306

def visit_END(node)
  start_line = node.location.start_line
  once_iseq =
    with_child_iseq(iseq.block_child_iseq(start_line)) do
      postexe_iseq =
        with_child_iseq(iseq.block_child_iseq(start_line)) do
          iseq.event(:RUBY_EVENT_B_CALL)

          *statements, last_statement = node.statements.body
          visit_all(statements)
          with_last_statement { visit(last_statement) }

          iseq.event(:RUBY_EVENT_B_RETURN)
          iseq.leave
        end

      iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
      iseq.send(
        YARV.calldata(:"core#set_postexe", 0, CallData::CALL_FCALL),
        postexe_iseq
      )
      iseq.leave
    end

  iseq.once(once_iseq, iseq.inline_storage)
  iseq.pop
end

#visit_ensure(node) ⇒ Object



925
926
# File 'lib/syntax_tree/yarv/compiler.rb', line 925

def visit_ensure(node)
end

#visit_field(node) ⇒ Object



928
929
930
# File 'lib/syntax_tree/yarv/compiler.rb', line 928

def visit_field(node)
  visit(node.parent)
end

#visit_float(node) ⇒ Object



932
933
934
# File 'lib/syntax_tree/yarv/compiler.rb', line 932

def visit_float(node)
  iseq.putobject(node.accept(RubyVisitor.new))
end

#visit_fndptn(node) ⇒ Object



936
937
# File 'lib/syntax_tree/yarv/compiler.rb', line 936

def visit_fndptn(node)
end

#visit_for(node) ⇒ Object



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
# File 'lib/syntax_tree/yarv/compiler.rb', line 939

def visit_for(node)
  visit(node.collection)

  name = node.index.value.value.to_sym
  iseq.local_table.plain(name)

  block_iseq =
    with_child_iseq(
      iseq.block_child_iseq(node.statements.location.start_line)
    ) do
      iseq.argument_options[:lead_num] ||= 0
      iseq.argument_options[:lead_num] += 1
      iseq.argument_options[:ambiguous_param0] = true

      iseq.argument_size += 1
      iseq.local_table.plain(2)

      iseq.getlocal(0, 0)

      local_variable = iseq.local_variable(name)
      iseq.setlocal(local_variable.index, local_variable.level)

      iseq.event(:RUBY_EVENT_B_CALL)
      iseq.nop

      visit(node.statements)
      iseq.event(:RUBY_EVENT_B_RETURN)
      iseq.leave
    end

  iseq.send(YARV.calldata(:each, 0, 0), block_iseq)
end

#visit_hash(node) ⇒ Object



972
973
974
975
976
977
978
979
# File 'lib/syntax_tree/yarv/compiler.rb', line 972

def visit_hash(node)
  if (compiled = RubyVisitor.compile(node))
    iseq.duphash(compiled)
  else
    visit_all(node.assocs)
    iseq.newhash(node.assocs.length * 2)
  end
end

#visit_heredoc(node) ⇒ Object



984
985
986
987
988
989
990
991
992
993
# File 'lib/syntax_tree/yarv/compiler.rb', line 984

def visit_heredoc(node)
  if node.beginning.value.end_with?("`")
    visit_xstring_literal(node)
  elsif node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
    visit(node.parts.first)
  else
    length = visit_string_parts(node)
    iseq.concatstrings(length)
  end
end

#visit_hshptn(node) ⇒ Object



981
982
# File 'lib/syntax_tree/yarv/compiler.rb', line 981

def visit_hshptn(node)
end

#visit_if(node) ⇒ Object



995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
# File 'lib/syntax_tree/yarv/compiler.rb', line 995

def visit_if(node)
  if node.predicate.is_a?(RangeNode)
    true_label = iseq.label
    false_label = iseq.label
    end_label = iseq.label

    iseq.getspecial(GetSpecial::SVAR_FLIPFLOP_START, 0)
    iseq.branchif(true_label)

    visit(node.predicate.left)
    iseq.branchunless(end_label)

    iseq.putobject(true)
    iseq.setspecial(GetSpecial::SVAR_FLIPFLOP_START)

    iseq.push(true_label)
    visit(node.predicate.right)
    iseq.branchunless(false_label)

    iseq.putobject(false)
    iseq.setspecial(GetSpecial::SVAR_FLIPFLOP_START)

    iseq.push(false_label)
    visit(node.statements)
    iseq.leave
    iseq.push(end_label)
    iseq.putnil
  else
    consequent_label = iseq.label

    visit(node.predicate)
    iseq.branchunless(consequent_label)
    visit(node.statements)

    if last_statement?
      iseq.leave
      iseq.push(consequent_label)
      node.consequent ? visit(node.consequent) : iseq.putnil
    else
      iseq.pop

      if node.consequent
        done_label = iseq.label
        iseq.jump(done_label)
        iseq.push(consequent_label)
        visit(node.consequent)
        iseq.push(done_label)
      else
        iseq.push(consequent_label)
      end
    end
  end
end

#visit_if_op(node) ⇒ Object



1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/syntax_tree/yarv/compiler.rb', line 1049

def visit_if_op(node)
  visit_if(
    IfNode.new(
      predicate: node.predicate,
      statements: node.truthy,
      consequent:
        Else.new(
          keyword: Kw.new(value: "else", location: Location.default),
          statements: node.falsy,
          location: Location.default
        ),
      location: Location.default
    )
  )
end

#visit_imaginary(node) ⇒ Object



1065
1066
1067
# File 'lib/syntax_tree/yarv/compiler.rb', line 1065

def visit_imaginary(node)
  iseq.putobject(node.accept(RubyVisitor.new))
end

#visit_int(node) ⇒ Object



1069
1070
1071
# File 'lib/syntax_tree/yarv/compiler.rb', line 1069

def visit_int(node)
  iseq.putobject(node.accept(RubyVisitor.new))
end

#visit_kwrest_param(node) ⇒ Object



1073
1074
1075
1076
1077
# File 'lib/syntax_tree/yarv/compiler.rb', line 1073

def visit_kwrest_param(node)
  iseq.argument_options[:kwrest] = iseq.argument_size
  iseq.argument_size += 1
  iseq.local_table.plain(node.name.value.to_sym)
end

#visit_label(node) ⇒ Object



1079
1080
1081
# File 'lib/syntax_tree/yarv/compiler.rb', line 1079

def visit_label(node)
  iseq.putobject(node.accept(RubyVisitor.new))
end

#visit_lambda(node) ⇒ Object



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
# File 'lib/syntax_tree/yarv/compiler.rb', line 1083

def visit_lambda(node)
  lambda_iseq =
    with_child_iseq(iseq.block_child_iseq(node.location.start_line)) do
      iseq.event(:RUBY_EVENT_B_CALL)
      visit(node.params)
      visit(node.statements)
      iseq.event(:RUBY_EVENT_B_RETURN)
      iseq.leave
    end

  iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
  iseq.send(YARV.calldata(:lambda, 0, CallData::CALL_FCALL), lambda_iseq)
end

#visit_lambda_var(node) ⇒ Object



1097
1098
1099
# File 'lib/syntax_tree/yarv/compiler.rb', line 1097

def visit_lambda_var(node)
  visit_block_var(node)
end

#visit_massign(node) ⇒ Object



1101
1102
1103
1104
1105
# File 'lib/syntax_tree/yarv/compiler.rb', line 1101

def visit_massign(node)
  visit(node.value)
  iseq.dup
  visit(node.target)
end

#visit_method_add_block(node) ⇒ Object



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/syntax_tree/yarv/compiler.rb', line 1107

def visit_method_add_block(node)
  visit_call(
    CommandCall.new(
      receiver: node.call.receiver,
      operator: node.call.operator,
      message: node.call.message,
      arguments: node.call.arguments,
      block: node.block,
      location: node.location
    )
  )
end

#visit_mlhs(node) ⇒ Object



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/syntax_tree/yarv/compiler.rb', line 1120

def visit_mlhs(node)
  lookups = []
  node.parts.each do |part|
    case part
    when VarField
      lookups << visit(part)
    end
  end

  iseq.expandarray(lookups.length, 0)
  lookups.each { |lookup| iseq.setlocal(lookup.index, lookup.level) }
end

#visit_module(node) ⇒ Object



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
# File 'lib/syntax_tree/yarv/compiler.rb', line 1133

def visit_module(node)
  name = node.constant.constant.value.to_sym
  module_iseq =
    with_child_iseq(
      iseq.module_child_iseq(name, node.location.start_line)
    ) do
      iseq.event(:RUBY_EVENT_CLASS)
      visit(node.bodystmt)
      iseq.event(:RUBY_EVENT_END)
      iseq.leave
    end

  flags = DefineClass::TYPE_MODULE

  case node.constant
  when ConstPathRef
    flags |= DefineClass::FLAG_SCOPED
    visit(node.constant.parent)
  when ConstRef
    iseq.putspecialobject(PutSpecialObject::OBJECT_CONST_BASE)
  when TopConstRef
    flags |= DefineClass::FLAG_SCOPED
    iseq.putobject(Object)
  end

  iseq.putnil
  iseq.defineclass(name, module_iseq, flags)
end

#visit_mrhs(node) ⇒ Object



1162
1163
1164
1165
1166
1167
1168
1169
# File 'lib/syntax_tree/yarv/compiler.rb', line 1162

def visit_mrhs(node)
  if (compiled = RubyVisitor.compile(node))
    iseq.duparray(compiled)
  else
    visit_all(node.parts)
    iseq.newarray(node.parts.length)
  end
end

#visit_next(node) ⇒ Object



1171
1172
# File 'lib/syntax_tree/yarv/compiler.rb', line 1171

def visit_next(node)
end

#visit_not(node) ⇒ Object



1174
1175
1176
1177
# File 'lib/syntax_tree/yarv/compiler.rb', line 1174

def visit_not(node)
  visit(node.statement)
  iseq.send(YARV.calldata(:!))
end

#visit_opassign(node) ⇒ Object



1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# File 'lib/syntax_tree/yarv/compiler.rb', line 1179

def visit_opassign(node)
  flag = CallData::CALL_ARGS_SIMPLE
  if node.target.is_a?(ConstPathField) || node.target.is_a?(TopConstField)
    flag |= CallData::CALL_FCALL
  end

  case (operator = node.operator.value.chomp("=").to_sym)
  when :"&&"
    done_label = iseq.label

    with_opassign(node) do
      iseq.dup
      iseq.branchunless(done_label)
      iseq.pop
      visit(node.value)
    end

    case node.target
    when ARefField
      iseq.leave
      iseq.push(done_label)
      iseq.setn(3)
      iseq.adjuststack(3)
    when ConstPathField, TopConstField
      iseq.push(done_label)
      iseq.swap
      iseq.pop
    else
      iseq.push(done_label)
    end
  when :"||"
    if node.target.is_a?(ConstPathField) ||
         node.target.is_a?(TopConstField)
      opassign_defined(node)
      iseq.swap
      iseq.pop
    elsif node.target.is_a?(VarField) &&
          [Const, CVar, GVar].include?(node.target.value.class)
      opassign_defined(node)
    else
      skip_value_label = iseq.label

      with_opassign(node) do
        iseq.dup
        iseq.branchif(skip_value_label)
        iseq.pop
        visit(node.value)
      end

      if node.target.is_a?(ARefField)
        iseq.leave
        iseq.push(skip_value_label)
        iseq.setn(3)
        iseq.adjuststack(3)
      else
        iseq.push(skip_value_label)
      end
    end
  else
    with_opassign(node) do
      visit(node.value)
      iseq.send(YARV.calldata(operator, 1, flag))
    end
  end
end

#visit_params(node) ⇒ Object



1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
# File 'lib/syntax_tree/yarv/compiler.rb', line 1245

def visit_params(node)
  if node.requireds.any?
    iseq.argument_options[:lead_num] = 0

    node.requireds.each do |required|
      iseq.local_table.plain(required.value.to_sym)
      iseq.argument_size += 1
      iseq.argument_options[:lead_num] += 1
    end
  end

  node.optionals.each do |(optional, value)|
    index = iseq.local_table.size
    name = optional.value.to_sym

    iseq.local_table.plain(name)
    iseq.argument_size += 1

    unless iseq.argument_options.key?(:opt)
      start_label = iseq.label
      iseq.push(start_label)
      iseq.argument_options[:opt] = [start_label]
    end

    visit(value)
    iseq.setlocal(index, 0)

    arg_given_label = iseq.label
    iseq.push(arg_given_label)
    iseq.argument_options[:opt] << arg_given_label
  end

  visit(node.rest) if node.rest

  if node.posts.any?
    iseq.argument_options[:post_start] = iseq.argument_size
    iseq.argument_options[:post_num] = 0

    node.posts.each do |post|
      iseq.local_table.plain(post.value.to_sym)
      iseq.argument_size += 1
      iseq.argument_options[:post_num] += 1
    end
  end

  if node.keywords.any?
    iseq.argument_options[:kwbits] = 0
    iseq.argument_options[:keyword] = []

    keyword_bits_name = node.keyword_rest ? 3 : 2
    iseq.argument_size += 1
    keyword_bits_index = iseq.local_table.locals.size + node.keywords.size

    node.keywords.each_with_index do |(keyword, value), keyword_index|
      name = keyword.value.chomp(":").to_sym
      index = iseq.local_table.size

      iseq.local_table.plain(name)
      iseq.argument_size += 1
      iseq.argument_options[:kwbits] += 1

      if value.nil?
        iseq.argument_options[:keyword] << name
      elsif (compiled = RubyVisitor.compile(value))
        iseq.argument_options[:keyword] << [name, compiled]
      else
        skip_value_label = iseq.label

        iseq.argument_options[:keyword] << [name]
        iseq.checkkeyword(keyword_bits_index, keyword_index)
        iseq.branchif(skip_value_label)
        visit(value)
        iseq.setlocal(index, 0)
        iseq.push(skip_value_label)
      end
    end

    iseq.local_table.plain(keyword_bits_name)
  end

  if node.keyword_rest.is_a?(ArgsForward)
    if RUBY_VERSION >= "3.2"
      iseq.local_table.plain(:*)
      iseq.local_table.plain(:&)
      iseq.local_table.plain(:"...")

      iseq.argument_options[:rest_start] = iseq.argument_size
      iseq.argument_options[:block_start] = iseq.argument_size + 1

      iseq.argument_size += 2
    else
      iseq.local_table.plain(:*)
      iseq.local_table.plain(:&)

      iseq.argument_options[:rest_start] = iseq.argument_size
      iseq.argument_options[:block_start] = iseq.argument_size + 1

      iseq.argument_size += 2
    end
  elsif node.keyword_rest
    visit(node.keyword_rest)
  end

  visit(node.block) if node.block
end

#visit_paren(node) ⇒ Object



1351
1352
1353
# File 'lib/syntax_tree/yarv/compiler.rb', line 1351

def visit_paren(node)
  visit(node.contents)
end

#visit_pinned_begin(node) ⇒ Object



1355
1356
# File 'lib/syntax_tree/yarv/compiler.rb', line 1355

def visit_pinned_begin(node)
end

#visit_pinned_var_ref(node) ⇒ Object



1358
1359
# File 'lib/syntax_tree/yarv/compiler.rb', line 1358

def visit_pinned_var_ref(node)
end

#visit_program(node) ⇒ Object



1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'lib/syntax_tree/yarv/compiler.rb', line 1361

def visit_program(node)
  node.statements.body.each do |statement|
    break unless statement.is_a?(Comment)

    if statement.value == "# frozen_string_literal: true"
      options.frozen_string_literal!
    end
  end

  preexes = []
  statements = []

  node.statements.body.each do |statement|
    case statement
    when Comment, EmbDoc, EndContent, VoidStmt
      # ignore
    when BEGINBlock
      preexes << statement
    else
      statements << statement
    end
  end

  top_iseq =
    InstructionSequence.new(
      "<compiled>",
      "<compiled>",
      1,
      :top,
      nil,
      options
    )

  with_child_iseq(top_iseq) do
    visit_all(preexes)

    if statements.empty?
      iseq.putnil
    else
      *statements, last_statement = statements
      visit_all(statements)
      with_last_statement { visit(last_statement) }
    end

    iseq.leave
  end

  top_iseq.compile!
  top_iseq
end

#visit_qsymbols(node) ⇒ Object



1412
1413
1414
# File 'lib/syntax_tree/yarv/compiler.rb', line 1412

def visit_qsymbols(node)
  iseq.duparray(node.accept(RubyVisitor.new))
end

#visit_qwords(node) ⇒ Object



1416
1417
1418
1419
1420
1421
1422
1423
# File 'lib/syntax_tree/yarv/compiler.rb', line 1416

def visit_qwords(node)
  if options.frozen_string_literal?
    iseq.duparray(node.accept(RubyVisitor.new))
  else
    visit_all(node.elements)
    iseq.newarray(node.elements.length)
  end
end

#visit_range(node) ⇒ Object



1425
1426
1427
1428
1429
1430
1431
1432
1433
# File 'lib/syntax_tree/yarv/compiler.rb', line 1425

def visit_range(node)
  if (compiled = RubyVisitor.compile(node))
    iseq.putobject(compiled)
  else
    visit(node.left)
    visit(node.right)
    iseq.newrange(node.operator.value == ".." ? 0 : 1)
  end
end

#visit_rassign(node) ⇒ Object



1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
# File 'lib/syntax_tree/yarv/compiler.rb', line 1435

def visit_rassign(node)
  iseq.putnil

  if node.operator.is_a?(Kw)
    match_label = iseq.label

    visit(node.value)
    iseq.dup

    visit_pattern(node.pattern, match_label)

    iseq.pop
    iseq.pop
    iseq.putobject(false)
    iseq.leave

    iseq.push(match_label)
    iseq.adjuststack(2)
    iseq.putobject(true)
  else
    no_key_label = iseq.label
    end_leave_label = iseq.label
    end_label = iseq.label

    iseq.putnil
    iseq.putobject(false)
    iseq.putnil
    iseq.putnil
    visit(node.value)
    iseq.dup

    visit_pattern(node.pattern, end_label)

    # First we're going to push the core onto the stack, then we'll check
    # if the value to match is truthy. If it is, we'll jump down to raise
    # NoMatchingPatternKeyError. Otherwise we'll raise
    # NoMatchingPatternError.
    iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
    iseq.topn(4)
    iseq.branchif(no_key_label)

    # Here we're going to raise NoMatchingPatternError.
    iseq.putobject(NoMatchingPatternError)
    iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
    iseq.putobject("%p: %s")
    iseq.topn(4)
    iseq.topn(7)
    iseq.send(YARV.calldata(:"core#sprintf", 3))
    iseq.send(YARV.calldata(:"core#raise", 2))
    iseq.jump(end_leave_label)

    # Here we're going to raise NoMatchingPatternKeyError.
    iseq.push(no_key_label)
    iseq.putobject(NoMatchingPatternKeyError)
    iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
    iseq.putobject("%p: %s")
    iseq.topn(4)
    iseq.topn(7)
    iseq.send(YARV.calldata(:"core#sprintf", 3))
    iseq.topn(7)
    iseq.topn(9)
    iseq.send(
      YARV.calldata(:new, 1, CallData::CALL_KWARG, i[matchee key])
    )
    iseq.send(YARV.calldata(:"core#raise", 1))

    iseq.push(end_leave_label)
    iseq.adjuststack(7)
    iseq.putnil
    iseq.leave

    iseq.push(end_label)
    iseq.adjuststack(6)
    iseq.putnil
  end
end

#visit_rational(node) ⇒ Object



1512
1513
1514
# File 'lib/syntax_tree/yarv/compiler.rb', line 1512

def visit_rational(node)
  iseq.putobject(node.accept(RubyVisitor.new))
end

#visit_redo(node) ⇒ Object



1516
1517
# File 'lib/syntax_tree/yarv/compiler.rb', line 1516

def visit_redo(node)
end

#visit_regexp_literal(node) ⇒ Object



1519
1520
1521
1522
1523
1524
1525
1526
1527
# File 'lib/syntax_tree/yarv/compiler.rb', line 1519

def visit_regexp_literal(node)
  if (compiled = RubyVisitor.compile(node))
    iseq.putobject(compiled)
  else
    flags = RubyVisitor.new.visit_regexp_literal_flags(node)
    length = visit_string_parts(node)
    iseq.toregexp(flags, length)
  end
end

#visit_rescue(node) ⇒ Object



1529
1530
# File 'lib/syntax_tree/yarv/compiler.rb', line 1529

def visit_rescue(node)
end

#visit_rescue_ex(node) ⇒ Object



1532
1533
# File 'lib/syntax_tree/yarv/compiler.rb', line 1532

def visit_rescue_ex(node)
end

#visit_rescue_mod(node) ⇒ Object



1535
1536
# File 'lib/syntax_tree/yarv/compiler.rb', line 1535

def visit_rescue_mod(node)
end

#visit_rest_param(node) ⇒ Object



1538
1539
1540
1541
1542
# File 'lib/syntax_tree/yarv/compiler.rb', line 1538

def visit_rest_param(node)
  iseq.local_table.plain(node.name.value.to_sym)
  iseq.argument_options[:rest_start] = iseq.argument_size
  iseq.argument_size += 1
end

#visit_retry(node) ⇒ Object



1544
1545
# File 'lib/syntax_tree/yarv/compiler.rb', line 1544

def visit_retry(node)
end

#visit_return(node) ⇒ Object



1547
1548
# File 'lib/syntax_tree/yarv/compiler.rb', line 1547

def visit_return(node)
end

#visit_sclass(node) ⇒ Object



1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
# File 'lib/syntax_tree/yarv/compiler.rb', line 1550

def visit_sclass(node)
  visit(node.target)
  iseq.putnil

  singleton_iseq =
    with_child_iseq(
      iseq.singleton_class_child_iseq(node.location.start_line)
    ) do
      iseq.event(:RUBY_EVENT_CLASS)
      visit(node.bodystmt)
      iseq.event(:RUBY_EVENT_END)
      iseq.leave
    end

  iseq.defineclass(
    :singletonclass,
    singleton_iseq,
    DefineClass::TYPE_SINGLETON_CLASS
  )
end

#visit_statements(node) ⇒ Object



1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
# File 'lib/syntax_tree/yarv/compiler.rb', line 1571

def visit_statements(node)
  statements =
    node.body.select do |statement|
      case statement
      when Comment, EmbDoc, EndContent, VoidStmt
        false
      else
        true
      end
    end

  statements.empty? ? iseq.putnil : visit_all(statements)
end

#visit_string_concat(node) ⇒ Object



1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
# File 'lib/syntax_tree/yarv/compiler.rb', line 1585

def visit_string_concat(node)
  value = node.left.parts.first.value + node.right.parts.first.value

  visit_string_literal(
    StringLiteral.new(
      parts: [TStringContent.new(value: value, location: node.location)],
      quote: node.left.quote,
      location: node.location
    )
  )
end

#visit_string_embexpr(node) ⇒ Object



1597
1598
1599
# File 'lib/syntax_tree/yarv/compiler.rb', line 1597

def visit_string_embexpr(node)
  visit(node.statements)
end

#visit_string_literal(node) ⇒ Object



1601
1602
1603
1604
1605
1606
1607
1608
# File 'lib/syntax_tree/yarv/compiler.rb', line 1601

def visit_string_literal(node)
  if node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
    visit(node.parts.first)
  else
    length = visit_string_parts(node)
    iseq.concatstrings(length)
  end
end

#visit_super(node) ⇒ Object



1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
# File 'lib/syntax_tree/yarv/compiler.rb', line 1610

def visit_super(node)
  iseq.putself
  visit(node.arguments)
  iseq.invokesuper(
    YARV.calldata(
      nil,
      argument_parts(node.arguments).length,
      CallData::CALL_FCALL | CallData::CALL_ARGS_SIMPLE |
        CallData::CALL_SUPER
    ),
    nil
  )
end

#visit_symbol_literal(node) ⇒ Object



1624
1625
1626
# File 'lib/syntax_tree/yarv/compiler.rb', line 1624

def visit_symbol_literal(node)
  iseq.putobject(node.accept(RubyVisitor.new))
end

#visit_symbols(node) ⇒ Object



1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
# File 'lib/syntax_tree/yarv/compiler.rb', line 1628

def visit_symbols(node)
  if (compiled = RubyVisitor.compile(node))
    iseq.duparray(compiled)
  else
    node.elements.each do |element|
      if element.parts.length == 1 &&
           element.parts.first.is_a?(TStringContent)
        iseq.putobject(element.parts.first.value.to_sym)
      else
        length = visit_string_parts(element)
        iseq.concatstrings(length)
        iseq.intern
      end
    end

    iseq.newarray(node.elements.length)
  end
end

#visit_top_const_ref(node) ⇒ Object



1647
1648
1649
# File 'lib/syntax_tree/yarv/compiler.rb', line 1647

def visit_top_const_ref(node)
  iseq.opt_getconstant_path(constant_names(node))
end

#visit_tstring_content(node) ⇒ Object



1651
1652
1653
1654
1655
1656
1657
# File 'lib/syntax_tree/yarv/compiler.rb', line 1651

def visit_tstring_content(node)
  if options.frozen_string_literal?
    iseq.putobject(node.accept(RubyVisitor.new))
  else
    iseq.putstring(node.accept(RubyVisitor.new))
  end
end

#visit_unary(node) ⇒ Object



1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'lib/syntax_tree/yarv/compiler.rb', line 1659

def visit_unary(node)
  method_id =
    case node.operator
    when "+", "-"
      "#{node.operator}@"
    else
      node.operator
    end

  visit_call(
    CommandCall.new(
      receiver: node.statement,
      operator: nil,
      message: Ident.new(value: method_id, location: Location.default),
      arguments: nil,
      block: nil,
      location: Location.default
    )
  )
end

#visit_undef(node) ⇒ Object



1680
1681
1682
1683
1684
1685
1686
1687
1688
# File 'lib/syntax_tree/yarv/compiler.rb', line 1680

def visit_undef(node)
  node.symbols.each_with_index do |symbol, index|
    iseq.pop if index != 0
    iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
    iseq.putspecialobject(PutSpecialObject::OBJECT_CBASE)
    visit(symbol)
    iseq.send(YARV.calldata(:"core#undef_method", 2))
  end
end

#visit_unless(node) ⇒ Object



1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
# File 'lib/syntax_tree/yarv/compiler.rb', line 1690

def visit_unless(node)
  statements_label = iseq.label

  visit(node.predicate)
  iseq.branchunless(statements_label)
  node.consequent ? visit(node.consequent) : iseq.putnil

  if last_statement?
    iseq.leave
    iseq.push(statements_label)
    visit(node.statements)
  else
    iseq.pop

    if node.consequent
      done_label = iseq.label
      iseq.jump(done_label)
      iseq.push(statements_label)
      visit(node.consequent)
      iseq.push(done_label)
    else
      iseq.push(statements_label)
    end
  end
end

#visit_until(node) ⇒ Object



1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
# File 'lib/syntax_tree/yarv/compiler.rb', line 1716

def visit_until(node)
  predicate_label = iseq.label
  statements_label = iseq.label

  iseq.jump(predicate_label)
  iseq.putnil
  iseq.pop
  iseq.jump(predicate_label)

  iseq.push(statements_label)
  visit(node.statements)
  iseq.pop

  iseq.push(predicate_label)
  visit(node.predicate)
  iseq.branchunless(statements_label)
  iseq.putnil if last_statement?
end

#visit_var_field(node) ⇒ Object



1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
# File 'lib/syntax_tree/yarv/compiler.rb', line 1735

def visit_var_field(node)
  case node.value
  when CVar, IVar
    name = node.value.value.to_sym
    iseq.inline_storage_for(name)
  when Ident
    name = node.value.value.to_sym

    if (local_variable = iseq.local_variable(name))
      local_variable
    else
      iseq.local_table.plain(name)
      iseq.local_variable(name)
    end
  end
end

#visit_var_ref(node) ⇒ Object



1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
# File 'lib/syntax_tree/yarv/compiler.rb', line 1752

def visit_var_ref(node)
  case node.value
  when Const
    iseq.opt_getconstant_path(constant_names(node))
  when CVar
    name = node.value.value.to_sym
    iseq.getclassvariable(name)
  when GVar
    iseq.getglobal(node.value.value.to_sym)
  when Ident
    lookup = iseq.local_variable(node.value.value.to_sym)

    case lookup.local
    when LocalTable::BlockLocal
      iseq.getblockparam(lookup.index, lookup.level)
    when LocalTable::PlainLocal
      iseq.getlocal(lookup.index, lookup.level)
    end
  when IVar
    name = node.value.value.to_sym
    iseq.getinstancevariable(name)
  when Kw
    case node.value.value
    when "false"
      iseq.putobject(false)
    when "nil"
      iseq.putnil
    when "self"
      iseq.putself
    when "true"
      iseq.putobject(true)
    end
  end
end

#visit_vcall(node) ⇒ Object



1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
# File 'lib/syntax_tree/yarv/compiler.rb', line 1787

def visit_vcall(node)
  iseq.putself
  iseq.send(
    YARV.calldata(
      node.value.value.to_sym,
      0,
      CallData::CALL_FCALL | CallData::CALL_VCALL |
        CallData::CALL_ARGS_SIMPLE
    )
  )
end

#visit_when(node) ⇒ Object



1799
1800
1801
# File 'lib/syntax_tree/yarv/compiler.rb', line 1799

def visit_when(node)
  visit(node.statements)
end

#visit_while(node) ⇒ Object



1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
# File 'lib/syntax_tree/yarv/compiler.rb', line 1803

def visit_while(node)
  predicate_label = iseq.label
  statements_label = iseq.label

  iseq.jump(predicate_label)
  iseq.putnil
  iseq.pop
  iseq.jump(predicate_label)

  iseq.push(statements_label)
  visit(node.statements)
  iseq.pop

  iseq.push(predicate_label)
  visit(node.predicate)
  iseq.branchif(statements_label)
  iseq.putnil if last_statement?
end

#visit_word(node) ⇒ Object



1822
1823
1824
1825
1826
1827
1828
1829
# File 'lib/syntax_tree/yarv/compiler.rb', line 1822

def visit_word(node)
  if node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
    visit(node.parts.first)
  else
    length = visit_string_parts(node)
    iseq.concatstrings(length)
  end
end

#visit_words(node) ⇒ Object



1831
1832
1833
1834
1835
1836
1837
1838
1839
# File 'lib/syntax_tree/yarv/compiler.rb', line 1831

def visit_words(node)
  if options.frozen_string_literal? &&
       (compiled = RubyVisitor.compile(node))
    iseq.duparray(compiled)
  else
    visit_all(node.elements)
    iseq.newarray(node.elements.length)
  end
end

#visit_xstring_literal(node) ⇒ Object



1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
# File 'lib/syntax_tree/yarv/compiler.rb', line 1841

def visit_xstring_literal(node)
  iseq.putself
  length = visit_string_parts(node)
  iseq.concatstrings(node.parts.length) if length > 1
  iseq.send(
    YARV.calldata(
      :`,
      1,
      CallData::CALL_FCALL | CallData::CALL_ARGS_SIMPLE
    )
  )
end

#visit_yield(node) ⇒ Object



1854
1855
1856
1857
1858
# File 'lib/syntax_tree/yarv/compiler.rb', line 1854

def visit_yield(node)
  parts = argument_parts(node.arguments)
  visit_all(parts)
  iseq.invokeblock(YARV.calldata(nil, parts.length))
end

#visit_zsuper(_node) ⇒ Object



1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
# File 'lib/syntax_tree/yarv/compiler.rb', line 1860

def visit_zsuper(_node)
  iseq.putself
  iseq.invokesuper(
    YARV.calldata(
      nil,
      0,
      CallData::CALL_FCALL | CallData::CALL_ARGS_SIMPLE |
        CallData::CALL_SUPER | CallData::CALL_ZSUPER
    ),
    nil
  )
end