Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
converting switch statement to ecma
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/johnson/trunk@111 54575175-8111-4fdf-a583-07ff49f40e23
  • Loading branch information
aaronp committed Apr 1, 2008
1 parent be039d7 commit fc1a3e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/johnson/visitors/ecma_visitor.rb
Expand Up @@ -97,6 +97,18 @@ def visit_Return(o)
"return#{o.value && ' '}#{o.value && o.value.accept(self)}" "return#{o.value && ' '}#{o.value && o.value.accept(self)}"
end end


def visit_Switch(o)
"switch(#{o.left.accept(self)}) #{o.right.accept(self)}"
end

def visit_Case(o)
"case #{o.left.accept(self)}: #{o.right.accept(self)}"
end

def visit_Default(o)
"default: #{o.right.accept(self)}"
end

def visit_Label(o) def visit_Label(o)
"#{o.left.accept(self)}: #{o.right.accept(self)}" "#{o.left.accept(self)}: #{o.right.accept(self)}"
end end
Expand Down
1 change: 1 addition & 0 deletions test/johnson/nodes/semi_test.rb
Expand Up @@ -3,5 +3,6 @@
class SemiTest < Johnson::NodeTestCase class SemiTest < Johnson::NodeTestCase
def test_null_semi def test_null_semi
assert_sexp([], @parser.parse(';')) assert_sexp([], @parser.parse(';'))
assert_ecma('', @parser.parse(';'))
end end
end end
18 changes: 18 additions & 0 deletions test/johnson/nodes/switch_test.rb
Expand Up @@ -5,6 +5,9 @@ def test_empty_switch
assert_sexp([[:switch, [:name, "o"], []]], assert_sexp([[:switch, [:name, "o"], []]],
@parser.parse('switch(o) { }') @parser.parse('switch(o) { }')
) )
assert_ecma('switch(o) { }',
@parser.parse('switch(o) { }')
)
end end


def test_switch_with_body def test_switch_with_body
Expand All @@ -14,6 +17,15 @@ def test_switch_with_body
] ]
]], @parser.parse('switch(o) { case j: foo; }') ]], @parser.parse('switch(o) { case j: foo; }')
) )
assert_ecma("switch(o) {\n case j: {\n foo;\n}\n}",
@parser.parse('switch(o) { case j: foo; }')
)
end

def test_switch_empty_case
assert_ecma("switch(o) {\n case j: { }\n}",
@parser.parse('switch(o) { case j: }')
)
end end


def test_switch_with_body_2_case def test_switch_with_body_2_case
Expand All @@ -30,8 +42,14 @@ def test_switch_with_default
assert_sexp([[:switch, [:name, "o"], [[:default, nil, [[:name, "bar"]]]]]], assert_sexp([[:switch, [:name, "o"], [[:default, nil, [[:name, "bar"]]]]]],
@parser.parse('switch(o) { default: bar; }') @parser.parse('switch(o) { default: bar; }')
) )
assert_ecma("switch(o) {\n default: {\n bar;\n}\n}",
@parser.parse('switch(o) { default: bar; }')
)
assert_sexp([[:switch, [:name, "o"], [[:default, nil, []]]]], assert_sexp([[:switch, [:name, "o"], [[:default, nil, []]]]],
@parser.parse('switch(o) { default: }') @parser.parse('switch(o) { default: }')
) )
assert_ecma("switch(o) {\n default: { }\n}",
@parser.parse('switch(o) { default: }')
)
end end
end end

0 comments on commit fc1a3e8

Please sign in to comment.