public
Last active

Rubinius AST transform by sublassing Melbourne

  • Download Gist
irb_session.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
# The goal is to not use the regular Symbol#to_proc
# (which is to_proc method in the Symbol class)
# but rather use an Abstract Syntax Tree transformation to
# transform map(&:to_s) into map {|x| x.to_s}
# This can be achieved by using the flexible compiler architecture
# in Rubinius and is presented below.
#
# First we undefine the regular Symbol#to_proc
 
class Symbol
def to_proc
raise "undefined"
end
end
 
# Make sure it doesn't work
rbx-head :006 > [1,2].map(&:to_s)
ArgumentError: Unable to convert :to_s to a Proc
from Proc.__from_block__ at kernel/common/proc.rb:12
from { } in Object#irb_binding at (irb):6
from Rubinius::BlockEnvironment#call_on_instance at kernel/common/block_environment.rb:72
 
# OK, now to see what we need to transform in terms of AST
# we create the AST for map(&:to_s)
 
rbx-head :004 > Rubinius::Melbourne.parse_string("map(&:to_s)").ascii_graph
Send
@name: map
@privately: true
@check_for_local: false
@vcall_style: false
@line: 1
@receiver: \
Self
@line: 1
@block: \
BlockPass
@line: 1
@body: \
SymbolLiteral
@value: to_s
@line: 1
 
# and AST for our desired form: map {|x| x.to_s}
 
rbx-head :005 > Rubinius::Melbourne.parse_string("map {|x| x.to_s}").ascii_graph
Send
@name: map
@privately: true
@check_for_local: false
@vcall_style: false
@line: 1
@receiver: \
Self
@line: 1
@block: \
Iter
@line: 1
@arguments: \
IterArguments
@arity: 1
@optional: 0
@required_args: 1
@splat_index: nil
@splat: nil
@prelude: single
@line: 1
@block: nil
@arguments: \
LocalVariableAssignment
@value: nil
@name: x
@variable: nil
@line: 1
@body: \
Send
@name: to_s
@privately: false
@check_for_local: false
@vcall_style: false
@block: nil
@line: 1
@receiver: \
LocalVariableAccess
@name: x
@variable: nil
@line: 1
 
# Then we define a custom processor for AST::BlockPass
# which will transform map(&:to_s) into map {|x| x.to_s}
# directly on the AST
 
class Macros < Melbourne
def process_block_pass(line, method_send, body)
if body.class == AST::SymbolLiteral
# Symbol#to_proc
name = body.value
arguments = AST::IterArguments.new(line, AST::LocalVariableAssignment.new(line, :x, nil))
receiver = AST::LocalVariableAccess.new line, :x
body = AST::Send.new line, receiver, body.value
iter = AST::Iter.new line, arguments, body
super(line, method_send, iter)
else
super
end
end
end
 
# We can try it out first:
 
rbx-head :006 > Rubinius::Macros.parse_string("map(&:to_s)").ascii_graph
Send
@name: map
@privately: true
@check_for_local: false
@vcall_style: false
@line: 1
@receiver: \
Self
@line: 1
@block: \
Iter
@line: 1
@arguments: \
IterArguments
@arity: 1
@optional: 0
@required_args: 1
@splat_index: nil
@splat: nil
@prelude: single
@line: 1
@block: nil
@arguments: \
IterArguments
@arity: 1
@optional: 0
@required_args: 1
@splat_index: nil
@splat: nil
@prelude: single
@line: 1
@block: nil
@arguments: \
LocalVariableAssignment
@value: nil
@name: x
@variable: nil
@line: 1
@body: \
Send
@name: to_s
@privately: false
@check_for_local: false
@vcall_style: false
@block: nil
@line: 1
@receiver: \
LocalVariableAccess
@name: x
@variable: nil
@line: 1
 
# Woohoo, we can see that our map(&:to_s) AST was transformed into the desired map {|x| x.to_s} form.
# Does it work ?, Let's see:
 
rbx-head :007 > [1,2].map(&:to_s)
=> ["1", "2"]
 
:))
 
If you want to learn more read about the bytecode compiler here:
 
http://rubini.us/doc/en/bytecode-compiler/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.