Class: CodeTools::AST::Match2

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/ast/control_flow.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(line, pattern, value) ⇒ Match2

Returns a new instance of Match2.



467
468
469
470
471
# File 'lib/rubinius/ast/control_flow.rb', line 467

def initialize(line, pattern, value)
  @line = line
  @pattern = pattern
  @value = value
end

Instance Attribute Details

#patternObject

Returns the value of attribute pattern.



465
466
467
# File 'lib/rubinius/ast/control_flow.rb', line 465

def pattern
  @pattern
end

#valueObject

Returns the value of attribute value.



465
466
467
# File 'lib/rubinius/ast/control_flow.rb', line 465

def value
  @value
end

Instance Method Details

#bytecode(g) ⇒ Object



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
# File 'lib/rubinius/ast/control_flow.rb', line 473

def bytecode(g)
  pos(g)

  @pattern.bytecode(g)
  @value.bytecode(g)
  g.send :=~, 1
  if @pattern.kind_of? RegexLiteral
    regexp = Regexp.new(@pattern.source)
    # TODO: this code cannot just use Rubinius-specific methods.
    # Fix without using respond_to?.
    if regexp.respond_to? :name_table
      if table = regexp.name_table
        table.sort_by { |name, idx| idx }.each do |name, idx|
          local = g.state.scope.new_local name
          g.last_match 5, idx.last - 1

          case local
          when Compiler::LocalVariable
            g.set_local local.slot
          when Compiler::EvalLocalVariable
            g.push_variables
            g.swap
            g.push_literal name
            g.swap
            g.send :set_eval_local, 2, false
          else
            raise CompileError, "unknown type of local #{local.inspect}"
          end

          g.pop
        end
      end
    end
  end
end

#to_sexpObject



509
510
511
# File 'lib/rubinius/ast/control_flow.rb', line 509

def to_sexp
  [:match2, @pattern.to_sexp, @value.to_sexp]
end