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
@@ -0,0 +1,38 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ class StructOrUnionMemberCall < CommandCall
5
+ def analyse_types(local_scope)
6
+ scope = @expr.type.base_type.scope
7
+
8
+ if @command.is_a? String
9
+ @command = Expression::Name.new @command
10
+ @command.analyse_types scope
11
+ elsif @command.is_a? Rubex::AST::Expression::ElementRef
12
+ @command = Expression::ElementRefMemberCall.new @expr, @command, @arg_list
13
+ @command.analyse_types local_scope, scope
14
+ end
15
+ @has_temp = @command.has_temp
16
+ @type = @command.type
17
+ @subexprs = [@expr, @command]
18
+ end
19
+
20
+ def generate_evaluation_code code, local_scope
21
+ @expr.generate_evaluation_code code, local_scope
22
+ @command.generate_evaluation_code code, local_scope
23
+ @c_code =
24
+ if @command.has_temp
25
+ @command.c_code(local_scope)
26
+ else
27
+ op = @expr.type.cptr? ? '->' : '.'
28
+ "#{@expr.c_code(local_scope)}#{op}#{@command.c_code(local_scope)}"
29
+ end
30
+ end
31
+
32
+ def c_code(_local_scope)
33
+ @c_code
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,64 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ class ElementRef < Base
5
+ # Readers needed for accessing elements due to delegator classes.
6
+ attr_reader :entry, :pos, :name, :subexprs
7
+ extend Forwardable
8
+ def_delegators :@element_ref, :generate_disposal_code, :generate_evaluation_code,
9
+ :analyse_statement, :generate_element_ref_code,
10
+ :generate_assignment_code, :has_temp, :c_code,
11
+ :allocate_temps, :release_temps, :to_ruby_object,
12
+ :from_ruby_object
13
+
14
+ def initialize(name, pos)
15
+ @name = name
16
+ @pos = pos
17
+ @subexprs = []
18
+ end
19
+
20
+ def analyse_types(local_scope, struct_scope = nil)
21
+ @entry = struct_scope.nil? ? local_scope.find(@name) : struct_scope[@name]
22
+ @type = @entry.type.object? ? @entry.type : @entry.type.type
23
+ @element_ref = proper_analysed_type
24
+ @element_ref.analyse_types local_scope
25
+ super(local_scope)
26
+ end
27
+
28
+ private
29
+
30
+ def proper_analysed_type
31
+ if ruby_object_c_array?
32
+ CVarElementRef.new(self)
33
+ else
34
+ if ruby_array?
35
+ RubyArrayElementRef.new(self)
36
+ elsif ruby_hash?
37
+ RubyHashElementRef.new(self)
38
+ elsif generic_ruby_object?
39
+ RubyObjectElementRef.new(self)
40
+ else
41
+ CVarElementRef.new(self)
42
+ end
43
+ end
44
+ end
45
+
46
+ def ruby_array?
47
+ @type.ruby_array?
48
+ end
49
+
50
+ def ruby_hash?
51
+ @type.ruby_hash?
52
+ end
53
+
54
+ def generic_ruby_object?
55
+ @type.object?
56
+ end
57
+
58
+ def ruby_object_c_array?
59
+ @entry.type.cptr? && @entry.type.type.object?
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,13 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ # Internal node that denotes empty expression for a statement for example
5
+ # the `return` for a C function with return type `void`.
6
+ class Empty < Base
7
+ def analyse_types(_local_scope)
8
+ @type = DataType::Void.new
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ # internal node for converting from ruby object.
5
+ class FromRubyObject < CoerceObject
6
+ # expr - Expression to convert
7
+ # from_node - LHS expression. Of type Rubex::AST::Expression
8
+ def initialize(expr, from_node)
9
+ @expr = expr
10
+ @type = @expr.type
11
+ @from_node = from_node
12
+ end
13
+
14
+ def c_code(local_scope)
15
+ @from_node.type.from_ruby_object(@expr.c_code(local_scope)).to_s
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # Function argument is a function pointer.
2
+ module Rubex
3
+ module AST
4
+ module Expression
5
+ class FuncPtrArgDeclaration < ArgDeclaration
6
+ def analyse_types(local_scope, extern: false)
7
+ var, dtype, ident, ptr_level, value = fetch_data
8
+ cfunc_return_type = Helpers.determine_dtype(dtype, ident[:return_ptr_level])
9
+ arg_list = ident[:arg_list].analyse_statement(local_scope)
10
+ ptr_level = '*' if ptr_level.empty?
11
+ name = ident[:name]
12
+ c_name = Rubex::ARG_PREFIX + ident[:name]
13
+ @type = Helpers.determine_dtype(
14
+ DataType::CFunction.new(name, c_name, arg_list, cfunc_return_type, nil), ptr_level
15
+ )
16
+ add_arg_to_symbol_table name, c_name, value, extern, local_scope
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ # Function argument is the argument of a function pointer.
5
+ class FuncPtrInternalArgDeclaration < ArgDeclaration
6
+ def analyse_types(_local_scope, extern: false)
7
+ var, dtype, ident, ptr_level, value = fetch_data
8
+ @type = Helpers.determine_dtype(dtype, ptr_level)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class Base < Rubex::AST::Expression::Base
6
+ def initialize(name)
7
+ @name = name
8
+ end
9
+
10
+ def c_code(local_scope)
11
+ code = super
12
+ code << @name
13
+ end
14
+
15
+ def c_name
16
+ @name
17
+ end
18
+
19
+ def literal?
20
+ true
21
+ end
22
+
23
+ def ==(other)
24
+ self.class == other.class && @name == other.name
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class ArrayLit < Base
6
+ include Enumerable
7
+
8
+ attr_accessor :c_array
9
+
10
+ def each(&block)
11
+ @array_list.each(&block)
12
+ end
13
+
14
+ def initialize(array_list)
15
+ @array_list = array_list
16
+ @subexprs = []
17
+ end
18
+
19
+ def analyse_types(local_scope)
20
+ @has_temp = true
21
+ @type = DataType::RubyObject.new
22
+ @array_list.map! do |e|
23
+ e.analyse_types local_scope
24
+ e = e.to_ruby_object
25
+ @subexprs << e
26
+ e
27
+ end
28
+ end
29
+
30
+ def generate_evaluation_code(code, local_scope)
31
+ code << "#{@c_code} = rb_ary_new2(#{@array_list.size});"
32
+ code.nl
33
+ @array_list.each do |e|
34
+ code << "rb_ary_push(#{@c_code}, #{e.c_code(local_scope)});"
35
+ code.nl
36
+ end
37
+ end
38
+
39
+ def generate_disposal_code(code)
40
+ code << "#{@c_code} = 0;"
41
+ code.nl
42
+ end
43
+
44
+ def c_code(_local_scope)
45
+ @c_code
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class CNull < Base
6
+ def initialize(name)
7
+ # Rubex treats NULL's dtype as void*
8
+ super
9
+ @type = Rubex::DataType::CPtr.new(Rubex::DataType::Void.new)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class Char < Base
6
+ def analyse_for_target_type(target_type, local_scope)
7
+ if target_type.char?
8
+ @type = Rubex::DataType::Char.new
9
+ elsif target_type.object?
10
+ @type = Rubex::DataType::RubyString.new
11
+ analyse_types local_scope
12
+ else
13
+ raise Rubex::TypeError, "Cannot assign #{target_type} to string."
14
+ end
15
+ end
16
+
17
+ def analyse_types(_local_scope)
18
+ @type ||= Rubex::DataType::RubyString.new
19
+ end
20
+
21
+ def generate_evaluation_code(_code, _local_scope)
22
+ @c_code = if @type.char?
23
+ @name
24
+ else
25
+ "rb_str_new2(\"#{@name[1]}\")"
26
+ end
27
+ end
28
+
29
+ def c_code(_local_scope)
30
+ @c_code
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class Double < Base
6
+ def initialize(name)
7
+ super
8
+ @type = Rubex::DataType::F64.new
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class False < Base
6
+ def initialize(name)
7
+ super
8
+ end
9
+
10
+ def analyse_for_target_type(target_type, _local_scope)
11
+ @type = if target_type.object?
12
+ Rubex::DataType::FalseType.new
13
+ else
14
+ Rubex::DataType::CBoolean.new
15
+ end
16
+ end
17
+
18
+ def analyse_types(_local_scope)
19
+ @type = Rubex::DataType::FalseType.new
20
+ end
21
+
22
+ def c_code(_local_scope)
23
+ if @type.object?
24
+ @name
25
+ else
26
+ '0'
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class HashLit < Base
6
+ def initialize(key_val_pairs)
7
+ @key_val_pairs = key_val_pairs
8
+ end
9
+
10
+ def analyse_types(local_scope)
11
+ @has_temp = true
12
+ @type = Rubex::DataType::RubyObject.new
13
+ @key_val_pairs.map! do |k, v|
14
+ k.analyse_for_target_type(@type, local_scope)
15
+ v.analyse_for_target_type(@type, local_scope)
16
+ [k.to_ruby_object, v.to_ruby_object]
17
+ end
18
+ @subexprs = @key_val_pairs.to_a.flatten
19
+ end
20
+
21
+ def generate_evaluation_code(code, local_scope)
22
+ code << "#{@c_code} = rb_hash_new();"
23
+ code.nl
24
+ @key_val_pairs.each do |k, v|
25
+ k.generate_evaluation_code(code, local_scope)
26
+ v.generate_evaluation_code(code, local_scope)
27
+
28
+ code << "rb_hash_aset(#{@c_code}, #{k.c_code(local_scope)}, "
29
+ code << "#{v.c_code(local_scope)});"
30
+ code.nl
31
+
32
+ k.generate_disposal_code code
33
+ v.generate_disposal_code code
34
+ code.nl
35
+ end
36
+ end
37
+
38
+ def c_code(_local_scope)
39
+ @c_code
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class Int < Base
6
+ def initialize(name)
7
+ super
8
+ @type = Rubex::DataType::Int.new
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class Nil < Base
6
+ def initialize(name)
7
+ super
8
+ @type = Rubex::DataType::NilType.new
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module Rubex
2
+ module AST
3
+ module Expression
4
+ module Literal
5
+ class RubySymbol < Base
6
+ def initialize(name)
7
+ super(name[1..-1])
8
+ @type = Rubex::DataType::RubySymbol.new
9
+ end
10
+
11
+ def generate_evaluation_code(_code, _local_scope)
12
+ @c_code = "ID2SYM(rb_intern(\"#{@name}\"))"
13
+ end
14
+
15
+ def c_code(_local_scope)
16
+ @c_code
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end