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
DocumentwithDeclarations of your choice. - Format it as a string with any
DocumentFormattersubclass.
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(pragma_once=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(...) -> Documentcxx.declarations.[specific declaration type](...) -> Declarationcxx.literals.[specific literal type](...) -> Literalcxx.operators.[specific operator type](...) -> Operatorcxx.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.ClassBuildercxx.EnumBuildercxx.FunctionBuildercxx.NamespaceBuildercxx.DocumentBuildercxx.CompoundStatementBuildercxx.FunctionCallOperatorBuildercxx.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cxx-0.2.0.tar.gz.
File metadata
- Download URL: cxx-0.2.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1011-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2852d667beebed71f527188aa00e2eb0355049a6ccc8a083bb1b24486347216
|
|
| MD5 |
b00d6ba91848173f7407fc4c15aa6190
|
|
| BLAKE2b-256 |
6be4f45c6fce5e5d26eaeafeeb1eef70f0e5d8cf1b880d2fb70dd1bd2a26856f
|
File details
Details for the file cxx-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cxx-0.2.0-py3-none-any.whl
- Upload date:
- Size: 55.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1011-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccb2f6ad1c68d593d683af072075f29472ce7803a86d8e8dd13a9fdeea12f75b
|
|
| MD5 |
8793f3786623eacfa94483d67a3b5f1f
|
|
| BLAKE2b-256 |
0195d8c7d810733e6a6240b39ed92d311a11352f53c0e12477e93c9a8b7d9155
|