Skip to content

Instantly share code, notes, and snippets.

@JacobNinja
Created August 30, 2012 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobNinja/3537082 to your computer and use it in GitHub Desktop.
Save JacobNinja/3537082 to your computer and use it in GitHub Desktop.
Rubinius parser confusion
# Here's the code being evaluated
# rubinius-2.0.0dev
html_class = build_class("") {|c| c << "required" if required }
# => "required"
### FIRST EXAMPLE
# Symbolic expression tree with no arguments to build_class
'html_class = build_class() {|c| c << "required" if required }'.to_sexp
# => [:lasgn, :html_class,
# [:iter,
# [:call, nil, :build_class, [:arglist]],
# [:lasgn, :c], [:if, [:call, nil, :required, nil], [:call, [:lvar, :c], :<<, [:arglist, [:str, "required"]]], nil]]]
# The block becomes the second argument of `iter` like expected
### SECOND EXAMPLE
# Symbolic expression tree with 1 argument to build_class
'html_class = build_class("") {|c| c << "required" if required }'.to_sexp
# => [:lasgn, :html_class,
# [:iter,
# [:call, nil, :build_class, [:arglist, [:str, ""], [:iter, [:lasgn, :c], [:if, [:call, nil, :required, nil], [:call, [:lvar, :c], :<<, [:arglist, [:str, "required"]]], nil]]]],
# [:lasgn, :c], [:if, [:call, nil, :required, nil], [:call, [:lvar, :c], :<<, [:arglist, [:str, "required"]]], nil]]]
# Why is the block repeated in the second example? It becomes a parameter to `arglist` and `iter` with the presence of a method argument
@steveklabnik
Copy link

No idea.

@steveklabnik
Copy link

From #rubinius:

14:49 < brixen> he should be looking at the AST output
14:49 < brixen> if there is confusion
14:50 < brixen> the sexp output is a hack on top of the AST
14:50 < brixen> that aside, I don't understand his question

@JacobNinja
Copy link
Author

Thanks Steve. I'll hop on

@JacobNinja
Copy link
Author

I found the issue in the #to_sexp output. I'll look into using the AST nodes instead

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