igrigorik-ruby2lolz 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,37 @@
1
+ Ruby2Lolz translates Ruby code into LOLCODE (http://www.lolcode.com)
2
+
3
+ For example:
4
+
5
+ class Simple
6
+ def add(n1, n2)
7
+ return n1 + n2
8
+ end
9
+ end
10
+
11
+ Ruby2Lolz.translate(Simple, :add)
12
+
13
+ HOW DUZ I HAZ add [YR n1, YR n2]
14
+ (n1 + n2)
15
+ IF U SAY SO
16
+
17
+ ### OR ###
18
+
19
+ > Ruby2Lolz.translate({:nickname => [:ig, :igrigorik]}.inspect)
20
+
21
+ OH HAI
22
+ I CAN HAS Nickname
23
+ I CAN MANY HAZ
24
+ AWSUM VAR
25
+ ig
26
+ KTHNX.
27
+ AWSUM VAR
28
+ igrigorik
29
+ KTHNX.
30
+ KTHNXBYE.
31
+ KTHNX.
32
+ KTHNXBYE.
33
+
34
+ ### OR ###
35
+
36
+ Put some Lolz into your API!
37
+ - http://pastie.org/327494
data/lib/ruby2lolz.rb ADDED
@@ -0,0 +1,187 @@
1
+ #!/usr/local/bin/ruby
2
+ # Author: Ilya Grigorik (www.igvita.com)
3
+
4
+ require "rubygems"
5
+ require "parse_tree"
6
+ require "parse_tree_extensions"
7
+ require "unified_ruby"
8
+
9
+ class Ruby2Lolz < SexpProcessor
10
+
11
+ def self.translate(klass_or_str, method = nil)
12
+ sexp = ParseTree.translate(klass_or_str, method)
13
+
14
+ # unified_ruby is a rewriter plugin that rewires
15
+ # the parse tree to make it easier to work with
16
+ # - defn arg above scope / making it arglist / ...
17
+ unifier = Unifier.new
18
+ unifier.processors.each do |p|
19
+ p.unsupported.delete :cfunc # HACK
20
+ end
21
+ sexp = unifier.process(sexp)
22
+
23
+ self.new.process(sexp)
24
+ end
25
+
26
+ def initialize
27
+ super
28
+ @indent = " "
29
+
30
+ self.auto_shift_type = true
31
+ self.strict = true
32
+ self.expected = String
33
+ end
34
+
35
+ ############################################################
36
+ # Processors
37
+
38
+ def process_lit(exp); exp.shift.to_s; end
39
+ def process_str(exp); exp.shift.to_s; end
40
+ def process_scope(exp); process(exp.shift); end
41
+ def process_lvar(exp); exp.shift.to_s; end
42
+
43
+
44
+ def process_arglist(exp, verbose = false)
45
+ code = []
46
+ until exp.empty? do
47
+ if verbose
48
+ code.push indent("AWSUM VAR\n #{indent(process(exp.shift))}\nKTHNX.")
49
+ else
50
+ code.push process(exp.shift)
51
+ end
52
+ end
53
+ code.join("\n")
54
+ end
55
+
56
+ def process_array(exp)
57
+ str = "I CAN MANY HAZ\n"
58
+ str << indent(process_arglist(exp, true))
59
+ str << "\nKTHNXBYE."
60
+ end
61
+
62
+ def process_hash(exp)
63
+ result = []
64
+
65
+ until exp.empty?
66
+ lhs = process(exp.shift)
67
+ rhs = exp.shift
68
+ t = rhs.first
69
+ rhs = process rhs
70
+ rhs = "#{rhs}" unless [:lit, :str].include? t # TODO: verify better!
71
+
72
+ result.push indent("I CAN HAS #{lhs.to_s.capitalize}\n #{indent(rhs)}\nKTHNX.")
73
+ end
74
+
75
+ case self.context[1]
76
+ when :arglist, :argscat then
77
+ return "#{result.join(', ')}"
78
+ else
79
+ return "OH HAI\n#{result.join("\n")}\nKTHNXBYE."
80
+ end
81
+ end
82
+
83
+ def process_defn(exp)
84
+ type1 = exp[1].first
85
+ type2 = exp[2].first rescue nil
86
+
87
+ case type1
88
+ when :scope, :args then
89
+ name = exp.shift
90
+ args = process(exp.shift)
91
+ args = "" if args == "[]"
92
+ body = indent(process(exp.shift))
93
+ return "HOW DUZ I HAZ #{name} [#{args}]\n#{body}\nIF U SAY SO"
94
+ else
95
+ raise "Unknown defn type: #{type1} for #{exp.inspect}"
96
+ end
97
+ end
98
+
99
+ def process_call(exp)
100
+ receiver_node_type = exp.first.nil? ? nil : exp.first.first
101
+ receiver = process exp.shift
102
+
103
+ name = exp.shift
104
+ args_exp = exp.shift rescue nil
105
+ if args_exp && args_exp.first == :array # FIX
106
+ args = "#{process(args_exp)[1..-2]}"
107
+ else
108
+ args = process args_exp
109
+ args = nil if args.empty?
110
+ end
111
+
112
+ case name
113
+ when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
114
+ "(#{receiver} #{name} #{args})"
115
+ when :[] then
116
+ "#{receiver}[#{args}]"
117
+ when :"-@" then
118
+ "-#{receiver}"
119
+ when :"+@" then
120
+ "+#{receiver}"
121
+ else
122
+ unless receiver.nil? then
123
+ "#{receiver}.#{name}#{args ? "#{args})" : args}"
124
+ else
125
+ "#{name}#{args ? " #{args}" : args}"
126
+ end
127
+ end
128
+ end
129
+
130
+ def process_args(exp)
131
+ args = []
132
+
133
+ until exp.empty? do
134
+ arg = exp.shift
135
+ case arg
136
+ when Symbol then
137
+ args << "YR #{arg}"
138
+ when Array then
139
+ case arg.first
140
+ when :block then
141
+ asgns = {}
142
+ arg[1..-1].each do |lasgn|
143
+ asgns[lasgn[1]] = process(lasgn)
144
+ end
145
+
146
+ args.each_with_index do |name, index|
147
+ args[index] = asgns[name] if asgns.has_key? name
148
+ end
149
+ else
150
+ raise "unknown arg type #{arg.first.inspect}"
151
+ end
152
+ else
153
+ raise "unknown arg type #{arg.inspect}"
154
+ end
155
+ end
156
+
157
+ return "#{args.join ', '}"
158
+ end
159
+
160
+ def process_block(exp)
161
+ result = []
162
+ exp << nil if exp.empty?
163
+ until exp.empty? do
164
+ code = exp.shift
165
+
166
+ if code.nil? or code.first == :nil then
167
+ result << "# do nothing"
168
+ else
169
+ result << process(code)
170
+ end
171
+ end
172
+
173
+ result = result.join "\n"
174
+ result = case self.context[1]
175
+ when nil, :scope, :if, :iter, :resbody, :when, :while then
176
+ result + "\n"
177
+ else
178
+ "(#{result})"
179
+ end
180
+
181
+ return result
182
+ end
183
+
184
+ def indent(s)
185
+ s.to_s.split(/\n/).map{|line| @indent + line}.join("\n")
186
+ end
187
+ end
data/ruby2lolz.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = 'ruby2lolz'
3
+ s.version = '0.1.1'
4
+ s.date = '2009-03-31'
5
+ s.summary = 'Ruby to Lolcode translator, kthnxbai.'
6
+ s.description = s.summary
7
+ s.email = 'ilya@igvita.com'
8
+ s.homepage = "http://github.com/igrigorik/ruby2lolz"
9
+ s.has_rdoc = true
10
+ s.authors = ["Ilya Grigorik"]
11
+ s.rubyforge_project = "ruby2lolz"
12
+
13
+ # ruby -rpp -e' pp `git ls-files`.split("\n") '
14
+ s.files = ["README",
15
+ "lib/ruby2lolz.rb",
16
+ "ruby2lolz.gemspec",
17
+ "spec/lolz_spec.rb",
18
+ "spec/specs/array.txt",
19
+ "spec/specs/hash.txt",
20
+ "spec/specs/hash_array.txt",
21
+ "spec/specs/method.txt"]
22
+ end
data/spec/lolz_spec.rb ADDED
@@ -0,0 +1,47 @@
1
+ # LOLZ SPECS v1.2, KTHNXBAI.
2
+ #
3
+ # - http://lolcode.com/specs/1.2
4
+ #
5
+
6
+ require "rubygems"
7
+ require "parse_tree"
8
+ require "parse_tree_extensions"
9
+ require "spec"
10
+ require "ruby2lolz"
11
+
12
+ def read_spec(name); File.open("spec/specs/#{name}.txt", "r").read.gsub("\r", ""); end
13
+
14
+ describe Ruby2Lolz do
15
+
16
+ it "should translate Ruby hash to lolz, kthnx" do
17
+ h = {:user => "Ilya"}.inspect
18
+ Ruby2Lolz.translate(h).should == read_spec("hash")
19
+ end
20
+
21
+ it "should translate Ruby array to lolz, kthnx" do
22
+ a = ["igrigorik", "ig"].inspect
23
+ Ruby2Lolz.translate(a).should == read_spec("array")
24
+ end
25
+
26
+ it "should translate mixed Ruby array/hash to lolz, kthnx" do
27
+ h = {:nickname => ["igrigorik", "ig"]}.inspect
28
+ Ruby2Lolz.translate(h).should == read_spec("hash_array")
29
+ end
30
+
31
+ it "should translate Ruby method to lolz, kthnx" do
32
+ # [:defn,
33
+ # :hello,
34
+ # [:scope, [:block, [:args, :str], [:fcall, :puts, [:array, [:lvar, :str]]]]]]
35
+ class Simple
36
+ def hello(str); puts str; end;
37
+ end
38
+
39
+ Ruby2Lolz.translate(Simple, :hello).should == read_spec("method")
40
+ end
41
+
42
+ # it "should translate Ruby block to lolz, kthnx" do
43
+ # b = Proc.new {|var| puts var }.to_ruby
44
+ # puts Ruby2Lolz.translate(b)
45
+ # Ruby2Lolz.translate(b).should == read_spec("block")
46
+ # end
47
+ end
@@ -0,0 +1,8 @@
1
+ I CAN MANY HAZ
2
+ AWSUM VAR
3
+ igrigorik
4
+ KTHNX.
5
+ AWSUM VAR
6
+ ig
7
+ KTHNX.
8
+ KTHNXBYE.
@@ -0,0 +1,5 @@
1
+ OH HAI
2
+ I CAN HAS User
3
+ Ilya
4
+ KTHNX.
5
+ KTHNXBYE.
@@ -0,0 +1,12 @@
1
+ OH HAI
2
+ I CAN HAS Nickname
3
+ I CAN MANY HAZ
4
+ AWSUM VAR
5
+ igrigorik
6
+ KTHNX.
7
+ AWSUM VAR
8
+ ig
9
+ KTHNX.
10
+ KTHNXBYE.
11
+ KTHNX.
12
+ KTHNXBYE.
@@ -0,0 +1,3 @@
1
+ HOW DUZ I HAZ hello [YR str]
2
+ puts str
3
+ IF U SAY SO
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: igrigorik-ruby2lolz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Grigorik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-31 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby to Lolcode translator, kthnxbai.
17
+ email: ilya@igvita.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - lib/ruby2lolz.rb
27
+ - ruby2lolz.gemspec
28
+ - spec/lolz_spec.rb
29
+ - spec/specs/array.txt
30
+ - spec/specs/hash.txt
31
+ - spec/specs/hash_array.txt
32
+ - spec/specs/method.txt
33
+ has_rdoc: true
34
+ homepage: http://github.com/igrigorik/ruby2lolz
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project: ruby2lolz
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Ruby to Lolcode translator, kthnxbai.
59
+ test_files: []
60
+