Getting Started

Hello, World!

This is the source code of a traditional Hello World program written in RPython.

def entry_point(argv):
    print "Hello, World!"
    return 0

# The target function is the main function of a RPython program. It takes
# command line arguments as inputs and return an entry point function.
def target(*args):
    return entry_point

To compile it, we should first install Python (both CPython and PyPy are OK). In addition, we need to obtain the RPython compiler from PyPy’s repository. The RPython compiler is located at pypy/rpython/bin/rpython.

Note

All examples in this doc are tested with the RPython compiler in PyPy’s codebase (tag: release-pypy2.7-v6.0.0 and release-pypy2.7-v7.1.0), and with CPython 2.7 (aka. Python 2.7) or PyPy 2.7 installed.

$ sudo apt-get install python2 libgc-dev
$ sudo apt-get install libgc-dev              # if you want to use Boehm GC
$ wget https://bitbucket.org/pypy/pypy/downloads/pypy2.7-v7.1.0-src.tar.bz2
$ tar jxf pypy2.7-v7.1.0-src.tar.bz2
$ export RPY=$(pwd)/pypy2.7-v7.1.0-src/rpython/bin/rpython
$ export PATH=$PATH:$(pwd)/pypy2.7-v7.1.0-src/rpython/bin
$ cd code && make hello_world

Now, we can compile our first hello world example by running:

$ rpython hello_world.py

An executable named hello_world-c is generated by the compiler. When launching the executable, a “Hello, World!” message will be printed out.

$ ./hello_world-c
Hello, World!

Since RPython syntax is a restricted version of Python, therefore you can use any Python interpreter to run the hello world example just like before. In this example, we should add an entry point in the script.

def entry_point(argv):
    print "Hello, World!"
    return 0

# The target function is the main function of a RPython program. It takes
# command line arguments as inputs and return an entry point function.
def target(*args):
    return entry_point

# This is the entry point for interpreting a script with Python.
if __name__ == "__main__":
    import sys
    entry_point(sys.argv)

Then, you can run the script as normal with Python.

$ python hello_world_python.py
Hello, World!

This implies that you can test your RPython code with any Python interpreter and then compile to native code.