Code Monkey home page Code Monkey logo

pyc's Introduction

pyc

A simple Python to C compiler written in Python making use of libpython.

Posts

Requirements

  • GCC
  • Python3
  • libpython (python3-devel on Fedora)
  • clang-format

Example

Take a simple Python program:

$ cat tests/recursive_fib.py
def fib(n):
    if n == 0 or n == 1:
        return n

    return fib(n - 1) + fib(n - 2)


def main():
    print(fib(40))

Compile and run it:

$ python3 pyc tests/recursive_fib.py
$ ./bin/a.out
102334155

Generated code

After compiling, the C program is stored in bin/main.c:

$ cat bin/main.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>

inline PyObject *PYC_Add(PyObject *l, PyObject *r) {
  // TODO: allow __add__ override

  // Includes ints and bools
  if (PyLong_Check(l) && PyLong_Check(r)) {
    return PyNumber_Add(l, r);
  }

  // TODO: handle str, etc.

  // TODO: throw exception
  return NULL;
}

inline PyObject *PYC_Sub(PyObject *l, PyObject *r) {
  // TODO: allow __add__ override

  // Includes ints and bools
  if (PyLong_Check(l) && PyLong_Check(r)) {
    return PyNumber_Subtract(l, r);
  }

  // TODO: handle str, etc.

  // TODO: throw exception
  return NULL;
}

inline PyObject *PYC_Print(PyObject *o) {
  PyObject_Print(o, stdout, Py_PRINT_RAW);
  printf("\n");
  return Py_None;
}

PyObject *fib_0(PyObject *n_1) {
  PyObject *boolop_2;
  PyObject *compare_4 = n_1;
  PyObject *num_5 = PyLong_FromLong(0);
  compare_4 = PyObject_RichCompare(compare_4, num_5, Py_EQ);
  boolop_2 = compare_4;
  if (PyObject_IsTrue(compare_4)) {
    goto done_or_3;
  }
  PyObject *compare_6 = n_1;
  PyObject *num_7 = PyLong_FromLong(1);
  compare_6 = PyObject_RichCompare(compare_6, num_7, Py_EQ);
  boolop_2 = compare_6;
  if (PyObject_IsTrue(compare_6)) {
    goto done_or_3;
  }
done_or_3:

  if (PyObject_IsTrue(boolop_2)) {

    return n_1;
  }

  PyObject *binop_8;
  PyObject *binop_9;
  PyObject *num_10 = PyLong_FromLong(1);
  binop_9 = PYC_Sub(n_1, num_10);
  PyObject *call_result_11 = fib_0(binop_9);
  PyObject *binop_12;
  PyObject *num_13 = PyLong_FromLong(2);
  binop_12 = PYC_Sub(n_1, num_13);
  PyObject *call_result_14 = fib_0(binop_12);
  binop_8 = PYC_Add(call_result_11, call_result_14);

  return binop_8;
}

PyObject *main_1() {
  PyObject *num_2 = PyLong_FromLong(40);
  PyObject *call_result_3 = fib_0(num_2);
  PyObject *call_result_4 = PYC_Print(call_result_3);
  // noop to hide unused warning
  call_result_4 += 0;
  return Py_None;
}

int main(int argc, char *argv[]) {
  Py_Initialize();

  // Initialize globals, if any.

  PyObject *r = main_1();
  return PyLong_AsLong(r);
}

pyc's People

Contributors

eatonphil avatar tekknolagi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.