-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
rewriter.rb
38 lines (33 loc) · 996 Bytes
/
rewriter.rb
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
begin require 'rubygems'; rescue LoadError; end
require 'sexp'
require 'sexp_processor'
require 'unique'
require 'typed_sexp' # ensure @@array_types is defined
class Sexp
# add arglist because we introduce the new array type in this file
@@array_types << :arglist
end
##
# Rewriter (probably should be renamed) is a first-pass filter that
# normalizes some of ruby's ASTs to make them more processable later
# in the pipeline. It only has processors for what it is interested
# in, so real the individual methods for a better understanding of
# what it does.
class Rewriter < SexpProcessor
def process exp, _src = nil, _timeout = nil
super(exp)
end
def rewrite_defn(exp)
case exp.last[0]
when :ivar then
ivar = exp.pop
exp.push s(:scope, s(:block, s(:return, ivar)))
when :attrset then
var = exp.pop
exp.push s(:scope,
s(:block,
s(:return, s(:iasgn, var.last, s(:lvar, :arg)))))
end
exp
end
end