Class: Ruby2Lolz

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/ruby2lolz.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuby2Lolz

Returns a new instance of Ruby2Lolz.



26
27
28
29
30
31
32
33
# File 'lib/ruby2lolz.rb', line 26

def initialize
  super
  @indent = "  "
  
  self.auto_shift_type = true
  self.strict = true
  self.expected = String
end

Class Method Details

.translate(klass_or_str, method = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby2lolz.rb', line 11

def self.translate(klass_or_str, method = nil)
  sexp = ParseTree.translate(klass_or_str, method)

  # unified_ruby is a rewriter plugin that rewires
  # the parse tree to make it easier to work with
  #  - defn arg above scope / making it arglist / ...
  unifier = Unifier.new
  unifier.processors.each do |p|
    p.unsupported.delete :cfunc # HACK
  end
  sexp = unifier.process(sexp)

  self.new.process(sexp)
end

Instance Method Details

#indent(s) ⇒ Object



184
185
186
# File 'lib/ruby2lolz.rb', line 184

def indent(s)
  s.to_s.split(/\n/).map{|line| @indent + line}.join("\n")
end

#process_arglist(exp, verbose = false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby2lolz.rb', line 44

def process_arglist(exp, verbose = false)
  code = []
  until exp.empty? do
    if verbose
      code.push indent("AWSUM VAR\n #{indent(process(exp.shift))}\nKTHNX.")
    else
      code.push process(exp.shift)
    end
  end
  code.join("\n")
end

#process_args(exp) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby2lolz.rb', line 130

def process_args(exp)
  args = []

  until exp.empty? do
    arg = exp.shift
    case arg
    when Symbol then
      args << "YR #{arg}"
    when Array then
      case arg.first
      when :block then
        asgns = {}
        arg[1..-1].each do |lasgn|
          asgns[lasgn[1]] = process(lasgn)
        end

        args.each_with_index do |name, index|
          args[index] = asgns[name] if asgns.has_key? name
        end
      else
        raise "unknown arg type #{arg.first.inspect}"
      end
    else
      raise "unknown arg type #{arg.inspect}"
    end
  end

  return "#{args.join ', '}"
end

#process_array(exp) ⇒ Object



56
57
58
59
60
# File 'lib/ruby2lolz.rb', line 56

def process_array(exp)
  str = "I CAN MANY HAZ\n"
  str << indent(process_arglist(exp, true))
  str << "\nKTHNXBYE."
end

#process_block(exp) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ruby2lolz.rb', line 160

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

  return result
end

#process_call(exp) ⇒ Object



99
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
# File 'lib/ruby2lolz.rb', line 99

def process_call(exp)
  receiver_node_type = exp.first.nil? ? nil : exp.first.first
  receiver = process exp.shift

  name = exp.shift
  args_exp = exp.shift rescue nil
  if args_exp && args_exp.first == :array # FIX
    args = "#{process(args_exp)[1..-2]}"
  else
    args = process args_exp
    args = nil if args.empty?
  end

  case name
  when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
    "(#{receiver} #{name} #{args})"
  when :[] then
    "#{receiver}[#{args}]"
  when :"-@" then
    "-#{receiver}"
  when :"+@" then
    "+#{receiver}"
  else
    unless receiver.nil? then
      "#{receiver}.#{name}#{args ? "#{args})" : args}"
    else
      "#{name}#{args ? " #{args}" : args}"
    end
  end
end

#process_defn(exp) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby2lolz.rb', line 83

def process_defn(exp)
  type1 = exp[1].first
  type2 = exp[2].first rescue nil

  case type1
  when :scope, :args then
    name = exp.shift
    args = process(exp.shift)
    args = "" if args == "[]"
    body = indent(process(exp.shift))
    return "HOW DUZ I HAZ #{name} [#{args}]\n#{body}\nIF U SAY SO"
  else
    raise "Unknown defn type: #{type1} for #{exp.inspect}"
  end   
end

#process_hash(exp) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby2lolz.rb', line 62

def process_hash(exp)
  result = []

  until exp.empty?
    lhs = process(exp.shift)
    rhs = exp.shift
    t = rhs.first
    rhs = process rhs
    rhs = "#{rhs}" unless [:lit, :str].include? t # TODO: verify better!

    result.push indent("I CAN HAS #{lhs.to_s.capitalize}\n #{indent(rhs)}\nKTHNX.")
  end

  case self.context[1]
  when :arglist, :argscat then
    return "#{result.join(', ')}"
  else
    return "OH HAI\n#{result.join("\n")}\nKTHNXBYE."
  end
end

#process_lit(exp) ⇒ Object

Processors



38
# File 'lib/ruby2lolz.rb', line 38

def process_lit(exp);   exp.shift.to_s;     end

#process_lvar(exp) ⇒ Object



41
# File 'lib/ruby2lolz.rb', line 41

def process_lvar(exp);  exp.shift.to_s;     end

#process_scope(exp) ⇒ Object



40
# File 'lib/ruby2lolz.rb', line 40

def process_scope(exp); process(exp.shift); end

#process_str(exp) ⇒ Object



39
# File 'lib/ruby2lolz.rb', line 39

def process_str(exp);   exp.shift.to_s;     end