Welcome to cgenโ€™s documentation!#

cgen offers a simple abstract syntax tree for C and related languages (C++/CUDA/OpenCL) to allow structured code generation from Python.

Hereโ€™s an example, to give you an impression:

import cgen as c

func = c.FunctionBody(
    c.FunctionDeclaration(c.Const(c.Pointer(c.Value("char", "greet"))), []),
    c.Block([
        c.Statement('return "hello world"')
        ])
    )

print(func)

This prints the following:

char const *greet()
{
  return "hello world";
}

cgen deliberately does not worry about expression trees. Its sister package pymbolic can help you build those.

Contents#

Indices and tables#