Created
August 2, 2012 21:16
-
-
Save pigoz/3240720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'ripper' | |
require 'sorcerer' | |
require 'awesome_print' | |
def ident(name, pos=[0, 0]) | |
[:@ident, name, pos] | |
end | |
def var_ref(name) | |
[:var_ref, ident(name)] | |
end | |
def binary(arg1, operator, arg2) | |
[:binary, arg1, operator, arg2] | |
end | |
def defun_sexp(function_name, params, body) | |
params_ary = [:params, params.map {|el| ident(el)}, nil, nil, nil, nil] | |
[:def, ident(function_name), | |
[:paren, params_ary], | |
[:bodystmt, [body], nil, nil, nil] | |
] | |
end | |
def program(program) | |
[:program, [program]] | |
end | |
target = <<RUBY | |
def sum(a,b) | |
a + b | |
end | |
RUBY | |
ap Ripper.sexp(target) | |
puts "########################################################################" | |
puts "########################################################################" | |
puts "########################################################################" | |
binaryop = binary(var_ref('a'), :+ , var_ref('b')) | |
sexp = program(defun_sexp('sum', ['a', 'b'], binaryop)) | |
ap sexp | |
puts Sorcerer.source(sexp, multiline: true) | |
puts Sorcerer.source(Ripper.sexp(target), multiline: true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment