Friday, January 11, 2013

Automatic C to Cython Translator


Blender is too large to rewrite in Cython entirely by hand. I am now working on a c-to-cython translator using Eli Bendersky's PyCParser. Eli already has a working c-to-c translator that I am using as the starting point for c-to-cython.


Fibonacci Series in C

void main()
{
   int n, first = 0, second = 1, next, c;
   n = 1000;
   printf("First %d terms of Fibonacci series are :-\n",n);
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
   return 0;
}

Fibonacci Series Automatically Translated to Cython

cpdef void main():
  cdef int n;
  cdef int first = 0;
  cdef int second = 1;
  cdef int next;
  cdef int c;
  n = 1000;
  print("First %d terms of Fibonacci series are :-\n", n);
  for c from 0 <= c < n:
    if (c <= 1):
      next = c;
    else:
      next = first + second;
      first = second;
      second = next;
    print("%d\n", next);
  return 0;

Typedef Structures - C

// basic typedef struct 
typedef struct A {
 int x;
 float y;
} A;

// struct with different typedef name
typedef struct Foo {
 int z;
} Bar;

// basic struct
struct B {
 int w;
};

Typedef Structures - Automatically Translated to Cython

ctypedef struct A:
  int x
  float y

cdef struct Foo:
  int z
ctypedef Foo Bar

cdef struct B:
  int w
c-to-cython source code (work in progress)

No comments:

Post a Comment