Class: Ionize::Php::Translate::FunctionArgs

Inherits:
Translator
  • Object
show all
Includes:
Debug
Defined in:
lib/ionize/translate/function_args.rb

Constant Summary

Constants inherited from Translator

Translator::SimpleTransforms

Instance Attribute Summary collapse

Attributes inherited from Translator

#parent

Instance Method Summary collapse

Methods included from Debug

#debug, #logger

Methods inherited from Translator

handle_node, #handle_num, #initialize, #send, #transform

Constructor Details

This class inherits a constructor from Ionize::Php::Translate::Translator

Instance Attribute Details

#assignsObject

Returns the value of attribute assigns.



7
8
9
# File 'lib/ionize/translate/function_args.rb', line 7

def assigns
  @assigns
end

Instance Method Details

#handle_a_variable(node) ⇒ Object



37
38
39
40
# File 'lib/ionize/translate/function_args.rb', line 37

def handle_a_variable(node)          
  debug "Variable inside function args #{node.inspect}"
  transform(node.first)
end

#handle_double_quoted_string(node) ⇒ Object



56
57
58
# File 'lib/ionize/translate/function_args.rb', line 56

def handle_double_quoted_string(node)
  [:str, node]
end

#handle_fun_args_within_call(node) ⇒ Object

args ###



79
80
81
82
83
84
85
86
87
88
# File 'lib/ionize/translate/function_args.rb', line 79

def handle_fun_args_within_call(node)
  args = node.first.to_a.first.last
  result = transform(args)
  debug "Fun args within call result #{result.inspect}"
  if result.empty?
    nil
  else
    [:array] + result
  end
end

#handle_multiple_args(node) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ionize/translate/function_args.rb', line 115

def handle_multiple_args(node)
  debug "Multiple args on FunctionArgs side #{node.inspect}"

  # Clear the assignment statements and perform the transformation
  self.assigns = {}
  result = transform(node.select {|c| c.values.first != "," })
  
  # Append the assignment statements spoken of in "term_assign"
  unless self.assigns.empty?
    result.flatten + [[:block, self.assigns.values.first]]
  else
    result.flatten
  end
end

#handle_no_args(node) ⇒ Object



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

def handle_no_args(node)
  [:args]
end

#handle_reference_term(node) ⇒ Object



42
43
44
45
46
# File 'lib/ionize/translate/function_args.rb', line 42

def handle_reference_term(node)
  # For now, I say screw references..
  transform(node.second)
  # TODO: But we might change this in the future.. Who knows.
end

#handle_regular_fun_call(node) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ionize/translate/function_args.rb', line 60

def handle_regular_fun_call(node)
  debug "Regular fun call #{node.inspect}"
  name = transform(node.first).to_sym
  if node.third.keys == [:no_args]
    [:fcall, name]
  else
    args = transform(node.third)
    if args.nil?
      [:fcall, name]
    else
      [:fcall, name, args]
    end
  end
end

#handle_single_arg(node) ⇒ Object



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

def handle_single_arg(node)
  transform(node)
end

#handle_single_quoted_string(node) ⇒ Object

literals ###



52
53
54
# File 'lib/ionize/translate/function_args.rb', line 52

def handle_single_quoted_string(node)
  [:str, node]
end

#handle_term(node) ⇒ Object



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

def handle_term(node)
  transform(node)
end

#handle_term_assign(node) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ionize/translate/function_args.rb', line 90

def handle_term_assign(node)
  term, op, expr = *node
  debug "Term assign #{node.inspect}"
  result   = transform(expr)
  variable = transform(term)

  # Okay, so this is kind of crazy but, in Ruby,  if you ever use the 
  # assignment operation inside the formal parameters, it creates
  # a block inside the function header AST. Run a ParseTree on
  # on a method which has defaults to see what I mean.
  
  # AFAICT, the most efficient way to handle this crazy *#$@-ing case
  # is to store the assignments later and only give back just the 
  # variable being assigned. The assignment statements are appended 
  # later in the transform 
  
  self.assigns[variable] = if result.is_a? Array
                             [:lasgn, variable, result]
                           else
                             [:lasgn, variable, [result]]
                           end
  
  variable
end

#handle_user_constant(node) ⇒ Object



25
26
27
# File 'lib/ionize/translate/function_args.rb', line 25

def handle_user_constant(node)
  [:const, transform(node).to_sym]
end

#handle_variable(node) ⇒ Object

Perhaps these two can be cleaned up? Not sure..



32
33
34
35
# File 'lib/ionize/translate/function_args.rb', line 32

def handle_variable(node)
  debug "Variable #{node.inspect}"
  node.first.gsub("$", "").to_sym
end

#handle_word(node) ⇒ Object



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

def handle_word(node)
  node
end