6 fun generator
(classes
: Array[nullable ObjcClass]) do
7 var init_with_alloc
= true
8 for classe
in classes
do
9 var file
= new FileWriter.open
(classe
.name
+ ".nit")
10 nit_class_generator
(classe
, file
, init_with_alloc
)
15 fun type_convertor
(type_word
: String): String do
16 var types
= new HashMap[String, String]
17 types
["char"] = "Byte"
18 types
["short"] = "Int"
19 types
["short int"] = "Int"
22 types
["long int"] = "Int"
23 types
["long long"] = "Int"
24 types
["long long int"] = "Int"
25 types
["float"] = "Float"
26 types
["double"] = "Float"
27 types
["long double"] = "Float"
29 types
["NSUInteger"] = "Int"
30 types
["BOOL"] = "Bool"
31 types
["id"] = "NSObject"
33 if types
.has_key
(type_word
) then
34 return types
[type_word
]
40 fun nit_class_generator
(classe
: nullable ObjcClass, file
: FileWriter, init_with_alloc
: Bool) do
41 var commented_methods
= new Array[ObjcMethod]
42 file
.write
"import cocoa::foundation\n\n"
43 file
.write
("extern class " + classe
.name
+ """ in "ObjC" `{ """ + classe.name + """ * `}\n""")
44 for super_name
in classe
.super_names
do
45 file
.write
(""" super """ + super_name
+ "\n")
47 if classe
.super_names
.is_empty
then file
.write
(""" super NSObject\n""")
48 new_nit_generator
(classe
, file
, init_with_alloc
)
50 for attribute
in classe
.attributes
do
51 nit_attribute_generator
(attribute
, file
)
53 for method
in classe
.methods
do
54 if method
.is_commented
then
55 commented_methods
.add
(method
)
57 if init_with_alloc
and method
.params
.first
.name
.has
("init") then continue
59 nit_method_generator
(method
, file
, init_with_alloc
)
60 file
.write
(""" in "ObjC" `{\n """)
61 objc_method_generator
(method
, file
)
63 if method
!= classe
.methods
.last
then file
.write
("\n\n")
66 for commented_method
in commented_methods
do
67 if commented_method
== commented_methods
.first
then file
.write
("\n")
69 nit_method_generator
(commented_method
, file
, init_with_alloc
)
70 if commented_method
!= commented_methods
.last
then file
.write
("\n")
75 fun new_nit_generator
(classe
: nullable ObjcClass, file
: FileWriter, init_with_alloc
: Bool) do
76 if init_with_alloc
then
77 for method
in classe
.methods
do
78 if method
.params
.first
.name
.has
("init") and not method
.is_commented
then
80 if method
.params
.first
.name
== "init" then
83 nit_method_generator
(method
, file
, init_with_alloc
)
85 file
.write
""" in "ObjC" `{\n"""
86 new_alloc_init_objc_generator
(classe
.name
, method
, file
)
87 file
.write
""" `}\n"""
91 file
.write
"""\n new in "ObjC"`{\n"""
92 new_objc_generator
(classe
, file
)
93 file
.write
""" `}\n"""
97 fun nit_attribute_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
98 nit_attribute_setter_generator
(attribute
, file
)
102 fun nit_attribute_setter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
103 file
.write
(""" fun """ + attribute
.name
.to_snake_case
+ ": " + type_convertor
(attribute
.return_type
))
104 file
.write
""" in "ObjC" `{\n"""
105 objc_attribute_setter_generator
(attribute
, file
)
106 file
.write
""" `}\n"""
109 fun nit_attribute_getter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
110 file
.write
(""" fun """ + attribute
.name
.to_snake_case
+ "=(value: " + type_convertor
(attribute
.return_type
) + ")")
111 file
.write
" in \"ObjC\
" `\{\n"
112 objc_attribute_getter_generator
(attribute
, file
)
113 file
.write
""" `}\n"""
116 fun nit_method_generator
(method
: ObjcMethod, file
: FileWriter, init_with_alloc
: Bool) do
118 for param
in method
.params
do
119 name
+= param
.name
[0].to_upper
.to_s
+ param
.name
.substring_from
(1)
120 name
= name
.to_snake_case
122 if name
.has
("init") and init_with_alloc
then
125 if not init_with_alloc
and name
== "init" then name
= "init_0"
129 for param
in method
.params
do
130 if param
== method
.params
.first
and not param
.is_single
then
131 file
.write
("(" + param
.variable_name
+ ": " + type_convertor
(param
.return_type
))
133 if param
!= method
.params
.first
and not param
.is_single
then
134 file
.write
(", " + param
.variable_name
+ ": " + type_convertor
(param
.return_type
))
136 if param
== method
.params
.last
and not param
.is_single
then
140 if method
.return_type
!= "void" and not method
.params
.first
.name
.has
("init") then file
.write
(": " + type_convertor
(method
.return_type
))
143 fun new_alloc_init_objc_generator
(classe_name
: String, method
: ObjcMethod, file
: FileWriter) do
144 file
.write
(""" return [[""" + classe_name
+ " alloc] ")
145 for param
in method
.params
do
146 if not param
.is_single
then
147 file
.write
(param
.name
+ ":" + param
.variable_name
)
148 if not param
== method
.params
.last
then file
.write
(" ")
150 file
.write param
.name
156 fun new_objc_generator
(classe
: nullable ObjcClass, file
: FileWriter) do
157 file
.write
(""" return [""" + classe
.name
+ " alloc];\n")
160 fun objc_attribute_setter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
161 file
.write
(""" return [self """ + attribute
.name
+ "];\n")
164 fun objc_attribute_getter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
165 file
.write
(""" self.""" + attribute
.name
+ " = value;\n")
168 fun objc_method_generator
(method
: ObjcMethod, file
: FileWriter) do
169 if method
.return_type
!= "void" then file
.write
("return ")
171 for param
in method
.params
do
172 if not param
.is_single
then
173 file
.write
(param
.name
+ ":" + param
.variable_name
)
174 if not param
== method
.params
.last
then file
.write
" "
176 file
.write
(param
.name
)