fastruby 0.0.17 → 0.0.18

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.
Files changed (67) hide show
  1. data/CHANGELOG +10 -0
  2. data/Rakefile +4 -2
  3. data/benchmarks/benchmark.rb +20 -44
  4. data/benchmarks/benchmark.rb~ +47 -0
  5. data/benchmarks/benchmark2.rb +19 -45
  6. data/benchmarks/benchmark2.rb~ +46 -0
  7. data/benchmarks/benchmark3.rb +19 -45
  8. data/benchmarks/benchmark3.rb~ +46 -0
  9. data/benchmarks/benchmark4.rb +30 -65
  10. data/benchmarks/benchmark4.rb~ +61 -0
  11. data/benchmarks/benchmark5.rb +25 -55
  12. data/benchmarks/benchmark5.rb~ +54 -0
  13. data/benchmarks/benchmark6.rb +19 -40
  14. data/benchmarks/benchmark6.rb~ +41 -0
  15. data/benchmarks/benchmark7.rb +23 -52
  16. data/benchmarks/benchmark7.rb~ +48 -0
  17. data/ext/fastruby_base/fastruby_base.inl +1 -0
  18. data/lib/fastruby/builder.rb +128 -76
  19. data/lib/fastruby/cache/cache.rb +9 -5
  20. data/lib/fastruby/fastruby_sexp.rb +18 -0
  21. data/lib/fastruby/inliner/inliner.rb +68 -0
  22. data/lib/fastruby/inliner/modules/call.rb +150 -0
  23. data/lib/fastruby/inliner/modules/recursive.rb +35 -0
  24. data/lib/fastruby/object.rb +17 -32
  25. data/lib/fastruby/reductor/modules/case.rb +49 -0
  26. data/lib/{fastruby.rb~ → fastruby/reductor/modules/for.rb} +11 -13
  27. data/lib/fastruby/reductor/modules/nontree.rb +31 -0
  28. data/lib/fastruby/reductor/modules/recursive.rb +31 -0
  29. data/lib/fastruby/reductor/reductor.rb +39 -0
  30. data/lib/fastruby/set_tree.rb +7 -1
  31. data/lib/fastruby/sexp_extension.rb +6 -0
  32. data/lib/fastruby/translator/modules/block.rb +4 -4
  33. data/lib/fastruby/translator/modules/call.rb +9 -26
  34. data/lib/fastruby/translator/modules/defn.rb +5 -3
  35. data/lib/fastruby/translator/modules/directive.rb +42 -0
  36. data/lib/fastruby/translator/modules/exceptions.rb +12 -30
  37. data/lib/fastruby/translator/modules/flow.rb +33 -83
  38. data/lib/fastruby/translator/modules/iter.rb +4 -7
  39. data/lib/fastruby/translator/modules/literal.rb +11 -25
  40. data/lib/fastruby/translator/modules/logical.rb +5 -3
  41. data/lib/fastruby/translator/modules/method_group.rb +4 -3
  42. data/lib/fastruby/translator/modules/nonlocal.rb +7 -5
  43. data/lib/fastruby/translator/modules/static.rb +242 -0
  44. data/lib/fastruby/translator/modules/variable.rb +16 -9
  45. data/lib/fastruby/translator/scope_mode_helper.rb +2 -27
  46. data/lib/fastruby/translator/translator.rb +131 -60
  47. data/lib/fastruby/translator/translator_modules.rb +6 -2
  48. data/lib/fastruby.rb +1 -1
  49. data/spec/reductor/base_spec.rb +46 -0
  50. data/spec/ruby/base_spec.rb~ +394 -0
  51. data/spec/ruby/block/arguments_spec.rb~ +214 -0
  52. data/spec/ruby/block/proc_as_block_spec.rb~ +23 -0
  53. data/spec/ruby/block/retry_spec.rb~ +43 -0
  54. data/spec/ruby/block_spec.rb~ +520 -0
  55. data/spec/ruby/defn/replacement_spec.rb~ +102 -0
  56. data/spec/ruby/integrity_spec.rb~ +40 -0
  57. data/spec/ruby/singleton_spec.rb~ +76 -0
  58. data/spec/scope_mode/base_spec.rb +14 -5
  59. data/spec/scope_mode/block_spec.rb +18 -9
  60. data/spec/scope_mode/call_spec.rb +11 -2
  61. data/spec/scope_mode/exception_spec.rb +11 -2
  62. data/spec/scope_mode/flow_spec.rb +18 -8
  63. data/spec/scope_mode/optimization_spec.rb +21 -13
  64. data/spec/static/base_spec.rb +54 -0
  65. data/spec/static/flow_spec.rb +48 -0
  66. data/spec/static/operator_spec.rb +104 -0
  67. metadata +58 -8
@@ -6,8 +6,17 @@ require "fastruby/translator/scope_mode_helper"
6
6
  $parser = RubyParser.new
7
7
 
8
8
  describe FastRuby::ScopeModeHelper, "scope mode helper" do
9
- it "method with only ONE call and read after call should return :dag scope mode" do
9
+
10
+ def get_scope_mode(tree)
10
11
  FastRuby::ScopeModeHelper.get_scope_mode(
12
+ FastRuby::Reductor.new.reduce(
13
+ FastRuby::FastRubySexp.from_sexp(tree)
14
+ )
15
+ )
16
+ end
17
+
18
+ it "method with only ONE call and read after call should return :dag scope mode" do
19
+ get_scope_mode(
11
20
  $parser.parse "def foo(a,b,c)
12
21
  a+b
13
22
  c
@@ -16,7 +25,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
16
25
  end
17
26
 
18
27
  it "method with only ONE call and self read after call should return :dag scope mode" do
19
- FastRuby::ScopeModeHelper.get_scope_mode(
28
+ get_scope_mode(
20
29
  $parser.parse "def foo(a,b,c)
21
30
  a+b
22
31
  self
@@ -25,7 +34,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
25
34
  end
26
35
 
27
36
  it "method with only ONE call and yield after call should return :dag scope mode" do
28
- FastRuby::ScopeModeHelper.get_scope_mode(
37
+ get_scope_mode(
29
38
  $parser.parse "def foo(a,b,c)
30
39
  a+b
31
40
  yield
@@ -34,7 +43,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
34
43
  end
35
44
 
36
45
  it "method with only ONE call and local call after call should return :dag scope mode" do
37
- FastRuby::ScopeModeHelper.get_scope_mode(
46
+ get_scope_mode(
38
47
  $parser.parse "def foo(a,b,c)
39
48
  a+b
40
49
  foo
@@ -43,7 +52,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
43
52
  end
44
53
 
45
54
  it "method call AFTER read inside while should return :dag scope" do
46
- FastRuby::ScopeModeHelper.get_scope_mode(
55
+ get_scope_mode(
47
56
  $parser.parse "def foo(a,b)
48
57
  while (true)
49
58
  a=b
@@ -6,8 +6,17 @@ require "fastruby/translator/scope_mode_helper"
6
6
  $parser = RubyParser.new
7
7
 
8
8
  describe FastRuby::ScopeModeHelper, "scope mode helper" do
9
- it "iter call with block accessing locals should return dag" do
9
+
10
+ def get_scope_mode(tree)
10
11
  FastRuby::ScopeModeHelper.get_scope_mode(
12
+ FastRuby::Reductor.new.reduce(
13
+ FastRuby::FastRubySexp.from_sexp(tree)
14
+ )
15
+ )
16
+ end
17
+
18
+ it "iter call with block accessing locals should return dag" do
19
+ get_scope_mode(
11
20
  $parser.parse "def foo(a)
12
21
  bar do
13
22
  a
@@ -17,7 +26,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
17
26
  end
18
27
 
19
28
  it "iter call with block doing yield should return dag" do
20
- FastRuby::ScopeModeHelper.get_scope_mode(
29
+ get_scope_mode(
21
30
  $parser.parse "def foo(a)
22
31
  bar do
23
32
  yield
@@ -27,7 +36,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
27
36
  end
28
37
 
29
38
  it "iter call with block with arguments should return dag" do
30
- FastRuby::ScopeModeHelper.get_scope_mode(
39
+ get_scope_mode(
31
40
  $parser.parse "def foo(a)
32
41
  bar do |x|
33
42
  end
@@ -36,7 +45,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
36
45
  end
37
46
 
38
47
  it "iter call with block writing local variable should return dag" do
39
- FastRuby::ScopeModeHelper.get_scope_mode(
48
+ get_scope_mode(
40
49
  $parser.parse "def foo(a)
41
50
  bar do
42
51
  a = 87
@@ -46,7 +55,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
46
55
  end
47
56
 
48
57
  it "two iter call, one empty and the second with yield should return dag" do
49
- FastRuby::ScopeModeHelper.get_scope_mode(
58
+ get_scope_mode(
50
59
  $parser.parse "def foo(a)
51
60
  bar do
52
61
  end
@@ -59,7 +68,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
59
68
  end
60
69
 
61
70
  it "lambda with yield must return :dag" do
62
- FastRuby::ScopeModeHelper.get_scope_mode(
71
+ get_scope_mode(
63
72
  $parser.parse " def foo
64
73
  lambda {
65
74
  yield
@@ -70,7 +79,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
70
79
  end
71
80
 
72
81
  it "method with return from inside block return :dag" do
73
- FastRuby::ScopeModeHelper.get_scope_mode(
82
+ get_scope_mode(
74
83
  $parser.parse " def bar
75
84
  foo do
76
85
  return 9
@@ -81,7 +90,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
81
90
  end
82
91
 
83
92
  it "method with self from inside block return :dag" do
84
- FastRuby::ScopeModeHelper.get_scope_mode(
93
+ get_scope_mode(
85
94
  $parser.parse " def bar
86
95
  foo do
87
96
  self
@@ -92,7 +101,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
92
101
  end
93
102
 
94
103
  it "local call from inside block should return :dag" do
95
- FastRuby::ScopeModeHelper.get_scope_mode(
104
+ get_scope_mode(
96
105
  $parser.parse " def bar
97
106
  foo do
98
107
  print
@@ -6,8 +6,17 @@ require "fastruby/translator/scope_mode_helper"
6
6
  $parser = RubyParser.new
7
7
 
8
8
  describe FastRuby::ScopeModeHelper, "scope mode helper" do
9
- it "method with two nested calls refering local vars should return :dag scope mode" do
9
+
10
+ def get_scope_mode(tree)
10
11
  FastRuby::ScopeModeHelper.get_scope_mode(
12
+ FastRuby::Reductor.new.reduce(
13
+ FastRuby::FastRubySexp.from_sexp(tree)
14
+ )
15
+ )
16
+ end
17
+
18
+ it "method with two nested calls refering local vars should return :dag scope mode" do
19
+ get_scope_mode(
11
20
  $parser.parse "def foo(a,b,c)
12
21
  a+b+c
13
22
  end"
@@ -15,7 +24,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
15
24
  end
16
25
 
17
26
  it "method with two nested calls refering local vars should return :dag scope mode" do
18
- FastRuby::ScopeModeHelper.get_scope_mode(
27
+ get_scope_mode(
19
28
  $parser.parse "def foo(a,b,c)
20
29
  a.foo(b){}.foo(c){}
21
30
  end"
@@ -6,8 +6,17 @@ require "fastruby/translator/scope_mode_helper"
6
6
  $parser = RubyParser.new
7
7
 
8
8
  describe FastRuby::ScopeModeHelper, "scope mode helper" do
9
- it "method with read on begin body, call on rescue body and retry should return :dag scope mode" do
9
+
10
+ def get_scope_mode(tree)
10
11
  FastRuby::ScopeModeHelper.get_scope_mode(
12
+ FastRuby::Reductor.new.reduce(
13
+ FastRuby::FastRubySexp.from_sexp(tree)
14
+ )
15
+ )
16
+ end
17
+
18
+ it "method with read on begin body, call on rescue body and retry should return :dag scope mode" do
19
+ get_scope_mode(
11
20
  $parser.parse "def foo(a,b,c)
12
21
  begin
13
22
  b
@@ -20,7 +29,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
20
29
  end
21
30
 
22
31
  it "method with read on begin body and retry should return :dag scope mode" do
23
- FastRuby::ScopeModeHelper.get_scope_mode(
32
+ get_scope_mode(
24
33
  $parser.parse "def foo(a,b,c)
25
34
  begin
26
35
  nil.bar(b)
@@ -2,12 +2,22 @@ require "fastruby"
2
2
  require "sexp"
3
3
  require "ruby_parser"
4
4
  require "fastruby/translator/scope_mode_helper"
5
+ require "fastruby/reductor/reductor"
5
6
 
6
7
  $parser = RubyParser.new
7
8
 
8
9
  describe FastRuby::ScopeModeHelper, "scope mode helper" do
9
- it "possible read on if after call should return :dag scope mode" do
10
+
11
+ def get_scope_mode(tree)
10
12
  FastRuby::ScopeModeHelper.get_scope_mode(
13
+ FastRuby::Reductor.new.reduce(
14
+ FastRuby::FastRubySexp.from_sexp(tree)
15
+ )
16
+ )
17
+ end
18
+
19
+ it "possible read on if after call should return :dag scope mode" do
20
+ get_scope_mode(
11
21
  $parser.parse "def foo(a,b,c)
12
22
  if (a > 0)
13
23
  c
@@ -17,7 +27,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
17
27
  end
18
28
 
19
29
  it "possible read on case after call should return :dag scope mode" do
20
- FastRuby::ScopeModeHelper.get_scope_mode(
30
+ get_scope_mode(
21
31
  $parser.parse "def foo(a,b,c)
22
32
  case (a > 0)
23
33
  when 0
@@ -28,7 +38,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
28
38
  end
29
39
 
30
40
  it "possible read on case (on enum) after call should return :dag scope mode" do
31
- FastRuby::ScopeModeHelper.get_scope_mode(
41
+ get_scope_mode(
32
42
  $parser.parse "def foo(a,b,c)
33
43
  case (a > 0)
34
44
  when c
@@ -39,7 +49,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
39
49
  end
40
50
 
41
51
  it "for with local read and call should return :dag scope mode" do
42
- FastRuby::ScopeModeHelper.get_scope_mode(
52
+ get_scope_mode(
43
53
  $parser.parse "def foo(a,b,c)
44
54
  for a in b
45
55
  foo
@@ -50,7 +60,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
50
60
  end
51
61
 
52
62
  it "empty for with local read should return :dag scope mode (because for is a iter call to each with one argument)" do
53
- FastRuby::ScopeModeHelper.get_scope_mode(
63
+ get_scope_mode(
54
64
  $parser.parse "def foo(a,b,c)
55
65
  for a in b
56
66
  end
@@ -59,7 +69,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
59
69
  end
60
70
 
61
71
  it "case with a when should act as call and local read after case when should return :dag" do
62
- FastRuby::ScopeModeHelper.get_scope_mode(
72
+ get_scope_mode(
63
73
  $parser.parse "def foo(a,b,c)
64
74
  case b
65
75
  when c
@@ -70,7 +80,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
70
80
  end
71
81
 
72
82
  it "case with a when should act as call and local read after case when should return :dag" do
73
- FastRuby::ScopeModeHelper.get_scope_mode(
83
+ get_scope_mode(
74
84
  $parser.parse "def foo(a,b,c)
75
85
  case b
76
86
  when c
@@ -83,7 +93,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
83
93
  end
84
94
 
85
95
  it "case with two call (when) after read should return :dag scope" do
86
- FastRuby::ScopeModeHelper.get_scope_mode(
96
+ get_scope_mode(
87
97
  $parser.parse "def foo(a,b,c)
88
98
  case a
89
99
  when b # call to a.===(b)
@@ -6,14 +6,22 @@ require "fastruby/translator/scope_mode_helper"
6
6
  $parser = RubyParser.new
7
7
 
8
8
  describe FastRuby::ScopeModeHelper, "scope mode helper" do
9
- it "empty method should return :linear scope mode" do
9
+ def get_scope_mode(tree)
10
10
  FastRuby::ScopeModeHelper.get_scope_mode(
11
+ FastRuby::Reductor.new.reduce(
12
+ FastRuby::FastRubySexp.from_sexp(tree)
13
+ )
14
+ )
15
+ end
16
+
17
+ it "empty method should return :linear scope mode" do
18
+ get_scope_mode(
11
19
  $parser.parse "def foo(); end"
12
20
  ).should be == :linear
13
21
  end
14
22
 
15
23
  it "method without calls should return :linear scope mode" do
16
- FastRuby::ScopeModeHelper.get_scope_mode(
24
+ get_scope_mode(
17
25
  $parser.parse "def foo(a,b,c)
18
26
  a
19
27
  end"
@@ -21,7 +29,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
21
29
  end
22
30
 
23
31
  it "method with only ONE call should return :linear scope mode" do
24
- FastRuby::ScopeModeHelper.get_scope_mode(
32
+ get_scope_mode(
25
33
  $parser.parse "def foo(a,b)
26
34
  a+b
27
35
  end"
@@ -29,7 +37,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
29
37
  end
30
38
 
31
39
  it "method call AFTER read should return :linear scope" do
32
- FastRuby::ScopeModeHelper.get_scope_mode(
40
+ get_scope_mode(
33
41
  $parser.parse "def foo(a,b)
34
42
  a=b
35
43
  a+b
@@ -38,7 +46,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
38
46
  end
39
47
 
40
48
  it "empty if should return :linear scope mode" do
41
- FastRuby::ScopeModeHelper.get_scope_mode(
49
+ get_scope_mode(
42
50
  $parser.parse "def foo(a,b,c)
43
51
  if (a)
44
52
  end
@@ -46,7 +54,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
46
54
  ).should be == :linear
47
55
  end
48
56
  it "iter call with empty block should return linear" do
49
- FastRuby::ScopeModeHelper.get_scope_mode(
57
+ get_scope_mode(
50
58
  $parser.parse "def foo
51
59
  bar do
52
60
  end
@@ -55,7 +63,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
55
63
  end
56
64
 
57
65
  it "return of simple call should return :linear" do
58
- FastRuby::ScopeModeHelper.get_scope_mode(
66
+ get_scope_mode(
59
67
  $parser.parse "def foo(a,b)
60
68
  return a+b
61
69
  end"
@@ -63,7 +71,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
63
71
  end
64
72
 
65
73
  it "call on if body and read on condition should return :linear (no read after call risk)" do
66
- FastRuby::ScopeModeHelper.get_scope_mode(
74
+ get_scope_mode(
67
75
  $parser.parse "def foo(a,b)
68
76
  if a
69
77
  b.foo
@@ -73,7 +81,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
73
81
  end
74
82
 
75
83
  it "call on if body and read on else body should return :linear (no read after call risk)" do
76
- FastRuby::ScopeModeHelper.get_scope_mode(
84
+ get_scope_mode(
77
85
  $parser.parse "def foo(a,b)
78
86
  if true
79
87
  b
@@ -85,7 +93,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
85
93
  end
86
94
 
87
95
  it "method with read on begin body should return :linear scope mode" do
88
- FastRuby::ScopeModeHelper.get_scope_mode(
96
+ get_scope_mode(
89
97
  $parser.parse "def foo(a,b,c)
90
98
  begin
91
99
  nil.bar(b)
@@ -96,7 +104,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
96
104
  end
97
105
 
98
106
  it "method with read on begin body and call on rescue body should return :linear scope mode" do
99
- FastRuby::ScopeModeHelper.get_scope_mode(
107
+ get_scope_mode(
100
108
  $parser.parse "def foo(a,b,c)
101
109
  begin
102
110
  b
@@ -108,7 +116,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
108
116
  end
109
117
 
110
118
  it "case with call (when) after read should return :linear scope" do
111
- FastRuby::ScopeModeHelper.get_scope_mode(
119
+ get_scope_mode(
112
120
  $parser.parse "def foo(a,b,c)
113
121
  case a
114
122
  when b
@@ -119,7 +127,7 @@ describe FastRuby::ScopeModeHelper, "scope mode helper" do
119
127
  end
120
128
 
121
129
  it "read of variable AFTER write without call between them should return :linear scope" do
122
- FastRuby::ScopeModeHelper.get_scope_mode(
130
+ get_scope_mode(
123
131
  $parser.parse "def foo(a,b)
124
132
  a+b
125
133
  c=55
@@ -0,0 +1,54 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby::FastRubySexp, "FastRubySexp" do
4
+
5
+ it "should accept _static keyword to compile static C calls" do
6
+ fastruby "
7
+ class STATICX1
8
+ def foo(a)
9
+ _static {
10
+ INT2FIX(FIX2INT(a))
11
+ }
12
+ end
13
+ end
14
+ "
15
+
16
+ STATICX1.new.foo(100).should be == 100
17
+ end
18
+
19
+ it "should allow native inmediates inside static blocks as native C semantic" do
20
+ fastruby "
21
+ class STATICX2
22
+ def foo
23
+ _static {
24
+ INT2FIX(100)
25
+ }
26
+ end
27
+ end
28
+ "
29
+
30
+ STATICX2.new.foo.should be == 100
31
+ end
32
+
33
+ it "should accept _dynamic to activate ruby sematic inside _static block" do
34
+ fastruby "
35
+ class STATICX3
36
+ def bar(a)
37
+ a+1
38
+ end
39
+
40
+ def foo(a)
41
+ _static {
42
+ INT2FIX(
43
+ FIX2INT(
44
+ _dynamic{bar(a)}
45
+ )
46
+ )
47
+ }
48
+ end
49
+ end
50
+ "
51
+
52
+ STATICX3.new.foo(100).should be == 101
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby::FastRubySexp, "FastRubySexp" do
4
+ fastruby "
5
+ class STATICFLOW1
6
+ def foo(a)
7
+ _static do
8
+ if (FIX2INT(a))
9
+ INT2FIX(20)
10
+ else
11
+ INT2FIX(40)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ "
17
+
18
+
19
+ def self.test_static_if(*args)
20
+ args.each do |arg|
21
+ it "should should evaluate #{arg} as #{arg != 0} on static if" do
22
+ STATICFLOW1.new.foo(arg).should be == (arg == 0 ? 40 : 20)
23
+ end
24
+ end
25
+ end
26
+
27
+ test_static_if false.__id__, true.__id__, nil.__id__, 10, 11
28
+
29
+ it "should allow static while" do
30
+ fastruby "
31
+ class STATICFLOW2
32
+ def foo(a)
33
+ ret = 0
34
+ _static do
35
+ while (FIX2INT(a) > 0)
36
+ a = INT2FIX(FIX2INT(a) - 1)
37
+ ret = INT2FIX(FIX2INT(ret) + 1)
38
+ end
39
+ end
40
+ ret
41
+ end
42
+ end
43
+ "
44
+
45
+ STATICFLOW2.new.foo(7).should be == 7
46
+ STATICFLOW2.new.foo(0).should be == 0
47
+ end
48
+ end
@@ -0,0 +1,104 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby::FastRubySexp, "FastRubySexp" do
4
+
5
+ def self.test_binary_operator(op, classname, input, expected)
6
+ it "should accept native operator #{op} with two numbers input:#{input} expected:#{expected}}" do
7
+ fastruby "
8
+ class #{classname}
9
+ def foo(a)
10
+ _static {
11
+ INT2FIX(FIX2INT(a)#{op}2)
12
+ }
13
+ end
14
+ end
15
+ "
16
+
17
+ eval(classname).new.foo(input).should be == expected
18
+ end
19
+ end
20
+
21
+ test_binary_operator("+", "STATICX1_1", 10, 12)
22
+ test_binary_operator("-", "STATICX1_2", 10, 8)
23
+ test_binary_operator("*", "STATICX1_3", 10, 20)
24
+ test_binary_operator("/", "STATICX1_4", 10, 5)
25
+
26
+ (3..4).each do |i|
27
+ test_binary_operator(">", "STATICX1_5_#{i}", i, 1)
28
+ end
29
+
30
+ (-1..2).each do |i|
31
+ test_binary_operator(">", "STATICX1_6_#{i.to_s.gsub('-','__')}", i, 0)
32
+ end
33
+
34
+ (-1..1).each do |i|
35
+ test_binary_operator("<", "STATICX1_6_#{i.to_s.gsub('-','__')}", i, 1)
36
+ end
37
+
38
+ (2..3).each do |i|
39
+ test_binary_operator("<", "STATICX1_7_#{i}", i, 0)
40
+ end
41
+
42
+
43
+ (2..4).each do |i|
44
+ test_binary_operator(">=", "STATICX1_8_#{i}", i, 1)
45
+ end
46
+
47
+ (-1..1).each do |i|
48
+ test_binary_operator(">=", "STATICX1_9_#{i.to_s.gsub('-','__')}", i, 0)
49
+ end
50
+
51
+ (-1..2).each do |i|
52
+ test_binary_operator("<=", "STATICX1_10_#{i.to_s.gsub('-','__')}", i, 1)
53
+ end
54
+
55
+ (3..4).each do |i|
56
+ test_binary_operator("<=", "STATICX1_11_#{i}", i, 0)
57
+ end
58
+
59
+
60
+ (3..5).each do |i|
61
+ test_binary_operator("==", "STATICX1_12_#{i}", i, i == 2 ? 1 : 0)
62
+ end
63
+
64
+ (3..5).each do |i|
65
+ test_binary_operator("===", "STATICX1_13_#{i}", i, i == 2 ? 1 : 0)
66
+ end
67
+
68
+ def self.test_bool_operator(code, classname, expected)
69
+ it "should execute boolean operation #{code} expected:#{expected}}" do
70
+ fastruby "
71
+ class #{classname}
72
+ def foo
73
+ _static {
74
+ if (#{code})
75
+ INT2FIX(40)
76
+ else
77
+ INT2FIX(20)
78
+ end
79
+ }
80
+ end
81
+ end
82
+ "
83
+
84
+ eval(classname).new.foo.should be == expected
85
+ end
86
+ end
87
+
88
+ test_bool_operator "4 and 4", "STATIC1_14", 40
89
+ test_bool_operator "4 or 4", "STATIC1_15", 40
90
+
91
+ it "should execute boolean operation not" do
92
+ fastruby "
93
+ class STATIC1_16
94
+ def foo
95
+ _static {
96
+ INT2FIX((not 4))
97
+ }
98
+ end
99
+ end
100
+ "
101
+
102
+ STATIC1_16.new.foo.should be == 0
103
+ end
104
+ end