ruby2c 1.0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ ��D�H�_CVy��!��&���BD�^?R"��^��T��6�W��LY����>�dR�^c����|�#�`���{���S����Q�ɏ�~���a���i���W��(�q%[�n��J�i�@��>��v|%/>c����5a����w!A�x�zn���̦#��Ԯ3�!M�������oL�G �D�/MPԞ�r���n�0�.:��S���])>���,@Y`�U�:~�t���Ð��f#���@��љ*��
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ Autotest.add_hook :initialize do |at|
6
+ l = %w(ruby_parser RubyInline sexp_processor minitest)
7
+ t = %w(ParseTree sexp_processor)
8
+
9
+ at.libs << ':' << l.map { |d| "../../#{d}/dev/lib" }.join(":")
10
+ at.libs << ':' << t.map { |d| "../../#{d}/dev/test" }.join(":")
11
+
12
+ at.extra_files << "../../ParseTree/dev/test/pt_testcase.rb"
13
+
14
+ at.add_mapping(/(pt_|r2c)testcase/) do |f, _|
15
+ at.files_matching(/\/test_.*rb$/)
16
+ end
17
+
18
+ at.extra_class_map["TestCRewriter"] = "test/test_crewriter.rb"
19
+
20
+ at.testlib = "minitest/autorun"
21
+ end
@@ -0,0 +1,173 @@
1
+ === 1.0.0.6 / 2009-06-23
2
+
3
+ 1 major enhancement:
4
+
5
+ * Switched from ParseTree to ruby_parser for 1.9 compatibility. woo.
6
+ * All translate/process methods are gone since we can't reflect at runtime.
7
+
8
+ 43 minor enhancements:
9
+
10
+ * Added IterRewriter for finding free variables in iter nodes.
11
+ * Added dxstr.
12
+ * Added lots of new and missing tests
13
+ * Added not, array, lit_long_negative, lit_range[23], lit_regexp{_n,_once}, lit_sym{,_splat}
14
+ * Added pct_w tests
15
+ * Added process_defx to ruby_to_ansi_c.rb
16
+ * Added test data for array_pct_W, array_pct_W_dstr, attrasgn_index_equals, attrasgn_index_equals_space, and attrset.
17
+ * Added tests for "and". PONG to eric
18
+ * Added to_ary and masgn for TypeChecker.
19
+ * Added xstr support.
20
+ * Added yield(*ary)
21
+ * Cleaned up rewriter.rb and made changes for pt_testcase.rb
22
+ * Cleaned up some free_vars processing code.
23
+ * Converted everything to new enhanced PTTC test scheme.
24
+ * Enhanced with_scope to allow for static arrays.
25
+ * Extended Environment to separately store both types and values.
26
+ * Extern methods to support rb_iterate, including #each and #each_with_index.
27
+ * Free var static declarations for C layers.
28
+ * Merged IterRewriter into CRewriter and make typed.
29
+ * Migrated as much out of rewriter as possible.
30
+ * Modernized via hoe.
31
+ * Moved to full PTTC + UnifiedRuby.
32
+ * Pulled dstr up from ZenObfuscator.
33
+ * Pulled up tests, process_args, with_scope, and process_scope from obfuscator
34
+ * Pushed unique.rb up to PT
35
+ * Removed Environment methods that pushed up to ruby_parser.
36
+ * Removed Sexp#sexp_type
37
+ * Removed accessor, accessor_equals since they don't exist in PTTC.
38
+ * Removed bin/*
39
+ * Removed dead test
40
+ * Removed redundant defn_lvar_boundary, but added call to lvar_def_boundary.
41
+ * Renamed blocks to extra_methods in ruby_to_ruby_c.
42
+ * Renamed environment.rb to r2cenvironment.rb (incl tests)
43
+ * Renamed iter_functions to extra_methods in crewriter.
44
+ * Reordered so RubyToRubyC was always after RubyToAnsiC.
45
+ * Some optimizations cleaning up Type#method_missing. Drops test time ~30%.
46
+ * Split rewriter.rb to crewriter.rb (and renamed class to CRewriter).
47
+ * Split up and deleted support.rb (and tests).
48
+ * Switched from makefile to rakefile.
49
+ * Switched to minitest
50
+ * Test or node.
51
+ * Track and trigger changes on pt_testcase.rb and r2ctestcase.rb
52
+ * Used Hoe#add_include_dirs to clean up rakefile.
53
+
54
+ * 12 bug fixes:
55
+
56
+ * Fix types in iter rewriting.
57
+ * Fix yield([42, 24])
58
+ * Fixed 1.9-isms.
59
+ * Fixed all rubygems loaders.
60
+ * Fixed dependencies on all tests.
61
+ * Fixed for new (incompatible?) Environment supplied by SexpProcessor
62
+ * Fixed incompatibility with new inline changes.
63
+ * Fixed requires on ruby_parser
64
+ * Fixed ruby2c to work with autotest.
65
+ * Fixed tests in r2ctestcase for all layers except the C layers.
66
+ * Fixed zentest dependency in rakefile
67
+ * Fixes for RAD, mostly renaming environment.rb to r2cenvironment.rb at this stage.
68
+
69
+ === 1.0.0-beta-5 / 2006-05-12
70
+
71
+ * 6 minor enhancements:
72
+
73
+ * Split RubyToC to RubyToRubyC and RubyToAnsiC.
74
+ * Extended Environment to be more flexible for various situations.
75
+ * Removed propaganda (bloat) from release.
76
+ * Gemified and reorganized things. Support still needs splitting up.
77
+ * Flipped a lot of internal naming to use Unique.
78
+ * Added ruby_to_c_show (like parse_tree_show).
79
+
80
+ * 4(ish) bug fixes:
81
+
82
+ * Use ivars instead of cvars so inheritance won't bugger the translator.
83
+ * Corrected unsupported node lists in pipeline.
84
+ * Fixed bugs for splat args, iters, optional args, method name map.
85
+ * Fixed many other bugs.
86
+
87
+ === 1.0.0-beta-4 / 2005-07-13
88
+
89
+ * 1 minor enhancements:
90
+
91
+ * Added gemspec (hastily).
92
+
93
+ * 2 bug fixes:
94
+
95
+ * Translates bool type to VALUE since we were using Qtrue/Qfalse.
96
+ * Fixed rubygems for non-gem systems.
97
+
98
+ === 1.0.0-beta-3 / 2005-06-08
99
+
100
+ * 16 minor enhancements:
101
+
102
+ * Added ivar and iasgn support. Needs more work.
103
+ * Added limited support for self.
104
+ * Added pipeline tests for bools, call_arglist, call_attrasgn, fbody.
105
+ * Added process_not to RubyToC.
106
+ * Added support for float and symbol literals.
107
+ * Added support for gasgn, cvasgn, const (class consts, not classes).
108
+ * Improved error handling/reporting, esp in RubyToC.
109
+ * In TypeChecker.boostrap, pre-registered all base classes.
110
+ * Modified process_class test to include a class const.
111
+ * Processing :class now adds class constants to the local var scope.
112
+ * Processing :const checks both genv and env now.
113
+ * Rearchitected all tests into a pipeline test class.
114
+ * Rewrite attrasgn into regular calls.
115
+ * Rewrite fbody into a regular defn.
116
+ * Rewrote :array inside call to :arglist.
117
+ * Rewrote :or nodes in process_case to correctly be binary.
118
+
119
+ * 1 bug fix:
120
+
121
+ * Fixed a bug where single line while bodies were missing a semicolon.
122
+
123
+ === 1.0.0-beta-2 / 2005-02-15
124
+
125
+ * 1 minor enhancement:
126
+
127
+ * Added post-condition while/until support and tests.
128
+
129
+ * 4 bug fixes:
130
+
131
+ * Fixed bug:1422: Escape newlines to prevent multi-line strings in C.
132
+ * Fixed bug:1429: Arrays of strings are not being properly.
133
+ * Fixed bug:1447/1448: Readme file's website and added ParseTree dependency.
134
+
135
+ === 1.0.0-beta-1 / 2005-02-01
136
+
137
+ * 1 major enhancements:
138
+
139
+ * Hit 80% non-error conversion threshold for public beta release.
140
+
141
+ * 3 minor enhancements:
142
+
143
+ * (Mostly) Filled out functionality in Rewriter and TypeChecker.
144
+ * Flushed out what we don't do in RubyToC.
145
+ * Wrote a ton of rdoc
146
+
147
+ === 1.0.0-a2 / 2004-12-31
148
+
149
+ * 7 major enhancements:
150
+
151
+ * Alpha 2 released to private group for critique.
152
+ * Refactored and split out ParseTree package.
153
+ * Gemified dependency on ParseTree.
154
+ * Added iter rewriting
155
+ * Added post type inference rewriting specific to C library.
156
+ * Massive increase to the base we can translate.
157
+ * We have stabilized the architecture but still have a ways to go.
158
+
159
+ * 2 minor enhancements:
160
+
161
+ * Added propaganda (presentations).
162
+ * Much better test coverage, now with ZenTest compliant naming.
163
+
164
+ * 2 bug fixes:
165
+
166
+ * Gem-proofed makefile.
167
+ * Tons of little fixes we didn't bother to track.
168
+
169
+ === 1.0.0-a1 / 2004-09-24
170
+
171
+ * 1 major enhancement:
172
+
173
+ * Birthday! Alpha 1 released to private group for critique.
@@ -0,0 +1,35 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ demo/char.rb
7
+ demo/factorial.rb
8
+ demo/hello.rb
9
+ demo/misc.rb
10
+ demo/newarray.rb
11
+ demo/strcat.rb
12
+ lib/crewriter.rb
13
+ lib/function_table.rb
14
+ lib/function_type.rb
15
+ lib/handle.rb
16
+ lib/r2cenvironment.rb
17
+ lib/rewriter.rb
18
+ lib/ruby_to_ansi_c.rb
19
+ lib/ruby_to_ruby_c.rb
20
+ lib/type.rb
21
+ lib/type_checker.rb
22
+ lib/typed_sexp.rb
23
+ test/r2ctestcase.rb
24
+ test/test_crewriter.rb
25
+ test/test_extras.rb
26
+ test/test_function_table.rb
27
+ test/test_function_type.rb
28
+ test/test_handle.rb
29
+ test/test_r2cenvironment.rb
30
+ test/test_rewriter.rb
31
+ test/test_ruby_to_ansi_c.rb
32
+ test/test_ruby_to_ruby_c.rb
33
+ test/test_type.rb
34
+ test/test_type_checker.rb
35
+ test/test_typed_sexp.rb
@@ -0,0 +1,76 @@
1
+ = RubyToC
2
+
3
+ * http://rubyforge.org/projects/ruby2c/
4
+ * ryand-ruby@zenspider.com
5
+ * ruby2c@zenspider.com - mailing list
6
+
7
+ == DESCRIPTION:
8
+
9
+ ruby_to_c translates a static ruby subset to C. Hopefully it works.
10
+
11
+ NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE
12
+
13
+ THIS IS BETA SOFTWARE!
14
+
15
+ NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE ! NOTE
16
+
17
+ RubyToC has the following modules:
18
+
19
+ * Rewriter - massages the sexp into a more consistent form.
20
+ * TypeChecker - type inferencer for the above sexps.
21
+ * RubyToRubyC - converts a ruby (subset) sexp to ruby interals C.
22
+ * RubyToAnsiC - converts a ruby (subset) sexp to ANSI C.
23
+
24
+ and the following tools:
25
+
26
+ * translate.rb - Translates a given file to C.
27
+
28
+ == FEATURES/PROBLEMS:
29
+
30
+ * This is a preview release! BETA BETA BETA! Do NOT use this!
31
+ * Please contact me or Eric (drbrain of segment7 dot net) if you:
32
+ * have any feedback!
33
+ * have any changes!
34
+ * want to work on this!
35
+
36
+ == SYNOPSYS:
37
+
38
+ ./translate.rb blah.rb > blah.c; gcc -c -I /rubylib/1.8/platform blah.c
39
+
40
+ == TODO:
41
+
42
+ * Numerous, but we are trying to get them in here... sorry...
43
+ * Want to move to a gem directory structure (lib/ test/ bin/ etc)
44
+
45
+ == REQUIREMENTS:
46
+
47
+ * ruby_parser - http://rubyforge.org/projects/parsetree/
48
+
49
+ == INSTALL:
50
+
51
+ * sudo gem install RubyToC
52
+
53
+ == LICENSE:
54
+
55
+ (The MIT License)
56
+
57
+ Copyright (c) 2001-2008 Ryan Davis, Zen Spider Software
58
+
59
+ Permission is hereby granted, free of charge, to any person obtaining
60
+ a copy of this software and associated documentation files (the
61
+ "Software"), to deal in the Software without restriction, including
62
+ without limitation the rights to use, copy, modify, merge, publish,
63
+ distribute, sublicense, and/or sell copies of the Software, and to
64
+ permit persons to whom the Software is furnished to do so, subject to
65
+ the following conditions:
66
+
67
+ The above copyright notice and this permission notice shall be
68
+ included in all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
71
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
72
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
73
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
74
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
75
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
76
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.add_include_dirs("../../ParseTree/dev/test",
7
+ "../../ruby_parser/dev/lib",
8
+ "../../sexp_processor/dev/lib",
9
+ "../../sexp_processor/dev/test",
10
+ "lib")
11
+
12
+ Hoe.plugin :seattlerb
13
+
14
+ Hoe.spec "ruby2c" do
15
+ developer 'Ryan Davis', 'ryand-ruby@zenspider.com'
16
+ developer 'Eric Hodel', 'drbrain@segment7.net'
17
+
18
+ extra_deps << "ruby_parser"
19
+ end
20
+
21
+ # vim: syntax=ruby
@@ -0,0 +1,13 @@
1
+
2
+ class CharDemo
3
+ def chardemo
4
+ s = "hello"
5
+ c = s[2]
6
+ return c
7
+ end
8
+
9
+ def main
10
+ chardemo
11
+ return 0
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ class F
2
+ def factorial(n)
3
+ f = 1
4
+ n.downto(2) { |x| f *= x }
5
+ return f
6
+ end
7
+
8
+ def main # horrid but funny hack
9
+ return factorial(5)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+
2
+ class Hello
3
+ def hello
4
+ puts "hello world"
5
+ end
6
+
7
+ def main # silly hack
8
+ hello
9
+ return 0
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+
2
+ class Misc
3
+ def assert(cond, n)
4
+ if (!cond) then
5
+ puts("-- SymTab fatal error ")
6
+ case (n)
7
+ when 3
8
+ puts("-- too many nodes in graph")
9
+ when 4
10
+ puts("-- too many sets")
11
+ when 6
12
+ puts("-- too many symbols")
13
+ when 7
14
+ puts("-- too many character classes")
15
+ end
16
+ # puts("Stack Trace = #{caller.join "\n"}")
17
+ exit(n)
18
+ end
19
+ end
20
+
21
+ def main
22
+ assert(1 == 1, 3)
23
+ return 0
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+
2
+ class NewArray
3
+ def newarray
4
+ a = [1, 2, 3]
5
+ return a[0]
6
+ end
7
+
8
+ def main
9
+ return newarray
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class Blah
2
+ def blah
3
+ a = "this"
4
+ b = a + "that"
5
+ puts(b)
6
+ end
7
+
8
+ def main
9
+ blah
10
+ return 0
11
+ end
12
+ end
@@ -0,0 +1,199 @@
1
+ # -*- ruby -*-
2
+
3
+ begin require 'rubygems'; rescue LoadError; end
4
+ require 'type'
5
+ require 'sexp_processor'
6
+ require 'r2cenvironment'
7
+
8
+ ##
9
+ # CRewriter (should probably move this out to its own file) does
10
+ # rewritings that are language specific to C.
11
+
12
+ class CRewriter < SexpProcessor
13
+
14
+ ##
15
+ # REWRITES maps a function signature to a proc responsible for
16
+ # generating the appropriate sexp for that rewriting.
17
+
18
+ REWRITES = {
19
+ [Type.str, :+, Type.str] => proc { |l,n,r|
20
+ t(:call, nil, :strcat, r.unshift(r.shift, l), Type.str)
21
+ },
22
+ [Type.file, :puts, Type.str] => proc { |l,n,r|
23
+ t(:call, nil, :fputs, r.push(l))
24
+ },
25
+ }
26
+
27
+ attr_reader :env
28
+ attr_reader :extra_methods
29
+
30
+ def initialize # :nodoc:
31
+ super
32
+ self.auto_shift_type = true
33
+ self.expected = TypedSexp
34
+ @env = ::R2CEnvironment.new
35
+ @extra_methods = []
36
+ end
37
+
38
+ def free # REFACTOR: this is a violation of responsibility, should be in Env
39
+ parent = @env.env[0..-2]
40
+ bound_in_parent = parent.map { |h| h.keys }.flatten
41
+
42
+ env = @env.all
43
+
44
+ free = env.select { |k, (t, v)| bound_in_parent.include? k or not v }
45
+ vars = free.map { |k, (t, v)| [k, t] }
46
+ return vars
47
+ end
48
+
49
+ ##
50
+ # Rewrites function calls by looking them up in the REWRITES map. If
51
+ # a match exists, it invokes the block passing in the lhs, rhs, and
52
+ # function name. If one does not exist, it simply repacks the sexp
53
+ # and sends it along.
54
+
55
+ def process_call(exp)
56
+ lhs = process exp.shift
57
+ name = exp.shift
58
+ rhs = process exp.shift
59
+
60
+ lhs_type = lhs.sexp_type rescue nil
61
+ type_signature = [lhs_type, name]
62
+ type_signature += rhs[1..-1].map { |sexp| sexp.sexp_type } unless rhs.nil?
63
+
64
+ result = if REWRITES.has_key? type_signature then
65
+ REWRITES[type_signature].call(lhs, name, rhs)
66
+ else
67
+ t(:call, lhs, name, rhs, exp.sexp_type)
68
+ end
69
+
70
+ return result
71
+ end
72
+
73
+ def process_class(exp)
74
+ klassname = exp.shift
75
+ superklassname = exp.shift
76
+
77
+ methods = []
78
+
79
+ until exp.empty? do
80
+ methods << process(exp.shift)
81
+ end
82
+
83
+ @extra_methods.reverse_each do |defx| methods.unshift defx end
84
+ @extra_methods.clear
85
+
86
+ result = t(:class, klassname, superklassname, Type.zclass)
87
+ result.push(*methods)
88
+
89
+ return result
90
+ end
91
+
92
+ ##
93
+ # TODO register statics
94
+
95
+ def process_iter(exp)
96
+ iter_method_name = Unique.next
97
+
98
+ value_var_name = Unique.next
99
+ value_var_type = Type.unknown
100
+
101
+ memo_var_name = Unique.next
102
+
103
+ call = process exp.shift
104
+ vars = process exp.shift
105
+ body = nil
106
+
107
+ free_vars = @env.scope do
108
+ body = process exp.shift
109
+ self.free.map { |name, type| [name, :"static_#{Unique.next}", type] }
110
+ end
111
+
112
+ var_names = var_names_in vars
113
+
114
+ frees = t(:array, Type.void)
115
+ statics = t(:array, Type.void)
116
+ defx_body_block = t(:block)
117
+
118
+ # set statics first so block vars can update statics
119
+ free_vars.each do |name, static_name, type| # free vars go on both sides
120
+ frees << t(:lvar, name, type)
121
+ statics << t(:lvar, static_name, type)
122
+ defx_body_block << t(:lasgn, name,
123
+ t(:lvar, static_name, type),
124
+ type)
125
+ end
126
+
127
+ if var_names.length == 1 then # expand block args to lasgn
128
+ value_var_type = var_names.first.last
129
+
130
+ defx_body_block << t(:lasgn, var_names.first.first,
131
+ t(:lvar, value_var_name, var_names.first.last),
132
+ var_names.first.last)
133
+
134
+ else # expand block args to masgn
135
+ value_var_type = Type.value
136
+ dyn_vars = t(:array)
137
+
138
+ var_names.each do |name, type|
139
+ dyn_vars << t(:lasgn, name, nil, type)
140
+ end
141
+
142
+ defx_body_block << t(:masgn,
143
+ dyn_vars,
144
+ t(:to_ary, t(:lvar, value_var_name, Type.value)))
145
+ end
146
+
147
+ defx_body_block << body
148
+
149
+ free_vars.each do |name, static_name, type|
150
+ defx_body_block << t(:lasgn, static_name, t(:lvar, name, type), type)
151
+ @extra_methods << t(:static, "static VALUE #{static_name};", Type.fucked)
152
+ end
153
+
154
+ defx_body_block << t(:return, t(:nil, Type.value))
155
+
156
+ defx = t(:defx,
157
+ iter_method_name,
158
+ t(:args,
159
+ t(value_var_name, value_var_type),
160
+ t(memo_var_name, Type.value)),
161
+ t(:scope, defx_body_block),
162
+ Type.void)
163
+
164
+ @extra_methods << defx
165
+
166
+ args = t(:args, frees, statics, Type.void)
167
+
168
+ return t(:iter, call, args, iter_method_name)
169
+ end
170
+
171
+ def process_lasgn(exp)
172
+ name = exp.shift
173
+ value = process(exp.shift)
174
+
175
+ @env.add name, exp.sexp_type
176
+ @env.set_val name, true
177
+
178
+ return t(:lasgn, name, value, exp.sexp_type)
179
+ end
180
+
181
+ def process_lvar(exp)
182
+ name = exp.shift
183
+
184
+ @env.add name, Type.value
185
+ @env.lookup name rescue @env.set_val name, false
186
+
187
+ return t(:lvar, name, exp.sexp_type)
188
+ end
189
+
190
+ def var_names_in(exp)
191
+ return [[exp.last, exp.sexp_type]] if exp.length == 2 and not Sexp === exp.last
192
+
193
+ var_names = []
194
+ exp.each_of_type :dasgn_curr do |sexp|
195
+ var_names << [sexp.sexp_body.first, sexp.sexp_type]
196
+ end
197
+ return var_names
198
+ end
199
+ end