From df41d85581ec285435c3d0896b54cf4608806c57 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Thu, 22 Dec 2011 17:39:35 +0100 Subject: [PATCH] More improvements to convert-constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This prepares for tuple and other conversions. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: RenΓ© Nussbaumer <rn@google.com> --- autotools/convert-constants | 41 +++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/autotools/convert-constants b/autotools/convert-constants index 8dd1a1162..d1744eac8 100755 --- a/autotools/convert-constants +++ b/autotools/convert-constants @@ -54,6 +54,26 @@ def DictKeyName(dict_name, key_name): return"%s_%s" % (dict_name, str(key_name).upper()) +def HaskellTypeVal(value): + """Returns the Haskell type and value for a Python value. + + Note that this only work for 'plain' Python types. + + @returns: (string, string) or None, if we can't determine the type. + + """ + if isinstance(value, basestring): + return ("String", "\"%s\"" % StringValueRules(value)) + elif isinstance(value, int): + return ("Int", "%d" % value) + elif isinstance(value, long): + return ("Integer", "%d" % value) + elif isinstance(value, float): + return ("Double", "%f" % value) + else: + return None + + def ConvertVariable(name, value): """Converts a given variable to Haskell code. @@ -64,24 +84,15 @@ def ConvertVariable(name, value): """ lines = [] hs_name = NameRules(name) + hs_typeval = HaskellTypeVal(value) if not CONSTANT_RE.match(name): lines.append("-- Skipped %s, not constant" % name) - elif isinstance(value, basestring): - lines.append("-- | Converted from Python constant %s" % name) - lines.append("%s :: String" % hs_name) - lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value))) - elif isinstance(value, int): - lines.append("-- | Converted from Python constant %s" % name) - lines.append("%s :: Int" % hs_name) - lines.append("%s = %d" % (hs_name, value)) - elif isinstance(value, long): - lines.append("-- | Converted from Python constant %s" % name) - lines.append("%s :: Integer" % hs_name) - lines.append("%s = %d" % (hs_name, value)) - elif isinstance(value, float): + elif hs_typeval is not None: + # this is a simple value + (hs_type, hs_val) = hs_typeval lines.append("-- | Converted from Python constant %s" % name) - lines.append("%s :: Double" % hs_name) - lines.append("%s = %f" % (hs_name, value)) + lines.append("%s :: %s" % (hs_name, hs_type)) + lines.append("%s = %s" % (hs_name, hs_val)) elif isinstance(value, dict): if value: lines.append("-- Following lines come from dictionary %s" % name) -- GitLab