Class: Ruby2Lolz
- Inherits:
-
SexpProcessor
- Object
- SexpProcessor
- Ruby2Lolz
- Defined in:
- lib/ruby2lolz.rb
Class Method Summary collapse
Instance Method Summary collapse
- #indent(s) ⇒ Object
-
#initialize ⇒ Ruby2Lolz
constructor
A new instance of Ruby2Lolz.
- #process_arglist(exp, verbose = false) ⇒ Object
- #process_args(exp) ⇒ Object
- #process_array(exp) ⇒ Object
- #process_block(exp) ⇒ Object
- #process_call(exp) ⇒ Object
- #process_defn(exp) ⇒ Object
- #process_hash(exp) ⇒ Object
-
#process_lit(exp) ⇒ Object
Processors.
- #process_lvar(exp) ⇒ Object
- #process_scope(exp) ⇒ Object
- #process_str(exp) ⇒ Object
Constructor Details
#initialize ⇒ Ruby2Lolz
Returns a new instance of Ruby2Lolz.
34 35 36 37 38 39 40 41 |
# File 'lib/ruby2lolz.rb', line 34 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ruby2lolz.rb', line 19 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
192 193 194 |
# File 'lib/ruby2lolz.rb', line 192 def indent(s) s.to_s.split(/\n/).map{|line| @indent + line}.join("\n") end |
#process_arglist(exp, verbose = false) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruby2lolz.rb', line 52 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ruby2lolz.rb', line 138 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
64 65 66 67 68 |
# File 'lib/ruby2lolz.rb', line 64 def process_array(exp) str = "I CAN MANY HAZ\n" str << indent(process_arglist(exp, true)) str << "\nKTHNXBYE." end |
#process_block(exp) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/ruby2lolz.rb', line 168 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ruby2lolz.rb', line 107 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ruby2lolz.rb', line 91 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ruby2lolz.rb', line 70 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
46 |
# File 'lib/ruby2lolz.rb', line 46 def process_lit(exp); exp.shift.to_s; end |
#process_lvar(exp) ⇒ Object
49 |
# File 'lib/ruby2lolz.rb', line 49 def process_lvar(exp); exp.shift.to_s; end |
#process_scope(exp) ⇒ Object
48 |
# File 'lib/ruby2lolz.rb', line 48 def process_scope(exp); process(exp.shift); end |
#process_str(exp) ⇒ Object
47 |
# File 'lib/ruby2lolz.rb', line 47 def process_str(exp); exp.shift.to_s; end |