rubex 0.1 → 0.1.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 (197) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -2
  3. data/.travis.yml +9 -1
  4. data/CONTRIBUTING.md +2 -2
  5. data/README.md +4 -1
  6. data/Rakefile +2 -2
  7. data/bin/rubex +4 -5
  8. data/lib/rubex.rb +4 -4
  9. data/lib/rubex/ast.rb +4 -1
  10. data/lib/rubex/ast/expression.rb +22 -1191
  11. data/lib/rubex/ast/expression/actual_arg_list.rb +40 -0
  12. data/lib/rubex/ast/expression/analysed_element_ref.rb +26 -0
  13. data/lib/rubex/ast/expression/analysed_element_ref/c_var_element_ref.rb +30 -0
  14. data/lib/rubex/ast/expression/analysed_element_ref/ruby_object_element_ref.rb +42 -0
  15. data/lib/rubex/ast/expression/arg_declaration.rb +43 -0
  16. data/lib/rubex/ast/expression/binary.rb +57 -0
  17. data/lib/rubex/ast/expression/binary/binary_boolean.rb +23 -0
  18. data/lib/rubex/ast/expression/binary/binary_boolean_special_op.rb +20 -0
  19. data/lib/rubex/ast/expression/binary/empty_classes.rb +62 -0
  20. data/lib/rubex/ast/expression/block_given.rb +15 -0
  21. data/lib/rubex/ast/expression/coerce_object.rb +15 -0
  22. data/lib/rubex/ast/expression/command_call.rb +74 -0
  23. data/lib/rubex/ast/expression/command_call/struct_or_union_member_call.rb +38 -0
  24. data/lib/rubex/ast/expression/element_ref.rb +64 -0
  25. data/lib/rubex/ast/expression/empty.rb +13 -0
  26. data/lib/rubex/ast/expression/from_ruby_object.rb +20 -0
  27. data/lib/rubex/ast/expression/func_ptr_arg_declaration.rb +21 -0
  28. data/lib/rubex/ast/expression/func_ptr_internal_arg_declaration.rb +13 -0
  29. data/lib/rubex/ast/expression/literal.rb +30 -0
  30. data/lib/rubex/ast/expression/literal/array_lit.rb +51 -0
  31. data/lib/rubex/ast/expression/literal/c_null.rb +15 -0
  32. data/lib/rubex/ast/expression/literal/char.rb +36 -0
  33. data/lib/rubex/ast/expression/literal/double.rb +14 -0
  34. data/lib/rubex/ast/expression/literal/false.rb +33 -0
  35. data/lib/rubex/ast/expression/literal/hash_lit.rb +45 -0
  36. data/lib/rubex/ast/expression/literal/int.rb +14 -0
  37. data/lib/rubex/ast/expression/literal/nil.rb +14 -0
  38. data/lib/rubex/ast/expression/literal/ruby_symbol.rb +22 -0
  39. data/lib/rubex/ast/expression/literal/string_lit.rb +45 -0
  40. data/lib/rubex/ast/expression/literal/true.rb +29 -0
  41. data/lib/rubex/ast/expression/method_call.rb +52 -0
  42. data/lib/rubex/ast/expression/method_call/c_function_call.rb +40 -0
  43. data/lib/rubex/ast/expression/method_call/ruby_method_call.rb +83 -0
  44. data/lib/rubex/ast/expression/name.rb +127 -0
  45. data/lib/rubex/ast/expression/ruby_constant.rb +25 -0
  46. data/lib/rubex/ast/expression/ruby_object_element_ref/ruby_array_element_ref.rb +20 -0
  47. data/lib/rubex/ast/expression/ruby_object_element_ref/ruby_hash_element_ref.rb +22 -0
  48. data/lib/rubex/ast/expression/self.rb +15 -0
  49. data/lib/rubex/ast/expression/size_of.rb +22 -0
  50. data/lib/rubex/ast/expression/struct_or_union_member_call/element_ref_member_call.rb +23 -0
  51. data/lib/rubex/ast/expression/to_ruby_object.rb +21 -0
  52. data/lib/rubex/ast/expression/typecast.rb +20 -0
  53. data/lib/rubex/ast/expression/typecast_to.rb +10 -0
  54. data/lib/rubex/ast/expression/unary.rb +37 -0
  55. data/lib/rubex/ast/expression/unary_base.rb +24 -0
  56. data/lib/rubex/ast/expression/unary_base/ampersand.rb +16 -0
  57. data/lib/rubex/ast/expression/unary_base/unary_bit_not.rb +18 -0
  58. data/lib/rubex/ast/expression/unary_base/unary_not.rb +18 -0
  59. data/lib/rubex/ast/expression/unary_base/unary_sub.rb +18 -0
  60. data/lib/rubex/ast/node.rb +111 -111
  61. data/lib/rubex/ast/statement.rb +9 -1160
  62. data/lib/rubex/ast/statement/alias.rb +43 -0
  63. data/lib/rubex/ast/statement/argument_list.rb +59 -0
  64. data/lib/rubex/ast/statement/assign.rb +35 -0
  65. data/lib/rubex/ast/statement/begin_block.rb +14 -0
  66. data/lib/rubex/ast/statement/begin_block/begin.rb +202 -0
  67. data/lib/rubex/ast/statement/begin_block/else.rb +21 -0
  68. data/lib/rubex/ast/statement/begin_block/ensure.rb +21 -0
  69. data/lib/rubex/ast/statement/begin_block/rescue.rb +34 -0
  70. data/lib/rubex/ast/statement/break.rb +18 -0
  71. data/lib/rubex/ast/statement/c_array_decl.rb +49 -0
  72. data/lib/rubex/ast/statement/c_base_type.rb +26 -0
  73. data/lib/rubex/ast/statement/c_function_decl.rb +30 -0
  74. data/lib/rubex/ast/statement/c_ptr_decl.rb +52 -0
  75. data/lib/rubex/ast/statement/c_ptr_decl/c_ptr_func_decl.rb +25 -0
  76. data/lib/rubex/ast/statement/c_struct_or_union_def.rb +49 -0
  77. data/lib/rubex/ast/statement/expression.rb +26 -0
  78. data/lib/rubex/ast/statement/for.rb +73 -0
  79. data/lib/rubex/ast/statement/forward_decl.rb +31 -0
  80. data/lib/rubex/ast/statement/if_block.rb +64 -0
  81. data/lib/rubex/ast/statement/if_block/else.rb +30 -0
  82. data/lib/rubex/ast/statement/if_block/elsif.rb +22 -0
  83. data/lib/rubex/ast/statement/if_block/helper.rb +38 -0
  84. data/lib/rubex/ast/statement/print.rb +49 -0
  85. data/lib/rubex/ast/statement/raise.rb +66 -0
  86. data/lib/rubex/ast/statement/return.rb +45 -0
  87. data/lib/rubex/ast/statement/var_decl.rb +49 -0
  88. data/lib/rubex/ast/statement/while.rb +34 -0
  89. data/lib/rubex/ast/statement/yield.rb +41 -0
  90. data/lib/rubex/ast/top_statement.rb +1 -815
  91. data/lib/rubex/ast/top_statement/c_bindings.rb +145 -0
  92. data/lib/rubex/ast/top_statement/klass.rb +125 -0
  93. data/lib/rubex/ast/top_statement/klass/attached_klass.rb +417 -0
  94. data/lib/rubex/ast/top_statement/method_def.rb +110 -0
  95. data/lib/rubex/ast/top_statement/method_def/c_function_def.rb +26 -0
  96. data/lib/rubex/ast/top_statement/method_def/ruby_method_def.rb +33 -0
  97. data/lib/rubex/cli.rb +26 -0
  98. data/lib/rubex/code_writer.rb +1 -1
  99. data/lib/rubex/compiler.rb +49 -28
  100. data/lib/rubex/compiler_config.rb +4 -2
  101. data/lib/rubex/constants.rb +71 -71
  102. data/lib/rubex/data_type.rb +9 -675
  103. data/lib/rubex/data_type/c_array.rb +33 -0
  104. data/lib/rubex/data_type/c_function.rb +23 -0
  105. data/lib/rubex/data_type/c_ptr.rb +71 -0
  106. data/lib/rubex/data_type/c_str.rb +23 -0
  107. data/lib/rubex/data_type/c_struct_or_union.rb +23 -0
  108. data/lib/rubex/data_type/char.rb +30 -0
  109. data/lib/rubex/data_type/f_32.rb +38 -0
  110. data/lib/rubex/data_type/f_64.rb +38 -0
  111. data/lib/rubex/data_type/int.rb +32 -0
  112. data/lib/rubex/data_type/int/c_boolean.rb +13 -0
  113. data/lib/rubex/data_type/int_16.rb +32 -0
  114. data/lib/rubex/data_type/int_32.rb +32 -0
  115. data/lib/rubex/data_type/int_64.rb +36 -0
  116. data/lib/rubex/data_type/int_8.rb +33 -0
  117. data/lib/rubex/data_type/l_int.rb +38 -0
  118. data/lib/rubex/data_type/l_l_int.rb +26 -0
  119. data/lib/rubex/data_type/ruby_method.rb +22 -0
  120. data/lib/rubex/data_type/ruby_object.rb +19 -0
  121. data/lib/rubex/data_type/ruby_object/boolean.rb +11 -0
  122. data/lib/rubex/data_type/ruby_object/boolean/false_type.rb +5 -0
  123. data/lib/rubex/data_type/ruby_object/boolean/true_type.rb +5 -0
  124. data/lib/rubex/data_type/ruby_object/nil_type.rb +9 -0
  125. data/lib/rubex/data_type/ruby_object/ruby_array.rb +10 -0
  126. data/lib/rubex/data_type/ruby_object/ruby_constant.rb +18 -0
  127. data/lib/rubex/data_type/ruby_object/ruby_constant/ruby_class.rb +18 -0
  128. data/lib/rubex/data_type/ruby_object/ruby_hash.rb +9 -0
  129. data/lib/rubex/data_type/ruby_object/ruby_string.rb +10 -0
  130. data/lib/rubex/data_type/ruby_object/ruby_symbol.rb +10 -0
  131. data/lib/rubex/data_type/type_def.rb +34 -0
  132. data/lib/rubex/data_type/u_char.rb +27 -0
  133. data/lib/rubex/data_type/u_int.rb +32 -0
  134. data/lib/rubex/data_type/u_int_16.rb +22 -0
  135. data/lib/rubex/data_type/u_int_32.rb +22 -0
  136. data/lib/rubex/data_type/u_int_64.rb +26 -0
  137. data/lib/rubex/data_type/u_int_8.rb +22 -0
  138. data/lib/rubex/data_type/u_l_int.rb +36 -0
  139. data/lib/rubex/data_type/u_l_int/size_t.rb +10 -0
  140. data/lib/rubex/data_type/u_l_l_int.rb +26 -0
  141. data/lib/rubex/data_type/void.rb +15 -0
  142. data/lib/rubex/data_type_helpers/float_helpers.rb +8 -0
  143. data/lib/rubex/data_type_helpers/helpers.rb +48 -0
  144. data/lib/rubex/data_type_helpers/int_helpers.rb +10 -0
  145. data/lib/rubex/data_type_helpers/u_int_helpers.rb +11 -0
  146. data/lib/rubex/helpers.rb +35 -118
  147. data/lib/rubex/helpers/node_type_methods.rb +9 -0
  148. data/lib/rubex/helpers/writers.rb +79 -0
  149. data/lib/rubex/parser.racc +83 -34
  150. data/lib/rubex/parser.racc.rb +233 -184
  151. data/lib/rubex/version.rb +2 -2
  152. data/rubex.gemspec +2 -0
  153. data/spec/basic_ruby_method_spec.rb +1 -1
  154. data/spec/binding_ptr_args_spec.rb +2 -2
  155. data/spec/bitwise_operators_spec.rb +1 -1
  156. data/spec/blocks_spec.rb +2 -2
  157. data/spec/c_bindings_spec.rb +1 -1
  158. data/spec/c_constants_spec.rb +1 -1
  159. data/spec/c_function_ptrs_spec.rb +1 -1
  160. data/spec/c_functions_spec.rb +2 -2
  161. data/spec/c_struct_interface_spec.rb +1 -1
  162. data/spec/call_by_reference_spec.rb +2 -2
  163. data/spec/class_methods_spec.rb +2 -2
  164. data/spec/class_spec.rb +4 -4
  165. data/spec/cli_spec.rb +43 -0
  166. data/spec/comments_spec.rb +2 -2
  167. data/spec/default_args_spec.rb +21 -23
  168. data/spec/error_handling_spec.rb +1 -1
  169. data/spec/examples_spec.rb +4 -4
  170. data/spec/expressions_spec.rb +1 -1
  171. data/spec/fixtures/cli/cli.rubex +3 -0
  172. data/spec/fixtures/examples/array_to_hash.rubex +1 -1
  173. data/spec/fixtures/examples/rcsv.rubex +10 -6
  174. data/spec/fixtures/loops/loops.rubex +1 -1
  175. data/spec/fixtures/ruby_strings/string_blank_bm.rb +7 -5
  176. data/spec/fixtures/struct/struct.rubex +7 -2
  177. data/spec/fixtures/temp_allocation/temp_allocation.rubex +8 -0
  178. data/spec/if_else_spec.rb +3 -7
  179. data/spec/implicit_lib_include_spec.rb +1 -1
  180. data/spec/init_ruby_objects_with_literal_syntax_spec.rb +1 -1
  181. data/spec/loops_spec.rb +1 -1
  182. data/spec/recursion_spec.rb +18 -21
  183. data/spec/ruby_constant_method_calls_spec.rb +4 -4
  184. data/spec/ruby_operators_spec.rb +1 -1
  185. data/spec/ruby_raise_spec.rb +1 -1
  186. data/spec/ruby_strings_spec.rb +3 -3
  187. data/spec/ruby_symbols_spec.rb +1 -1
  188. data/spec/ruby_types_spec.rb +2 -2
  189. data/spec/spec_helper.rb +42 -10
  190. data/spec/statement_expression_spec.rb +3 -3
  191. data/spec/static_array_spec.rb +3 -3
  192. data/spec/string_literals_spec.rb +2 -2
  193. data/spec/struct_spec.rb +4 -4
  194. data/spec/temp_allocation_spec.rb +35 -0
  195. data/spec/typecasting_spec.rb +2 -2
  196. data/spec/var_declarions_spec.rb +2 -2
  197. metadata +168 -3
@@ -0,0 +1,110 @@
1
+ module Rubex
2
+ module AST
3
+ module TopStatement
4
+ class MethodDef
5
+ include Rubex::Helpers::Writers
6
+ # Ruby name of the method.
7
+ attr_reader :name
8
+ # Method arguments. Accessor because arguments need to be modified in
9
+ # case of auxillary C functions of attach classes.
10
+ attr_accessor :arg_list
11
+ # The statments/expressions contained within the method.
12
+ attr_reader :statements
13
+ # Symbol Table entry.
14
+ attr_reader :entry
15
+ # Instance of Scope::Local for this method.
16
+ attr_reader :scope
17
+ # Variable name that identifies 'self'
18
+ attr_reader :self_name
19
+
20
+ def initialize(name, arg_list, statements)
21
+ @name = name
22
+ @arg_list = arg_list
23
+ @statements = statements
24
+ @self_name = Rubex::ARG_PREFIX + 'self'
25
+ end
26
+
27
+ def analyse_statement(outer_scope)
28
+ @entry = outer_scope.find @name
29
+ @scope = @entry.type.scope
30
+ @scope.type = @entry.type
31
+ @scope.self_name = @self_name
32
+ @arg_list = @entry.type.arg_list
33
+ @statements.each do |stat|
34
+ stat.analyse_statement @scope
35
+ end
36
+ end
37
+
38
+ # Option c_function - When set to true, certain code that is not required
39
+ # for Ruby methods will be generated too.
40
+ def generate_code(code, c_function: false)
41
+ code.block do
42
+ generate_function_definition code, c_function: c_function
43
+ end
44
+ end
45
+
46
+ def rescan_declarations(_scope)
47
+ @statements.each do |stat|
48
+ stat.respond_to?(:rescan_declarations) &&
49
+ stat.rescan_declarations(@scope)
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def generate_function_definition(code, c_function:)
56
+ declare_types code, @scope
57
+ declare_args code unless c_function
58
+ declare_vars code, @scope
59
+ declare_carrays code, @scope
60
+ declare_ruby_objects code, @scope
61
+ declare_temps code, @scope
62
+ generate_arg_checking code unless c_function
63
+ init_args code unless c_function
64
+ declare_carrays_using_init_var_value code
65
+ generate_statements code
66
+ end
67
+
68
+ def declare_args(code)
69
+ @scope.arg_entries.each do |arg|
70
+ code.declare_variable type: arg.type.to_s, c_name: arg.c_name
71
+ end
72
+ end
73
+
74
+ def generate_statements(code)
75
+ @statements.each do |stat|
76
+ stat.generate_code code, @scope
77
+ end
78
+ end
79
+
80
+ def init_args(code)
81
+ @scope.arg_entries.each_with_index do |arg, i|
82
+ code << arg.c_name + '=' + arg.type.from_ruby_object("argv[#{i}]") + ';'
83
+ code.nl
84
+ end
85
+ end
86
+
87
+ def declare_carrays_using_init_var_value(code)
88
+ @scope.carray_entries.reject do |s|
89
+ s.type.dimension.is_a?(Rubex::AST::Expression::Literal::Base)
90
+ end. each do |arr|
91
+ type = arr.type.type.to_s
92
+ c_name = arr.c_name
93
+ dimension = arr.type.dimension.c_code(@scope)
94
+ value = arr.value.map { |a| a.c_code(@scope) } if arr.value
95
+ code.declare_carray(type: type, c_name: c_name, dimension: dimension,
96
+ value: value)
97
+ end
98
+ end
99
+
100
+ def generate_arg_checking(code)
101
+ code << 'if (argc < ' + @scope.arg_entries.size.to_s + ')'
102
+ code.block do
103
+ code << %{rb_raise(rb_eArgError, "Need #{@scope.arg_entries.size} args, not %d", argc);\n}
104
+ end
105
+ end
106
+ end
107
+
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,26 @@
1
+ module Rubex
2
+ module AST
3
+ module TopStatement
4
+ class CFunctionDef < MethodDef
5
+ attr_reader :type, :return_ptr_level
6
+
7
+ def initialize(type, return_ptr_level, name, arg_list, statements)
8
+ super(name, arg_list, statements)
9
+ @type = type
10
+ @return_ptr_level = return_ptr_level
11
+ end
12
+
13
+ def analyse_statement(outer_scope, extern: false)
14
+ super(outer_scope)
15
+ end
16
+
17
+ def generate_code(code)
18
+ code.write_c_method_header(type: @entry.type.type.to_s,
19
+ c_name: @entry.c_name, args: Helpers.create_arg_arrays(@arg_list))
20
+ super code, c_function: true
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ module Rubex
2
+ module AST
3
+ module TopStatement
4
+ class RubyMethodDef < MethodDef
5
+ attr_reader :singleton
6
+
7
+ def initialize(name, arg_list, statements, singleton: false)
8
+ super(name, arg_list, statements)
9
+ @singleton = singleton
10
+ end
11
+
12
+ def analyse_statement(local_scope)
13
+ super
14
+ @entry.singleton = @singleton
15
+ end
16
+
17
+ def generate_code(code)
18
+ code.write_ruby_method_header(type: @entry.type.type.to_s,
19
+ c_name: @entry.c_name)
20
+ super
21
+ end
22
+
23
+ def ==(other)
24
+ self.class == other.class && @name == other.name &&
25
+ @c_name == other.c_name && @arg_list == other.arg_list &&
26
+ @statements == other.statements && @entry == other.entry &&
27
+ @type == other.type
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ require 'thor'
2
+ module Rubex
3
+ # Cli for rubex using Thor(http://whatisthor.com/)
4
+ class Cli < Thor
5
+ desc 'generate FILE', 'generates directory with name specified in the argument and creates an extconf.rb file which is required for C extensions'
6
+ option :force, aliases: '-f', desc: 'replace existing files and directories'
7
+ option :dir, aliases: '-d', desc: 'specify a directory for generating files', type: :string
8
+ option :install, aliases: '-i', desc: 'automatically run install command after generating Makefile'
9
+ option :debug, aliases: '-g', desc: 'enable debugging symbols when compiling with GCC'
10
+ def generate(file)
11
+ if (force = options[:force])
12
+ directory = (options[:dir] ? options[:dir].to_s : Dir.pwd) + "/#{Rubex::Compiler.extract_target_name(file)}"
13
+ STDOUT.puts "Warning! you are about to replace contents in the directory '#{directory}', Are you sure? [Yn] "
14
+ confirmation = STDIN.gets.chomp
15
+ force = (confirmation == 'Y')
16
+ end
17
+ Rubex::Compiler.compile file, directory: options[:dir], force: force, make: options[:install], debug: options[:debug]
18
+ end
19
+
20
+ desc 'install PATH', 'run "make" utility to generate a shared object file required for C extensions'
21
+ def install(path)
22
+ Rubex::Compiler.run_make path
23
+ end
24
+
25
+ end
26
+ end
@@ -34,7 +34,7 @@ module Rubex
34
34
  end
35
35
 
36
36
  def write_ruby_method_header type: , c_name:
37
- args = [["int", "argc"], ["VALUE*", "argv"],
37
+ args = [["int", "argc"], ["VALUE*", "argv"],
38
38
  ["VALUE", "#{Rubex::ARG_PREFIX + "self"}"]]
39
39
  write_func_prototype type, c_name, args
40
40
  end
@@ -1,64 +1,65 @@
1
+ require 'fileutils'
1
2
  module Rubex
2
3
  class Compiler
3
4
  CONFIG = Rubex::CompilerConfig.new
4
5
 
5
6
  class << self
6
- def compile path, test: false, directory: nil
7
+ def compile path, test: false, directory: nil, force: false, make: false, debug: false
7
8
  tree = ast path, test: test
8
9
  target_name = extract_target_name path
9
10
  code = generate_code tree, target_name
10
11
  ext = extconf target_name, directory: directory
11
12
  CONFIG.flush
12
-
13
+ CONFIG.debug = debug
14
+
13
15
  return [tree, code, ext] if test
14
- write_files target_name, code, ext, directory: directory
16
+ write_files target_name, code, ext, directory: directory, force: force
17
+ full_path = build_path(directory, target_name)
18
+ load_extconf full_path
19
+ run_make full_path if make
15
20
  end
16
21
 
17
22
  def ast path, test: false
18
- if test
19
- parser = Rubex::Parser.new
20
- parser.parse(path)
21
- parser.do_parse
22
- else
23
- begin
24
- parser = Rubex::Parser.new
25
- parser.parse(path)
26
- parser.do_parse
27
- rescue Racc::ParseError => e
28
- error_msg = "\nPARSE ERROR:\n"
29
- error_msg << "Line: #{parser.string.split("\n")[parser.lineno-1]}\n"
30
- error_msg << "Location: #{parser.location}\n"
31
- error_msg << "Error:\n#{e}"
32
- STDERR.puts error_msg
33
- end
34
- end
23
+ parser = Rubex::Parser.new
24
+ parser.parse(path)
25
+ parser.do_parse
26
+ rescue Racc::ParseError => e
27
+ raise e if test
28
+
29
+ error_msg = "\nPARSE ERROR:\n"
30
+ error_msg << "Line: #{parser.string.split("\n")[parser.lineno-1]}\n"
31
+ error_msg << "Location: #{parser.location}\n"
32
+ error_msg << "Error:\n#{e}"
33
+ STDERR.puts error_msg
35
34
  end
36
35
 
37
36
  def extconf target_name, directory: nil
38
- path = directory ? directory : "#{Dir.pwd}/#{target_name}"
37
+ path = build_path(directory, target_name)
39
38
  extconf = ""
40
39
  extconf << "require 'mkmf'\n"
41
40
  extconf << "$libs += \" #{CONFIG.link_flags}\"\n"
41
+ extconf << "$CFLAGS += \" -g \"\n" if CONFIG.debug
42
42
  extconf << "create_makefile('#{path}/#{target_name}')\n"
43
43
  extconf
44
44
  end
45
45
 
46
46
  def generate_code tree, target_name
47
47
  code = Rubex::CodeWriter.new target_name
48
- raise "Must be a Rubex::AST::Node, not #{tree.class}" unless
48
+ raise "Must be a Rubex::AST::Node, not #{tree.class}" unless
49
49
  tree.is_a? Rubex::AST::Node
50
50
  tree.process_statements target_name, code
51
51
  code
52
52
  end
53
53
 
54
54
  def extract_target_name path
55
- File.basename(path).split('.')[0]
55
+ File.basename(path).split('.')[0]
56
56
  end
57
57
 
58
- def write_files target_name, code, ext, directory: nil
59
- path = directory ? directory : "#{Dir.pwd}/#{target_name}"
60
- Dir.mkdir(path) unless directory
61
-
58
+ def write_files target_name, code, ext, directory: nil, force: false
59
+ path = build_path(directory, target_name)
60
+ FileUtils.rm_rf(path) if force && Dir.exist?(path)
61
+ Dir.mkdir(path) unless Dir.exist?(path)
62
+
62
63
  code_file = File.new "#{path}/#{target_name}.c", "w+"
63
64
  code_file.puts code.to_s
64
65
  code_file.close
@@ -67,6 +68,26 @@ module Rubex
67
68
  extconf_file.puts ext
68
69
  extconf_file.close
69
70
  end
71
+
72
+ def load_extconf path
73
+ Dir.chdir(path) do
74
+ system("ruby #{path}/extconf.rb")
75
+ end
76
+ end
77
+
78
+ def run_make path
79
+ Dir.chdir(path) do
80
+ system("make -C #{path}")
81
+ end
82
+ end
83
+
84
+ def build_path directory, target_name
85
+ directory = (directory ? directory.to_s : Dir.pwd)
86
+ unless directory.end_with?(target_name)
87
+ directory += "/#{target_name}"
88
+ end
89
+ directory
90
+ end
70
91
  end
71
92
  end
72
- end
93
+ end
@@ -1,7 +1,9 @@
1
1
  module Rubex
2
2
  class CompilerConfig
3
+ attr_accessor :debug
4
+
3
5
  def initialize
4
- @links = []
6
+ @links = []
5
7
  end
6
8
 
7
9
  def add_link link_str
@@ -16,4 +18,4 @@ module Rubex
16
18
  @links = []
17
19
  end
18
20
  end
19
- end
21
+ end
@@ -1,7 +1,7 @@
1
1
  module Rubex
2
2
  RUBEX_PREFIX = "__rubex_"
3
3
  GLOBAL_PREFIX = "global_"
4
-
4
+
5
5
  TEMP_PREFIX = RUBEX_PREFIX + "temp_"
6
6
  RUBY_FUNC_PREFIX = RUBEX_PREFIX + "rb_f_"
7
7
  C_FUNC_PREFIX = RUBEX_PREFIX + "c_f_"
@@ -50,75 +50,75 @@ module Rubex
50
50
  CUSTOM_TYPES = {}
51
51
 
52
52
  DEFAULT_CLASS_MAPPINGS = {
53
- "Kernel" => "rb_mKernel",
54
- "Comparable" => "rb_mComparable",
55
- "Enumerable" => "rb_mEnumerable",
56
- "Errno" => "rb_mErrno",
57
- "FileTest" => "rb_mFileTest",
58
- "GC" => "rb_mGC",
59
- "Math" => "rb_mMath",
60
- "Process" => "rb_mProcess",
61
- "WaitReadable" => "rb_mWaitReadable",
62
- "WaitWritable" => "rb_mWaitWritable",
63
- "BasicObject" => "rb_cBasicObject",
64
- "Object" => "rb_cObject",
65
- "Array" => "rb_cArray",
66
- "Bignum" => "rb_cBignum",
67
- "Binding" => "rb_cBinding",
68
- "Class" => "rb_cClass",
69
- "Cont" => "rb_cCont",
70
- "Dir" => "rb_cDir",
71
- "Data" => "rb_cData",
72
- "FalseClass" => "rb_cFalseClass",
73
- "Encoding" => "rb_cEncoding",
74
- "Enumerator" => "rb_cEnumerator",
75
- "File" => "rb_cFile",
76
- "Fixnum" => "rb_cFixnum",
77
- "Float" => "rb_cFloat",
78
- "Hash" => "rb_cHash",
79
- "Integer" => "rb_cInteger",
80
- "IO" => "rb_cIO",
81
- "Match" => "rb_cMatch",
82
- "Method" => "rb_cMethod",
83
- "Module" => "rb_cModule",
84
- "NameErrorMesg" => "rb_cNameErrorMesg",
85
- "NilClass" => "rb_cNilClass",
86
- "Numeric" => "rb_cNumeric",
87
- "Proc" => "rb_cProc",
88
- "Random" => "rb_cRandom",
89
- "Range" => "rb_cRange",
90
- "Rational" => "rb_cRational",
91
- "Complex" => "rb_cComplex",
92
- "Regexp" => "rb_cRegexp",
93
- "Stat" => "rb_cStat",
94
- "String" => "rb_cString",
95
- "Struct" => "rb_cStruct",
96
- "Symbol" => "rb_cSymbol",
97
- "Thread" => "rb_cThread",
98
- "Time" => "rb_cTime",
99
- "TrueClass" => "rb_cTrueClass",
100
- "UnboundMethod" => "rb_cUnboundMethod",
101
- "Exception" => "rb_eException",
102
- "StandardError" => "rb_eStandardError",
103
- "SystemExit" => "rb_eSystemExit",
104
- "Interrupt" => "rb_eInterrupt",
105
- "Signal" => "rb_eSignal",
106
- "Fatal" => "rb_eFatal",
107
- "ArgumentError" => "rb_eArgError",
108
- "EOFError" => "rb_eEOFError",
109
- "IndexError" => "rb_eIndexError",
110
- "StopIteration" => "rb_eStopIteration",
111
- "KeyError" => "rb_eKeyError",
112
- "RangeError" => "rb_eRangeError",
113
- "IOError" => "rb_eIOError",
114
- "RuntimeError" => "rb_eRuntimeError",
115
- "SecurityError" => "rb_eSecurityError",
116
- "SystemCallError" => "rb_eSystemCallError",
117
- "ThreadError" => "rb_eThreadError",
118
- "TypeError" => "rb_eTypeError",
119
- "ZeroDivError" => "rb_eZeroDivError",
120
- "NotImpError" => "rb_eNotImpError",
121
- "NoMemError" => "rb_eNoMemError",
53
+ "Kernel" => "rb_mKernel",
54
+ "Comparable" => "rb_mComparable",
55
+ "Enumerable" => "rb_mEnumerable",
56
+ "Errno" => "rb_mErrno",
57
+ "FileTest" => "rb_mFileTest",
58
+ "GC" => "rb_mGC",
59
+ "Math" => "rb_mMath",
60
+ "Process" => "rb_mProcess",
61
+ "WaitReadable" => "rb_mWaitReadable",
62
+ "WaitWritable" => "rb_mWaitWritable",
63
+ "BasicObject" => "rb_cBasicObject",
64
+ "Object" => "rb_cObject",
65
+ "Array" => "rb_cArray",
66
+ "Bignum" => "rb_cBignum",
67
+ "Binding" => "rb_cBinding",
68
+ "Class" => "rb_cClass",
69
+ "Cont" => "rb_cCont",
70
+ "Dir" => "rb_cDir",
71
+ "Data" => "rb_cData",
72
+ "FalseClass" => "rb_cFalseClass",
73
+ "Encoding" => "rb_cEncoding",
74
+ "Enumerator" => "rb_cEnumerator",
75
+ "File" => "rb_cFile",
76
+ "Fixnum" => "rb_cFixnum",
77
+ "Float" => "rb_cFloat",
78
+ "Hash" => "rb_cHash",
79
+ "Integer" => "rb_cInteger",
80
+ "IO" => "rb_cIO",
81
+ "Match" => "rb_cMatch",
82
+ "Method" => "rb_cMethod",
83
+ "Module" => "rb_cModule",
84
+ "NameErrorMesg" => "rb_cNameErrorMesg",
85
+ "NilClass" => "rb_cNilClass",
86
+ "Numeric" => "rb_cNumeric",
87
+ "Proc" => "rb_cProc",
88
+ "Random" => "rb_cRandom",
89
+ "Range" => "rb_cRange",
90
+ "Rational" => "rb_cRational",
91
+ "Complex" => "rb_cComplex",
92
+ "Regexp" => "rb_cRegexp",
93
+ "Stat" => "rb_cStat",
94
+ "String" => "rb_cString",
95
+ "Struct" => "rb_cStruct",
96
+ "Symbol" => "rb_cSymbol",
97
+ "Thread" => "rb_cThread",
98
+ "Time" => "rb_cTime",
99
+ "TrueClass" => "rb_cTrueClass",
100
+ "UnboundMethod" => "rb_cUnboundMethod",
101
+ "Exception" => "rb_eException",
102
+ "StandardError" => "rb_eStandardError",
103
+ "SystemExit" => "rb_eSystemExit",
104
+ "Interrupt" => "rb_eInterrupt",
105
+ "Signal" => "rb_eSignal",
106
+ "Fatal" => "rb_eFatal",
107
+ "ArgumentError" => "rb_eArgError",
108
+ "EOFError" => "rb_eEOFError",
109
+ "IndexError" => "rb_eIndexError",
110
+ "StopIteration" => "rb_eStopIteration",
111
+ "KeyError" => "rb_eKeyError",
112
+ "RangeError" => "rb_eRangeError",
113
+ "IOError" => "rb_eIOError",
114
+ "RuntimeError" => "rb_eRuntimeError",
115
+ "SecurityError" => "rb_eSecurityError",
116
+ "SystemCallError" => "rb_eSystemCallError",
117
+ "ThreadError" => "rb_eThreadError",
118
+ "TypeError" => "rb_eTypeError",
119
+ "ZeroDivError" => "rb_eZeroDivError",
120
+ "NotImpError" => "rb_eNotImpError",
121
+ "NoMemError" => "rb_eNoMemError",
122
122
  "NoMethodError" => "rb_eNoMethodError",
123
123
  "FloatDomainError" => "rb_eFloatDomainError",
124
124
  "LocalJumpError" => "rb_eLocalJumpError",
@@ -138,7 +138,7 @@ module Rubex
138
138
 
139
139
  C_MACRO_INT2BOOL = Rubex::RUBEX_PREFIX + "INT2BOOL"
140
140
  C_FUNC_CHAR2RUBYSTR = Rubex::RUBEX_PREFIX + "char2rubystr"
141
-
141
+
142
142
  ALLOC_FUNC_NAME = 'allocate'
143
143
  DEALLOC_FUNC_NAME = 'deallocate'
144
144
  MEMCOUNT_FUNC_NAME = 'memcount'