Class: DoNotation::Rewriter

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

Instance Method Summary collapse

Instance Method Details

#process(exp) ⇒ Object

Raises:

  • (ArgumentError)


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

def process exp
  raise ArgumentError, "expected :iter, got #{exp[0].inspect}"                             unless exp[0] == :iter
  raise ArgumentError, "expected s(:call, nil, :proc, s(:arglist)), got #{exp[1].inspect}" unless exp[1] == s(:call, nil, :proc, s(:arglist))
  raise ArgumentError, "expected nil, got #{exp[2].inspect}"                               unless exp[2] == nil

  if exp[3].is_a?(Sexp) and exp[3][0] == :block
    iter, call, nil_val, block = exp.shift, exp.shift, exp.shift, exp.shift
    raise ArgumentError, "unexpected extra syntax: #{exp.inspect}" unless exp.empty?
    s(iter, call, nil_val, *rewrite_assignments(block[1..-1]))
  else
    exp
  end
end

#rewrite_assignments(exp) ⇒ Object



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

def rewrite_assignments exp
  return [] if exp.empty?

  head = exp.shift


  if head[0] == :call and head[1] and head[1][0] == :call and head[2] == :< and head[3][0] == :arglist and head[3][1][2] == :-@
    var_name = head[1][2]
    expression = head[3][1][1]

    body = rewrite_assignments(exp)

    if body.first.is_a? Symbol
      body = [s(*body)]
    end

    [s(:iter,
       s(:call, nil, :bind, s(:arglist, expression)),
       s(:lasgn, var_name),
       *body)]
  elsif exp.empty?
    [head]
  else
    [s(:iter,
       s(:call, nil, :bind_const, s(:arglist, head)),
       nil,
       *rewrite_assignments(exp))]
  end
end