Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 358d029

Browse files
committed
Better handle visit_string_parts
1 parent b022d56 commit 358d029

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

lib/syntax_tree/visitor/compiler.rb

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,8 @@ def visit_heredoc(node)
16361636
elsif node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
16371637
visit(node.parts.first)
16381638
else
1639-
visit_string_parts(node)
1640-
builder.concatstrings(node.parts.length)
1639+
length = visit_string_parts(node)
1640+
builder.concatstrings(length)
16411641
end
16421642
end
16431643

@@ -2026,10 +2026,9 @@ def visit_rational(node)
20262026
def visit_regexp_literal(node)
20272027
builder.putobject(node.accept(RubyVisitor.new))
20282028
rescue RubyVisitor::CompilationError
2029-
visit_string_parts(node)
2030-
20312029
flags = RubyVisitor.new.visit_regexp_literal_flags(node)
2032-
builder.toregexp(flags, node.parts.length)
2030+
length = visit_string_parts(node)
2031+
builder.toregexp(flags, length)
20332032
end
20342033

20352034
def visit_rest_param(node)
@@ -2086,8 +2085,8 @@ def visit_string_literal(node)
20862085
if node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
20872086
visit(node.parts.first)
20882087
else
2089-
visit_string_parts(node)
2090-
builder.concatstrings(node.parts.length)
2088+
length = visit_string_parts(node)
2089+
builder.concatstrings(length)
20912090
end
20922091
end
20932092

@@ -2114,13 +2113,7 @@ def visit_symbols(node)
21142113
element.parts.first.is_a?(TStringContent)
21152114
builder.putobject(element.parts.first.value.to_sym)
21162115
else
2117-
length = element.parts.length
2118-
unless element.parts.first.is_a?(TStringContent)
2119-
builder.putobject("")
2120-
length += 1
2121-
end
2122-
2123-
visit_string_parts(element)
2116+
length = visit_string_parts(element)
21242117
builder.concatstrings(length)
21252118
builder.intern
21262119
end
@@ -2299,13 +2292,7 @@ def visit_word(node)
22992292
if node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
23002293
visit(node.parts.first)
23012294
else
2302-
length = node.parts.length
2303-
unless node.parts.first.is_a?(TStringContent)
2304-
builder.putobject("")
2305-
length += 1
2306-
end
2307-
2308-
visit_string_parts(node)
2295+
length = visit_string_parts(node)
23092296
builder.concatstrings(length)
23102297
end
23112298
end
@@ -2330,8 +2317,8 @@ def visit_words(node)
23302317

23312318
def visit_xstring_literal(node)
23322319
builder.putself
2333-
visit_string_parts(node)
2334-
builder.concatstrings(node.parts.length) if node.parts.length > 1
2320+
length = visit_string_parts(node)
2321+
builder.concatstrings(node.parts.length) if length > 1
23352322
builder.send(:`, 1, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE)
23362323
end
23372324

@@ -2493,6 +2480,13 @@ def push_interpolate
24932480
# heredocs, etc. This method will visit all the parts of a string within
24942481
# those containers.
24952482
def visit_string_parts(node)
2483+
length = 0
2484+
2485+
unless node.parts.first.is_a?(TStringContent)
2486+
builder.putobject("")
2487+
length += 1
2488+
end
2489+
24962490
node.parts.each do |part|
24972491
case part
24982492
when StringDVar
@@ -2504,7 +2498,11 @@ def visit_string_parts(node)
25042498
when TStringContent
25052499
builder.putobject(part.accept(RubyVisitor.new))
25062500
end
2501+
2502+
length += 1
25072503
end
2504+
2505+
length
25082506
end
25092507

25102508
# The current instruction sequence that we're compiling is always stored

0 commit comments

Comments
 (0)