Permalink
Browse files

added support for arrays and hashes. Still no symbols.

  • Loading branch information...
1 parent 7b1c6b0 commit b84a9e59273d77babe050f1a2e4d900d00545f91 @mattknox committed May 10, 2010
Showing with 15 additions and 7 deletions.
  1. +3 −2 bin/dev.rb
  2. +10 −4 lib/8ball_visitor.rb
  3. +2 −1 spec/8ball_spec.rb
View
@@ -18,6 +18,7 @@ def reload(f)
v = EightBallVisitor.new
a = e.parse 'lambda {|x| x + 1}'
b = e.parse 'Proc.new {|x| x + 1 }'
-d = e.parse 'class Foo < Fixnum;def bar; 1+2;end;end'
+d = e.parse '{1 => "foo", "bar" => true, false => [2]}'
+
x = a.child_nodes.first.next_node
-a.accept v
+d.accept v
View
@@ -68,17 +68,23 @@ def visit_const_node(node)
"(#{node.get_name})"
end
+ def visit_array_node(node)
+ "([#{node.child_nodes.map { |x| visit(x) }.join(', ')}])"
+ end
+
+ def visit_hash_node(node)
+ acc = []
+ node.first_child.child_nodes.each_slice(2) {|x,y| acc << "#{visit(x)} => #{visit(y)}"}
+ "({#{acc.join(', ')}})"
+ end
+
# def visitClassNode(node)
# gather("this.define_class(\"#{node.getCPath.getName}\",",
# "function() {",
# visit(node.bodyNode),
# "})")
# end
- # def visitDefnNode(node)
- # compile_function(node.get_name, node.args_node.args, node.body_node)
- # end
-
def visitArgsNode(node)
node.args.child_nodes.to_a.map {|x| x.name}.join(", ")
end
View
@@ -16,9 +16,10 @@
cs('Proc.new {|x| x + 1}').should == "(function (x){\nreturn (x.rubyplus((1)));\n}\n)"
cs('/foo/').should == "(/foo/)"
cs('Foo').should == "(Foo)"
- cs('class Foo < Fixnum;def bar; 1+2;end;end').should == "Class.define(Foo, Fixnum, function bar (){\nreturn ((1).rubyplus((2)));\n}\n)"
+ cs('class Foo < Fixnum;def bar;1+2;end;end').should == "Class.define(Foo, Fixnum, function bar (){\nreturn ((1).rubyplus((2)));\n}\n)"
cs('puts "hello"').should == "EightBall.puts(('hello'));"
cs("'foo'.length").should == "('foo').length()"
+ cs("[2,3]").should == "([(2), (3)])"
cs("").should == ""
end

0 comments on commit b84a9e5

Please sign in to comment.