Class: Cry::Codegen::Crystallizer
- Inherits:
-
Object
- Object
- Cry::Codegen::Crystallizer
- Defined in:
- lib/cry/codegen/crystallizer.rb
Instance Attribute Summary collapse
-
#crystal_arg_types ⇒ Object
Returns the value of attribute crystal_arg_types.
-
#crystal_ret_type ⇒ Object
Returns the value of attribute crystal_ret_type.
-
#ruby_method ⇒ Object
Returns the value of attribute ruby_method.
Instance Method Summary collapse
- #crystallize ⇒ Object
- #function_declaration(ruby_method, crystal_arg_types, crystal_ret_type) ⇒ Object
- #function_definition(ruby_method) ⇒ Object
-
#initialize(ruby_method, interface) ⇒ Crystallizer
constructor
A new instance of Crystallizer.
- #source ⇒ Object
Constructor Details
#initialize(ruby_method, interface) ⇒ Crystallizer
Returns a new instance of Crystallizer.
8 9 10 11 12 |
# File 'lib/cry/codegen/crystallizer.rb', line 8 def initialize(ruby_method, interface) @ruby_method = ruby_method @crystal_arg_types = interface.crystal_arg_types @crystal_ret_type = interface.crystal_ret_type end |
Instance Attribute Details
#crystal_arg_types ⇒ Object
Returns the value of attribute crystal_arg_types.
6 7 8 |
# File 'lib/cry/codegen/crystallizer.rb', line 6 def crystal_arg_types @crystal_arg_types end |
#crystal_ret_type ⇒ Object
Returns the value of attribute crystal_ret_type.
6 7 8 |
# File 'lib/cry/codegen/crystallizer.rb', line 6 def crystal_ret_type @crystal_ret_type end |
#ruby_method ⇒ Object
Returns the value of attribute ruby_method.
6 7 8 |
# File 'lib/cry/codegen/crystallizer.rb', line 6 def ruby_method @ruby_method end |
Instance Method Details
#crystallize ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cry/codegen/crystallizer.rb', line 14 def crystallize funcs = [] declaration, initialization = function_declaration(ruby_method, crystal_arg_types, crystal_ret_type) if crystal_ret_type.is_array? initialization << "\n__return_array_=(" definition = function_definition(ruby_method) definition = definition.delete_suffix("end\n") definition << <<~CODE ) l = __return_array_.size.to_i32 __return_len_[0] = l __return_array_.to_unsafe end CODE elsif crystal_ret_type == 'String' initialization << "\n__return_str_=(" definition = function_definition(ruby_method) definition = definition.delete_suffix("end\n") definition << <<~CODE ) l = __return_str_.bytesize __return_len_[0] = l __return_str_.bytes.to_unsafe end CODE else definition = function_definition(ruby_method) end funcs << CrystalFunction.new(declaration, initialization, definition) funcs end |
#function_declaration(ruby_method, crystal_arg_types, crystal_ret_type) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cry/codegen/crystallizer.rb', line 46 def function_declaration(ruby_method, crystal_arg_types, crystal_ret_type) if ruby_method.arg_names.size != crystal_arg_types.size raise "The number of arguments of #{ruby_method.name} is different: #{ruby_method.arg_names.size} != #{crystal_arg_types.size}" end init = [] d = CrystalFunction::Declaration.new d.name = ruby_method.name ruby_method.arg_names.zip(crystal_arg_types).each do |n, t| case t when /Array\((.*)\)/ init << " #{n} = #{t}.new(__#{n}_len_){|i| __#{n}_ptr_[i]}" # better copy ? d.arg_names << "__#{n}_ptr_" d.arg_types << "#{::Regexp.last_match(1)}*" d.arg_names << "__#{n}_len_" d.arg_types << 'Int32' when 'String' init << " #{n} = String.new(__#{n}_ptr_, __#{n}_len_)" d.arg_names << "__#{n}_ptr_" d.arg_types << 'UInt8*' d.arg_names << "__#{n}_len_" d.arg_types << 'Int32' else d.arg_names << n d.arg_types << t end end if crystal_ret_type.is_array? d.arg_names << '__return_len_' d.arg_types << 'Int32*' d.ret_type = crystal_ret_type.inner_pointer elsif crystal_ret_type == 'String' d.arg_names << '__return_len_' d.arg_types << 'Int32*' d.ret_type = 'UInt8*' else d.ret_type = crystal_ret_type end initialization = init.join("\n") [d.source, initialization] end |
#function_definition(ruby_method) ⇒ Object
90 91 92 93 |
# File 'lib/cry/codegen/crystallizer.rb', line 90 def function_definition(ruby_method) # FIXME ruby_method.source.lines[1..].join end |
#source ⇒ Object
95 96 97 |
# File 'lib/cry/codegen/crystallizer.rb', line 95 def source crystallize.map(&:source).join("\n\n") end |