Permalink
Browse files

converting switch statement to ecma

git-svn-id: svn+ssh://rubyforge.org/var/svn/johnson/trunk@111 54575175-8111-4fdf-a583-07ff49f40e23
  • Loading branch information...
1 parent be039d7 commit fc1a3e89b3e4910dc51c728db387af1218bc6394 aaronp committed Apr 1, 2008
Showing with 31 additions and 0 deletions.
  1. +12 −0 lib/johnson/visitors/ecma_visitor.rb
  2. +1 −0 test/johnson/nodes/semi_test.rb
  3. +18 −0 test/johnson/nodes/switch_test.rb
@@ -97,6 +97,18 @@ def visit_Return(o)
"return#{o.value && ' '}#{o.value && o.value.accept(self)}"
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)
"#{o.left.accept(self)}: #{o.right.accept(self)}"
end
@@ -3,5 +3,6 @@
class SemiTest < Johnson::NodeTestCase
def test_null_semi
assert_sexp([], @parser.parse(';'))
+ assert_ecma('', @parser.parse(';'))
end
end
@@ -5,6 +5,9 @@ def test_empty_switch
assert_sexp([[:switch, [:name, "o"], []]],
@parser.parse('switch(o) { }')
)
+ assert_ecma('switch(o) { }',
+ @parser.parse('switch(o) { }')
+ )
end
def test_switch_with_body
@@ -14,6 +17,15 @@ def test_switch_with_body
]
]], @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
def test_switch_with_body_2_case
@@ -30,8 +42,14 @@ def test_switch_with_default
assert_sexp([[:switch, [:name, "o"], [[:default, nil, [[:name, "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, []]]]],
@parser.parse('switch(o) { default: }')
)
+ assert_ecma("switch(o) {\n default: { }\n}",
+ @parser.parse('switch(o) { default: }')
+ )
end
end

0 comments on commit fc1a3e8

Please sign in to comment.