Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save francescoagati/406841 to your computer and use it in GitHub Desktop.
Save francescoagati/406841 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'v8'
require 'johnson'
require 'ruby_parser'
sexp=Johnson::Parser.parse %{
var sum=function sum (x,y) {
return x+y
};
sum(1,1);
}
V8::Context.open do |context|
context["sexp"]=Johnson::Parser.parse %{
var sum=function sum (x,y) {
return x+y
};
sum(1,1);
}
puts context.eval %{
sexp
}
end
#risultato sexp javascript
#[[:var, [[:assign, [:name, "sum"], [:func_expr, "sum", ["x", "y"], [[:return, [:op_add, [:name, "x"], [:name, "y"]]]]]]]], [:function_call, [[:name, "sum"], [:lit, 1], [:lit, 1]]]]
puts "-----------------------"
p RubyParser.new.parse %{
def sum(x,y)
x.length
return x + y
end
sum(1,1)
}
#risultato sexp ruby
#s(:block, s(:defn, :sum, s(:args, :x, :y), s(:scope, s(:block, s(:call, s(:lvar, :x), :length, s(:arglist)), s(:return, s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))))), s(:call, nil, :sum, s(:arglist, s(:lit, 1), s(:lit, 1))))
@francescoagati
Copy link
Author

and the result:

3
[[:var, [[:assign, [:name, "sum"], [:func_expr, "sum", ["x", "y"], [[:return, [:op_add, [:name, "x"], [:name, "y"]]]]]]]], [:function_call, [[:name, "sum"], [:lit, 1], [:lit, 1]]]]
s(:block, s(:defn, :sum, s(:args, :x, :y), s(:scope, s(:block, s(:call, s(:lvar, :x), :length, s(:arglist)), s(:return, s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))))), s(:call, nil, :sum, s(:arglist, s(:lit, 1), s(:lit, 1))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment