rubex 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,678 +1,12 @@
1
- module Rubex
2
- module DataType
3
- # Citations
4
- # Printf arguments:
5
- # http://www.thinkage.ca/english/gcos/expl/c/lib/printf.html
6
- module Helpers
7
- include ::Comparable
8
- [
9
- :float?, :float32?, :float64?,
10
- :int?, :int8?, :int16?, :int32?, :int64?,
11
- :uint?, :uint8?, :uint16?, :uint32?, :uint64?,
12
- :lint?, :ulint?, :llint?, :ullint?,
13
- :char?, :object?, :bool?, :carray?, :cbool?,
14
- :cptr?, :nil_type?, :struct_or_union?,
15
- :alias_type?, :string?, :cstr?, :ruby_class?,
16
- :ruby_method?, :c_function?, :ruby_constant?, :void?,
17
- :ruby_string?, :uchar?, :ruby_array?, :ruby_hash?
18
- ].each do |dtype|
19
- define_method(dtype) { return false }
20
- end
1
+ require_relative 'data_type_helpers/helpers'
2
+ Dir['./lib/rubex/data_type_helpers/**/*.rb'].sort.each { |f| require f }
3
+ Dir['./lib/rubex/data_type/**/*.rb'].sort.each { |f| require f }
21
4
 
22
- def == other
23
- self.class == other.class
24
- end
5
+ # TODO: How to store this in a Ruby class? Use BigDecimal?
6
+ # class LF64
7
+ # def to_s; "long double"; end
25
8
 
26
- def to_ruby_object(arg); arg; end
9
+ # def to_ruby_object(arg); "INT2NUM"; end
27
10
 
28
- def from_ruby_object(arg); arg; end
29
-
30
- def base_type; self; end
31
-
32
- # Helper function to know if a dtype is a char pointer.
33
- def char_ptr?
34
- if self.cptr? && self.base_type.char?
35
- true
36
- else
37
- false
38
- end
39
- end
40
-
41
- def c_function_ptr?
42
- if self.cptr? && self.base_type.c_function?
43
- true
44
- else
45
- false
46
- end
47
- end
48
- end
49
-
50
- module IntHelpers
51
- include Helpers
52
- def to_ruby_object(arg); "INT2NUM(#{arg})"; end
53
- end
54
-
55
- module UIntHelpers
56
- include Helpers
57
- def to_ruby_object(arg); "UINT2NUM(#{arg})"; end
58
- end
59
-
60
- module FloatHelpers
61
- include Helpers
62
- end
63
-
64
- class Void
65
- include Helpers
66
-
67
- def void?; true; end
68
-
69
- def to_s; "void"; end
70
- end
71
-
72
- class RubyObject
73
- include Helpers
74
- def to_s; "VALUE"; end
75
-
76
- def object?; true; end
77
-
78
- def p_formatter; "%s"; end
79
- end
80
-
81
- class RubySymbol < RubyObject
82
- def ruby_symbol?; true; end
83
- end
84
-
85
- class RubyString < RubyObject
86
- def ruby_string?; true; end
87
- end
88
-
89
- class RubyArray < RubyObject
90
- def ruby_array?; true; end
91
- end
92
-
93
- class RubyHash < RubyObject
94
- def ruby_hash?; true; end
95
- end
96
-
97
- class Char
98
- include Helpers
99
- def to_s; "char"; end
100
-
101
- def to_ruby_object(arg, literal=false)
102
- "#{Rubex::C_FUNC_CHAR2RUBYSTR}(#{arg})"
103
- end
104
-
105
- def from_ruby_object(arg); "(char)NUM2INT(#{arg})"; end
106
-
107
- def p_formatter; "%c"; end
108
-
109
- def char?; true; end
110
-
111
- def <=> other
112
- if other.char?
113
- return 0
114
- else
115
- return 1
116
- end
117
- end
118
- end
119
-
120
- class UChar
121
- include Helpers
122
-
123
- def to_s; "unsigned char"; end
124
-
125
- def from_ruby_object(arg); "(unsigned char)NUM2INT(#{arg})"; end
126
-
127
- def p_formatter; "%d"; end
128
-
129
- def uchar?; true; end
130
-
131
- def <=> other
132
- if other.char? || other.uchar?
133
- return 0
134
- else
135
- return 1
136
- end
137
- end
138
- end
139
-
140
- class Int8
141
- include IntHelpers
142
-
143
- def to_s; "int8_t"; end
144
-
145
- def from_ruby_object(arg); "(int8_t)NUM2INT(#{arg})"; end
146
-
147
- def int8?; true; end
148
-
149
- def p_formatter; "%d"; end
150
-
151
- def <=> other
152
- if other.char?
153
- return 1
154
- elsif other.int8?
155
- return 0
156
- else
157
- return -1
158
- end
159
- end
160
- end
161
-
162
- class Int16
163
- include IntHelpers
164
- def to_s; "int16_t"; end
165
-
166
- def from_ruby_object(arg); "(int16_t)NUM2INT(#{arg})"; end
167
-
168
- def int16?; true; end
169
-
170
- def p_formatter; "%d"; end
171
-
172
- def <=> other
173
- if other.char? || other.int8?
174
- return 1
175
- elsif other.int16?
176
- return 0
177
- else
178
- return -1
179
- end
180
- end
181
- end
182
-
183
- class Int32
184
- include IntHelpers
185
- def to_s; "int32_t"; end
186
-
187
- def from_ruby_object(arg); "(int32_t)NUM2INT(#{arg})"; end
188
-
189
- def int32?; true; end
190
-
191
- def p_formatter; "%d"; end
192
-
193
- def <=> other
194
- if other.char? || other.int8? || other.int16?
195
- return 1
196
- elsif other.int32? || other.int?
197
- return 0
198
- else
199
- return -1
200
- end
201
- end
202
- end
203
-
204
- class Int64
205
- include IntHelpers
206
- def to_s; "int64_t"; end
207
-
208
- def to_ruby_object(arg); "LONG2NUM(#{arg})"; end
209
-
210
- def from_ruby_object(arg); "(int64_t)NUM2LONG(#{arg})"; end
211
-
212
- def p_formatter; "%ld"; end
213
-
214
- def int64?; true; end
215
-
216
- def <=> other
217
- if other.char? || other.int8? || other.int16? || other.int32? ||
218
- other.int?
219
- return 1
220
- elsif other.int64?
221
- return 0
222
- else
223
- return -1
224
- end
225
- end
226
- end
227
-
228
- class UInt8
229
- include UIntHelpers
230
- def to_s; "uint8_t"; end
231
-
232
- def from_ruby_object(arg); "(uint8_t)NUM2UINT(#{arg})"; end
233
-
234
- def uint8?; true; end
235
-
236
- def p_formatter; "%u"; end
237
- end
238
-
239
- class UInt16
240
- include UIntHelpers
241
- def to_s; "uint16_t"; end
242
-
243
- def from_ruby_object(arg); "(uint16_t)NUM2UINT(#{arg})"; end
244
-
245
- def uint16?; true; end
246
-
247
- def p_formatter; "%u"; end
248
- end
249
-
250
- class UInt32
251
- include UIntHelpers
252
- def to_s; "uint32_t"; end
253
-
254
- def from_ruby_object(arg); "(int32_t)NUM2UINT(#{arg})"; end
255
-
256
- def uint32?; true; end
257
-
258
- def p_formatter; "%u"; end
259
- end
260
-
261
- class UInt64
262
- include UIntHelpers
263
- def to_s; "uint64_t"; end
264
-
265
- def to_ruby_object(arg); "ULONG2NUM(#{arg})"; end
266
-
267
- def from_ruby_object(arg); "(int64_t)NUM2UINT(#{arg})"; end
268
-
269
- def uint64?; true; end
270
-
271
- def p_formatter; "%lu"; end
272
- end
273
-
274
- class Int
275
- include IntHelpers
276
- def to_s; "int"; end
277
-
278
- def from_ruby_object(arg); "NUM2INT(#{arg})"; end
279
-
280
- def int?; true; end
281
-
282
- def p_formatter; "%d"; end
283
-
284
- def <=> other
285
- if other.char? || other.int8? || other.int16?
286
- return 1
287
- elsif other.int? || other.int32?
288
- return 0
289
- else # other is int64 or greater
290
- return -1
291
- end
292
- end
293
- end
294
-
295
- class CBoolean < Int
296
- def cbool?; true; end
297
-
298
- def to_ruby_object arg
299
- Rubex::C_MACRO_INT2BOOL + "(" + arg + ")"
300
- end
301
- end
302
-
303
- class UInt
304
- include UIntHelpers
305
- def to_s; "unsigned int"; end
306
-
307
- def from_ruby_object(arg); "(unsigned int)NUM2UINT(#{arg})"; end
308
-
309
- def uint?; true; end
310
-
311
- def p_formatter; "%u"; end
312
-
313
- def <=> other
314
- if other.char? || other.int8? || other.int16? || other.uint8? || other.uint16?
315
- return 1
316
- elsif other.uint? || other.int? || other.int32? || other.uint32?
317
- return 0
318
- else
319
- return -1
320
- end
321
- end
322
- end
323
-
324
- class LInt
325
- include IntHelpers
326
- def to_s; "long int"; end
327
-
328
- def to_ruby_object(arg); "LONG2NUM(#{arg})"; end
329
-
330
- def from_ruby_object(arg); "NUM2LONG(#{arg})"; end
331
-
332
- def lint?; true; end
333
-
334
- def p_formatter; "%ld"; end
335
-
336
- def <=> other
337
- if other.char? || other.int8? || other.int16? || other.int32? ||
338
- other .int64? || other.uint8? || other.uint16? || other.uint32? ||
339
- other.int?
340
- return 1
341
- elsif other.lint?
342
- return 0
343
- else
344
- return -1
345
- end
346
- end
347
- end
348
-
349
- class ULInt
350
- include UIntHelpers
351
- def to_s; "unsigned long int"; end
352
-
353
- def to_ruby_object(arg); "ULONG2NUM(#{arg})"; end
354
-
355
- def from_ruby_object(arg); "NUM2ULONG(#{arg})"; end
356
-
357
- def ulint?; true; end
358
-
359
- def p_formatter; "%lu"; end
360
-
361
- def <=> other
362
- if other.char? || other.int8? || other.int16? || other.int32? ||
363
- other .int64? || other.uint8? || other.uint16? || other.uint32? ||
364
- other.int?
365
- return 1
366
- elsif other.ulint?
367
- return 0
368
- else
369
- return -1
370
- end
371
- end
372
- end
373
-
374
- class Size_t < ULInt
375
- def to_s
376
- "size_t"
377
- end
378
- end
379
-
380
- class LLInt
381
- include IntHelpers
382
- def to_s; "long long int"; end
383
-
384
- def to_ruby_object(arg); "LL2NUM(#{arg})"; end
385
-
386
- def from_ruby_object(arg); "NUM2LL(#{arg})"; end
387
-
388
- def llint?; true; end
389
-
390
- def p_formatter; "%ll"; end
391
- end
392
-
393
- class ULLInt
394
- include UIntHelpers
395
- def to_s; "unsigned long long int"; end
396
-
397
- def to_ruby_object(arg); "ULL2NUM(#{arg})"; end
398
-
399
- def from_ruby_object(arg); "NUM2ULL(#{arg})"; end
400
-
401
- def ullint?; true; end
402
-
403
- def p_formatter; "%llu"; end
404
- end
405
-
406
- class F32
407
- include FloatHelpers
408
- def to_s; "float"; end
409
-
410
- def to_ruby_object(arg); "rb_float_new((double)(#{arg}))"; end
411
-
412
- def from_ruby_object(arg); "(float)NUM2DBL(#{arg})"; end
413
-
414
- def float32?; true; end
415
-
416
- def p_formatter; "%f"; end
417
-
418
- def <=> other
419
- if other.char? || other.int8? || other.int16? || other.int32? ||
420
- other.int64? || other.int? || other.uint8? || other.uint16?||
421
- other.uint32? || other.uint64?
422
- return 1
423
- elsif other.float32?
424
- return 0
425
- else # other is float64
426
- return -1
427
- end
428
- end
429
-
430
- end
431
-
432
- class F64
433
- include FloatHelpers
434
- def to_s; "double"; end
435
-
436
- def to_ruby_object(arg); "rb_float_new(#{arg})"; end
437
-
438
- def from_ruby_object(arg); "NUM2DBL(#{arg})"; end
439
-
440
- def float64?; true; end
441
-
442
- def p_formatter; "%f"; end
443
-
444
- def <=> other
445
- if other.char? || other.int8? || other.int16? || other.int32? ||
446
- other.int64? || other.int? || other.uint8? || other.uint16?||
447
- other.uint32? || other.uint64? || other.float32?
448
- return 1
449
- elsif other.float64?
450
- return 0
451
- else
452
- return -1
453
- end
454
- end
455
- end
456
-
457
- class CArray
458
- include Helpers
459
- # Dimension of the array
460
- attr_reader :dimension
461
- # Type of elements stored in array
462
- attr_reader :type # FIXME: Make this base_type to make it more explicit.
463
-
464
- def initialize dimension, type
465
- @dimension, @type = dimension, type
466
- end
467
-
468
- def carray?; true; end
469
-
470
- def <=> other
471
- if self.class == other.class
472
- @type <=> other.type
473
- else
474
- @type <=> other
475
- end
476
- end
477
-
478
- def base_type
479
- @type
480
- end
481
- end
482
-
483
- class CPtr
484
- include Helpers
485
- # The data type that this pointer is pointing to.
486
- attr_reader :type
487
-
488
- def initialize type
489
- @type = type
490
- end
491
-
492
- def p_formatter
493
- "%s" if char_ptr?
494
- end
495
-
496
- def cptr?; true; end
497
-
498
- def to_s
499
- base_type = @type.base_type
500
- if base_type.c_function?
501
- ptr = ptr_level
502
-
503
- str = "#{base_type.type.to_s} (#{ptr} #{base_type.c_name.to_s})"
504
- str << "(" + base_type.arg_list.map { |e| e.type.to_s }.join(',') + ")"
505
- else
506
- t = @type
507
- str = "*"
508
- if t.cptr?
509
- str << "*"
510
- t = t.type
511
- end
512
- str.prepend t.to_s
513
- str
514
- end
515
- end
516
-
517
- def base_type
518
- return @type if !@type.is_a?(self.class)
519
- return @type.base_type
520
- end
521
-
522
- def ptr_level
523
- temp = @type
524
- ptr = "*"
525
- while temp.cptr?
526
- ptr << "*"
527
- temp = @type.type
528
- end
529
-
530
- ptr
531
- end
532
-
533
- # from a Ruby function get a pointer to some value.
534
- def from_ruby_object arg
535
- return "StringValueCStr(#{arg})" if @type.char?
536
- arg
537
- end
538
-
539
- def to_ruby_object arg
540
- return "rb_str_new2(#{arg})" if @type.char?
541
- arg
542
- end
543
-
544
- def <=> other
545
- return -1
546
- end
547
- end
548
-
549
- class Boolean < RubyObject
550
- include Helpers
551
-
552
- def bool?; true; end
553
- end
554
-
555
- class TrueType < Boolean; end
556
-
557
- class FalseType < Boolean; end
558
-
559
- class NilType < RubyObject
560
- def nil_type?; true; end
561
- end
562
-
563
- class CStructOrUnion
564
- include Helpers
565
- attr_reader :kind, :name, :c_name, :scope
566
-
567
- def initialize kind, name, c_name, scope
568
- @kind, @name, @c_name, @scope = kind, name, c_name, scope
569
- end
570
-
571
- def struct_or_union?; true; end
572
-
573
- def to_s; "#{@c_name}"; end
574
- end
575
-
576
- # FIXME: Find out a better way to generically find the old type of a typedef
577
- # when the new type is encountered. Should cover cases where the new type
578
- # is aliased with some other name too. In other words, reach the actual
579
- # type in the most generic way possible without too many checks.
580
- class TypeDef
581
- # include Helpers
582
- attr_reader :type, :old_type, :new_type
583
-
584
- def initialize old_type, new_type, type
585
- @old_type, @new_type, @type = old_type, new_type, type
586
- end
587
-
588
- def alias_type?; true; end
589
-
590
- def to_s
591
- @new_type.to_s
592
- end
593
-
594
- def base_type
595
- @old_type
596
- end
597
-
598
- def method_missing meth, *args, &block
599
- @old_type.send(meth, *args, &block)
600
- end
601
- end
602
-
603
- class CStr
604
- include Helpers
605
-
606
- def cstr?; true; end
607
-
608
- def p_formatter; "%s"; end
609
-
610
- def from_ruby_object arg
611
- "StringValueCStr(#{arg})"
612
- end
613
-
614
- def to_ruby_object arg
615
- "rb_str_new_cstr(#{arg})"
616
- end
617
- end
618
-
619
- class RubyConstant < RubyObject
620
- attr_reader :name, :type
621
-
622
- def initialize name
623
- @name = name
624
- # FIXME: make this flexible so that consts set to primitive types can be
625
- # easily converted to C types.
626
- @type = RubyObject.new
627
- end
628
-
629
- def ruby_constant?; true; end
630
- end
631
-
632
- class RubyClass < RubyConstant
633
- attr_reader :name, :c_name, :scope, :ancestor
634
-
635
- def initialize name, c_name, scope, ancestor
636
- @name, @c_name, @scope, @ancestor = name, c_name, scope, ancestor
637
- end
638
-
639
- def ruby_class?; true; end
640
- end
641
-
642
- class RubyMethod
643
- include Helpers
644
-
645
- attr_reader :name, :c_name, :type
646
- attr_accessor :scope, :arg_list
647
-
648
- def initialize name, c_name, scope, arg_list
649
- @name, @c_name, @scope, @arg_list = name, c_name, scope, arg_list
650
- @type = RubyObject.new
651
- end
652
-
653
- def ruby_method?; true; end
654
- end
655
-
656
- class CFunction
657
- include Helpers
658
- attr_reader :name, :type, :c_name
659
- attr_accessor :scope, :arg_list
660
-
661
- # FIXME: all attributes should be initialized upon class creation to maintain
662
- # sanity and consistency.
663
- def initialize name, c_name, arg_list, type, scope
664
- @name, @c_name, @arg_list, @type, @scope = name, c_name, arg_list, type, scope
665
- end
666
-
667
- def c_function?; true; end
668
- end
669
- # TODO: How to store this in a Ruby class? Use BigDecimal?
670
- # class LF64
671
- # def to_s; "long double"; end
672
-
673
- # def to_ruby_object(arg); "INT2NUM"; end
674
-
675
- # def from_ruby_object(arg); "(int32_t)NUM2INT"; end
676
- # end
677
- end
678
- end
11
+ # def from_ruby_object(arg); "(int32_t)NUM2INT"; end
12
+ # end