Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'ruby_parser'
- require 'sexp_processor'
- class Ruby2PHP < SexpProcessor
- # Nodes that represent assignment and probably need () around them.
- ASSIGN_NODES = [
- :dasgn,
- :flip2,
- :flip3,
- :lasgn,
- :masgn,
- :attrasgn,
- :op_asgn1,
- :op_asgn2,
- :op_asgn_and,
- :op_asgn_or,
- :return,
- ]
- def initialize
- super
- @indent = " "
- self.auto_shift_type = true
- self.strict = true
- self.expected = String
- end
- def process_arglist(exp)
- code = []
- until exp.empty? do
- code << process(exp.shift)
- end
- code.join(', ')
- end
- def process_attrasgn(exp)
- receiver = process exp.shift
- name = exp.shift
- args = exp.empty? ? nil : exp.shift
- case name
- when :[]= then
- rhs = process args.pop
- "#{receiver}[#{process(args)}] = #{rhs}"
- else
- name = name.to_s.sub(/=$/, '')
- if args && args != s(:arglist) then
- "#{receiver}.#{name} = #{process(args)}"
- end
- end
- end
- def process_block(exp)
- result = []
- exp << nil if exp.empty?
- until exp.empty? do
- code = exp.shift
- if code.nil? or code.first == :nil then
- result << "# do nothing"
- else
- result << process(code)
- end
- end
- result = result.join("\n")
- result = case self.context[1]
- when nil, :scope, :if, :iter, :resbody, :when, :while then
- result + "\n"
- else
- "(#{result})"
- end
- self.context.size == 1 ? result + ';' : result
- end
- def process_call(exp)
- receiver_node_type = exp.first.nil? ? nil : exp.first.first
- receiver = process exp.shift
- # receiver = "(#{receiver})" if
- # Ruby2PHP::ASSIGN_NODES.include? receiver_node_type
- name = exp.shift
- args = exp.shift rescue nil
- result = case name
- when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
- "(#{receiver} #{name} #{process args})"
- when :[] then
- receiver = "self" if receiver.nil?
- "#{receiver}[#{process args}]"
- when :[]= then
- receiver = "self" if receiver.nil?
- lhs = args.pop
- "#{receiver}[#{process args}] = #{process lhs}"
- when :"-@" then
- "-#{receiver}"
- when :"+@" then
- "+#{receiver}"
- else
- args = process args
- args = nil if args.empty?
- args = "#{args}" if args
- receiver = "#{receiver}->" if receiver
- "#{receiver}#{name}(#{args})"
- end
- self.context.size == 1 ? result + ';' : result
- end
- def process_iasgn(exp)
- result = '$this->' + exp.shift.to_s[1..-1]
- result += " = #{process(exp.shift)}" unless exp.empty?
- self.context.size == 1 ? result + ';' : result
- end
- def process_ivar(exp)
- '$this->' + exp.shift.to_s[1..-1]
- end
- def process_lasgn(exp)
- s = '$' + exp.shift.to_s
- s += " = #{process exp.shift};" unless exp.empty?
- s
- end
- def process_lvar(exp)
- '$' + exp.shift.to_s
- end
- def process_lit(exp)
- result = exp.shift.to_s
- self.context.size == 1 ? result + ';' : result
- end
- def process_str(exp)
- # FIXME don’t dump
- result = exp.shift.dump
- self.context.size == 1 ? result + ';' : result
- end
- end
Add Comment
Please, Sign In to add comment