Class: CodeTools::AST::DynamicString

Inherits:
StringLiteral show all
Defined in:
lib/rubinius/code/ast/literals.rb

Direct Known Subclasses

DynamicExecuteString, DynamicRegex, DynamicSymbol

Instance Attribute Summary collapse

Attributes inherited from StringLiteral

#string

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, 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, str, array) ⇒ DynamicString

Returns a new instance of DynamicString.



422
423
424
425
426
# File 'lib/rubinius/code/ast/literals.rb', line 422

def initialize(line, str, array)
  @line = line
  @string = str
  @array = array
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



420
421
422
# File 'lib/rubinius/code/ast/literals.rb', line 420

def array
  @array
end

#optionsObject

Returns the value of attribute options.



420
421
422
# File 'lib/rubinius/code/ast/literals.rb', line 420

def options
  @options
end

Instance Method Details

#bytecode(g) ⇒ Object

This extensive logic is 100% for optimizing rather ridiculous edge cases for string interpolation and I (brixen) do not think it is worthwhile.

Some examples:

"#{}"
"#{} foo"
"foo #{}"
"#{}#{}"
"#{bar}"

Also, as Zocx pointed out in IRC, the following code is not compatible in Rubinius because we convert an empty evstr into “” directly in Melbourne parse tree to AST processor rather than calling #to_s on ‘nil’.

def nil.to_s; “hello”; end a = “#{}” # => “hello” in MRI

We also do not handle any case where #to_s does not actually return a String instance.



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
# File 'lib/rubinius/code/ast/literals.rb', line 451

def bytecode(g)
  pos(g)

  if @string.empty?
    if @array.size == 1
      case x = @array.first
      when StringLiteral
        x.bytecode(g)
      else
        x.bytecode(g)
        # string_build has some auto-conversion logic, use it.
        g.string_build 1
      end
      return
    end

    prefix = false
  else
    prefix = true
    g.push_literal @string
  end

  total = 0
  @array.each do |e|
    case e
    when StringLiteral
      unless e.string.empty?
        g.push_literal e.string
        total += 1
      end
    else
      e.bytecode(g)
      total += 1
    end
  end

  if prefix
    if total == 0
      g.string_dup
      return
    end
    total += 1
  else
    if total == 0
      g.push_literal ""
      g.string_dup
      return
    elsif total == 1
      g.string_build 1
      return
    end
  end

  g.string_build total
end

#defined(g) ⇒ Object



507
508
509
510
# File 'lib/rubinius/code/ast/literals.rb', line 507

def defined(g)
  g.push_literal "expression"
  g.string_dup
end

#sexp_nameObject



512
513
514
# File 'lib/rubinius/code/ast/literals.rb', line 512

def sexp_name
  :dstr
end

#to_sexpObject



516
517
518
# File 'lib/rubinius/code/ast/literals.rb', line 516

def to_sexp
  @array.inject([sexp_name, @string]) { |s, x| s << x.to_sexp }
end