Permalink
Browse files

Quick and dirty attr_accessor / attr_reader / attr_writer implementat…

…ion and minor bug fix (array index e[1] => e[2])
  • Loading branch information...
vidarh committed Sep 25, 2014
1 parent 29a0a13 commit ee85225f2c874ded66425982c5271b238dcce74e
Showing with 20 additions and 6 deletions.
  1. +20 −6 transform.rb
View
@@ -274,7 +274,6 @@ def rewrite_let_env(exp)
vars << :__tmp_proc # Used in rewrite_lambda. Same caveats as for __env_
e[3] = E[e.position,:let, vars,*e[3]]
# We store the variables by descending frequency for future use in register
# allocation.
e[3].extra[:varfreq] = freq.sort_by {|k,v| -v }.collect{|a| a.first }
@@ -346,21 +345,36 @@ def build_class_scopes(exps, scope = nil)
end
end
elsif e[0] == :call && e[1] == :attr_accessor
elsif e[0] == :call && (e[1] == :attr_accessor || e[1] == :attr_reader || e[1] == :attr_writer)
# This is a bit presumptious, assuming noone are stupid enough to overload
# attr_accessor, attr_reader without making them do more or less the same thing.
# but the right thing to do is actually to call the method.
#
# In any case there is no actual harm in allocating the vtable
# entry.`
#
# We may do a quick temporary hack to synthesize the methods,
# though, as otherwise we need to implement the full define_method
# etc.
arr = e[1].is_a?(Array) ? e[2] : [e[2]]
arr = e[2].is_a?(Array) ? e[2] : [e[2]]
arr.each {|entry|
scope.add_vtable_entry(entry.to_s[1..-1].to_sym)
}
# Then let's do the quick hack:
#
type = e[1]
syms = e[2]
e[0] = :do
e.slice!(1..-1)
syms.each do |mname|
mname = mname.to_s[1..-1].to_sym
if (type == :attr_reader || type == :attr_accessor)
e << E[:defm, mname, [], ["@#{mname}".to_sym]]
end
if (type == :attr_writer || type == :attr_accessor)
e << E[:defm, "#{mname}=".to_sym, [:value], [[:assign, "@#{mname}".to_sym, :value]]]
end
end
elsif e[0] == :class || e[0] == :module
superclass = e[2]
superc = @classes[superclass.to_sym]

0 comments on commit ee85225

Please sign in to comment.