fastruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/fastruby.rb ADDED
@@ -0,0 +1,24 @@
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
+ require "fastruby/translator"
22
+ require "fastruby/object"
23
+ require "fastruby/exceptions"
24
+
data/spec/base_spec.rb ADDED
@@ -0,0 +1,357 @@
1
+ require "fastruby"
2
+
3
+ class X
4
+ fastruby "
5
+ def foo(a,b)
6
+ return a+b
7
+ end
8
+ "
9
+ def foo2(a,b)
10
+ return a+b
11
+ end
12
+
13
+ fastruby "
14
+ def foo3(a)
15
+ 9
16
+ end
17
+ "
18
+
19
+ end
20
+
21
+ describe FastRuby, "fastruby" do
22
+
23
+ def self.test_foo(a,b)
24
+ it "should execute a method with + of #{a.class}" do
25
+ x = X.new
26
+ x.foo(a,b).should be == x.foo2(a,b)
27
+ end
28
+ end
29
+
30
+ test_foo(5353531,6000000)
31
+ test_foo("5353531","6000000")
32
+ test_foo([1,2,3], [5,6])
33
+
34
+ it "methods without return should return last expression result" do
35
+ X.new.foo3(0).should be == 9
36
+ end
37
+
38
+ class ::X2
39
+ fastruby "
40
+ def foo
41
+ 0
42
+ end
43
+ "
44
+ end
45
+
46
+ it "methods without arguments should be called" do
47
+ ::X2.new.foo.should be == 0
48
+ end
49
+
50
+ class ::X3
51
+ fastruby "
52
+ def foo
53
+ while false
54
+ end
55
+ 0
56
+ end
57
+ "
58
+ end
59
+
60
+ it "should execute methods with while" do
61
+ ::X3.new.foo.should be == 0
62
+ end
63
+
64
+ class ::X4
65
+ fastruby "
66
+ def foo
67
+ i = 10
68
+ i
69
+ end
70
+ "
71
+ end
72
+
73
+ it "should assign and read locals" do
74
+ ::X4.new.foo.should be == 10
75
+ end
76
+
77
+ class ::X5
78
+ fastruby "
79
+ def foo
80
+ i = 10
81
+ while i > 0
82
+ i = i - 1
83
+ end
84
+ 0
85
+ end
86
+ "
87
+ end
88
+
89
+ it "should run 10 iterations" do
90
+ ::X5.new.foo.should be == 0
91
+ end
92
+
93
+ class ::A1
94
+ fastruby "
95
+ def foo
96
+ i = 9
97
+ end
98
+ "
99
+ end
100
+
101
+ it "should compile a methd with lvar assignment as the last instruction" do
102
+ ::A1.new.foo.should be == 9
103
+ end
104
+
105
+ class ::B2
106
+ fastruby "
107
+ def foo
108
+ self
109
+ end
110
+ "
111
+ end
112
+ class ::A2
113
+ fastruby "
114
+ def foo(b2)
115
+ b2.foo
116
+ end
117
+ "
118
+ end
119
+
120
+ it "should read self of nested frame" do
121
+ b2 = ::B2.new
122
+ ::A2.new.foo(b2).should be == b2
123
+ end
124
+
125
+ class ::A3
126
+ fastruby "
127
+ def foo(x)
128
+ x.to_i(16)
129
+ end
130
+ "
131
+ end
132
+
133
+ it "should execute native methods with variable arguments" do
134
+ ::A3.new.foo("40").should be == 64
135
+ end
136
+
137
+ class ::A4
138
+ fastruby "
139
+ def foo(x)
140
+ x.to_i
141
+ end
142
+ "
143
+ end
144
+
145
+ it "should execute native methods with variable arguments (and no arguments is passed)" do
146
+ ::A4.new.foo("40").should be == 40
147
+ end
148
+
149
+ class ::A5
150
+ fastruby "
151
+ def bar(x)
152
+ x
153
+ end
154
+ "
155
+
156
+ fastruby "
157
+ def foo(x,a,b)
158
+ bar(
159
+ if (x)
160
+ a
161
+ else
162
+ b
163
+ end
164
+ )
165
+ end
166
+ "
167
+ end
168
+
169
+ it "should compile inline if when passed as argument in a method call" do
170
+ ::A5.new.foo(true,11,12).should be == 11
171
+ end
172
+
173
+ class ::A6
174
+ fastruby "
175
+ def bar(x)
176
+ x
177
+ end
178
+ "
179
+
180
+ fastruby "
181
+ def foo(x,a,b)
182
+ bar(
183
+ if (x)
184
+ x.to_s
185
+ a
186
+ else
187
+ x.to_s
188
+ b
189
+ end
190
+ )
191
+ end
192
+ "
193
+ end
194
+
195
+ it "should compile inline if when passed as argument in a method call. if as many lines" do
196
+ ::A6.new.foo(true,11,12).should be == 11
197
+ end
198
+
199
+ class ::A7
200
+ fastruby "
201
+ def bar(x)
202
+ x
203
+ end
204
+ "
205
+
206
+ fastruby "
207
+ def foo(x,a,b)
208
+ bar(
209
+ if (x)
210
+ x.to_s
211
+ a
212
+ else
213
+ x.to_s
214
+ b
215
+ end
216
+ ) {
217
+ }
218
+ end
219
+ "
220
+ end
221
+
222
+ it "should compile inline if when passed as argument in a method call with block. if has many lines" do
223
+ ::A7.new.foo(true,11,12).should be == 11
224
+ end
225
+
226
+ class ::A8
227
+ fastruby "
228
+ def foo(x)
229
+ a = if (x)
230
+ x.to_s
231
+ 1
232
+ else
233
+ x.to_s
234
+ 2
235
+ end
236
+
237
+ a
238
+ end
239
+ "
240
+ end
241
+
242
+ it "should compile inline if at lvar assignment" do
243
+ ::A8.new.foo(true).should be == 1
244
+ ::A8.new.foo(false).should be == 2
245
+ end
246
+
247
+ class ::A9
248
+ fastruby "
249
+ def foo(x)
250
+ if (x)
251
+ x.to_s
252
+ 1
253
+ else
254
+ x.to_s
255
+ 2
256
+ end
257
+ end
258
+ "
259
+ end
260
+
261
+ it "should compile inline if at end of method" do
262
+ ::A9.new.foo(true).should be == 1
263
+ ::A9.new.foo(false).should be == 2
264
+ end
265
+
266
+ class ::A10
267
+ fastruby '
268
+ def foo
269
+ a = nil
270
+ inline_c "plocals->a = INT2FIX(143)"
271
+ a
272
+ end
273
+ '
274
+ end
275
+
276
+ it "should compile inline C when using inline_c directive" do
277
+ ::A10.new.foo().should be == 143;
278
+ end
279
+
280
+ class ::A11
281
+ fastruby '
282
+ def foo(b)
283
+ a = b
284
+ inline_c " if (plocals->a == Qnil) {
285
+ plocals->a = INT2FIX(43);
286
+ } else {
287
+ plocals->a = INT2FIX(44);
288
+ }
289
+ "
290
+ a
291
+ end
292
+ '
293
+ end
294
+
295
+ it "should compile inline C if when using inline_c directive" do
296
+ ::A11.new.foo(nil).should be == 43;
297
+ ::A11.new.foo(true).should be == 44;
298
+ end
299
+
300
+ class ::A12
301
+ fastruby '
302
+ def foo(b)
303
+ a = b
304
+ x = inline_c(" if (plocals->a == Qnil) {
305
+ plocals->a = INT2FIX(43);
306
+ } else {
307
+ plocals->a = INT2FIX(44);
308
+ }
309
+ ")
310
+ a
311
+ end
312
+ '
313
+ end
314
+
315
+ it "should compile inline C when it is used as rvalue and return nil when no return is specified" do
316
+ ::A12.new.foo(55).should be == 44;
317
+ end
318
+
319
+ class ::A13
320
+ fastruby '
321
+ def foo(b)
322
+ a = b
323
+ x = inline_c(" if (plocals->a == Qnil) {
324
+ plocals->a = INT2FIX(43);
325
+ } else {
326
+ plocals->a = INT2FIX(44);
327
+ }
328
+ ")
329
+ x
330
+ end
331
+ '
332
+ end
333
+
334
+ it "should compile inline C when it is used as rvalue and assign nil if not return is specified" do
335
+ ::A13.new.foo(55).should be == nil;
336
+ end
337
+
338
+ class ::A14
339
+ fastruby '
340
+ def foo(b)
341
+ a = b
342
+ x = inline_c(" if (plocals->a == Qnil) {
343
+ return INT2FIX(43);
344
+ } else {
345
+ return INT2FIX(44);
346
+ }
347
+ ")
348
+ x
349
+ end
350
+ '
351
+ end
352
+
353
+ it "should compile inline C when it is used as rvalue and assign the returned expression" do
354
+ ::A14.new.foo(55).should be == 44;
355
+ end
356
+
357
+ end