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
@@ -0,0 +1,10 @@
1
+ def adder(int left, float right)
2
+ float mid = left - right
3
+ float t = -(mid / 3) * (left + right) - left / mid
4
+ float z
5
+
6
+ z = t - 3
7
+ rr = -3 - 545
8
+
9
+ return rr
10
+ end
@@ -0,0 +1,77 @@
1
+ def adder_if_else(int a, i64 b, f64 c)
2
+ i32 d
3
+
4
+ if a < 5
5
+ d = 5
6
+ elsif b == 44
7
+ d = 44
8
+ elsif c >= 211
9
+ d = 211
10
+
11
+ if c == 225
12
+ d = 221
13
+ end
14
+ else
15
+ d = 666
16
+ end
17
+
18
+ if !(d > 32)
19
+ d = -69
20
+ end
21
+
22
+ if !d || a == 2
23
+ d = 4
24
+ end
25
+
26
+ if a
27
+ print "hello"
28
+ end
29
+
30
+ d = 5643 if d + 4 == 69
31
+ if c - 43 >= 321 then b = -43 end
32
+
33
+ return d
34
+ end
35
+
36
+ class IfElseTest
37
+ def ruby_obj_in_condition
38
+ int i = 0
39
+ a = nil
40
+ b = false
41
+
42
+ if a
43
+ i += 1
44
+ else
45
+ i += 10
46
+ end
47
+
48
+ if b
49
+ i += 1
50
+ else
51
+ i += 10
52
+ end
53
+
54
+ return i
55
+ end
56
+ end
57
+
58
+ def multi_if_elsif
59
+ opts = {}
60
+ opts[:a] = "hello world"
61
+ opts[:b] = 3
62
+
63
+ if opts[:b] == 3 && opts[:a] == "hello world"
64
+ return true
65
+ elsif opts[:a] == 3 && opts[:b] == "hello world"
66
+ return false
67
+ end
68
+ end
69
+
70
+ #def multi_line_if
71
+ # if true || false ||
72
+ # false && true
73
+ # return true
74
+ # else
75
+ # return false
76
+ # end
77
+ #end
@@ -0,0 +1,15 @@
1
+ lib "rubex/ruby"; end
2
+
3
+ def test_method
4
+ int *i = <int*>xmalloc(sizeof(int)*5)
5
+ int j = 0, num
6
+
7
+ while j < 5 do
8
+ i[j] = j + 5
9
+ num = i[j]
10
+ j += 1
11
+ end
12
+
13
+ xfree(i)
14
+ return num
15
+ end
@@ -0,0 +1,17 @@
1
+ class DataInit
2
+ def init_this(a, b, c)
3
+ array = [1,2,3,4,5,6]
4
+ string = "Hello world! Lets have a picnic!"
5
+ h = {
6
+ "hello" => array,
7
+ "world" => 666,
8
+ "message" => string
9
+ }
10
+ emp = []
11
+ emp2 = {}
12
+
13
+ print h["hello"]
14
+
15
+ return h
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ def looper(i32 size)
2
+ i32 a[size], i = 0, j = size, m
3
+
4
+ for i <= m < j do
5
+ a[m] = 5*m
6
+ end
7
+
8
+ m = 0
9
+
10
+ for j > m >= i do
11
+ print a[m]
12
+ end
13
+
14
+ i32 f = 0
15
+ while !(a[f] != 45) do
16
+ print a[f]
17
+ f = f + 1
18
+ end
19
+
20
+ return a[2]
21
+ end
22
+
23
+ def break_stmt
24
+ int i[5] = [1,2,3,4,5]
25
+ int j
26
+
27
+ while j < 5 do
28
+ break if i[j] == 3
29
+ j += 1
30
+ end
31
+
32
+ return i[j]
33
+ end
@@ -0,0 +1,9 @@
1
+ class Fibonnaci
2
+ def compute(int n)
3
+ if n == 0 || n == 1
4
+ return n
5
+ end
6
+
7
+ return compute(n - 1) + compute(n - 2)
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ class InitRubyClass
2
+ def init_classes
3
+ a = String.new
4
+ a[0] = "5"
5
+
6
+ f = StringIO.new("Hello! This is a test")
7
+ s = f.read
8
+
9
+ array = Array.new(2,3)
10
+ array.push(s)
11
+
12
+ st = String.try_convert("converted.")
13
+ array.push(st)
14
+
15
+ return array
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ class StringCaller
2
+ def call_now(string)
3
+ string[2] = "h"
4
+ print string[1]
5
+
6
+ return string[0]
7
+ end
8
+ end
9
+
10
+ class BinaryOperators
11
+ def double_eq(a, b)
12
+ int i = 1
13
+ !(a == b && i == 1)
14
+
15
+ if a == b
16
+ return true
17
+ else
18
+ return false
19
+ end
20
+ end
21
+
22
+ def lt(a, b)
23
+ if a < b
24
+ return true
25
+ else
26
+ return false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ class RaiseTester
2
+ def test_raise(switch)
3
+ if switch
4
+ raise ArgumentError, "You raised an ArgumentError!"
5
+ else
6
+ raise(NoMethodError, "You raised an NoMethodError!")
7
+ end
8
+ end
9
+
10
+ def test_raise1
11
+ raise
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ lib "<ruby.h>"
2
+ double RSTRING_LEN(object)
3
+ end
4
+
5
+ def blank?(string)
6
+ i32 i = 0
7
+ char *s = string
8
+ i32 length = <i32>RSTRING_LEN(string)
9
+
10
+ while i < length do
11
+ if s[i] != ' '
12
+ return false
13
+ end
14
+
15
+ i += 1
16
+ end
17
+
18
+ return true
19
+ end
@@ -0,0 +1,37 @@
1
+ # This file contains a benchmark between Rubex's blank? and the String#blank?
2
+ # from the the fast_blank gem.
3
+
4
+ require_relative 'ruby_strings.so'
5
+ require 'fast_blank'
6
+ require 'benchmark'
7
+ require 'benchmark/ips'
8
+
9
+ str= " "*2500 + "dff"
10
+
11
+ Benchmark.ips do |x|
12
+ x.config(:time => 5, :warmup => 2)
13
+
14
+ x.time = 5
15
+ x.warmup = 2
16
+
17
+ x.report("fast_blank") do
18
+ str.blank?
19
+ end
20
+
21
+ x.report("blank?") do
22
+ blank? str
23
+ end
24
+
25
+ x.compare!
26
+ end
27
+
28
+ # Warming up --------------------------------------
29
+ # fast_blank 3.401k i/100ms
30
+ # blank? 57.041k i/100ms
31
+ # Calculating -------------------------------------
32
+ # fast_blank 35.068k (± 0.4%) i/s - 176.852k in 5.043263s
33
+ # blank? 671.289k (± 1.1%) i/s - 3.365M in 5.014016s
34
+ #
35
+ # Comparison:
36
+ # blank?: 671289.0 i/s
37
+ # fast_blank: 35067.6 i/s - 19.14x slower
@@ -0,0 +1,12 @@
1
+ class RubySymbols
2
+ def symbol_support(a, b)
3
+ h = Hash.new
4
+ h[:first] = a
5
+ h[:second] = b
6
+
7
+ other_arr = Array.new(2,3)
8
+ h[:third] = other_arr
9
+
10
+ return h
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ class RubyTypes
2
+ def these_types(str string, arr array, hsh h)
3
+ string.concat("a")
4
+ array.push(44)
5
+
6
+ print string[0]
7
+ print array[0]
8
+
9
+ print h["fff"]
10
+ h["fff"] = 565
11
+ print h["fff"]
12
+
13
+ return h
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ # This file contains sample Rubex code that contains an expression as a
2
+ # statement. For example, simply stating `1+2` or `function_name` is an
3
+ # expression that is actually a statement.
4
+
5
+ def test_string_return
6
+ print "This string."
7
+ end
8
+
9
+ def test_with_args(int a)
10
+ print "hello", a
11
+ end
12
+
13
+ def expr_stat
14
+ int a = 3
15
+
16
+ test_string_return
17
+ test_with_args(a)
18
+ foo
19
+ 4 + 5 * 6 - 333 + (3455)
20
+ a * 5 + 3
21
+
22
+ return "success."
23
+ end
@@ -0,0 +1,20 @@
1
+ def static_array
2
+ i32 a[10], c[32]
3
+ i64 b[5] = [1,2,3,4,5], t[4] = [4,6,2,12]
4
+ i8 size = 54
5
+ i32 e[size]
6
+
7
+ a[2] = 443
8
+ print a[2]
9
+
10
+ if a[2] == 443
11
+ b[4] = 123
12
+ end
13
+
14
+ e[53] = 4311
15
+
16
+ print e[53]
17
+ print b[4]
18
+
19
+ return b[4]
20
+ end
@@ -0,0 +1,15 @@
1
+ def strings
2
+ char* s = "My name is Ted."
3
+ obj = "Ted says \"Oh my God thats a big sandwich!\""
4
+ int a = 4
5
+ float b = 5.6
6
+
7
+ print "The number a is : #{a}\nThe number b is: #{b}.\n Their sum is #{a+b}."
8
+ print "a - b :", a - b, " a * b : ", a * b, " a + b * a : ", a + b * a
9
+
10
+ return obj
11
+ end
12
+
13
+ def string_ret
14
+ return "This is a returned string."
15
+ end
@@ -0,0 +1,82 @@
1
+ def structure(a, i32 b, i32 c)
2
+ fwd struct other_node
3
+ i32 f[10]
4
+
5
+ struct node
6
+ i32 a, b
7
+ other_node *hello
8
+ f32 c[10]
9
+ end
10
+
11
+ struct other_node
12
+ i64 a, b
13
+ char *string
14
+ end
15
+
16
+ struct empty_struct
17
+ end
18
+
19
+ node n
20
+
21
+ n.a = b
22
+ n.b = c
23
+
24
+ other_node shell
25
+
26
+ shell.string = a
27
+ shell.a = 666
28
+ shell.b = 555
29
+
30
+ i32 i
31
+ for 0 <= i < 10 do
32
+ n.c[i] = i*43
33
+ f[i] = i + 4
34
+ end
35
+
36
+ print n.c[7]
37
+
38
+ return shell.a
39
+ end
40
+
41
+ def struct_index
42
+ struct node
43
+ int a
44
+ int ptr[20], ptr1[5]
45
+ end
46
+
47
+ int i = 0
48
+ node n
49
+
50
+ for 0 <= i < 20 do
51
+ n.ptr[i] = i
52
+ end
53
+ n.a = 4
54
+ n.ptr1[4] = 4
55
+
56
+ return n.ptr[n.ptr1[n.a]]
57
+ end
58
+
59
+ def access_struct_obj
60
+ struct node
61
+ object array
62
+ end
63
+
64
+ node a
65
+
66
+ a.array = []
67
+ a.array.push(1)
68
+
69
+ return a.array[0]
70
+ end
71
+
72
+ def struct_ptr
73
+ struct node
74
+ int i, j
75
+ end
76
+
77
+ node *a
78
+ a.i = 3
79
+ a.j = 4
80
+
81
+ return a.i + a.j
82
+ end