rubex 0.0.1 → 0.1

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 (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
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = "comments"
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 "generate 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(crazy_comment).to eq(4)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ pending("implement default args") do
5
+ test_case = "default_args"
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 "generates a valid AST" do
14
+ t = Rubex::Compiler.ast(@path + '.rubex')
15
+ end
16
+ end
17
+
18
+ context ".compile", focus: true do
19
+ it "compiles to 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
+ o = { a: "world!!" }
29
+ expect(default_method(1)).to eq(nil)
30
+ expect(default_method(1, true, o)).to eq(7)
31
+ expect(default_method(1, nil, {a: 44})).to eq(5)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = "error_handling"
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
+ cls = Handler.new
29
+
30
+ expect(cls.error_test(1)).to eq(6)
31
+ expect(cls.error_test(2)).to eq(7)
32
+ expect(cls.error_test(3)).to eq(8)
33
+ expect(cls.error_test(4)).to eq(9)
34
+
35
+ expect {cls.test_uncaught_error}.to raise_error(ArgumentError)
36
+ expect(cls.test_without_rescue).to eq(1)
37
+ expect(cls.test_decl_inside_begin).to eq(44)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = 'examples'
5
+
6
+ examples = ['rcsv', 'array_to_hash'].each do |example|
7
+ context "Case: #{test_case}/#{example}" do
8
+ before do
9
+ @path = path_str test_case, example
10
+ end
11
+
12
+ context ".ast" do
13
+ it "generates the AST" do
14
+ t = Rubex::Compiler.ast(@path + '.rubex')
15
+ end
16
+ end
17
+
18
+ context ".compile", now: true do
19
+ it "compiles to valid C file" do
20
+ t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
21
+ end
22
+ end
23
+
24
+ context "Black Box testing", now: true do
25
+ it "compiles and checks for valid output" do
26
+ def rcsv
27
+ result = [
28
+ ["Name", "age", "sex"],
29
+ ["Sameer", "24", "M"],
30
+ ["Ameya", "23", "M"],
31
+ ["Neeraja", "23", "F"],
32
+ ["Shounak", "24", "M"]
33
+ ]
34
+
35
+ a = Rcsv.parse(File.read('spec/fixtures/examples/rcsv.csv'), {})
36
+ expect(a).to eq(result)
37
+ end
38
+
39
+ def array_to_hash
40
+ a = ["a", "b", "c"]
41
+ expect(a.each_with_index.to_h).to eq(Array2Hash.convert(a))
42
+ end
43
+
44
+ setup_and_teardown_compiled_files(test_case, example) do |dir|
45
+ require_relative "#{dir}/#{example}.so"
46
+ self.send(example.to_sym)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = "expressions"
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(adder(1,3.4)).to eq(-548)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,5 @@
1
1
  def addition (i32 a, i32 b)
2
+ t = "aaa"
3
+ t.nil?
2
4
  return a + b
3
5
  end
@@ -0,0 +1,30 @@
1
+ lib "<ruby/encoding.h>"
2
+ struct rb_encoding
3
+
4
+ end
5
+
6
+ alias rb_encoding = struct rb_encoding
7
+
8
+ rb_encoding *rb_enc_from_index(int)
9
+ unsigned int rb_enc_codepoint(char *, char *, rb_encoding *)
10
+ int ENCODING_GET(object)
11
+ end
12
+
13
+ lib "<ruby.h>"
14
+ char* RSTRING_PTR(object)
15
+ char* RSTRING_END(object)
16
+ end
17
+
18
+ def test_function(string)
19
+ rb_encoding *enc
20
+ unsigned int cc
21
+ char* s
22
+ char* e
23
+
24
+ s = RSTRING_PTR(string)
25
+ e = RSTRING_END(string)
26
+ enc = rb_enc_from_index(ENCODING_GET(string))
27
+ cc = rb_enc_codepoint(s, e, enc)
28
+
29
+ return cc
30
+ end
@@ -0,0 +1,40 @@
1
+ class BitWise
2
+ def wise_or
3
+ int a = 1
4
+ int b = 3
5
+
6
+ return a | b
7
+ end
8
+
9
+ def wise_and
10
+ int a = 1
11
+ char b = 5
12
+
13
+ return a & b
14
+ end
15
+
16
+ def wise_xor
17
+ int a = 4
18
+ int b = 4
19
+
20
+ return a ^ b
21
+ end
22
+
23
+ def wise_compli
24
+ int a = 4
25
+
26
+ return ~a
27
+ end
28
+
29
+ def wise_lshift
30
+ int a = 3
31
+
32
+ return a << 1
33
+ end
34
+
35
+ def wise_rshift
36
+ int a = 4
37
+
38
+ return a >> 1
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ def block_scene
2
+ int i[6] = [1,2,3,4,5,6]
3
+ int j = 0
4
+
5
+ if block_given?
6
+ while j < 6 do
7
+ yield(i[j])
8
+ j += 1
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ lib "<math.h>"
2
+ double cos(double a)
3
+
4
+ struct exception
5
+ int type
6
+ char *name
7
+ end
8
+
9
+ double pow(double, double)
10
+
11
+ alias exec = struct exception
12
+ end
13
+
14
+ lib "<math.h>"
15
+ double sin(double)
16
+ end
17
+
18
+ class RMath
19
+ def ruby_sin(double x)
20
+ return sin(x)
21
+ end
22
+ end
23
+
24
+ def maths(double a, double b, c)
25
+ alias int_64 = i64
26
+ int_64 p = cos(a)
27
+ int_64 rr
28
+
29
+ rr = 332
30
+
31
+ exec e
32
+ e.type = 3
33
+
34
+ struct new_struct
35
+ double a,b,c
36
+ char* string
37
+ end
38
+
39
+ new_struct s
40
+ s.a = a
41
+ s.b = b
42
+
43
+ if (s.a > s.b)
44
+ s.string = c
45
+ end
46
+
47
+ return pow(6.7, s.a)
48
+ end
49
+
50
+ def stray_cos
51
+ return cos(4.5)
52
+ end
53
+
54
+ class A
55
+ def ruby_cos
56
+ return cos(4.5)
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ lib "<ruby.h>"
2
+ int HAVE_RUBY_DEFINES_H
3
+ end
4
+
5
+ def have_ruby_h
6
+ return HAVE_RUBY_DEFINES_H
7
+ end
@@ -0,0 +1,52 @@
1
+ cfunc int foo1(int a)
2
+ return a + 1
3
+ end
4
+
5
+ cfunc int foo2(int a)
6
+ return a + 2
7
+ end
8
+
9
+ cfunc int baz1(int a, int b)
10
+ return a + b + 1
11
+ end
12
+
13
+ cfunc int baz2(int a, int b)
14
+ return a + b + 2
15
+ end
16
+
17
+ cfunc int bar(int (*func1)(int), int (*func2)(int, int), int a, int b)
18
+ int ans1
19
+ int ans2
20
+
21
+ ans1 = func1(a)
22
+ ans2 = func2(a, b)
23
+
24
+ return ans1 + ans2
25
+ end
26
+
27
+ class CFunctionPtrs
28
+ def test_c_function_pointers(switch)
29
+ alias goswim = int (*ernub)(int, int)
30
+ int (*func_ptr1)(int)
31
+ goswim func_ptr2
32
+ int a = 1
33
+ int b = 1
34
+
35
+ if switch
36
+ func_ptr1 = foo1
37
+ func_ptr2 = baz1
38
+ else
39
+ func_ptr1 = foo2
40
+ func_ptr2 = baz2
41
+ end
42
+
43
+ return bar(func_ptr1, func_ptr2, a, b)
44
+ end
45
+
46
+ def test_pass_by_name
47
+ int a = 5
48
+ int b = 5
49
+
50
+ return bar(&foo1, &baz1, a, b)
51
+ end
52
+ end
@@ -0,0 +1,25 @@
1
+ cfunc object this_is_it
2
+ return "this is it."
3
+ end
4
+
5
+ class CFunctions
6
+ def pure_ruby_method
7
+ int a = 55
8
+ float b = 5.43
9
+ int c = first_c_function(a, b)
10
+ d = this_is_it
11
+
12
+ return a + c
13
+ end
14
+
15
+ cfunc int first_c_function(int a, float b)
16
+ int c = a + 5
17
+ int d = (c * b + 3)/5
18
+
19
+ return c - d
20
+ end
21
+
22
+ cfunc int empty
23
+
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ lib "rubex/ruby"; end
2
+
3
+ struct mp3info
4
+ char *artist, *title
5
+ int id
6
+ end
7
+
8
+ class Music attach mp3info
9
+ def initialize(artist, title, id)
10
+ mp3info* mp3 = data$.mp3info
11
+ int a_size = artist.size
12
+ int t_size = title.size
13
+
14
+ mp3.artist = artist # implicit conversion of Ruby String to char*
15
+ mp3.title = title # implicit conversion of Ruby String to char*
16
+ mp3.id = id # implicit typecast to int
17
+ end
18
+
19
+ cfunc void deallocate
20
+ xfree(data$.mp3info)
21
+ end
22
+
23
+ def artist
24
+ return data$.mp3info.artist # typecast to ruby string
25
+ end
26
+
27
+ def title
28
+ return data$.mp3info.title # typecast to ruby string
29
+ end
30
+
31
+ def id
32
+ return data$.mp3info.id
33
+ end
34
+ end