Program classes

The Program classes provide a high level interface for working with PPCI. Each class represents one language / code representation. They have a common API to get reporting, compile into other program representations, and export to e.g. textual or binary representations.

Source codeIntermediateMachine code
c3 python ir wasm arm x86

Base program classes

class ppci.programs.Program(*items, previous=None, debugdb=None)

Abstract base class to represent a computer program. Subclasses represent languages (i.e. code representations), e.g. Python, IR, or X86. Program objects can be compiled into one another using the to_xx() methods.

Each instance can have multiple “items”, which can represent files or modules, and which can in some cases be bound/linked into a single object. Some Program classes also provide optimizations. Many languages can be represented in textual or binary form, and can be imported/exported as such.

Each subclass needs to implement:

  • A docstring with a brief description of the language.
  • Method _check_items(items) to test input at initialization.
  • Method _copy().
  • Method _get_report(html).

Each subclasses should implement as applicable:

  • Method optimize().
  • Export methods like as_text().
  • Import classmethods like from_text().

A tuple with the names of the languages that the current object originated from.


Make a (deep) copy of the program.


Get a textual representation of the program for introspection and debugging purposes. If html the report may be html-formatted.


The list of items, representing components such as files or modules.


Get a previous Program instance, or None.

The which arg can be: * int: Go this many steps back in the compile chain (default 1). * str: Get the program in the compile chain that represents the given language. * Program instance/class: Get the program in the compile chain that represents the given Program class.


The toplevel Program instance that is the source of the compile chain.

to(language, **options)

Compile this program into another representation. The tree is traversed to find the lowest cost (i.e. shortest) chain of compilers to the target language.

Experimental; use with care.

class ppci.programs.SourceCodeProgram(*items, previous=None, debugdb=None)

Base class for source code (i.e. intended to be read and written by humans).


Get the program in the form of a series of (standardized) tokens, for the purpose of syntax highlighting.

class ppci.programs.IntermediateProgram(*items, previous=None, debugdb=None)

Base class for code representation that are not human readable nor machine executable.

class ppci.programs.MachineProgram(*items, previous=None, debugdb=None)

Base class for executable machine code.


If the architecture of the code matches the current machine, execute the code in this Python process.

Source code programs

class ppci.programs.C3Program(*items, previous=None, debugdb=None)

C3 is a novel programming language ispired by C, but avoiding some of its contraptions.

The items in a C3Program are strings.

to_ir(includes=None, march=None, reporter=None)

Compile C3 to PPCI IR for the given architecture.

class ppci.programs.PythonProgram(*items, previous=None, debugdb=None)

Python is a dynamic programming language, which is popular due to its great balance between simpliciy and expressiveness, and a thriving community.

The items in a PythonProgram are strings.


Run (i.e. exec()) the code in the current interpreter.


Compile Python to PPCI IR.

Status: very preliminary.


Compile Python to WASM.

Status: can compile a subset of Python, and assumes all floats.

Intermediate programs

class ppci.programs.IrProgram(*items, previous=None, debugdb=None)

PPCI IR code is the intermediate representation used in PPCI.


Optimize the ir program


Compile to ARM machine code.

Status: …


Compile PPCI IR to Python. Not very efficient or pretty code, but it can be useful to test the IR code without compiling to machine code.

Status: complete: can compile the full IR spec.


Compile PPCI IR to WASM.

Do this by taking each ir module into a wasm module.


Compile to X86 machine code.

Status: …

class ppci.programs.WasmProgram(*items, previous=None, debugdb=None)

WASM (a.k.a. Web Assembly) is an open standard to represent code in a compact, low level format that can be easily converterted to machine code, and run fast as well as safe.

Items in a WasmProgram are WasmModule objects.


Convert to WASM binary representation.


Turn into a hex representation (using either the byte representation or the text representation). Raises NotImplementedError if this program does not have a binary nor textual representation.


Compile WASM to IR.

Status: very basic.

Machine code programs

class ppci.programs.ArmProgram(*items, previous=None, debugdb=None)

Machine code for most mobile devices and e.g. the Raspberry Pi.


Export as binary code object (bytes)

class ppci.programs.X86Program(*items, previous=None, debugdb=None)

Machine code for most common desktops and laptops.


Export as elf file.


Export as a system executable file.


Export as binary code object (bytes)