Skip to main content

A C/C++ source generation package.

Project description

Cxx

A C/C++ source generation package.

Usage

First, some context:

  • Documents contain declarations.
  • Declarations contain other declarations, statements and expressions.
  • Some statements contain other statements and expressions.
  • Expressions are either operators or literals. Operators contain other expressions.

The general usage flow is:

  • Populate a Document with Declarations of your choice.
  • Format it as a string with any DocumentFormatter subclass.

Simple Example

import cxx

even_odd_enum = (
    cxx.EnumBuilder("EvenOdd", scoped=True)
    .add_member("Even")
    .add_member("Odd")
    .build()
)

get_even_odd_function = (
    cxx.FunctionBuilder("GetEvenOdd", "EvenOdd")
    .add_specifier("inline")
    .add_argument("int", "value", cxx.literals.integer(0))
    .add_statement(
        cxx.statements.return_(
            cxx.operators.other.conditional(
                cxx.operators.arithmetic.remainder(
                    cxx.literals.identifier("value"),
                    cxx.literals.integer(2),
                ),
                cxx.literals.identifier("EvenOdd::Odd"),
                cxx.literals.identifier("EvenOdd::Even"),
            )
        )
    )
    .build()
)

document = (
    cxx.DocumentBuilder(header=True)
    .add_declaration(even_odd_enum.definition)
    .add_declaration(get_even_odd_function.definition)
    .build()
)
formatter = cxx.ClangFormatter(configuration_style="google")
print(formatter.format(document))

Output:

#pragma once

enum class EvenOdd {
  Even,
  Odd,
};

inline EvenOdd GetEvenOdd(int value = 0) {
  return value % 2 ? EvenOdd::Odd : EvenOdd::Even;
}

"Factory" API

The "factory" API allows you to create any element:

  • cxx.document(...) -> Document
  • cxx.declarations.[specific declaration type](...) -> Declaration
  • cxx.literals.[specific literal type](...) -> Literal
  • cxx.operators.[specific operator type](...) -> Operator
  • cxx.statements.[specific statement type](...) -> Statement

Some elements are awkward to create this way - such as functions, which contain arguments, internal statements, specifiers, and so on. For those elements, you should use a builder.

Builder API

There are several builders:

  • cxx.ClassBuilder
  • cxx.EnumBuilder
  • cxx.FunctionBuilder
  • cxx.NamespaceBuilder
  • cxx.DocumentBuilder
  • cxx.CompoundStatementBuilder
  • cxx.FunctionCallOperatorBuilder
  • cxx.VariableDeclarationBuilder

Current Unsupported Features

This is a list of language features this package doesn't currently support. This is not due to any inherent difficulty - other things were prioritized.

  • Initializer list - int arr[3] = { 0, 1, 2 };
  • For-range loop - for (auto a : vec) { ... }
  • Structured binding - auto [a, b] = ...;
  • Multiple-variable simple declaration - int a=5, b=2;
  • Multiple-type typedef declaration - typedef int INT, *PINT;
  • Typedef-specifier for declaration - typedef struct { ... } S;
  • Exception handling - try, catch, throw
  • Template and template specialization - template <typename T>
  • New operator - new
  • Delete operator - delete
  • Float/decimal literals - 0.123

Development Setup

Install and configure poetry:

pip3 install poetry
poetry config virtualenvs.in-project true

Install development dependencies and pre-commit hook

poetry install --with dev
pre-commit install

Run the tests:

poetry run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cxx-0.3.0.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

cxx-0.3.0-py3-none-any.whl (55.8 kB view details)

Uploaded Python 3

File details

Details for the file cxx-0.3.0.tar.gz.

File metadata

  • Download URL: cxx-0.3.0.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1012-azure

File hashes

Hashes for cxx-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1e6c6447572e62a2a2fa32671f9a39027d223db8fd378db41f2689247889da1a
MD5 38c8eeb0cc85737a647664dc7159c8ff
BLAKE2b-256 43779c40c6540ba081a3c4c6e00e57b3295503a2dd1c48978d0e78c9a98bee1f

See more details on using hashes here.

File details

Details for the file cxx-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: cxx-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 55.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1012-azure

File hashes

Hashes for cxx-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1bd4fc1422794c0689768038e8d8631b64c510b09a7d5a520344a76adb56324
MD5 995314b36835557a8e41d7296fd398d1
BLAKE2b-256 298c0fb1ebb8f84c5653991cce7685c22f9a0cef8b5fbb3392eca57e4dcea22d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page