-
Notifications
You must be signed in to change notification settings - Fork 9
/
ArgConversions.cr
318 lines (300 loc) · 18.5 KB
/
ArgConversions.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
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
45
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
module Anyolite
module Macro
macro get_raw_args(rb, regular_args, context = nil)
{% options = {:context => context} %}
%args = Anyolite::Macro.generate_arg_tuple({{rb}}, {{regular_args}}, options: {{options}})
%format_string = Anyolite::Macro.format_string({{regular_args}}, options: {{options}})
Anyolite::Macro.load_args_into_vars({{regular_args}}, %format_string, %args)
%args
end
# Converts Ruby values to Crystal values
macro convert_regular_arg(rb, arg, arg_type, options = {} of Symbol => NoReturn, debug_information = nil)
{% if arg_type.stringify.includes?("->") || arg_type.stringify.includes?(" Proc(") %}
{% puts "\e[33m> INFO: Proc types are not allowed as arguments.\e[0m" %}
raise "Proc types are not allowed as arguments ({{debug_information.id}})"
{% elsif arg_type.stringify.includes?(" Slice(") %}
{% puts "\e[33m> INFO: Slice types are not allowed as arguments (#{debug_information.id}).\e[0m" %}
raise "Slice types are not allowed as arguments ({{debug_information.id}})"
{% elsif arg_type.is_a?(Generic) %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{arg}}, dummy_arg : {{arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% elsif arg_type.is_a?(TypeDeclaration) && arg_type.type.is_a?(Union) %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{arg}}, {{arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% elsif arg_type.is_a?(TypeDeclaration) %}
Anyolite::Macro.convert_regular_arg({{rb}}, {{arg}}, {{arg_type.type}}, options: {{options}}, debug_information: {{debug_information}})
{% elsif options[:context] %}
Anyolite::Macro.resolve_regular_arg({{rb}}, {{arg}}, {{options[:context]}}::{{arg_type.stringify.starts_with?("::") ? arg_type.stringify[2..-1].id : arg_type}}, {{arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% else %}
Anyolite::Macro.resolve_regular_arg({{rb}}, {{arg}}, {{arg_type}}, {{arg_type}}, debug_information: {{debug_information}})
{% end %}
end
macro resolve_regular_arg(rb, arg, arg_type, raw_arg_type, options = {} of Symbol => NoReturn, debug_information = nil)
{% if arg_type.resolve? %}
{% if arg_type.resolve <= Nil %}
nil
{% elsif arg_type.resolve <= Bool %}
({{arg}} != 0)
{% elsif arg_type.resolve == Number %}
begin
Float64.new({{arg}})
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{{{arg}}} to {{arg_type}}.")
Float64.new(0.0)
end
{% elsif arg_type.resolve == Int %}
begin
Int64.new({{arg}})
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{{{arg}}} to {{arg_type}}.")
Int64.new(0)
end
{% elsif arg_type.resolve <= Int %}
begin
{{arg_type}}.new({{arg}})
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{{{arg}}} to {{arg_type}}.")
{{arg_type}}.new(0)
end
{% elsif arg_type.resolve == Float %}
begin
Float64.new({{arg}})
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{{{arg}}} to {{arg_type}}.")
Float64.new(0.0)
end
{% elsif arg_type.resolve <= Float %}
begin
{{arg_type}}.new({{arg}})
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{{{arg}}} to {{arg_type}}.")
{{arg_type}}.new(0.0)
end
{% elsif arg_type.resolve <= Char %}
Anyolite::RbCast.cast_to_char({{rb}}, {{arg}})
{% elsif arg_type.resolve <= String %}
{{arg_type}}.new({{arg}})
{% elsif arg_type.resolve <= Anyolite::RbRef %}
{{arg_type}}.new({{arg}})
{% elsif arg_type.resolve <= Regex %}
Anyolite::Macro.convert_regex_from_ruby_to_crystal({{rb}}, {{arg}}, {{arg_type}})
{% elsif arg_type.resolve <= Array %}
%array_size = Anyolite::RbCore.array_length({{arg}})
%converted_array = {{arg_type}}.new(size: %array_size) do |%index|
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, Anyolite::RbCore.rb_ary_entry({{arg}}, %index), {{arg_type.type_vars[0]}}, options: {{options}}, debug_information: {{debug_information}})
end
%converted_array
{% elsif arg_type.resolve <= Hash %}
%hash_size = Anyolite::RbCore.rb_hash_size({{rb}}, {{arg}})
%all_rb_hash_keys = Anyolite::RbCore.rb_hash_keys({{rb}}, {{arg}})
%all_converted_hash_keys = Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, %all_rb_hash_keys, Array({{arg_type.type_vars[0]}}), options: {{options}}, debug_information: {{debug_information}})
%converted_hash = {{arg_type}}.new(initial_capacity: %hash_size)
%all_converted_hash_keys.each_with_index do |%key, %index|
%rb_key = Anyolite::RbCore.rb_ary_entry(%all_rb_hash_keys, %index)
%rb_value = Anyolite::RbCore.rb_hash_get({{rb}}, {{arg}}, %rb_key)
%converted_hash[%key] = Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, %rb_value, {{arg_type.type_vars[1]}}, options: {{options}}, debug_information: {{debug_information}})
end
%converted_hash
{% elsif arg_type.resolve <= Pointer %}
{{arg_type}}.new(address: UInt64.new({{arg.address}}))
{% elsif arg_type.resolve <= Struct || arg_type.resolve <= Enum %}
Anyolite::Macro.convert_from_ruby_struct({{rb}}, {{arg}}, {{arg_type}}).value.content
{% else %}
Anyolite::Macro.convert_from_ruby_object({{rb}}, {{arg}}, {{arg_type}}).value
{% end %}
{% elsif options[:context] %}
{% if options[:context].names[0..-2].size > 0 %}
{% new_context = options[:context].names[0..-2].join("::").gsub(/(\:\:)+/, "::").id %}
{% options[:context] = new_context %}
Anyolite::Macro.resolve_regular_arg({{rb}}, {{arg}}, {{new_context}}::{{raw_arg_type.stringify.starts_with?("::") ? raw_arg_type.stringify[2..-1].id : raw_arg_type}}, {{raw_arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% else %}
Anyolite::Macro.resolve_regular_arg({{rb}}, {{arg}}, {{raw_arg_type}}, {{raw_arg_type}}, debug_information: {{debug_information}})
{% end %}
{% else %}
{% raise "Could not resolve #{arg_type}, which is a #{arg_type.class_name}, in any meaningful way (#{debug_information.id})" %}
{% end %}
end
macro convert_from_ruby_to_crystal(rb, arg, arg_type, options = {} of Symbol => NoReturn, debug_information = nil)
{% if arg_type.stringify.includes?("->") || arg_type.stringify.includes?(" Proc(") %}
{% puts "\e[33m> INFO: Proc types are not allowed as arguments (#{debug_information.id}).\e[0m" %}
raise "Proc types are not allowed as arguments ({{debug_information.id}})"
{% elsif arg_type.stringify.includes?(" Slice(") %}
{% puts "\e[33m> INFO: Slice types are not allowed as arguments (#{debug_information.id}).\e[0m" %}
raise "Slice types are not allowed as arguments ({{debug_information.id}})"
{% elsif options[:type_var_names] && options[:type_var_names].includes?(arg_type.type) %}
{% options[:type_var_names].each_with_index { |element, index| result = index if element == arg_type.type } %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{arg}}, {{options[:type_vars][result]}}, options: {{{:context => options[:context]}}}, debug_information: {{debug_information}})
{% elsif options[:type_var_names] %}
{% magical_regex = /([\(\s\:])([A-Z]+)([\),\s])/ %}
{% replacement_arg_type = arg_type.type.stringify.gsub(magical_regex, "\\1\#\\2\#\\3") %}
{% for type_var_name, index in options[:type_var_names] %}
{% split_types = replacement_arg_type.split("\#") %}
{% odd = true %}
{% final_split_types = [] of ASTNode %}
{% for split_type, split_type_index in split_types %}
{% if !odd %}
{% result = nil %}
{% options[:type_var_names].each_with_index { |element, index| result = index if element.stringify == split_type } %}
{% if result %}
{% final_split_types.push(options[:type_vars][result].stringify) %}
{% else %}
{% final_split_types.push(split_type) %}
{% end %}
{% else %}
{% final_split_types.push(split_type) %}
{% end %}
{% odd = !odd %}
{% end %}
{% end %}
{% if arg_type.value %}
{% final_type_def = "#{arg_type.var} : #{final_split_types.join("").id} = #{arg_type.value}" %}
{% else %}
{% final_type_def = "#{arg_type.var} : #{final_split_types.join("").id}" %}
{% end %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{arg}}, {{final_type_def.id}}, options: {{{:context => options[:context]}}}, debug_information: {{debug_information}})
{% elsif arg_type.is_a?(Call) %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{arg}}, dummy_arg : {{arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% elsif arg_type.is_a?(TypeDeclaration) %}
if Anyolite::RbCast.is_undef?({{arg}})
{% if arg_type.value || arg_type.value == false || arg_type.value == nil %}
{{arg_type.value}}
{% else %}
# Should only happen if no default value was given
Anyolite.raise_argument_error("Undefined argument #{{{arg}}} of {{arg_type}} in context {{options[:context]}} (#{{{debug_information.stringify}}})")
# Code should jump to somewhere else before this point, but we want to have a NoReturn type here
raise("Should not be reached")
{% end %}
else
{% if arg_type.type.is_a?(Union) %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{arg}}, Union({{arg_type.type}}), options: {{options}}, debug_information: {{debug_information}})
{% else %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{arg}}, {{arg_type.type}}, options: {{options}}, debug_information: {{debug_information}})
{% end %}
end
# TODO: Check if this might need some improvement
{% elsif options[:context] && !arg_type.stringify.starts_with?("Union") %}
Anyolite::Macro.resolve_from_ruby_to_crystal({{rb}}, {{arg}}, {{options[:context]}}::{{arg_type.stringify.starts_with?("::") ? arg_type.stringify[2..-1].id : arg_type}}, {{arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% else %}
Anyolite::Macro.resolve_from_ruby_to_crystal({{rb}}, {{arg}}, {{arg_type}}, {{arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% end %}
end
macro resolve_from_ruby_to_crystal(rb, arg, arg_type, raw_arg_type, options = {} of Symbol => NoReturn, debug_information = nil)
{% if arg_type.stringify.starts_with?("Union") || arg_type.stringify.starts_with?("::Union") %}
# This sadly needs some uncanny magic
{% char_parser = "" %}
{% brace_counter = 0 %}
{% for c in arg_type.stringify[(arg_type.stringify.starts_with?("::") ? 8 : 6)..-2].chars %}
{% brace_counter += 1 if c == '(' %}
{% brace_counter -= 1 if c == ')' %}
{% char_parser += (brace_counter == 0 && c == '|' ? ',' : c) %}
{% end %}
Anyolite::Macro.cast_to_union_value({{rb}}, {{arg}}, {{"[#{char_parser.id}]".id}}, options: {{options}}, debug_information: {{debug_information}})
{% elsif arg_type.resolve? %}
{% if arg_type.resolve <= Nil %}
Anyolite::RbCast.cast_to_nil({{rb}}, {{arg}})
{% elsif arg_type.resolve <= Bool %}
Anyolite::RbCast.cast_to_bool({{rb}}, {{arg}})
{% elsif arg_type.resolve == Number %}
begin
Float64.new(Anyolite::RbCast.cast_to_float({{rb}}, {{arg}}))
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{Anyolite::RbCast.cast_to_float({{rb}}, {{arg}})} to {{arg_type}}.")
Float64.new(0.0)
end
{% elsif arg_type.resolve == Int %}
begin
Int64.new(Anyolite::RbCast.cast_to_int({{rb}}, {{arg}}))
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{Anyolite::RbCast.cast_to_int({{rb}}, {{arg}})} to {{arg_type}}.")
Int64.new(0)
end
{% elsif arg_type.resolve <= Int %}
begin
{{arg_type}}.new(Anyolite::RbCast.cast_to_int({{rb}}, {{arg}}))
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{Anyolite::RbCast.cast_to_int({{rb}}, {{arg}})} to {{arg_type}}.")
{{arg_type}}.new(0)
end
{% elsif arg_type.resolve == Float %}
begin
Float64.new(Anyolite::RbCast.cast_to_float({{rb}}, {{arg}}))
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{Anyolite::RbCast.cast_to_float({{rb}}, {{arg}})} to {{arg_type}}.")
Float64.new(0.0)
end
{% elsif arg_type.resolve <= Float %}
begin
{{arg_type}}.new(Anyolite::RbCast.cast_to_float({{rb}}, {{arg}}))
rescue OverflowError
Anyolite.raise_range_error("Overflow while casting #{Anyolite::RbCast.cast_to_float({{rb}}, {{arg}})} to {{arg_type}}.")
{{arg_type}}.new(0.0)
end
{% elsif arg_type.resolve <= Char %}
Anyolite::RbCast.cast_to_char({{rb}}, {{arg}})
{% elsif arg_type.resolve <= String %}
Anyolite::RbCast.cast_to_string({{rb}}, {{arg}})
{% elsif arg_type.resolve <= Regex %}
Anyolite::Macro.convert_regex_from_ruby_to_crystal({{rb}}, {{arg}}, {{arg_type}})
{% elsif arg_type.resolve <= Anyolite::RbRef %}
{{arg_type}}.new({{arg}})
{% elsif arg_type.resolve <= Array %}
# TODO: Make a macro out of this
%array_size = Anyolite::RbCore.array_length({{arg}})
%converted_array = {{arg_type}}.new(size: %array_size) do |%index|
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, Anyolite::RbCore.rb_ary_entry({{arg}}, %index), {{arg_type.type_vars[0]}}, options: {{options}}, debug_information: {{debug_information}})
end
%converted_array
{% elsif arg_type.resolve <= Hash %}
%hash_size = Anyolite::RbCore.rb_hash_size({{rb}}, {{arg}})
%all_rb_hash_keys = Anyolite::RbCore.rb_hash_keys({{rb}}, {{arg}})
%all_converted_hash_keys = Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, %all_rb_hash_keys, Array({{arg_type.type_vars[0]}}), options: {{options}}, debug_information: {{debug_information}})
%converted_hash = {{arg_type}}.new(initial_capacity: %hash_size)
%all_converted_hash_keys.each_with_index do |%key, %index|
%rb_key = Anyolite::RbCore.rb_ary_entry(%all_rb_hash_keys, %index)
%rb_value = Anyolite::RbCore.rb_hash_get({{rb}}, {{arg}}, %rb_key)
%converted_hash[%key] = Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, %rb_value, {{arg_type.type_vars[1]}}, options: {{options}}, debug_information: {{debug_information}})
end
%converted_hash
{% elsif arg_type.resolve <= Pointer %}
%helper_ptr = Anyolite::Macro.convert_from_ruby_object({{rb}}, {{arg}}, Anyolite::HelperClasses::AnyolitePointer).value
Box({{arg_type}}).unbox(%helper_ptr.retrieve_ptr)
{% elsif arg_type.resolve <= Struct || arg_type.resolve <= Enum %}
Anyolite::Macro.convert_from_ruby_struct({{rb}}, {{arg}}, {{arg_type}}).value.content
{% elsif arg_type.resolve? %}
Anyolite::Macro.convert_from_ruby_object({{rb}}, {{arg}}, {{arg_type}}).value
{% else %}
{% raise "Could not resolve type #{arg_type}, which is a #{arg_type.class_name.id} (#{debug_information.id})" %}
{% end %}
{% elsif options[:context] %}
{% if options[:context].names[0..-2].size > 0 %}
{% new_context = options[:context].names[0..-2].join("::").gsub(/(\:\:)+/, "::").id %}
{% options[:context] = new_context %}
Anyolite::Macro.resolve_from_ruby_to_crystal({{rb}}, {{arg}}, {{new_context}}::{{raw_arg_type.stringify.starts_with?("::") ? raw_arg_type.stringify[2..-1].id : raw_arg_type}}, {{raw_arg_type}}, options: {{options}}, debug_information: {{debug_information}})
{% else %}
Anyolite::Macro.resolve_from_ruby_to_crystal({{rb}}, {{arg}}, {{raw_arg_type}}, {{raw_arg_type}}, debug_information: {{debug_information}})
{% end %}
{% else %}
{% raise "Could not resolve type #{arg_type}, which is a #{arg_type.class_name.id} (#{debug_information.id})" %}
{% end %}
end
macro convert_regular_args(rb, args, regular_args, options = {} of Symbol => NoReturn, debug_information = nil)
Tuple.new(
{% c = 0 %}
{% if regular_args %}
{% for arg in regular_args %}
{% if ANYOLITE_INTERNAL_FLAG_USE_GENERAL_OBJECT_FORMAT_CHARS %}
Anyolite::Macro.convert_from_ruby_to_crystal({{rb}}, {{args}}[{{c}}].value, {{arg}}, options: {{options}}, debug_information: {{debug_information}}),
{% else %}
Anyolite::Macro.convert_regular_arg({{rb}}, {{args}}[{{c}}].value, {{arg}}, options: {{options}}, debug_information: {{debug_information}}),
{% end %}
{% c += 1 %}
{% end %}
{% end %}
)
end
macro get_converted_args(rb, regular_args, options = {} of Symbol => NoReturn)
%args = Anyolite::Macro.generate_arg_tuple({{rb}}, {{regular_args}}, options: {{options}})
%format_string = Anyolite::Macro.format_string({{regular_args}}, options: {{options}})
Anyolite::Macro.load_args_into_vars({{regular_args}}, %format_string, %args)
Anyolite::Macro.convert_regular_args({{rb}}, %args, {{regular_args}}, options: {{options}})
end
end
end