Permalink
Browse files

Put all functionalities to generate cython code into cycodegen.

  • Loading branch information...
1 parent fd3050c commit a5ec2f7bf0891f149270d965d94ce9e391e7abd6 @cournape committed Dec 1, 2008
Showing with 66 additions and 69 deletions.
  1. +66 −1 cycodegen.py
  2. +0 −68 cython_gen.py
View
@@ -3,8 +3,9 @@
from ctypeslib.codegen import typedesc
from tp_puller import TypePuller
-from cython_gen import cy_generate
from misc import classify, query_items
+from cytypes import generic_decl, generic_named_decl
+from funcs import generic_as_arg, named_pointer_decl
def generate_cython(output, genitems, enumvals):
# Generate the cython code
@@ -22,3 +23,67 @@ def generate_cython(output, genitems, enumvals):
output.write("\t%s\n" % j)
else:
output.write("\t%s\n" % i[0])
+
+def cy_generate_typedef(item):
+ if not isinstance(item.typ, typedesc.PointerType):
+ return ["ctypedef %s %s" % (item.typ.name, item.name)]
+ else:
+ return ["ctypedef %s" % (named_pointer_decl(item.typ) % item.name)]
+
+def cy_generate_structure(tp, union=False):
+ if union:
+ output = ['cdef union %s:' % tp.name]
+ else:
+ output = ['cdef struct %s:' % tp.name]
+ for m in tp.members:
+ if isinstance(m, typedesc.Field):
+ output.append("\t" + (generic_named_decl(m.typ) % m.name))
+ elif isinstance(m, typedesc.Structure):
+ output.append("\t%s" % generic_decl(m))
+ else:
+ print "Struct member not handled:", m
+ if not tp.members:
+ output.append("\tpass")
+
+ return output
+
+def cy_generate_enumeration(tp):
+ output = ['cdef enum %s:' % tp.name]
+ for v in tp.values:
+ output.append("\t%s = %s" % (v.name, v.value))
+
+ return output
+
+def cy_generate_function(func):
+ args = [generic_as_arg(a) for a in func.iterArgTypes()]
+ return ["%s %s(%s)" % (generic_as_arg(func.returns),
+ func.name, ", ".join(args))]
+
+def cy_generate_enum_value(tp):
+ output = ['cdef enum:']
+ output.append("\t%s = %d" % (tp.name, int(tp.value)))
+ return output
+
+def cy_generate(item):
+ if isinstance(item, typedesc.Typedef):
+ #print "Typedef Generating", item, item.name
+ return cy_generate_typedef(item)
+ elif isinstance(item, typedesc.Structure):
+ #print "Struct Generating", item, item.name
+ return cy_generate_structure(item)
+ elif isinstance(item, typedesc.Union):
+ #print "Union Generating", item, item.name
+ return cy_generate_structure(item, union=True)
+ elif isinstance(item, typedesc.Function):
+ #print "FunctionType Generating", item, item.name
+ return cy_generate_function(item)
+ elif isinstance(item, typedesc.EnumValue):
+ #print "FunctionType Generating", item
+ return cy_generate_enum_value(item)
+ elif isinstance(item, typedesc.Enumeration):
+ #print "FunctionType Generating", item
+ return cy_generate_enumeration(item)
+ else:
+ print "Item not handled for cy_generate", item
+ # raise ValueError, ("item not handled:", item)
+
View
@@ -1,68 +0,0 @@
-from ctypeslib.codegen import typedesc
-
-from cytypes import generic_decl, generic_named_decl
-from funcs import generic_as_arg, named_pointer_decl
-
-def cy_generate_typedef(item):
- if not isinstance(item.typ, typedesc.PointerType):
- return ["ctypedef %s %s" % (item.typ.name, item.name)]
- else:
- return ["ctypedef %s" % (named_pointer_decl(item.typ) % item.name)]
-
-def cy_generate_structure(tp, union=False):
- if union:
- output = ['cdef union %s:' % tp.name]
- else:
- output = ['cdef struct %s:' % tp.name]
- for m in tp.members:
- if isinstance(m, typedesc.Field):
- output.append("\t" + (generic_named_decl(m.typ) % m.name))
- elif isinstance(m, typedesc.Structure):
- output.append("\t%s" % generic_decl(m))
- else:
- print "Struct member not handled:", m
- if not tp.members:
- output.append("\tpass")
-
- return output
-
-def cy_generate_enumeration(tp):
- output = ['cdef enum %s:' % tp.name]
- for v in tp.values:
- output.append("\t%s = %s" % (v.name, v.value))
-
- return output
-
-def cy_generate_function(func):
- args = [generic_as_arg(a) for a in func.iterArgTypes()]
- return ["%s %s(%s)" % (generic_as_arg(func.returns),
- func.name, ", ".join(args))]
-
-def cy_generate_enum_value(tp):
- output = ['cdef enum:']
- output.append("\t%s = %d" % (tp.name, int(tp.value)))
- return output
-
-def cy_generate(item):
- if isinstance(item, typedesc.Typedef):
- #print "Typedef Generating", item, item.name
- return cy_generate_typedef(item)
- elif isinstance(item, typedesc.Structure):
- #print "Struct Generating", item, item.name
- return cy_generate_structure(item)
- elif isinstance(item, typedesc.Union):
- #print "Union Generating", item, item.name
- return cy_generate_structure(item, union=True)
- elif isinstance(item, typedesc.Function):
- #print "FunctionType Generating", item, item.name
- return cy_generate_function(item)
- elif isinstance(item, typedesc.EnumValue):
- #print "FunctionType Generating", item
- return cy_generate_enum_value(item)
- elif isinstance(item, typedesc.Enumeration):
- #print "FunctionType Generating", item
- return cy_generate_enumeration(item)
- else:
- print "Item not handled for cy_generate", item
- # raise ValueError, ("item not handled:", item)
-

0 comments on commit a5ec2f7

Please sign in to comment.