From 76e4d8172d047ec164394a96520909fbff7ed0ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Wed, 26 Aug 2015 08:45:11 -0400 Subject: [PATCH] contrib/objcwrapper: replace `type_convector` by a singleton and `to_nit_type` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/objcwrapper/src/objc_generator.nit | 71 ++++++++++++++++------------ 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/contrib/objcwrapper/src/objc_generator.nit b/contrib/objcwrapper/src/objc_generator.nit index 25bd84f..380f61e 100644 --- a/contrib/objcwrapper/src/objc_generator.nit +++ b/contrib/objcwrapper/src/objc_generator.nit @@ -20,8 +20,29 @@ import opts import objc_model redef class Sys + # Path to the output file var opt_output = new OptionString("Output file", "-o") + + private var nit_to_java_types: Map[String, String] is lazy do + var types = new HashMap[String, String] + types["char"] = "Byte" + types["short"] = "Int" + types["short int"] = "Int" + types["int"] = "Int" + types["long"] = "Int" + types["long int"] = "Int" + types["long long"] = "Int" + types["long long int"] = "Int" + types["float"] = "Float" + types["double"] = "Float" + types["long double"] = "Float" + + types["NSUInteger"] = "Int" + types["BOOL"] = "Bool" + types["id"] = "NSObject" + return types + end end class CodeGenerator @@ -65,32 +86,6 @@ class CodeGenerator if path != null then file.close end - private fun type_convertor(type_word: String): String - do - var types = new HashMap[String, String] - types["char"] = "Byte" - types["short"] = "Int" - types["short int"] = "Int" - types["int"] = "Int" - types["long"] = "Int" - types["long int"] = "Int" - types["long long"] = "Int" - types["long long int"] = "Int" - types["float"] = "Float" - types["double"] = "Float" - types["long double"] = "Float" - - types["NSUInteger"] = "Int" - types["BOOL"] = "Bool" - types["id"] = "NSObject" - - if types.has_key(type_word) then - return types[type_word] - else - return type_word - end - end - private fun write_class(classe: ObjcClass, file: Writer) do var commented_methods = new Array[ObjcMethod] @@ -158,7 +153,7 @@ class CodeGenerator private fun write_attribute_getter(attribute: ObjcAttribute, file: Writer) do - file.write """ fun """ + attribute.name.to_snake_case + ": " + type_convertor(attribute.return_type) + file.write """ fun """ + attribute.name.to_snake_case + ": " + attribute.return_type.to_nit_type file.write """ in "ObjC" `{\n""" file.write """ return [self """ + attribute.name + "];\n" file.write """ `}\n""" @@ -166,7 +161,7 @@ class CodeGenerator private fun write_attribute_setter(attribute: ObjcAttribute, file: Writer) do - file.write """ fun """ + attribute.name.to_snake_case + "=(value: " + type_convertor(attribute.return_type) + ")" + file.write """ fun """ + attribute.name.to_snake_case + "=(value: " + attribute.return_type.to_nit_type + ")" file.write " in \"ObjC\" `\{\n" file.write """ self.""" + attribute.name + " = value;\n" file.write """ `}\n""" @@ -188,17 +183,17 @@ class CodeGenerator file.write name for param in method.params do if param == method.params.first and not param.is_single then - file.write "(" + param.variable_name + ": " + type_convertor(param.return_type) + file.write "(" + param.variable_name + ": " + param.return_type.to_nit_type end if param != method.params.first and not param.is_single then - file.write ", " + param.variable_name + ": " + type_convertor(param.return_type) + file.write ", " + param.variable_name + ": " + param.return_type.to_nit_type end if param == method.params.last and not param.is_single then file.write ")" end end if method.return_type != "void" and not method.params.first.name.has("init") then - file.write ": " + type_convertor(method.return_type) + file.write ": " + method.return_type.to_nit_type end end @@ -231,3 +226,17 @@ class CodeGenerator file.write "];\n" end end + +redef class Text + # Nit equivalent to this type + private fun to_nit_type: String + do + var types = sys.nit_to_java_types + + if types.has_key(self) then + return types[self] + else + return to_s + end + end +end -- 1.7.9.5