Skip to content

Commit b129538

Browse files
committed
New RubyVisitor.compile
1 parent b80c9ff commit b129538

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

lib/syntax_tree/visitor/compiler.rb

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class RubyVisitor < BasicVisitor
5656
class CompilationError < StandardError
5757
end
5858

59+
# This will attempt to compile the given node. If it's possible, then
60+
# it will return the compiled object. Otherwise it will return nil.
61+
def self.compile(node)
62+
node.accept(new)
63+
rescue CompilationError
64+
end
65+
5966
def visit_array(node)
6067
visit_all(node.contents.parts)
6168
end
@@ -997,27 +1004,29 @@ def visit_args(node)
9971004
end
9981005

9991006
def visit_array(node)
1000-
builder.duparray(node.accept(RubyVisitor.new))
1001-
rescue RubyVisitor::CompilationError
1002-
length = 0
1007+
if compiled = RubyVisitor.compile(node)
1008+
builder.duparray(compiled)
1009+
else
1010+
length = 0
10031011

1004-
node.contents.parts.each do |part|
1005-
if part.is_a?(ArgStar)
1006-
if length > 0
1007-
builder.newarray(length)
1008-
length = 0
1009-
end
1012+
node.contents.parts.each do |part|
1013+
if part.is_a?(ArgStar)
1014+
if length > 0
1015+
builder.newarray(length)
1016+
length = 0
1017+
end
10101018

1011-
visit(part.value)
1012-
builder.concatarray
1013-
else
1014-
visit(part)
1015-
length += 1
1019+
visit(part.value)
1020+
builder.concatarray
1021+
else
1022+
visit(part)
1023+
length += 1
1024+
end
10161025
end
1017-
end
10181026

1019-
builder.newarray(length) if length > 0
1020-
builder.concatarray if length > 0 && length != node.contents.parts.length
1027+
builder.newarray(length) if length > 0
1028+
builder.concatarray if length > 0 && length != node.contents.parts.length
1029+
end
10211030
end
10221031

10231032
def visit_assign(node)
@@ -1105,9 +1114,11 @@ def visit_backref(node)
11051114
end
11061115

11071116
def visit_bare_assoc_hash(node)
1108-
builder.duphash(node.accept(RubyVisitor.new))
1109-
rescue RubyVisitor::CompilationError
1110-
visit_all(node.assocs)
1117+
if compiled = RubyVisitor.compile(node)
1118+
builder.duphash(compiled)
1119+
else
1120+
visit_all(node.assocs)
1121+
end
11111122
end
11121123

11131124
def visit_binary(node)

0 commit comments

Comments
 (0)