PyCon India 2019 Keynote


Starts creating a simple Stack based machine with push and pop. Then adds execute.

def execute(self, ins):
    for op, *args in instructions:
        if op == 'const':
        elif op == 'add':
            right = self.pop()
            left = self.pop()
            self.push(right + left)
        # eg, mul
            raise RuntimeError(f'Bad op {op}')

This uses Python 3’s f-Strings.

Creates a byte array of memory, with load and store. Using struct for packing values.

Introduces a Function class.

To look at the Python code of a function, you can do:

def update_position(x, v, dt):
    return x + v * dt

import dis

Python uses a stack machine.

This machine (the one he’s creating, not Python) is missing control flow (ifs and whiles). For this you need branches and therefore goto. He uses exceptions instead.

    elif op == 'br':
        # args[0] is how many blocks you break out.
        raise Break(args[0])
    elif op == 'br_if':
        if self.pop():
            raise Break(args[0])
    elif op == 'block':
        # a code block
            self.execute(args[0], locals)
        except Break as b:
            if b.level > 0:
                raise Break(b - 1)

Using a test instruction you can use this to make ifs and whiles. He does a similar thing with Return.

This is a Web Assembly interpreter (almost)!

Web Assembly has four data types - int, float at 32 and 64 bits, so we must restrict our types to those. Various functions have different versions for each of those types. There are also some special purpose functions (eg, square root).

Web Assembly rethought the concept of a DLL.

He then hooks everything up to Pygame - so you have a Rust program compiled to Web Assembly that is running in Python.