Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r53:313a8c6c6a9d
Date: 2014-11-17 22:23 +0100
http://bitbucket.org/cffi/creflect/changeset/313a8c6c6a9d/

Log:    Kill stuff, and start to pass some tests.

diff --git a/creflect/codegen.py b/creflect/codegen.py
--- a/creflect/codegen.py
+++ b/creflect/codegen.py
@@ -1,21 +1,10 @@
 
 class CodeBlock(object):
 
-    def __init__(self, tr):
-        self.tr = tr
-        self.blocknum = tr.next_block_num()
-        self.skip_label_active = False
-        self.includes = []
+    def __init__(self, parent):
         self.decllines = []
         self.codelines = []
-        self.sprintfleft = ""
-        self.sprintfright = ""
-        self.sprintfright_extra = ("", 0)
-        self.sprintfmax = []
-
-    def add_include(self, includename):
-        if includename not in self.includes:
-            self.includes.append(includename)
+        self.crx_type_vars = [] if parent is None else parent.crx_type_vars
 
     def writedecl(self, line):
         self.decllines.append(line)
@@ -23,137 +12,31 @@
     def writeline(self, line):
         self.codelines.append(line)
 
-    def sprintf_add_left(self, msg):
-        assert '&' not in msg
-        self.sprintfleft = self.sprintfleft + msg
-
-    def sprintf_add_right(self, msg, extra="", extralength=None):
-        assert '&' not in msg
-        self.sprintfright = msg + self.sprintfright
-        if extra:
-            assert extralength is not None, "specify 'extralength' explicitly"
-            if self.sprintfright_extra[0]:
-                extra += ', '
-            self.sprintfright_extra = (
-                extra + self.sprintfright_extra[0],
-                extralength + self.sprintfright_extra[1])
-
-    def sprintf_add_both_sides(self, marker):
-        i = marker.index('&')
-        self.sprintf_add_left(marker[:i])
-        self.sprintf_add_right(marker[i+1:])
-
-    def sprintf(self, buf, extra="", extralength=None, indent=0):
-        if extralength is None:
-            assert not extra, "specify 'extralength' explicitly"
-            extralength = 0
-        if extra:
-            extra = ', ' + extra
-        self.writeline('%sr += sprintf(r, "%s"%s);' %
-                       (' ' * indent, buf, extra))
-        self.sprintfmax.append(len(buf) + extralength)
-
-    def flushleft(self):
-        buf = self.sprintfleft
-        if buf:
-            self.sprintfleft = ""
-            self.sprintf(buf)
-        return len(self.sprintfmax)
-
-    def end_disjoint_cases(self, startindex):
-        if startindex == len(self.sprintfmax):
-            return
-        assert startindex < len(self.sprintfmax)
-        m = max(self.sprintfmax[startindex:])
-        self.sprintfmax[startindex:] = [m]
-
-    def flush(self):
-        buf = self.sprintfleft + self.sprintfright
-        if buf:
-            self.sprintfleft = ""
-            self.sprintfright = ""
-            extra, extralength = self.sprintfright_extra
-            self.sprintfright_extra = ("", 0)
-            self.sprintf(buf, extra=extra, extralength=extralength)
-
-    def get_skip_label(self):
-        if not self.skip_label_active:
-            self.skip_label_active = True
-            self.writedecl('char *r0 = r;')
-        return 'f%d' % (self.blocknum,)
-
-    def close(self):
-        self.flush()
-        if self.skip_label_active:
-            self.writeline('%s:;' % (self.get_skip_label(),))
-            self.skip_label_active = False
-
     def write_subblock(self, subblock):
-        subblock.close()
-        for incl in subblock.includes:
-            self.add_include(incl)
         self.writeline("{")
         for line in subblock.decllines + subblock.codelines:
             self.writeline("    " + line)
         self.writeline("}")
-        self.sprintfmax.extend(subblock.sprintfmax)
 
+    def add_crx_type_var(self):
+        name = 't%d' % (len(self.crx_type_vars) + 1)
+        self.crx_type_vars.append('*%s' % name)
+        return name
 
-class TransformCDef(object):
 
-    def __init__(self, csource):
-        self.csource = csource
-        self.skip_label_counter = 0
-        self.skip_label_active = False
-        self.block_counter = 0
-        self.return_value = False
+def transform_cdef(csource, creflect_func_name):
+    outerblock = CodeBlock(parent=None)
+    outerblock.writeline('void %s(crx_builder_t *cb)' % (creflect_func_name,))
 
-    def next_block_num(self):
-        r = self.block_counter
-        self.block_counter = r + 1
-        return r
+    funcblock = CodeBlock(outerblock)
 
-    def need_return_value(self):
-        if not self.return_value:
-            self.return_value = True
-            self.funcblock.writedecl("int r1 = 0;")
+    for decl in csource.get_declarations():
+        decl.write_declaration(funcblock)
 
-    def generate(self, funcname):
-        outerblock = CodeBlock(self)
-        outerblock.add_include('stdio.h')
-        outerblock.writeline('int %s(char *r)' % (funcname,))
+    if funcblock.crx_type_vars:
+        funcblock.writedecl("crx_type_t %s;" % (
+            ', '.join(funcblock.crx_type_vars),))
 
-        funcblock = CodeBlock(self)
-        self.funcblock = funcblock
-
-        for decl in self.csource.get_declarations():
-            decl.write_declaration(funcblock)
-
-        funcblock.writedecl('if (!r)')
-        if funcblock.sprintfmax:
-            funcblock.writedecl('    return %s;' %
-                                (' + '.join(map(str, funcblock.sprintfmax)),))
-        else:
-            funcblock.writedecl('    return 1;')
-            funcblock.writedecl('*r = 0;')
-        if self.return_value:
-            funcblock.writeline('return r1;')
-        else:
-            funcblock.writeline('return 0;')
-
-        outerblock.write_subblock(funcblock)
-        outerblock.writeline('')
-        #
-        if outerblock.includes:
-            extra = ['#include <%s>' % includename
-                     for includename in outerblock.includes]
-            extra.append('')
-        else:
-            extra = []
-        return extra + outerblock.codelines
-
-
-def transform_cdef(csource, creflect_func_num=1):
-    transform = TransformCDef(csource)
-    outputlines = transform.generate(creflect_func_num)
-    return '\n'.join(outputlines)
+    outerblock.write_subblock(funcblock)
+    outerblock.writeline('')     # for the final \n below
+    return '\n'.join(outerblock.codelines)
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -15,9 +15,7 @@
                 for name in (self._attrs_ + 'const')]
 
     def inspect_type(self, block, inspect):
-        if inspect.started:
-            inspect.assign_to_p1('0')
-        block.sprintf_add_both_sides(self.c_name_with_marker)
+        raise NotImplementedError
 
     def __eq__(self, other):
         return (self.__class__ == other.__class__ and
@@ -36,8 +34,13 @@
     def __init__(self, const):
         self.const = const
         self.c_name_with_marker = 'void &'
-        if const:
-            self.c_name_with_marker = 'const ' + self.c_name_with_marker
+
+    def inspect_type(self, block, inspect):
+        if inspect.started:
+            inspect.assign_to_p1('0')
+        t1 = block.add_crx_type_var()
+        block.writeline('%s = cb->get_void_type(cb);' % (t1,))
+        return t1
 
 void_type = VoidType(const=False)
 const_void_type = VoidType(const=True)
@@ -89,51 +92,20 @@
         block.writeline("(void)(%s << 1);%s" % (star_p1, comment2))
         if self.const:
             block.sprintf_add_left("const ")
-        disjunction = block.flushleft()
-        if self.const:
-            block.add_include("string.h")
             block.writeline("memset(b, -1, sizeof(b));")
         else:
             block.writeline("%s = -1;%s" % (star_p1, comment3))
-        block.writeline("if (%s > 0) {" % (star_p1,))
-        block.writeline("    if (sizeof(%s) == 1 && %s == 1)" %
-                        (star_p1, star_p1))
-        block.sprintf("_Bool", indent=8)
-        #
-        checktypes = ["char", "short", "int", "long", "long long"]
-        if self.name in checktypes:
-            i = checktypes.index(self.name)
-        elif self.name.startswith("unsigned ") and self.name[9:] in checktypes:
-            i = checktypes.index(self.name[9:])
+        t1 = block.add_crx_type_var()
+        if self.is_integer_type():
+            block.writeline('%s = CRX_INT_TYPE(cb, %s, "%s");' % (
+                t1, star_p1, self.name))
+        elif self.is_char_type():
+            block.writeline('%s = cb->get_char_type(cb);' % (t1,))
+        elif self.is_float_type():
+            xxx
         else:
-            i = 0
-        checktypes = checktypes[i::-1] + checktypes[i+1:]
-        for t in checktypes:
-            ut = "unsigned " + t
-            if self.name == t == "char":
-                ut = "char"
-            block.writeline("    else if (sizeof(%s) == sizeof(%s))" %
-                            (star_p1, ut))
-            block.sprintf(ut, indent=8)
-        block.writeline("    else")
-        block.sprintf("uint%u_t", "(int)sizeof(%s) * 8" % (star_p1,),
-                      extralength=10, indent=8)
-        block.writeline("}")
-        block.writeline("else {")
-        elsecase = ""
-        for t in checktypes:
-            if t == "char" and self.name != "char":
-                t = "signed char"
-            block.writeline("    %sif (sizeof(%s) == sizeof(%s))" %
-                            (elsecase, star_p1, t))
-            block.sprintf("%s" % (t,), indent=8)
-            elsecase = "else "
-        block.writeline("    else")
-        block.sprintf("int%u_t", "(int)sizeof(%s) * 8" % (star_p1,),
-                      extralength=10, indent=8)
-        block.writeline("}")
-        block.end_disjoint_cases(disjunction)
-        block.sprintf_add_left(" ")
+            raise AssertionError
+        return t1
 
 
 class BaseFunctionType(BaseType):
@@ -185,7 +157,7 @@
         self.totype.inspect_type(block, inspect)
         #
         extra = self._base_pattern[self.totype.is_array_type, self.const]
-        block.sprintf_add_both_sides(extra)
+        #block.sprintf_add_both_sides(extra)
 
 
 class ArrayType(BaseType):
@@ -419,11 +391,9 @@
         self.type = type
 
     def write_declaration(self, funcblock):
-        block = CodeBlock(funcblock.tr)
-        block.sprintf_add_left('typedef ')
-        block.sprintf_add_right(r';\n')
+        block = CodeBlock(funcblock)
         inspect = TypeInspector(block, self.name)
-        self.type.inspect_type(block, inspect)
+        t1 = self.type.inspect_type(block, inspect)
         inspect.stop()
-        block.sprintf_add_left(self.name)
+        block.writeline('cb->define_type(cb, "%s", %s);' % (self.name, t1))
         funcblock.write_subblock(block)
diff --git a/test/test_codegen.py b/test/test_codegen.py
--- a/test/test_codegen.py
+++ b/test/test_codegen.py
@@ -1,7 +1,7 @@
 import py
 import os
 from creflect.cparser import CSource
-from creflect.codegen import TransformCDef
+from creflect.codegen import transform_cdef
 
 
 def _trimlines(lines):
@@ -37,12 +37,12 @@
     f.close()
     print
     #
+    csource = CSource(''.join(cdefblock))
     funcname = 'test' + os.path.splitext(filename)[0].replace('-','_')
-    transform = TransformCDef(CSource(''.join(cdefblock)))
-    got = transform.generate(funcname)
-    print '\n'.join(got)
+    text = transform_cdef(csource, funcname)
+    print text
     #
-    got = _trimlines(got)
+    got = _trimlines(text.splitlines())
     expected = _trimlines(expected)
     compare_lists(got, expected)
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to