Module: Ionize::Php::Translate::Rewritable

Included in:
Rewrites
Defined in:
lib/ionize/translate/rewritable.rb

Instance Method Summary collapse

Instance Method Details

#fcall_rewritable?(name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ionize/translate/rewritable.rb', line 17

def fcall_rewritable?(name)
  respond_to? "rewrite_fcall_#{name}"
end

#handle_regular_fun_call(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ionize/translate/rewritable.rb', line 25

def handle_regular_fun_call(node)
  debug "Regular fun call #{node.inspect}"
  name = parent.transform(node.first).to_sym
  
  # TODO: Simplify this condition..
  result = if node.third[:fun_args_within_call] == [{:no_args => []}] or 
               node.third[:fun_args_within_call] == [{:no_args_within_call=>[]}]
             args = nil
             [:fcall, name]
           else
             args = parent.transform(node.third)
             [:fcall, name, args]
           end

  if fcall_rewritable? name
    rewrite_fcall(name, args)
  else
    result
  end
end

#handle_term_assign(node) ⇒ Object



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
# File 'lib/ionize/translate/rewritable.rb', line 100

def handle_term_assign(node)
  debug "Term assign node is #{node.inspect}"
  
  if node.first.node_type == :oo_variable
    return handle_term_assign_oo_variable(node)
  elsif node.first.node_type == :oo_array_lookup
    return handle_term_assign_oo_array_lookup(node)
  elsif node.first.node_type == :oo_variable_variable
    return handle_term_assign_oo_variable_variable(node)
  elsif node.first.node_type == :array_lookup
    return handle_term_assign_array_lookup(node)
  elsif node.first.node_type == :array_append
    return handle_term_assign_array_append(node)
  end

  term, op, expr = *node
  var = parent.transform(term).last
  debug "Term assign #{term.inspect}"

  expr = parent.transform(expr)
  if expr.is_a? Array
    if expr.first == :fcall and lasgn_rewritable? expr.second
      rewrite_lasgn(expr.second, var, expr)
    else
      [:lasgn, var, expr]
    end
  else
    [:lasgn, var, [expr]]
  end          
end

#handle_term_assign_array_append(node) ⇒ Object



96
97
98
# File 'lib/ionize/translate/rewritable.rb', line 96

def handle_term_assign_array_append(node)
  [:call, transform(node.first), :<<, [:array, transform(node.third)]]
end

#handle_term_assign_array_lookup(node) ⇒ Object



90
91
92
93
94
# File 'lib/ionize/translate/rewritable.rb', line 90

def handle_term_assign_array_lookup(node)
  term = transform(node.first)
  expr = transform(node.third)
  [:attrasgn] + [term[1]] + [:[]=] + [term[3] + [expr]]
end

#handle_term_assign_oo_array_lookup(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ionize/translate/rewritable.rb', line 68

def handle_term_assign_oo_array_lookup(node)
  debug "Term assign oo array lookup is #{node.inspect}"
  
  term = transform(node.first)
  expr = transform(node.third)
  
  debug "Term is #{term.inspect}"
  
  [:attrasgn] + 
    [term.second] + 
    [:[]=] + 
    [term.fourth + [expr]]
end

#handle_term_assign_oo_variable(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ionize/translate/rewritable.rb', line 46

def handle_term_assign_oo_variable(node)
  debug "The oo assign node is #{node.inspect}"
  # TODO: Pretty this up some.. If possible..
  object = parent.transform(node.first)

  debug "The object to be set is #{object.inspect}"
  object.shift # discard leading :call

  attribute = object.pop
  debug "The attribute to be set is #{attribute.inspect}"
  
  assigned = attribute.to_s + "=" # pop off assigned to var
  debug "Before transforming, the expression to be set is #{node.third.inspect}"
  expr = parent.transform(node.third)
  debug "The expression to be set is #{expr.inspect}"
  
  # TODO: Move this rewrite to the Rewrites class
  object = (if object.first == [:lvar, :this] then [:self] else object.first end)

  [:attrasgn, object, assigned.to_id, [:array, expr]]          
end

#handle_term_assign_oo_variable_variable(node) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/ionize/translate/rewritable.rb', line 82

def handle_term_assign_oo_variable_variable(node)
  debug "Term assign oo variable variable is #{node.inspect}"
  term = transform(node.first)
  expr = transform(node.third)
  debug "Term inside oo variable variable is #{term.inspect}"
  term[0..1]  + [:set] + [term[3] + [expr]]
end

#lasgn_rewritable?(name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ionize/translate/rewritable.rb', line 21

def lasgn_rewritable?(name)
  respond_to? "rewrite_lasgn_#{name}"
end

#rewrite_fcall(name, args) ⇒ Object



9
10
11
# File 'lib/ionize/translate/rewritable.rb', line 9

def rewrite_fcall(name, args)          
  __send__("rewrite_fcall_#{name}", args)
end

#rewrite_lasgn(name, var, expr) ⇒ Object



13
14
15
# File 'lib/ionize/translate/rewritable.rb', line 13

def rewrite_lasgn(name, var, expr)
  __send__("rewrite_lasgn_#{name}", var, expr)
end

#transform(*args) ⇒ Object



5
6
7
# File 'lib/ionize/translate/rewritable.rb', line 5

def transform(*args)
  parent.transform(*args)
end