Revision df41d855

b/autotools/convert-constants
54 54
  return"%s_%s" % (dict_name, str(key_name).upper())
55 55

  
56 56

  
57
def HaskellTypeVal(value):
58
  """Returns the Haskell type and value for a Python value.
59

  
60
  Note that this only work for 'plain' Python types.
61

  
62
  @returns: (string, string) or None, if we can't determine the type.
63

  
64
  """
65
  if isinstance(value, basestring):
66
    return ("String", "\"%s\"" % StringValueRules(value))
67
  elif isinstance(value, int):
68
    return ("Int", "%d" % value)
69
  elif isinstance(value, long):
70
    return ("Integer", "%d" % value)
71
  elif isinstance(value, float):
72
    return ("Double", "%f" % value)
73
  else:
74
    return None
75

  
76

  
57 77
def ConvertVariable(name, value):
58 78
  """Converts a given variable to Haskell code.
59 79

  
......
64 84
  """
65 85
  lines = []
66 86
  hs_name = NameRules(name)
87
  hs_typeval = HaskellTypeVal(value)
67 88
  if not CONSTANT_RE.match(name):
68 89
    lines.append("-- Skipped %s, not constant" % name)
69
  elif isinstance(value, basestring):
70
    lines.append("-- | Converted from Python constant %s" % name)
71
    lines.append("%s :: String" % hs_name)
72
    lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value)))
73
  elif isinstance(value, int):
74
    lines.append("-- | Converted from Python constant %s" % name)
75
    lines.append("%s :: Int" % hs_name)
76
    lines.append("%s = %d" % (hs_name, value))
77
  elif isinstance(value, long):
78
    lines.append("-- | Converted from Python constant %s" % name)
79
    lines.append("%s :: Integer" % hs_name)
80
    lines.append("%s = %d" % (hs_name, value))
81
  elif isinstance(value, float):
90
  elif hs_typeval is not None:
91
    # this is a simple value
92
    (hs_type, hs_val) = hs_typeval
82 93
    lines.append("-- | Converted from Python constant %s" % name)
83
    lines.append("%s :: Double" % hs_name)
84
    lines.append("%s = %f" % (hs_name, value))
94
    lines.append("%s :: %s" % (hs_name, hs_type))
95
    lines.append("%s = %s" % (hs_name, hs_val))
85 96
  elif isinstance(value, dict):
86 97
    if value:
87 98
      lines.append("-- Following lines come from dictionary %s" % name)

Also available in: Unified diff