Commits

Anonymous committed d7636ea

cddec_parse can parse array like a[i,j,k] now

  • Participants
  • Parent commits 83e3b9f
  • Branches flattened_carray

Comments (0)

Files changed (2)

File railgun/cdata.py

     C-data declaration
     """
 
+    def __init__(self):
+        self.carrtype = None
+
     def as_dict(self):
         keys = ["cdt", "vname", "valtype", "idx", "ndim", "default",
-                "has_default"]
+                "has_default", "carrtype"]
         return dict((k, getattr(self, k)) for k in keys if hasattr(self, k))
 
     def _set_valtype_idx_ndim(self, idx):
         if idx:
             self.valtype = 'array'
-            self.idx = cddec_idx_parse(idx)
+            (self.idx, self.carrtype) = cddec_idx_parse(idx)
             self.ndim = len(self.idx)
         else:
             self.valtype = 'scalar'
     """Parse string like '[i][j]' to ('i', 'j')"""
     idxtr = idxtr.strip()
     if idxtr == '':
-        return ()
+        return ((), None)
     else:
-        return tuple(idxtr.strip('[]').split(']['))
+        stripped = idxtr.strip('[]')  # "i][j][k" or "i,j,k"
+        if "," in stripped:
+            return (tuple(stripped.split(",")), "flat")
+        else:
+            return (tuple(stripped.split('][')), "iliffe")
 
 
 cddec_parse = _CDataDeclaration.from_string

File tests/test_cdata.py

     ]
 
 
-def dict_cdec_parse(cdt, vname, idx=(), ndim=0, default=None, valtype=None):
+def dict_cdec_parse(cdt, vname, idx=(), ndim=0, default=None, valtype=None,
+                    carrtype="iliffe"):
     if valtype is not None:
         return dict(cdt=cdt, vname=vname, valtype=valtype,
-                    has_default=default is not None)
+                    has_default=default is not None, carrtype=None)
     elif ndim == 0:
         valtype = 'scalar'
+        carrtype = None
     elif ndim > 0:
         valtype = 'array'
     return dict(cdt=cdt, vname=vname, idx=idx, ndim=ndim, default=default,
-                has_default=default is not None, valtype=valtype)
+                has_default=default is not None, valtype=valtype,
+                carrtype=carrtype)
 
 
 class DmyCDT(object):
     ("int a = 1", dict_cdec_parse('int', 'a', default=1)),
     ("int a[i] =2", dict_cdec_parse('int', 'a', tuple('i'), 1, 2)),
     ("int a[i][j]=3", dict_cdec_parse('int', 'a', tuple('ij'), 2, 3)),
+    ("int a[i,j]", dict_cdec_parse('int', 'a', tuple('ij'), 2,
+                                   carrtype="flat")),
+    ("int a[i,j]=3", dict_cdec_parse('int', 'a', tuple('ij'), 2, 3,
+                                     carrtype="flat")),
     ('num_i = 10', dict_cdec_parse('int', 'num_i', default=10)),
     (cmem(DmyCDT, 'obj'), dict_cdec_parse(DmyCDT, 'obj', valtype='object')),
     ]
 
 
 def check_cddec_idx_parse(idxtr, dimtuple):
-    ret = cddec_idx_parse(idxtr)
+    ret = cddec_idx_parse(idxtr)[0]
     eq_(ret, dimtuple,
         msg='cddec_idx_parse(%r) = %r != %r' % (idxtr, ret, dimtuple))