fastruby 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +8 -0
- data/{README → README.rdoc} +6 -1
- data/Rakefile +7 -7
- data/benchmarks/benchmark.rb~ +14 -2
- data/ext/fastruby_base/fastruby_base.inl +8 -4
- data/lib/fastruby/builder/inference_updater.rb +76 -0
- data/lib/fastruby/builder/inference_updater.rb~ +76 -0
- data/lib/fastruby/builder/inferencer.rb +38 -0
- data/lib/fastruby/{inliner → builder}/inliner.rb +16 -27
- data/lib/fastruby/builder/inliner.rb~ +60 -0
- data/lib/fastruby/builder/locals_inference.rb +53 -0
- data/lib/fastruby/builder/lvar_type.rb +43 -0
- data/lib/fastruby/builder/lvar_type.rb~ +44 -0
- data/lib/fastruby/builder/pipeline.rb +43 -0
- data/lib/fastruby/builder/pipeline.rb~ +43 -0
- data/lib/fastruby/{reductor → builder}/reductor.rb +6 -3
- data/lib/fastruby/builder/reductor.rb~ +42 -0
- data/lib/fastruby/builder.rb +73 -25
- data/lib/fastruby/builder.rb~ +311 -0
- data/lib/fastruby/corelib/fixnum.rb +75 -0
- data/lib/fastruby/corelib/fixnum.rb~ +146 -0
- data/lib/fastruby/corelib/integer.rb +96 -0
- data/lib/fastruby/corelib/integer.rb~ +96 -0
- data/lib/fastruby/corelib.rb +23 -0
- data/lib/fastruby/corelib.rb~ +23 -0
- data/lib/fastruby/getlocals.rb +3 -1
- data/lib/fastruby/logging.rb +2 -2
- data/lib/fastruby/modules/inferencer/infer.rb +31 -0
- data/lib/fastruby/modules/inferencer/literal.rb +42 -0
- data/lib/fastruby/modules/inliner/call.rb +327 -0
- data/lib/fastruby/{inliner/modules/call.rb → modules/inliner/call.rb~} +14 -24
- data/lib/fastruby/modules/inliner/defn.rb +41 -0
- data/lib/fastruby/modules/inliner/defn.rb~ +29 -0
- data/lib/fastruby/modules/inliner/recursive.rb +40 -0
- data/lib/fastruby/{inliner/modules/recursive.rb → modules/inliner/recursive.rb~} +1 -1
- data/lib/fastruby/modules/lvar_type/call.rb +36 -0
- data/lib/fastruby/modules/lvar_type/call.rb~ +36 -0
- data/lib/fastruby/modules/lvar_type/defn.rb +42 -0
- data/lib/fastruby/modules/lvar_type/defn.rb~ +42 -0
- data/lib/fastruby/modules/lvar_type/lasgn.rb +41 -0
- data/lib/fastruby/modules/lvar_type/lasgn.rb~ +42 -0
- data/lib/fastruby/modules/lvar_type/recursive.rb +33 -0
- data/lib/fastruby/modules/lvar_type/recursive.rb~ +33 -0
- data/lib/fastruby/{reductor/modules → modules/reductor}/case.rb +0 -0
- data/lib/fastruby/modules/reductor/fastruby_flag.rb +33 -0
- data/lib/fastruby/{reductor/modules → modules/reductor}/for.rb +0 -0
- data/lib/fastruby/{reductor/modules → modules/reductor}/nontree.rb +0 -0
- data/lib/fastruby/modules/reductor/nontree.rb~ +32 -0
- data/lib/fastruby/{reductor/modules → modules/reductor}/recursive.rb +1 -1
- data/lib/fastruby/modules/reductor/recursive.rb~ +31 -0
- data/lib/fastruby/{translator/modules → modules/translator}/block.rb +0 -0
- data/lib/fastruby/modules/translator/call.rb +344 -0
- data/lib/fastruby/{translator/modules/call.rb → modules/translator/call.rb~} +24 -3
- data/lib/fastruby/{translator/modules → modules/translator}/defn.rb +10 -9
- data/lib/fastruby/modules/translator/defn.rb~ +267 -0
- data/lib/fastruby/{translator/modules → modules/translator}/directive.rb +3 -1
- data/lib/fastruby/modules/translator/directive.rb~ +44 -0
- data/lib/fastruby/{translator/modules → modules/translator}/exceptions.rb +3 -1
- data/lib/fastruby/modules/translator/exceptions.rb~ +120 -0
- data/lib/fastruby/{translator/modules → modules/translator}/flow.rb +0 -0
- data/lib/fastruby/modules/translator/iter.rb +745 -0
- data/lib/fastruby/{translator/modules/iter.rb → modules/translator/iter.rb~} +103 -48
- data/lib/fastruby/modules/translator/literal.rb +150 -0
- data/lib/fastruby/{translator/modules/literal.rb → modules/translator/literal.rb~} +3 -3
- data/lib/fastruby/{translator/modules → modules/translator}/logical.rb +0 -0
- data/lib/fastruby/{translator/modules → modules/translator}/method_group.rb +0 -0
- data/lib/fastruby/{translator/modules → modules/translator}/nonlocal.rb +18 -6
- data/lib/fastruby/modules/translator/nonlocal.rb~ +298 -0
- data/lib/fastruby/modules/translator/static.rb +290 -0
- data/lib/fastruby/{translator/modules/static.rb → modules/translator/static.rb~} +66 -17
- data/lib/fastruby/modules/translator/variable.rb +280 -0
- data/lib/fastruby/{translator/modules/variable.rb → modules/translator/variable.rb~} +14 -44
- data/lib/fastruby/modules.rb +30 -0
- data/lib/fastruby/object.rb +42 -6
- data/lib/fastruby/object.rb~ +159 -0
- data/lib/fastruby/set_tree.rb +7 -11
- data/lib/fastruby/set_tree.rb~ +71 -0
- data/lib/fastruby/sexp_extension.rb +29 -7
- data/lib/fastruby/sexp_extension.rb~ +262 -0
- data/lib/fastruby/translator/scope_mode_helper.rb~ +138 -0
- data/lib/fastruby/translator/translator.rb +87 -92
- data/lib/fastruby/translator/translator.rb~ +1600 -0
- data/lib/fastruby/translator/translator_modules.rb +3 -1
- data/lib/fastruby/translator/translator_modules.rb~ +53 -0
- data/lib/fastruby.rb +3 -1
- data/lib/fastruby.rb~ +3 -1
- data/lib/fastruby_only/base.rb +1 -0
- data/spec/corelib/numeric/fixnum_spec.rb +110 -0
- data/spec/corelib/numeric/fixnum_spec.rb~ +104 -0
- data/spec/corelib/numeric/integer_spec.rb +173 -0
- data/spec/corelib/numeric/integer_spec.rb~ +173 -0
- data/spec/fastruby_only/base_spec.rb +74 -0
- data/spec/graph/base_spec.rb +2 -1
- data/spec/graph/base_spec.rb~ +35 -0
- data/spec/graph/path_spec.rb +2 -2
- data/spec/graph/path_spec.rb~ +48 -0
- data/spec/graph/vertex_spec.rb +2 -1
- data/spec/graph/vertex_spec.rb~ +58 -0
- data/spec/reductor/base_spec.rb +1 -1
- data/spec/ruby/block/lambda_spec.rb~ +163 -0
- data/spec/ruby/block/proc_as_block_spec.rb~ +69 -1
- data/spec/ruby/block_spec.rb~ +2 -494
- data/spec/ruby/call/base_call_spec.rb +1 -1
- data/spec/ruby/call/base_call_spec.rb~ +2 -60
- data/spec/ruby/defn/replacement_spec.rb +26 -14
- data/spec/ruby/defn/replacement_spec.rb~ +13 -3
- data/spec/ruby/exception/internal_ex_spec.rb~ +86 -0
- data/spec/ruby/integrity_spec.rb~ +35 -1
- data/spec/ruby/variable_spec.rb~ +31 -0
- data/spec/scope_mode/flow_spec.rb +1 -1
- data/spec/scope_mode/flow_spec.rb~ +109 -0
- data/spec/sugar/base_spec.rb +29 -0
- data/spec/sugar/base_spec.rb~ +16 -0
- metadata +100 -43
- data/spec/fastruby/inliner/modules/call_spec.rb +0 -0
- data/spec/fastruby/translator/modules/nonlocal_spec.rb +0 -0
- data/spec/fastruby/translator/translator_spec.rb +0 -0
- data/spec/ruby/block/arguments_spec.rb~ +0 -214
- data/spec/ruby/block/break_spec.rb~ +0 -236
- data/spec/ruby/block/next_spec.rb~ +0 -85
- data/spec/ruby/block/retry_spec.rb~ +0 -43
|
@@ -23,8 +23,8 @@ module FastRuby
|
|
|
23
23
|
define_translator_for(:iter, :arity => 1, :priority => 1) { |*x|
|
|
24
24
|
ret = nil
|
|
25
25
|
tree = x.first
|
|
26
|
-
|
|
27
26
|
enable_handler_group(:static_call) do
|
|
27
|
+
require "pry"; binding.pry
|
|
28
28
|
ret = x.size == 1 ? to_c(tree[3]) : to_c(tree[3], x.last)
|
|
29
29
|
end
|
|
30
30
|
ret
|
|
@@ -47,20 +47,47 @@ module FastRuby
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
handler_scope(:group => :static_call, :priority => 1000) do
|
|
50
|
-
define_translator_for(:call) do |
|
|
50
|
+
define_translator_for(:call) do |*x|
|
|
51
|
+
tree, result = x
|
|
52
|
+
|
|
51
53
|
method_name = tree[2].to_s
|
|
52
54
|
recv_tree = tree[1]
|
|
53
55
|
|
|
54
|
-
if
|
|
56
|
+
if method_name == "c_address_of"
|
|
57
|
+
arg = tree[3][1]
|
|
58
|
+
return "&"+to_c(arg)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
if method_name == "inline_c"
|
|
62
|
+
arg = tree[3][1]
|
|
63
|
+
|
|
64
|
+
if arg.node_type != :str
|
|
65
|
+
raise RuntimeError, "invalid node for inline_c directive #{arg}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
next arg[1]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
if recv_tree and recv_tree.node_type != :self
|
|
55
72
|
if (not tree[2].to_s =~ /^[a-zA-Z]/) and tree[2].to_s.size <= 3
|
|
56
73
|
c_op = tree[2].to_s
|
|
57
74
|
c_op = '==' if tree[2] == :===
|
|
58
75
|
code = "( ( #{to_c(tree[1])} )#{c_op}(#{to_c(tree[3][1])}) )"
|
|
76
|
+
elsif tree[2] == :_invariant
|
|
77
|
+
name = self.add_global_name("VALUE", "Qnil");
|
|
78
|
+
|
|
79
|
+
init_extra << "
|
|
80
|
+
#{name} = #{to_c tree[1]};
|
|
81
|
+
rb_funcall(#{name},#{intern_num :gc_register_object},0);
|
|
82
|
+
"
|
|
83
|
+
|
|
84
|
+
code = name;
|
|
59
85
|
else
|
|
60
|
-
|
|
86
|
+
args = tree[3][1..-1].map{|st| to_c(st)}.join(",")
|
|
87
|
+
code = "#{method_name}( #{args} )"
|
|
61
88
|
end
|
|
62
89
|
else
|
|
63
|
-
args = tree[3][1..-1].map(
|
|
90
|
+
args = tree[3][1..-1].map{|st| to_c(st)}.join(",")
|
|
64
91
|
code = "#{method_name}( #{args} )"
|
|
65
92
|
end
|
|
66
93
|
|
|
@@ -71,7 +98,9 @@ module FastRuby
|
|
|
71
98
|
end
|
|
72
99
|
end
|
|
73
100
|
|
|
74
|
-
define_translator_for(:lit) do |
|
|
101
|
+
define_translator_for(:lit) do |*x|
|
|
102
|
+
tree, result = x
|
|
103
|
+
|
|
75
104
|
if result
|
|
76
105
|
"#{result} = #{tree[1]};"
|
|
77
106
|
else
|
|
@@ -79,7 +108,9 @@ module FastRuby
|
|
|
79
108
|
end
|
|
80
109
|
end
|
|
81
110
|
|
|
82
|
-
define_translator_for(:if) do |
|
|
111
|
+
define_translator_for(:if) do |*x|
|
|
112
|
+
tree, result_variable_ = x
|
|
113
|
+
|
|
83
114
|
condition_tree = tree[1]
|
|
84
115
|
impl_tree = tree[2]
|
|
85
116
|
else_tree = tree[3]
|
|
@@ -108,7 +139,19 @@ module FastRuby
|
|
|
108
139
|
end
|
|
109
140
|
end
|
|
110
141
|
|
|
111
|
-
define_translator_for(:
|
|
142
|
+
define_translator_for(:const) do |*x|
|
|
143
|
+
tree, result_var = x
|
|
144
|
+
|
|
145
|
+
if result_var
|
|
146
|
+
"#{result_var} = #{tree[1].to_s};"
|
|
147
|
+
else
|
|
148
|
+
"#{tree[1].to_s}"
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
define_translator_for(:while) do |*x|
|
|
153
|
+
tree, result_var = x
|
|
154
|
+
|
|
112
155
|
begin_while = "begin_while_"+rand(10000000).to_s
|
|
113
156
|
end_while = "end_while_"+rand(10000000).to_s
|
|
114
157
|
aux_varname = "_aux_" + rand(10000000).to_s
|
|
@@ -142,7 +185,9 @@ module FastRuby
|
|
|
142
185
|
|
|
143
186
|
end
|
|
144
187
|
|
|
145
|
-
define_translator_for(:and) do |
|
|
188
|
+
define_translator_for(:and) do |*x|
|
|
189
|
+
tree, return_var = x
|
|
190
|
+
|
|
146
191
|
if return_var
|
|
147
192
|
"
|
|
148
193
|
{
|
|
@@ -159,7 +204,9 @@ module FastRuby
|
|
|
159
204
|
|
|
160
205
|
end
|
|
161
206
|
|
|
162
|
-
define_translator_for(:or) do |
|
|
207
|
+
define_translator_for(:or) do |*x|
|
|
208
|
+
tree, return_var = x
|
|
209
|
+
|
|
163
210
|
if return_var
|
|
164
211
|
"
|
|
165
212
|
{
|
|
@@ -176,7 +223,9 @@ module FastRuby
|
|
|
176
223
|
|
|
177
224
|
end
|
|
178
225
|
|
|
179
|
-
define_translator_for(:not) do |
|
|
226
|
+
define_translator_for(:not) do |*x|
|
|
227
|
+
tree, return_var = x
|
|
228
|
+
|
|
180
229
|
if return_var
|
|
181
230
|
"
|
|
182
231
|
{
|
|
@@ -197,11 +246,11 @@ module FastRuby
|
|
|
197
246
|
end
|
|
198
247
|
|
|
199
248
|
|
|
200
|
-
define_method_handler(:infer_value) { |tree
|
|
249
|
+
define_method_handler(:infer_value) { |*x| tree = x.first;
|
|
201
250
|
Value.new(eval(tree[1].to_s))
|
|
202
|
-
}.condition{|tree
|
|
251
|
+
}.condition{|*x| tree = x.first; tree.node_type == :const}
|
|
203
252
|
|
|
204
|
-
define_method_handler(:infer_value) { |tree
|
|
253
|
+
define_method_handler(:infer_value) { |*x| tree = x.first;
|
|
205
254
|
args_tree = tree[3]
|
|
206
255
|
receiver_tree = tree[1]
|
|
207
256
|
|
|
@@ -211,7 +260,7 @@ module FastRuby
|
|
|
211
260
|
next false unless (value_1 and value_2)
|
|
212
261
|
|
|
213
262
|
Value.new(value_1.value == value_2.value)
|
|
214
|
-
}.condition{|tree
|
|
263
|
+
}.condition{|*x| tree = x.first;
|
|
215
264
|
next false unless tree.node_type == :call
|
|
216
265
|
|
|
217
266
|
args_tree = tree[3]
|
|
@@ -222,7 +271,7 @@ module FastRuby
|
|
|
222
271
|
|
|
223
272
|
true
|
|
224
273
|
}
|
|
225
|
-
define_method_handler(:infer_value) { |tree
|
|
274
|
+
define_method_handler(:infer_value) { |*x| tree = x.first;
|
|
226
275
|
args_tree = tree[3]
|
|
227
276
|
receiver_tree = tree[1]
|
|
228
277
|
infered_type = infer_type(receiver_tree)
|
|
@@ -232,7 +281,7 @@ module FastRuby
|
|
|
232
281
|
else
|
|
233
282
|
nil
|
|
234
283
|
end
|
|
235
|
-
}.condition{|tree
|
|
284
|
+
}.condition{|*x| tree = x.first;
|
|
236
285
|
next false unless tree.node_type == :call
|
|
237
286
|
method_name = tree[2]
|
|
238
287
|
next false unless method_name == :_class
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
|
|
3
|
+
This file is part of the fastruby project, http://github.com/tario/fastruby
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
|
|
6
|
+
|
|
7
|
+
fastruby is free software: you can redistribute it and/or modify
|
|
8
|
+
it under the terms of the gnu general public license as published by
|
|
9
|
+
the free software foundation, either version 3 of the license, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
fastruby is distributed in the hope that it will be useful,
|
|
13
|
+
but without any warranty; without even the implied warranty of
|
|
14
|
+
merchantability or fitness for a particular purpose. see the
|
|
15
|
+
gnu general public license for more details.
|
|
16
|
+
|
|
17
|
+
you should have received a copy of the gnu general public license
|
|
18
|
+
along with fastruby. if not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
|
|
20
|
+
=end
|
|
21
|
+
module FastRuby
|
|
22
|
+
class Context
|
|
23
|
+
define_translator_for(:cvar, :method => :to_c_cvar, :arity => 1)
|
|
24
|
+
def to_c_cvar(tree)
|
|
25
|
+
"rb_cvar_get(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]})"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
define_translator_for(:cvasgn, :method => :to_c_cvasgn)
|
|
29
|
+
def to_c_cvasgn(tree, result_var = nil)
|
|
30
|
+
if result_var
|
|
31
|
+
"
|
|
32
|
+
{
|
|
33
|
+
VALUE recv = CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self;
|
|
34
|
+
|
|
35
|
+
#{to_c tree[2], result_var};
|
|
36
|
+
|
|
37
|
+
#{if RUBY_VERSION =~ /^1\.9/
|
|
38
|
+
"rb_cvar_set(recv,#{intern_num tree[1]},#{result_var});"
|
|
39
|
+
elsif RUBY_VERSION =~ /^1\.8/
|
|
40
|
+
"rb_cvar_set(recv,#{intern_num tree[1]},#{result_var},Qfalse);"
|
|
41
|
+
else
|
|
42
|
+
raise RuntimeError, "unsupported ruby version #{RUBY_VERSION}"
|
|
43
|
+
end
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
"
|
|
47
|
+
else
|
|
48
|
+
"__rb_cvar_set(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]},#{to_c tree[2]},Qfalse)"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
define_translator_for(:gvar, :method => :to_c_gvar, :arity => 1)
|
|
53
|
+
def to_c_gvar(tree)
|
|
54
|
+
if (tree[1] == :$!)
|
|
55
|
+
"pframe->thread_data->exception"
|
|
56
|
+
else
|
|
57
|
+
"rb_gvar_get((struct global_entry*)#{global_entry(tree[1])})"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
define_translator_for(:gasgn, :method => :to_c_gasgn)
|
|
62
|
+
def to_c_gasgn(tree, result_var = nil)
|
|
63
|
+
if result_var
|
|
64
|
+
"
|
|
65
|
+
{
|
|
66
|
+
#{to_c tree[2], result_var};
|
|
67
|
+
rb_gvar_set((void*)#{global_entry(tree[1])},#{result_var});
|
|
68
|
+
}
|
|
69
|
+
"
|
|
70
|
+
else
|
|
71
|
+
"_rb_gvar_set((void*)#{global_entry(tree[1])}, #{to_c tree[2]})"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
define_translator_for(:ivar, :method => :to_c_ivar, :arity => 1)
|
|
76
|
+
def to_c_ivar(tree)
|
|
77
|
+
"rb_ivar_get(#{locals_accessor}self,#{intern_num tree[1]})"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
define_translator_for(:iasgn, :method => :to_c_iasgn)
|
|
81
|
+
def to_c_iasgn(tree, result_var = nil)
|
|
82
|
+
if result_var
|
|
83
|
+
"
|
|
84
|
+
{
|
|
85
|
+
#{to_c tree[2], result_var};
|
|
86
|
+
rb_ivar_set(#{locals_accessor}self,#{intern_num tree[1]},#{result_var});
|
|
87
|
+
}
|
|
88
|
+
"
|
|
89
|
+
else
|
|
90
|
+
"_rb_ivar_set(#{locals_accessor}self,#{intern_num tree[1]},#{to_c tree[2]})"
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
define_translator_for(:const, :arity => 1) do |*x| tree = x.first;
|
|
95
|
+
"rb_const_get(CLASS_OF(plocals->self), #{intern_num(tree[1])})"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
define_translator_for(:cdecl, :method => :to_c_cdecl)
|
|
99
|
+
def to_c_cdecl(tree, result_var_ = nil)
|
|
100
|
+
|
|
101
|
+
result_var = result_var_ || "value"
|
|
102
|
+
|
|
103
|
+
if tree[1].instance_of? Symbol
|
|
104
|
+
code = proc{"
|
|
105
|
+
{
|
|
106
|
+
// set constant #{tree[1].to_s}
|
|
107
|
+
#{to_c tree[2], result_var};
|
|
108
|
+
rb_const_set(rb_cObject, #{intern_num tree[1]}, #{result_var});
|
|
109
|
+
}
|
|
110
|
+
"}
|
|
111
|
+
elsif tree[1].instance_of? FastRuby::FastRubySexp
|
|
112
|
+
|
|
113
|
+
if tree[1].node_type == :colon2
|
|
114
|
+
code = proc{"
|
|
115
|
+
{
|
|
116
|
+
// set constant #{tree[1].to_s}
|
|
117
|
+
#{to_c tree[2], result_var};
|
|
118
|
+
VALUE klass = Qnil;
|
|
119
|
+
#{to_c tree[1][1], "klass"};
|
|
120
|
+
rb_const_set(klass, #{intern_num tree[1][2]}, #{result_var});
|
|
121
|
+
}
|
|
122
|
+
"}
|
|
123
|
+
elsif tree[1].node_type == :colon3
|
|
124
|
+
code = proc{"
|
|
125
|
+
{
|
|
126
|
+
// set constant #{tree[1].to_s}
|
|
127
|
+
#{to_c tree[2], result_var};
|
|
128
|
+
rb_const_set(rb_cObject, #{intern_num tree[1][1]}, #{result_var});
|
|
129
|
+
}
|
|
130
|
+
"}
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
if result_var_
|
|
135
|
+
code.call
|
|
136
|
+
else
|
|
137
|
+
inline_block{"VALUE #{result_var} = Qnil;\n" + code.call + "
|
|
138
|
+
return #{result_var};
|
|
139
|
+
"}
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
define_translator_for(:colon3, :method => :to_c_colon3, :arity => 1)
|
|
144
|
+
def to_c_colon3(tree)
|
|
145
|
+
"rb_const_get_from(rb_cObject, #{intern_num tree[1]})"
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
define_translator_for(:colon2, :method => :to_c_colon2)
|
|
149
|
+
def to_c_colon2(tree, result_var = nil)
|
|
150
|
+
code = proc{ "
|
|
151
|
+
{
|
|
152
|
+
VALUE klass = Qnil;
|
|
153
|
+
|
|
154
|
+
#{to_c tree[1],"klass"};
|
|
155
|
+
#{
|
|
156
|
+
if result_var
|
|
157
|
+
"#{result_var} = Qnil;"
|
|
158
|
+
end
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (rb_is_const_id(#{intern_num tree[2]})) {
|
|
162
|
+
switch (TYPE(klass)) {
|
|
163
|
+
case T_CLASS:
|
|
164
|
+
case T_MODULE:
|
|
165
|
+
#{
|
|
166
|
+
if result_var
|
|
167
|
+
"#{result_var} = rb_const_get_from(klass, #{intern_num tree[2]});"
|
|
168
|
+
else
|
|
169
|
+
"return rb_const_get_from(klass, #{intern_num tree[2]});"
|
|
170
|
+
end
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
default:
|
|
174
|
+
#{_raise("rb_eTypeError","not a class/module")};
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
#{
|
|
180
|
+
if result_var
|
|
181
|
+
"#{result_var} = rb_funcall(klass, #{intern_num tree[2]}, 0, 0);"
|
|
182
|
+
else
|
|
183
|
+
"return rb_funcall(klass, #{intern_num tree[2]}, 0, 0);"
|
|
184
|
+
end
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#{
|
|
189
|
+
unless result_var
|
|
190
|
+
"return Qnil;"
|
|
191
|
+
end
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
"}
|
|
195
|
+
|
|
196
|
+
if result_var
|
|
197
|
+
code.call
|
|
198
|
+
else
|
|
199
|
+
inline_block &code
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
define_translator_for(:lasgn, :method => :to_c_lasgn)
|
|
204
|
+
def to_c_lasgn(tree, result_var = nil)
|
|
205
|
+
code = "
|
|
206
|
+
{
|
|
207
|
+
#{to_c tree[2], result_var};
|
|
208
|
+
#{locals_accessor}#{tree[1]} = #{result_var};
|
|
209
|
+
}
|
|
210
|
+
"
|
|
211
|
+
|
|
212
|
+
if result_var
|
|
213
|
+
code
|
|
214
|
+
else
|
|
215
|
+
"_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
define_translator_for(:lvar, :arity => 1) do |*x| tree = x.first;
|
|
220
|
+
locals_accessor + tree[1].to_s
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
define_translator_for(:defined, :method => :to_c_defined, :arity => 1)
|
|
224
|
+
def to_c_defined(tree)
|
|
225
|
+
obj_tree = tree[1]
|
|
226
|
+
|
|
227
|
+
if obj_tree[0] == :call && obj_tree[2] == :infer
|
|
228
|
+
obj_tree = obj_tree[1]
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
nt = obj_tree.node_type
|
|
232
|
+
|
|
233
|
+
if nt == :self
|
|
234
|
+
'rb_str_new2("self")'
|
|
235
|
+
elsif nt == :true
|
|
236
|
+
'rb_str_new2("true")'
|
|
237
|
+
elsif nt == :false
|
|
238
|
+
'rb_str_new2("false")'
|
|
239
|
+
elsif nt == :nil
|
|
240
|
+
'rb_str_new2("nil")'
|
|
241
|
+
elsif nt == :lvar
|
|
242
|
+
'rb_str_new2("local-variable")'
|
|
243
|
+
elsif nt == :gvar
|
|
244
|
+
"rb_gvar_defined((struct global_entry*)#{global_entry(obj_tree[1])}) ? #{literal_value "global-variable"} : Qnil"
|
|
245
|
+
elsif nt == :const
|
|
246
|
+
"rb_const_defined(rb_cObject, #{intern_num obj_tree[1]}) ? #{literal_value "constant"} : Qnil"
|
|
247
|
+
elsif nt == :call
|
|
248
|
+
if RUBY_VERSION =~ /^1\.8/
|
|
249
|
+
"rb_method_node(CLASS_OF(#{to_c obj_tree[1]}), #{intern_num obj_tree[2]}) ? #{literal_value "method"} : Qnil"
|
|
250
|
+
else
|
|
251
|
+
"rb_method_entry(CLASS_OF(#{to_c obj_tree[1]}), #{intern_num obj_tree[2]}) ? #{literal_value "method"} : Qnil"
|
|
252
|
+
end
|
|
253
|
+
elsif nt == :yield
|
|
254
|
+
"rb_block_given_p() ? #{literal_value "yield"} : Qnil"
|
|
255
|
+
elsif nt == :ivar
|
|
256
|
+
"rb_ivar_defined(plocals->self,#{intern_num obj_tree[1]}) ? #{literal_value "instance-variable"} : Qnil"
|
|
257
|
+
elsif nt == :attrset or
|
|
258
|
+
nt == :op_asgn1 or
|
|
259
|
+
nt == :op_asgn2 or
|
|
260
|
+
nt == :op_asgn_or or
|
|
261
|
+
nt == :op_asgn_and or
|
|
262
|
+
nt == :op_asgn_masgn or
|
|
263
|
+
nt == :masgn or
|
|
264
|
+
nt == :lasgn or
|
|
265
|
+
nt == :dasgn or
|
|
266
|
+
nt == :dasgn_curr or
|
|
267
|
+
nt == :gasgn or
|
|
268
|
+
nt == :iasgn or
|
|
269
|
+
nt == :cdecl or
|
|
270
|
+
nt == :cvdecl or
|
|
271
|
+
nt == :cvasgn
|
|
272
|
+
literal_value "assignment"
|
|
273
|
+
else
|
|
274
|
+
literal_value "expression"
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
end
|
|
280
|
+
end
|
|
@@ -210,45 +210,9 @@ module FastRuby
|
|
|
210
210
|
"
|
|
211
211
|
|
|
212
212
|
if result_var
|
|
213
|
-
|
|
214
|
-
klass = @infer_lvar_map[tree[1]]
|
|
215
|
-
if klass
|
|
216
|
-
"
|
|
217
|
-
{
|
|
218
|
-
#{to_c tree[2], result_var};
|
|
219
|
-
if (CLASS_OF(#{result_var})!=#{literal_value klass}) {
|
|
220
|
-
#{_raise(literal_value(FastRuby::TypeMismatchAssignmentException), "Illegal assignment at runtime (type mismatch)")};
|
|
221
|
-
}
|
|
222
|
-
#{locals_accessor}#{tree[1]} = #{result_var};
|
|
223
|
-
}
|
|
224
|
-
"
|
|
225
|
-
else
|
|
226
|
-
code
|
|
227
|
-
end
|
|
228
|
-
else
|
|
229
|
-
code
|
|
230
|
-
end
|
|
213
|
+
code
|
|
231
214
|
else
|
|
232
|
-
|
|
233
|
-
klass = @infer_lvar_map[tree[1]]
|
|
234
|
-
if klass
|
|
235
|
-
verify_type_function = proc { |name| "
|
|
236
|
-
static VALUE #{name}(VALUE arg, void* pframe ) {
|
|
237
|
-
if (CLASS_OF(arg)!=#{literal_value klass}) {
|
|
238
|
-
#{_raise(literal_value(FastRuby::TypeMismatchAssignmentException), "Illegal assignment at runtime (type mismatch)")};
|
|
239
|
-
}
|
|
240
|
-
return arg;
|
|
241
|
-
}
|
|
242
|
-
"
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
"_lvar_assing(&#{locals_accessor}#{tree[1]}, #{anonymous_function(&verify_type_function)}(#{to_c tree[2]},pframe))"
|
|
246
|
-
else
|
|
247
|
-
"_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
|
|
248
|
-
end
|
|
249
|
-
else
|
|
250
|
-
"_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
|
|
251
|
-
end
|
|
215
|
+
"_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
|
|
252
216
|
end
|
|
253
217
|
end
|
|
254
218
|
|
|
@@ -258,7 +222,13 @@ module FastRuby
|
|
|
258
222
|
|
|
259
223
|
define_translator_for(:defined, :method => :to_c_defined, :arity => 1)
|
|
260
224
|
def to_c_defined(tree)
|
|
261
|
-
|
|
225
|
+
obj_tree = tree[1]
|
|
226
|
+
|
|
227
|
+
if obj_tree[0] == :call && obj_tree[2] == :infer
|
|
228
|
+
obj_tree = obj_tree[1]
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
nt = obj_tree.node_type
|
|
262
232
|
|
|
263
233
|
if nt == :self
|
|
264
234
|
'rb_str_new2("self")'
|
|
@@ -271,19 +241,19 @@ module FastRuby
|
|
|
271
241
|
elsif nt == :lvar
|
|
272
242
|
'rb_str_new2("local-variable")'
|
|
273
243
|
elsif nt == :gvar
|
|
274
|
-
"rb_gvar_defined((struct global_entry*)#{global_entry(
|
|
244
|
+
"rb_gvar_defined((struct global_entry*)#{global_entry(obj_tree[1])}) ? #{literal_value "global-variable"} : Qnil"
|
|
275
245
|
elsif nt == :const
|
|
276
|
-
"rb_const_defined(rb_cObject, #{intern_num
|
|
246
|
+
"rb_const_defined(rb_cObject, #{intern_num obj_tree[1]}) ? #{literal_value "constant"} : Qnil"
|
|
277
247
|
elsif nt == :call
|
|
278
248
|
if RUBY_VERSION =~ /^1\.8/
|
|
279
|
-
"rb_method_node(CLASS_OF(#{to_c
|
|
249
|
+
"rb_method_node(CLASS_OF(#{to_c obj_tree[1]}), #{intern_num obj_tree[2]}) ? #{literal_value "method"} : Qnil"
|
|
280
250
|
else
|
|
281
|
-
"rb_method_entry(CLASS_OF(#{to_c
|
|
251
|
+
"rb_method_entry(CLASS_OF(#{to_c obj_tree[1]}), #{intern_num obj_tree[2]}) ? #{literal_value "method"} : Qnil"
|
|
282
252
|
end
|
|
283
253
|
elsif nt == :yield
|
|
284
254
|
"rb_block_given_p() ? #{literal_value "yield"} : Qnil"
|
|
285
255
|
elsif nt == :ivar
|
|
286
|
-
"rb_ivar_defined(plocals->self,#{intern_num
|
|
256
|
+
"rb_ivar_defined(plocals->self,#{intern_num obj_tree[1]}) ? #{literal_value "instance-variable"} : Qnil"
|
|
287
257
|
elsif nt == :attrset or
|
|
288
258
|
nt == :op_asgn1 or
|
|
289
259
|
nt == :op_asgn2 or
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
|
|
3
|
+
This file is part of the fastruby project, http://github.com/tario/fastruby
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
|
|
6
|
+
|
|
7
|
+
fastruby is free software: you can redistribute it and/or modify
|
|
8
|
+
it under the terms of the gnu general public license as published by
|
|
9
|
+
the free software foundation, either version 3 of the license, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
fastruby is distributed in the hope that it will be useful,
|
|
13
|
+
but without any warranty; without even the implied warranty of
|
|
14
|
+
merchantability or fitness for a particular purpose. see the
|
|
15
|
+
gnu general public license for more details.
|
|
16
|
+
|
|
17
|
+
you should have received a copy of the gnu general public license
|
|
18
|
+
along with fastruby. if not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
|
|
20
|
+
=end
|
|
21
|
+
|
|
22
|
+
module FastRuby
|
|
23
|
+
module Modules
|
|
24
|
+
def self.load_all(name = "")
|
|
25
|
+
Dir.glob(FastRuby.fastruby_load_path + "/fastruby/modules/#{name}/**/*.rb").each do |path|
|
|
26
|
+
require path
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/lib/fastruby/object.rb
CHANGED
|
@@ -22,6 +22,7 @@ require "fastruby/builder"
|
|
|
22
22
|
require "fastruby/getlocals"
|
|
23
23
|
require "fastruby/method_extension"
|
|
24
24
|
require "fastruby/cache/cache"
|
|
25
|
+
require "fastruby/sexp_extension"
|
|
25
26
|
require "fastruby"
|
|
26
27
|
require "digest"
|
|
27
28
|
require "method_source"
|
|
@@ -58,10 +59,44 @@ module FastRuby
|
|
|
58
59
|
end
|
|
59
60
|
end
|
|
60
61
|
|
|
62
|
+
class Object
|
|
63
|
+
def fastruby?
|
|
64
|
+
false
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
61
68
|
class Object
|
|
62
69
|
|
|
63
70
|
def infer(a); self; end
|
|
64
|
-
def fastruby(
|
|
71
|
+
def fastruby(*arguments, &blk)
|
|
72
|
+
if blk
|
|
73
|
+
method_container = Class.new
|
|
74
|
+
class << method_container
|
|
75
|
+
attr_accessor :_self
|
|
76
|
+
attr_accessor :fastruby_options
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
method_container._self = self
|
|
80
|
+
method_container.fastruby_options = arguments.inject({},&:merge)
|
|
81
|
+
|
|
82
|
+
def method_container.method_added(mname)
|
|
83
|
+
m = instance_method(mname)
|
|
84
|
+
if fastruby_options[:fastruby_only]
|
|
85
|
+
tree = FastRuby::FastRubySexp.parse m.source
|
|
86
|
+
FastRuby.set_tree(_self, tree[1], tree, fastruby_options)
|
|
87
|
+
else
|
|
88
|
+
@_self.fastruby m.source, fastruby_options
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
method_container.class_eval(&blk)
|
|
93
|
+
|
|
94
|
+
return nil
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
options_hashes = arguments[1..-1]
|
|
98
|
+
argument = arguments.first
|
|
99
|
+
|
|
65
100
|
options_hash = {:validate_lvar_types => true}
|
|
66
101
|
options_hashes.each do |opt|
|
|
67
102
|
options_hash.merge!(opt)
|
|
@@ -69,13 +104,14 @@ class Object
|
|
|
69
104
|
|
|
70
105
|
tree = nil
|
|
71
106
|
|
|
72
|
-
require "fastruby/fastruby_sexp"
|
|
107
|
+
require "fastruby/fastruby_sexp" unless defined? FastRuby::FastRubySexp
|
|
73
108
|
if argument.instance_of? FastRuby::FastRubySexp
|
|
74
109
|
tree = argument
|
|
75
110
|
elsif argument.instance_of? String
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
111
|
+
unless defined? RubyParser
|
|
112
|
+
require "rubygems"
|
|
113
|
+
require "ruby_parser"
|
|
114
|
+
end
|
|
79
115
|
tree = RubyParser.new.parse(argument).to_fastruby_sexp
|
|
80
116
|
else
|
|
81
117
|
raise ArgumentError
|
|
@@ -100,7 +136,7 @@ class Object
|
|
|
100
136
|
self_ = options_hash[:self]
|
|
101
137
|
self_ = self unless self_.instance_of? Class
|
|
102
138
|
|
|
103
|
-
FastRuby.set_tree(self_, method_name, tree, options_hash
|
|
139
|
+
FastRuby.set_tree(self_, method_name, tree, options_hash)
|
|
104
140
|
|
|
105
141
|
class << self
|
|
106
142
|
$metaclass = self
|