Class: Rewriter

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/do_notation/rewriter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pp(obj, indent = '') ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/do_notation/rewriter.rb', line 45

def self.pp(obj, indent='')
  return obj.inspect unless obj.is_a? Array
  return '()' if obj.empty?

  str = '(' + pp(obj.first, indent + ' ')

  if obj.length > 1
    str << ' '

    next_indent = indent + (' ' * str.length)

    str << obj[1..-1].map{ |o| pp(o, next_indent) }.join("\n#{next_indent}")
  end

  str << ')'

  str
end

Instance Method Details

#process_bmethod(exp) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/do_notation/rewriter.rb', line 2

def process_bmethod exp
  type = exp.shift

  exp.shift # throw away arguments

  body = process(exp.shift)

  if body[0] == :block
     body.shift
  else
     body = s([*body])
  end

  s(:iter,  s(:fcall, :proc),  nil, *rewrite_assignments(body))
end

#rewrite_assignments(exp) ⇒ Object



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
# File 'lib/do_notation/rewriter.rb', line 18

def rewrite_assignments exp
  return [] if exp.empty?

  head = exp.shift

  if head.first == :call and head[1].first == :vcall and head[2] == :< and head[3].first == :array and head[3][1].last == :-@
    var_name = head[1][1]
    expression = head[3][1][1]
  
    body = rewrite_assignments(exp)
  
    if body.first.is_a? Symbol
      body = [s(*body)]
    end
  
    [s(:iter,
       s(:call, process(expression), :bind),
       s(:dasgn_curr, var_name),
       *body)]
  elsif exp.empty?
    [process(head)]
  else
    [s(:iter, s(:call, process(head)  , :bind_const), nil , 
           *rewrite_assignments(exp)) ]
  end
end