rubex 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/CONTRIBUTING.md +73 -9
  4. data/HISTORY.md +19 -0
  5. data/README.md +53 -8
  6. data/REFERENCE.md +112 -44
  7. data/benchmarks/no_gil/no_gil.rb +24 -0
  8. data/benchmarks/no_gil/no_gil.rubex +22 -0
  9. data/bin/rubex +1 -1
  10. data/examples/c_struct_interface/c_struct_interface.rubex +1 -0
  11. data/lib/rubex.rb +1 -0
  12. data/lib/rubex/ast.rb +11 -7
  13. data/lib/rubex/ast/expression.rb +1 -1
  14. data/lib/rubex/ast/expression/actual_arg_list.rb +7 -0
  15. data/lib/rubex/ast/expression/analysed_element_ref/c_var_element_ref.rb +5 -2
  16. data/lib/rubex/ast/expression/analysed_element_ref/ruby_object_element_ref.rb +26 -9
  17. data/lib/rubex/ast/expression/binary/binary_boolean.rb +1 -1
  18. data/lib/rubex/ast/expression/binary/binary_expo.rb +34 -0
  19. data/lib/rubex/ast/expression/binary/colon2.rb +34 -0
  20. data/lib/rubex/ast/expression/binary/empty_classes.rb +0 -3
  21. data/lib/rubex/ast/expression/command_call.rb +24 -0
  22. data/lib/rubex/ast/{statement → expression/command_call}/print.rb +4 -5
  23. data/lib/rubex/ast/{statement → expression/command_call}/raise.rb +29 -24
  24. data/lib/rubex/ast/expression/command_call/require.rb +27 -0
  25. data/lib/rubex/ast/expression/command_call/yield.rb +36 -0
  26. data/lib/rubex/ast/expression/element_ref.rb +10 -5
  27. data/lib/rubex/ast/expression/instance_var.rb +33 -0
  28. data/lib/rubex/ast/expression/method_call.rb +4 -2
  29. data/lib/rubex/ast/expression/method_call/c_function_call.rb +4 -3
  30. data/lib/rubex/ast/expression/method_call/ruby_method_call.rb +7 -5
  31. data/lib/rubex/ast/expression/name.rb +10 -1
  32. data/lib/rubex/ast/expression/ruby_object_element_ref/ruby_array_element_ref.rb +5 -2
  33. data/lib/rubex/ast/expression/ruby_object_element_ref/ruby_hash_element_ref.rb +10 -2
  34. data/lib/rubex/ast/expression/to_ruby_object.rb +1 -0
  35. data/lib/rubex/ast/expression/unary.rb +7 -3
  36. data/lib/rubex/ast/expression/unary_base/ampersand.rb +5 -0
  37. data/lib/rubex/ast/node.rb +213 -185
  38. data/lib/rubex/ast/node/file_node.rb +25 -0
  39. data/lib/rubex/ast/node/main_node.rb +56 -0
  40. data/lib/rubex/ast/statement/begin_block/begin.rb +4 -3
  41. data/lib/rubex/ast/statement/c_array_decl.rb +1 -1
  42. data/lib/rubex/ast/statement/c_ptr_decl.rb +2 -0
  43. data/lib/rubex/ast/statement/no_gil_block.rb +70 -0
  44. data/lib/rubex/ast/statement/return.rb +1 -0
  45. data/lib/rubex/ast/top_statement.rb +1 -1
  46. data/lib/rubex/ast/top_statement/klass.rb +4 -0
  47. data/lib/rubex/ast/top_statement/klass/attached_klass.rb +88 -10
  48. data/lib/rubex/ast/top_statement/method_def.rb +2 -3
  49. data/lib/rubex/ast/top_statement/method_def/c_function_def.rb +10 -4
  50. data/lib/rubex/cli.rb +11 -6
  51. data/lib/rubex/code_supervisor.rb +49 -0
  52. data/lib/rubex/code_writer.rb +22 -1
  53. data/lib/rubex/compiler.rb +109 -30
  54. data/lib/rubex/compiler_config.rb +14 -1
  55. data/lib/rubex/constants.rb +3 -0
  56. data/lib/rubex/data_type.rb +2 -3
  57. data/lib/rubex/data_type/ruby_object/ruby_symbol.rb +0 -1
  58. data/lib/rubex/error.rb +4 -0
  59. data/lib/rubex/helpers/writers.rb +33 -4
  60. data/lib/rubex/lexer.rex +9 -1
  61. data/lib/rubex/lexer.rex.rb +15 -2
  62. data/lib/rubex/parser.racc +125 -49
  63. data/lib/rubex/parser.racc.rb +1526 -1376
  64. data/lib/rubex/rake_task.rb +42 -6
  65. data/lib/rubex/symbol_table/entry.rb +6 -0
  66. data/lib/rubex/symbol_table/scope.rb +28 -3
  67. data/lib/rubex/version.rb +1 -1
  68. data/rubex.gemspec +1 -0
  69. data/spec/basic_ruby_method_spec.rb +2 -2
  70. data/spec/blocks_spec.rb +2 -2
  71. data/spec/box_op_multi_args_spec.rb +34 -0
  72. data/spec/c_function_ptrs_spec.rb +2 -2
  73. data/spec/c_functions_spec.rb +2 -0
  74. data/spec/c_struct_interface_spec.rb +8 -3
  75. data/spec/default_args_spec.rb +2 -2
  76. data/spec/external_c_struct_spec.rb +33 -0
  77. data/spec/fixtures/api/consumer.rubex +0 -0
  78. data/spec/fixtures/api/implementation.rubex +0 -0
  79. data/spec/fixtures/api/implementation.rubexd +0 -0
  80. data/spec/fixtures/box_op_multi_args/box_op_multi_args.rubex +3 -0
  81. data/spec/fixtures/c_functions/c_functions.rubex +13 -0
  82. data/spec/fixtures/c_struct_interface/c_struct_interface.rubex +28 -0
  83. data/spec/fixtures/class_methods/class_methods.rubex +1 -1
  84. data/spec/fixtures/error_handling/error_handling.rubex +2 -2
  85. data/spec/fixtures/external_c_struct/external_c_struct.rubex +16 -0
  86. data/spec/fixtures/if_else/if_else.rubex +1 -1
  87. data/spec/fixtures/init_ruby_objects_with_literal_syntax/init_ruby_objects_with_literal_syntax.rubex +1 -1
  88. data/spec/fixtures/instance_variables/instance_variables.rubex +25 -0
  89. data/spec/fixtures/loops/loops.rubex +2 -2
  90. data/spec/fixtures/module/module.rubex +28 -0
  91. data/spec/fixtures/multi_file_programs/Rakefile +8 -0
  92. data/spec/fixtures/multi_file_programs/a.rubex +5 -0
  93. data/spec/fixtures/multi_file_programs/b.rubex +5 -0
  94. data/spec/fixtures/multi_file_programs/multi_file_programs.rubex +14 -0
  95. data/spec/fixtures/no_gil/no_gil.rubex +24 -0
  96. data/spec/fixtures/no_gil_attach_class/no_gil_attach_class.rubex +23 -0
  97. data/spec/fixtures/no_gil_compile_check/no_gil_compile_check.rubex +4 -0
  98. data/spec/fixtures/outside_stmts/outside_stmts.rubex +6 -0
  99. data/spec/fixtures/pow/pow.rubex +4 -0
  100. data/spec/fixtures/rake_task/single_file/test.rubex +3 -0
  101. data/spec/fixtures/recursion/recursion.rubex +1 -1
  102. data/spec/fixtures/ruby_constant_scoping/ruby_constant_scoping.rubex +7 -0
  103. data/spec/fixtures/ruby_operators/ruby_operators.rubex +1 -1
  104. data/spec/fixtures/ruby_raise/ruby_raise.rubex +2 -2
  105. data/spec/fixtures/ruby_types/ruby_types.rubex +4 -4
  106. data/spec/fixtures/statement_expression/statement_expression.rubex +2 -2
  107. data/spec/fixtures/static_array/static_array.rubex +3 -3
  108. data/spec/fixtures/string_literals/string_literals.rubex +12 -2
  109. data/spec/fixtures/struct/struct.rubex +1 -1
  110. data/spec/fixtures/var_declarations/var_declarations.rubex +1 -1
  111. data/spec/implicit_lib_include_spec.rb +2 -2
  112. data/spec/init_ruby_objects_with_literal_syntax_spec.rb +2 -2
  113. data/spec/instance_variables_spec.rb +33 -0
  114. data/spec/loops_spec.rb +2 -2
  115. data/spec/module_spec.rb +39 -0
  116. data/spec/multi_file_programs_spec.rb +41 -0
  117. data/spec/no_gil_attach_class_spec.rb +33 -0
  118. data/spec/no_gil_compile_check_spec.rb +25 -0
  119. data/spec/no_gil_spec.rb +36 -0
  120. data/spec/outside_stmts_spec.rb +34 -0
  121. data/spec/pow_spec.rb +33 -0
  122. data/spec/rake_task_spec.rb +142 -0
  123. data/spec/recursion_spec.rb +4 -4
  124. data/spec/ruby_constant_scoping_spec.rb +42 -0
  125. data/spec/ruby_raise_spec.rb +2 -2
  126. data/spec/ruby_symbols_spec.rb +2 -2
  127. data/spec/ruby_types_spec.rb +2 -2
  128. data/spec/spec_helper.rb +17 -3
  129. data/spec/string_literals_spec.rb +1 -0
  130. metadata +90 -6
  131. data/lib/rubex/ast/statement/yield.rb +0 -41
@@ -0,0 +1,16 @@
1
+ lib "<stdio.h>"
2
+ struct FILE
3
+ end
4
+
5
+ alias FILE = struct FILE
6
+ end
7
+
8
+ struct hello
9
+ FILE f
10
+ end
11
+
12
+ class HelloFile
13
+ def foo
14
+ FILE a
15
+ end
16
+ end
@@ -24,7 +24,7 @@ def adder_if_else(int a, i64 b, f64 c)
24
24
  end
25
25
 
26
26
  if a
27
- print "hello"
27
+ print("hello")
28
28
  end
29
29
 
30
30
  d = 5643 if d + 4 == 69
@@ -10,7 +10,7 @@ class DataInit
10
10
  emp = []
11
11
  emp2 = {}
12
12
 
13
- print h["hello"]
13
+ print(h["hello"])
14
14
 
15
15
  return h
16
16
  end
@@ -0,0 +1,25 @@
1
+ class Test
2
+ def initialize(foo, bar)
3
+ @foo = foo
4
+ @bar = bar
5
+ end
6
+
7
+ def addition
8
+ return @foo + @bar
9
+ end
10
+ end
11
+
12
+ class ToChar
13
+ def initialize(foo)
14
+ @foo = foo
15
+ end
16
+
17
+ def bar
18
+ char * b
19
+ b = @foo
20
+
21
+ char * c = @foo
22
+
23
+ return b
24
+ end
25
+ end
@@ -8,12 +8,12 @@ def looper(i32 size)
8
8
  m = 0
9
9
 
10
10
  for j > m >= i do
11
- print a[m]
11
+ print(a[m])
12
12
  end
13
13
 
14
14
  i32 f = 0
15
15
  while !(a[f] != 45) do
16
- print a[f]
16
+ print(a[f])
17
17
  f = f + 1
18
18
  end
19
19
 
@@ -0,0 +1,28 @@
1
+ module Foo
2
+ class Bar
3
+ def initialize
4
+ end
5
+
6
+ def baz
7
+ return "hello world"
8
+ end
9
+ end
10
+ end
11
+
12
+ module Bar
13
+ def self.a
14
+ return "a"
15
+ end
16
+
17
+ def b
18
+ return "b"
19
+ end
20
+ end
21
+
22
+ module Outer
23
+ module Inner
24
+ def abc
25
+ return "abc"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubex/rake_task'
2
+
3
+ # specify all the rubex files to be compiled, along with the name of the main
4
+ # program that serves as the 'main' entry point.
5
+ Rubex::RakeTask.new("multi_file_programs") do
6
+ rubex_files ["a.rubex", "b.rubex", "multi_file_program.rubex"]
7
+ end
8
+
@@ -0,0 +1,5 @@
1
+ class A
2
+ def bar
3
+ return @a
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class B
2
+ def initialize(foo)
3
+ @foo = foo
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require_rubex("a")
2
+ require_rubex("b.rubex")
3
+
4
+ class C < A
5
+ def initialize(a)
6
+ @a = a
7
+ end
8
+ end
9
+
10
+ class D < B
11
+ def foo
12
+ return @foo + "hello world"
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ lib "rubex/ruby"; end
2
+
3
+ cfunc void _work_without_gil(double n) no_gil
4
+ while n > 0 do
5
+ n ** 0.5 + 4
6
+ n -= 1
7
+ end
8
+ end
9
+
10
+ def work_without_gil(n)
11
+ double i = n
12
+ no_gil
13
+ _work_without_gil(i)
14
+ end
15
+
16
+ return i
17
+ end
18
+
19
+ def work_with_gil(double n)
20
+ while n > 0 do
21
+ n ** 0.5 + 4
22
+ n -= 1
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ struct test
2
+ int a
3
+ end
4
+
5
+ class A attach test
6
+ cfunc void __bar(int a) no_gil
7
+ int b = a
8
+ end
9
+
10
+ def foo
11
+ data$.test.a = 4
12
+
13
+ no_gil
14
+ __bar(data$.test.a)
15
+ end
16
+
17
+ return data$.test.a
18
+ end
19
+
20
+ cfunc void deallocate
21
+ xfree(data$.test)
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ cfunc void problem_function(int a) no_gil
2
+ b = Array.new(a)
3
+ end
4
+
@@ -0,0 +1,6 @@
1
+ require("numo/narray")
2
+
3
+ def use_narray
4
+ a = Numo::DFloat.new(4,4).seq
5
+ return a[3]
6
+ end
@@ -0,0 +1,4 @@
1
+ def test_pow(double n, double a)
2
+ return n ** a + 4
3
+ end
4
+
@@ -0,0 +1,3 @@
1
+ def foo
2
+ return "hello world"
3
+ end
@@ -6,4 +6,4 @@ class Fibonnaci
6
6
 
7
7
  return compute(n - 1) + compute(n - 2)
8
8
  end
9
- end
9
+ end
@@ -0,0 +1,7 @@
1
+ def get_eid
2
+ return Process::UID.eid
3
+ end
4
+
5
+ def user_chained_const
6
+ return Foo::Bar::Baz::NUMBER
7
+ end
@@ -1,7 +1,7 @@
1
1
  class StringCaller
2
2
  def call_now(string)
3
3
  string[2] = "h"
4
- print string[1]
4
+ print(string[1])
5
5
 
6
6
  return string[0]
7
7
  end
@@ -1,7 +1,7 @@
1
1
  class RaiseTester
2
2
  def test_raise(switch)
3
3
  if switch
4
- raise ArgumentError, "You raised an ArgumentError!"
4
+ raise(ArgumentError, "You raised an ArgumentError!")
5
5
  else
6
6
  raise(NoMethodError, "You raised an NoMethodError!")
7
7
  end
@@ -10,4 +10,4 @@ class RaiseTester
10
10
  def test_raise1
11
11
  raise
12
12
  end
13
- end
13
+ end
@@ -3,12 +3,12 @@ class RubyTypes
3
3
  string.concat("a")
4
4
  array.push(44)
5
5
 
6
- print string[0]
7
- print array[0]
6
+ print(string[0])
7
+ print(array[0])
8
8
 
9
- print h["fff"]
9
+ print(h["fff"])
10
10
  h["fff"] = 565
11
- print h["fff"]
11
+ print(h["fff"])
12
12
 
13
13
  return h
14
14
  end
@@ -3,11 +3,11 @@
3
3
  # expression that is actually a statement.
4
4
 
5
5
  def test_string_return
6
- print "This string."
6
+ print("This string.")
7
7
  end
8
8
 
9
9
  def test_with_args(int a)
10
- print "hello", a
10
+ print("hello", a)
11
11
  end
12
12
 
13
13
  def expr_stat
@@ -5,7 +5,7 @@ def static_array
5
5
  i32 e[size]
6
6
 
7
7
  a[2] = 443
8
- print a[2]
8
+ print(a[2])
9
9
 
10
10
  if a[2] == 443
11
11
  b[4] = 123
@@ -13,8 +13,8 @@ def static_array
13
13
 
14
14
  e[53] = 4311
15
15
 
16
- print e[53]
17
- print b[4]
16
+ print(e[53])
17
+ print(b[4])
18
18
 
19
19
  return b[4]
20
20
  end
@@ -4,8 +4,8 @@ def strings
4
4
  int a = 4
5
5
  float b = 5.6
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
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
9
 
10
10
  return obj
11
11
  end
@@ -13,3 +13,13 @@ end
13
13
  def string_ret
14
14
  return "This is a returned string."
15
15
  end
16
+
17
+ def char_literal
18
+ char *s = "hello world"
19
+
20
+ if (s[0] == 'h')
21
+ return true
22
+ else
23
+ return false
24
+ end
25
+ end
@@ -35,7 +35,7 @@ def structure(a, i32 b, i32 c)
35
35
  f[i] = i + 4
36
36
  end
37
37
 
38
- print n.c[7]
38
+ print(n.c[7])
39
39
 
40
40
  return shell.a
41
41
  end
@@ -9,7 +9,7 @@ def additive(float a, u64 b, i32 qq)
9
9
  i64 ww = -342, rew = 2342
10
10
  unsigned char delhi = 55
11
11
 
12
- print q
12
+ print(q)
13
13
 
14
14
  return a + c
15
15
  end
@@ -14,13 +14,13 @@ describe Rubex do
14
14
  end
15
15
  end
16
16
 
17
- context ".compile", focus: true do
17
+ context ".compile" do
18
18
  it "compiles to valid C file" do
19
19
  t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
20
  end
21
21
  end
22
22
 
23
- context "Black Box testing", focus: true do
23
+ context "Black Box testing" do
24
24
  it "compiles and checks for valid output" do
25
25
  setup_and_teardown_compiled_files(test_case) do |dir|
26
26
  require_relative "#{dir}/#{test_case}.#{os_extension}"
@@ -14,13 +14,13 @@ describe Rubex do
14
14
  end
15
15
  end
16
16
 
17
- context ".compile", focus: true do
17
+ context ".compile" do
18
18
  it "compiles to valid C code" do
19
19
  t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
20
  end
21
21
  end
22
22
 
23
- context "Black Box testing", focus: true do
23
+ context "Black Box testing" do
24
24
  it "compiles and checks for valid output" do
25
25
  setup_and_teardown_compiled_files(test_case) do |dir|
26
26
  require_relative "#{dir}/#{test_case}.#{os_extension}"
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubex do
4
+ test_case = "instance_variables"
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}.#{os_extension}"
27
+ expect(Test.new("hello", " ruby").addition).to eq("hello ruby")
28
+ expect(ToChar.new("hello").bar).to eq("hello")
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -14,13 +14,13 @@ describe Rubex do
14
14
  end
15
15
  end
16
16
 
17
- context ".compile", focus: true do
17
+ context ".compile" do
18
18
  it "compiles to valid C file" do
19
19
  t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20
20
  end
21
21
  end
22
22
 
23
- context "Black Box testing", focus: true do
23
+ context "Black Box testing" do
24
24
  it "compiles and checks for valid output" do
25
25
  setup_and_teardown_compiled_files(test_case) do |dir|
26
26
  require_relative "#{dir}/#{test_case}.#{os_extension}"