Guest User

Untitled

a guest
Jul 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'ruby_parser'
  4. require 'sexp_processor'
  5.  
  6. class Ruby2PHP < SexpProcessor
  7.  
  8. # Nodes that represent assignment and probably need () around them.
  9. ASSIGN_NODES = [
  10. :dasgn,
  11. :flip2,
  12. :flip3,
  13. :lasgn,
  14. :masgn,
  15. :attrasgn,
  16. :op_asgn1,
  17. :op_asgn2,
  18. :op_asgn_and,
  19. :op_asgn_or,
  20. :return,
  21. ]
  22.  
  23. def initialize
  24. super
  25.  
  26. @indent = " "
  27.  
  28. self.auto_shift_type = true
  29. self.strict = true
  30. self.expected = String
  31. end
  32.  
  33. def process_arglist(exp)
  34. code = []
  35. until exp.empty? do
  36. code << process(exp.shift)
  37. end
  38. code.join(', ')
  39. end
  40.  
  41. def process_attrasgn(exp)
  42. receiver = process exp.shift
  43. name = exp.shift
  44. args = exp.empty? ? nil : exp.shift
  45.  
  46. case name
  47. when :[]= then
  48. rhs = process args.pop
  49. "#{receiver}[#{process(args)}] = #{rhs}"
  50. else
  51. name = name.to_s.sub(/=$/, '')
  52. if args && args != s(:arglist) then
  53. "#{receiver}.#{name} = #{process(args)}"
  54. end
  55. end
  56. end
  57.  
  58. def process_block(exp)
  59. result = []
  60.  
  61. exp << nil if exp.empty?
  62. until exp.empty? do
  63. code = exp.shift
  64. if code.nil? or code.first == :nil then
  65. result << "# do nothing"
  66. else
  67. result << process(code)
  68. end
  69. end
  70.  
  71. result = result.join("\n")
  72.  
  73. result = case self.context[1]
  74. when nil, :scope, :if, :iter, :resbody, :when, :while then
  75. result + "\n"
  76. else
  77. "(#{result})"
  78. end
  79.  
  80. self.context.size == 1 ? result + ';' : result
  81. end
  82.  
  83. def process_call(exp)
  84. receiver_node_type = exp.first.nil? ? nil : exp.first.first
  85. receiver = process exp.shift
  86.  
  87. # receiver = "(#{receiver})" if
  88. # Ruby2PHP::ASSIGN_NODES.include? receiver_node_type
  89.  
  90. name = exp.shift
  91. args = exp.shift rescue nil
  92.  
  93. result = case name
  94. when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
  95. "(#{receiver} #{name} #{process args})"
  96. when :[] then
  97. receiver = "self" if receiver.nil?
  98. "#{receiver}[#{process args}]"
  99. when :[]= then
  100. receiver = "self" if receiver.nil?
  101. lhs = args.pop
  102. "#{receiver}[#{process args}] = #{process lhs}"
  103. when :"-@" then
  104. "-#{receiver}"
  105. when :"+@" then
  106. "+#{receiver}"
  107. else
  108. args = process args
  109. args = nil if args.empty?
  110. args = "#{args}" if args
  111. receiver = "#{receiver}->" if receiver
  112.  
  113. "#{receiver}#{name}(#{args})"
  114. end
  115.  
  116. self.context.size == 1 ? result + ';' : result
  117. end
  118.  
  119. def process_iasgn(exp)
  120. result = '$this->' + exp.shift.to_s[1..-1]
  121. result += " = #{process(exp.shift)}" unless exp.empty?
  122.  
  123. self.context.size == 1 ? result + ';' : result
  124. end
  125.  
  126. def process_ivar(exp)
  127. '$this->' + exp.shift.to_s[1..-1]
  128. end
  129.  
  130. def process_lasgn(exp)
  131. s = '$' + exp.shift.to_s
  132. s += " = #{process exp.shift};" unless exp.empty?
  133. s
  134. end
  135.  
  136. def process_lvar(exp)
  137. '$' + exp.shift.to_s
  138. end
  139.  
  140. def process_lit(exp)
  141. result = exp.shift.to_s
  142.  
  143. self.context.size == 1 ? result + ';' : result
  144. end
  145.  
  146. def process_str(exp)
  147. # FIXME don’t dump
  148. result = exp.shift.dump
  149.  
  150. self.context.size == 1 ? result + ';' : result
  151. end
  152.  
  153. end
Add Comment
Please, Sign In to add comment