rubex 0.0.1 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.travis.yml +14 -0
  4. data/CONTRIBUTING.md +101 -0
  5. data/HISTORY.md +3 -0
  6. data/README.md +112 -297
  7. data/REFERENCE.md +753 -0
  8. data/Rakefile +4 -1
  9. data/TUTORIAL.md +234 -0
  10. data/bin/rubex +1 -1
  11. data/docs/_config.yml +1 -0
  12. data/docs/index.html +1 -0
  13. data/examples/c_struct_interface/c_struct_interface.rb +6 -0
  14. data/examples/c_struct_interface/c_struct_interface.rubex +47 -0
  15. data/examples/linked_list/linked_list.rubex +39 -0
  16. data/examples/linked_list/rb_linked_list.rb +8 -0
  17. data/examples/rcsv wrapper/rcsv/README.md +1 -0
  18. data/examples/rcsv wrapper/rcsv/Rakefile +7 -0
  19. data/examples/rcsv wrapper/rcsv/ext/rcsv/extconf.rb +3 -0
  20. data/examples/rcsv wrapper/rcsv/ext/rcsv/rcsv.c +302 -0
  21. data/examples/rcsv wrapper/rcsv/ext/rcsv/rcsv.rubex +124 -0
  22. data/examples/rcsv wrapper/rcsv/lib/rcsv.rb +8 -0
  23. data/examples/rcsv wrapper/rcsv/lib/rcsv.so +0 -0
  24. data/examples/rcsv wrapper/rcsv/lib/rcsv/version.rb +1 -0
  25. data/examples/rcsv wrapper/rcsv/rcsv.gemspec +27 -0
  26. data/examples/rcsv wrapper/rcsv/spec/rcsv.csv +5 -0
  27. data/examples/rcsv wrapper/rcsv/spec/rcsv_spec.rb +17 -0
  28. data/examples/rcsv wrapper/rcsv/spec/spec_helper.rb +6 -0
  29. data/{spec/fixtures/basic_ruby_method/Makefile → examples/rcsv wrapper/rcsv/tmp/x86_64-linux/rcsv/2.3.3/Makefile } +20 -20
  30. data/examples/rcsv wrapper/rcsv/tmp/x86_64-linux/rcsv/2.3.3/rcsv.o +0 -0
  31. data/examples/rcsv wrapper/rcsv/tmp/x86_64-linux/rcsv/2.3.3/rcsv.so +0 -0
  32. data/examples/rcsv wrapper/rcsv/tmp/x86_64-linux/stage/lib/rcsv.so +0 -0
  33. data/lib/rubex.rb +6 -50
  34. data/lib/rubex/ast.rb +1 -3
  35. data/lib/rubex/ast/expression.rb +1257 -8
  36. data/lib/rubex/ast/node.rb +226 -28
  37. data/lib/rubex/ast/statement.rb +1162 -35
  38. data/lib/rubex/ast/top_statement.rb +815 -0
  39. data/lib/rubex/code_writer.rb +103 -26
  40. data/lib/rubex/compiler.rb +72 -0
  41. data/lib/rubex/compiler_config.rb +19 -0
  42. data/lib/rubex/constants.rb +145 -8
  43. data/lib/rubex/data_type.rb +667 -4
  44. data/lib/rubex/error.rb +15 -0
  45. data/lib/rubex/helpers.rb +154 -0
  46. data/lib/rubex/lexer.rex +186 -22
  47. data/lib/rubex/lexer.rex.rb +261 -35
  48. data/lib/rubex/parser.racc +876 -28
  49. data/lib/rubex/parser.racc.rb +2845 -90
  50. data/lib/rubex/rake_task.rb +34 -0
  51. data/lib/rubex/symbol_table/entry.rb +17 -3
  52. data/lib/rubex/symbol_table/scope.rb +298 -25
  53. data/lib/rubex/version.rb +1 -1
  54. data/rubex.gemspec +11 -3
  55. data/spec/basic_ruby_method_spec.rb +15 -21
  56. data/spec/binding_ptr_args_spec.rb +33 -0
  57. data/spec/bitwise_operators_spec.rb +40 -0
  58. data/spec/blocks_spec.rb +35 -0
  59. data/spec/c_bindings_spec.rb +36 -0
  60. data/spec/c_constants_spec.rb +33 -0
  61. data/spec/c_function_ptrs_spec.rb +38 -0
  62. data/spec/c_functions_spec.rb +35 -0
  63. data/spec/c_struct_interface_spec.rb +38 -0
  64. data/spec/call_by_reference_spec.rb +33 -0
  65. data/spec/class_methods_spec.rb +33 -0
  66. data/spec/class_spec.rb +40 -0
  67. data/spec/comments_spec.rb +33 -0
  68. data/spec/default_args_spec.rb +37 -0
  69. data/spec/error_handling_spec.rb +42 -0
  70. data/spec/examples_spec.rb +52 -0
  71. data/spec/expressions_spec.rb +33 -0
  72. data/spec/fixtures/basic_ruby_method/basic_ruby_method.rubex +2 -0
  73. data/spec/fixtures/binding_ptr_args/binding_ptr_args.rubex +30 -0
  74. data/spec/fixtures/bitwise_operators/bitwise_operators.rubex +40 -0
  75. data/spec/fixtures/blocks/blocks.rubex +11 -0
  76. data/spec/fixtures/c_bindings/c_bindings.rubex +58 -0
  77. data/spec/fixtures/c_constants/c_constants.rubex +7 -0
  78. data/spec/fixtures/c_function_ptrs/c_function_ptrs.rubex +52 -0
  79. data/spec/fixtures/c_functions/c_functions.rubex +25 -0
  80. data/spec/fixtures/c_struct_interface/c_struct_interface.rubex +34 -0
  81. data/spec/fixtures/call_by_reference/call_by_reference.rubex +30 -0
  82. data/spec/fixtures/class/class.rubex +20 -0
  83. data/spec/fixtures/class_methods/class_methods.rubex +12 -0
  84. data/spec/fixtures/comments/comments.rubex +9 -0
  85. data/spec/fixtures/default_args/default_args.rubex +11 -0
  86. data/spec/fixtures/error_handling/error_handling.rubex +54 -0
  87. data/spec/fixtures/examples/array_to_hash.rubex +14 -0
  88. data/spec/fixtures/examples/rcsv.csv +5 -0
  89. data/spec/fixtures/examples/rcsv.rubex +329 -0
  90. data/spec/fixtures/expressions/expressions.rubex +10 -0
  91. data/spec/fixtures/if_else/if_else.rubex +77 -0
  92. data/spec/fixtures/implicit_lib_include/implicit_lib_include.rubex +15 -0
  93. data/spec/fixtures/init_ruby_objects_with_literal_syntax/init_ruby_objects_with_literal_syntax.rubex +17 -0
  94. data/spec/fixtures/loops/loops.rubex +33 -0
  95. data/spec/fixtures/recursion/recursion.rubex +9 -0
  96. data/spec/fixtures/ruby_constant_method_calls/ruby_constant_method_calls.rubex +17 -0
  97. data/spec/fixtures/ruby_operators/ruby_operators.rubex +29 -0
  98. data/spec/fixtures/ruby_raise/ruby_raise.rubex +13 -0
  99. data/spec/fixtures/ruby_strings/ruby_strings.rubex +19 -0
  100. data/spec/fixtures/ruby_strings/string_blank_bm.rb +37 -0
  101. data/spec/fixtures/ruby_symbols/ruby_symbols.rubex +12 -0
  102. data/spec/fixtures/ruby_types/ruby_types.rubex +15 -0
  103. data/spec/fixtures/statement_expression/statement_expression.rubex +23 -0
  104. data/spec/fixtures/static_array/static_array.rubex +20 -0
  105. data/spec/fixtures/string_literals/string_literals.rubex +15 -0
  106. data/spec/fixtures/struct/struct.rubex +82 -0
  107. data/spec/fixtures/typecasting/typecasting.rubex +23 -0
  108. data/spec/fixtures/var_declarations/var_declarations.rubex +39 -0
  109. data/spec/if_else_spec.rb +39 -0
  110. data/spec/implicit_lib_include_spec.rb +33 -0
  111. data/spec/init_ruby_objects_with_literal_syntax_spec.rb +39 -0
  112. data/spec/loops_spec.rb +34 -0
  113. data/spec/recursion_spec.rb +35 -0
  114. data/spec/ruby_constant_method_calls_spec.rb +35 -0
  115. data/spec/ruby_operators_spec.rb +40 -0
  116. data/spec/ruby_raise_spec.rb +35 -0
  117. data/spec/ruby_strings_spec.rb +33 -0
  118. data/spec/ruby_symbols_spec.rb +37 -0
  119. data/spec/ruby_types_spec.rb +35 -0
  120. data/spec/spec_helper.rb +54 -1
  121. data/spec/statement_expression_spec.rb +34 -0
  122. data/spec/static_array_spec.rb +33 -0
  123. data/spec/string_literals_spec.rb +34 -0
  124. data/spec/struct_spec.rb +36 -0
  125. data/spec/typecasting_spec.rb +38 -0
  126. data/spec/var_declarions_spec.rb +35 -0
  127. metadata +255 -29
  128. data/lib/rubex/ast/argument_list.rb +0 -20
  129. data/lib/rubex/ast/c_base_type.rb +0 -11
  130. data/lib/rubex/ast/ruby_method_def.rb +0 -84
  131. data/spec/fixtures/basic_ruby_method/basic.rb +0 -3
  132. data/spec/fixtures/basic_ruby_method/basic_ruby_method.c +0 -16
  133. data/spec/fixtures/basic_ruby_method/basic_ruby_method.o +0 -0
  134. data/spec/fixtures/basic_ruby_method/basic_ruby_method.so +0 -0
  135. data/spec/fixtures/basic_ruby_method/extconf.rb +0 -3
@@ -1,40 +1,34 @@
1
1
  require 'spec_helper'
2
+ include Rubex::AST
2
3
 
3
4
  describe Rubex do
4
- context "basic Rubex Ruby callable method" do
5
+ test_case = 'basic_ruby_method'
6
+
7
+ context "Case : #{test_case}" do
5
8
  before do
6
- @path = 'spec/fixtures/basic_ruby_method/basic_ruby_method.rubex'
7
- include Rubex::AST
9
+ @path = path_str test_case
8
10
  end
9
11
 
10
12
  context ".ast" do
11
13
  it "returns a valid Abstract Syntax Tree" do
12
- arguments = ArgumentList.new
13
- arguments.push CBaseType.new('i32', 'b')
14
- arguments.push CBaseType.new('i32', 'a')
15
- method = RubyMethodDef.new('addition', arguments)
16
- expr = Expression::Addition.new 'a', 'b'
17
- statements = Statement::Return.new expr
18
- node = Node.new method
19
-
20
- # TODO: Define == method on all nodes of AST.
21
- # expect(Rubex.ast(@path)).to eq(node)
14
+ t = Rubex::Compiler.ast @path + ".rubex"
22
15
  end
23
16
  end
24
17
 
25
18
  context ".compile" do
26
19
  it "generates valid C code" do
27
- Rubex.compile @path, true
20
+ t, c, e = Rubex::Compiler.compile @path + ".rubex", test: true
28
21
  end
29
22
  end
30
23
 
31
- context ".extconf" do
32
- it "generates extconf for creating Makefile" do
33
- f = "require 'mkmf'\n"
34
- f << "create_makefile('basic_ruby_method/basic_ruby_method')\n"
35
-
36
- expect(Rubex.extconf("basic_ruby_method")).to eq(f)
24
+ context "Black Box testing" do
25
+ it "compiles and checks for valid output" do
26
+ setup_and_teardown_compiled_files(test_case) do
27
+ dir = dir_str test_case
28
+ require_relative "#{dir}/#{test_case}.so"
29
+ expect(addition(4,5)).to eq(9)
30
+ end
37
31
  end
38
32
  end
39
33
  end
40
- end
34
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = "binding_ptr_args"
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates the AST." do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile" do
18
+ it "compiles to C." do
19
+ t, c, e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing" do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ expect(test_function("hello")).to eq(104)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ include Rubex::AST
3
+
4
+ describe Rubex do
5
+ test_case = 'bitwise_operators'
6
+
7
+ context "Case : #{test_case}" do
8
+ before do
9
+ @path = path_str test_case
10
+ end
11
+
12
+ context ".ast" do
13
+ it "returns a valid Abstract Syntax Tree" do
14
+ t = Rubex::Compiler.ast @path + ".rubex"
15
+ end
16
+ end
17
+
18
+ context ".compile" do
19
+ it "generates valid C code" do
20
+ t, c, e = Rubex::Compiler.compile @path + ".rubex", test: true
21
+ end
22
+ end
23
+
24
+ context "Black Box testing" do
25
+ it "compiles and checks for valid output" do
26
+ setup_and_teardown_compiled_files(test_case) do |dir|
27
+ require_relative "#{dir}/#{test_case}.so"
28
+
29
+ cls = BitWise.new
30
+ expect(cls.wise_or).to eq(3)
31
+ expect(cls.wise_and).to eq(1)
32
+ expect(cls.wise_compli).to eq(-5)
33
+ expect(cls.wise_xor).to eq(0)
34
+ expect(cls.wise_lshift).to eq(6)
35
+ expect(cls.wise_rshift).to eq(2)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = 'blocks'
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates the AST" do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile", focus: true do
18
+ it "compiles to valid C file" do
19
+ t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing", focus: true do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ block_scene do |num|
29
+ expect([1,2,3,4,5,6].include?(num)).to eq(true)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = "c_bindings"
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates the AST" do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile" do
18
+ it "compiles to valid C file" do
19
+ t,c,e = Rubex::Compiler.compile((@path + '.rubex'), test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing" do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ expect(maths(3,5,"hello")).to be_within(0.001).of(300.763)
29
+ expect(stray_cos).to be_within(0.001).of(-0.210)
30
+ expect(A.new.ruby_cos).to be_within(0.001).of(-0.210)
31
+ expect(RMath.new.ruby_sin(90)).to be_within(0.001).of(0.893)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = "c_constants"
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates a valid AST" do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile", focus: true do
18
+ it "compiles to valid C code" do
19
+ t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing", focus: true do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ expect(have_ruby_h).to eq(1)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ include Rubex::AST
3
+
4
+ describe Rubex do
5
+ test_case = 'c_function_ptrs'
6
+
7
+ context "Case : #{test_case}" do
8
+ before do
9
+ @path = path_str test_case
10
+ end
11
+
12
+ context ".ast" do
13
+ it "returns a valid Abstract Syntax Tree" do
14
+ t = Rubex::Compiler.ast @path + ".rubex"
15
+ end
16
+ end
17
+
18
+ context ".compile", focus: true do
19
+ it "generates valid C code" do
20
+ t, c, e = Rubex::Compiler.compile @path + ".rubex", test: true
21
+ end
22
+ end
23
+
24
+ context "Black Box testing", focus: true do
25
+ it "compiles and checks for valid output" do
26
+ setup_and_teardown_compiled_files(test_case) do |dir|
27
+ require_relative "#{dir}/#{test_case}.so"
28
+
29
+ cls = CFunctionPtrs.new
30
+
31
+ expect(cls.test_c_function_pointers(true)) .to eq(5)
32
+ expect(cls.test_c_function_pointers(false)).to eq(7)
33
+ expect(cls.test_pass_by_name).to eq(17)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = 'c_functions'
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates the AST" do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile" do
18
+ it "compiles to valid C file" do
19
+ t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing" do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ c = CFunctions.new
29
+ expect(c.pure_ruby_method).to eq(50)
30
+ expect { c.first_c_function(1,2) }.to raise_error(NoMethodError)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ include Rubex::AST
3
+
4
+ describe Rubex do
5
+ test_case = 'c_struct_interface'
6
+
7
+ context "Case : #{test_case}" do
8
+ before do
9
+ @path = path_str test_case
10
+ end
11
+
12
+ context ".ast" do
13
+ it "returns a valid Abstract Syntax Tree" do
14
+ t = Rubex::Compiler.ast @path + ".rubex"
15
+ end
16
+ end
17
+
18
+ context ".compile", focus: true do
19
+ it "generates valid C code" do
20
+ t, c, e = Rubex::Compiler.compile @path + ".rubex", test: true
21
+ end
22
+ end
23
+
24
+ context "Black Box testing", focus: true do
25
+ it "compiles and checks for valid output" do
26
+ setup_and_teardown_compiled_files(test_case) do |dir|
27
+ require_relative "#{dir}/#{test_case}.so"
28
+ id = 44
29
+ m = Music.new("Animals as Leaders", "CAFO", id)
30
+
31
+ expect(m.artist).to eq("Animals as Leaders")
32
+ expect(m.title) .to eq("CAFO")
33
+ expect(m.id) .to eq(id)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = 'call_by_reference'
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates the AST" do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile" do
18
+ it "compiles to valid C file" do
19
+ t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing" do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ expect(CallByReference.new.ref_call).to eq(4)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = 'class_methods'
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates the AST" do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile" do
18
+ it "compiles to valid C file" do
19
+ t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing" do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ expect(Bhau.say_what("hello there!")).to eq(10)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = 'class'
5
+
6
+ context "Case: #{test_case}" do
7
+ before do
8
+ @path = path_str test_case
9
+ end
10
+
11
+ context ".ast" do
12
+ it "generates the AST" do
13
+ t = Rubex::Compiler.ast(@path + '.rubex')
14
+ end
15
+ end
16
+
17
+ context ".compile", focus: true do
18
+ it "compiles to valid C file" do
19
+ t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
+ end
21
+ end
22
+
23
+ context "Black Box testing", focus: true do
24
+ it "compiles and checks for valid output" do
25
+ setup_and_teardown_compiled_files(test_case) do |dir|
26
+ require_relative "#{dir}/#{test_case}.so"
27
+
28
+ k = Kustom.new
29
+ expect(k.bye).to eq("Bye world!")
30
+
31
+ k2 = Kustom2.new
32
+ expect(k2.hello).to eq("This is a prelude.Hello world!")
33
+
34
+ expect(Kustom2.ancestors[1]).to eq(Kustom)
35
+ expect(RandomRubyError.ancestors[1]).to eq(StandardError)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end