cshape
A dependency-free C AST + printer for Python: build a tree and get C11
source back. No parsing, no dependencies beyond the standard library —
just CType* / CValue* / CStmt* nodes and a render() function that
turns them into text.
Originally the C backend's printer for the Modest compiler, split out because it doesn't know anything about Modest — it only knows about C.
Install
pip install cshape
Example
from cshape import *
func = CStmtDefFunc(
id_str="add",
type=CTypeFunc(
params=[CField(id_str="a", type=CTypeNamed("int")),
CField(id_str="b", type=CTypeNamed("int"))],
to=CTypeNamed("int"),
),
block=CStmtBlock([
CStmtReturn(CValueAdd(CValueNamed("a"), CValueNamed("b"))),
]),
)
print(render(func).strip())
int add(int a, int b) {
return a + b;
}
(render() returns the node's own leading blank lines too — CStmtDefFunc
starts with a blank line by default, so .strip() if you're printing a
single node standalone rather than concatenating several.)
What it covers
- Types: named types, pointers, arrays, function types, structs/unions, enums
- Values: the full C11 expression grammar with correct operator precedence and parenthesization (including the extra parens GCC/Clang warn about for mixed bitwise/shift expressions)
- Statements: blocks, if/while, return, break/continue, inline asm, raw inserts
- Preprocessor:
#define,#undef,#include,#if/#elif/#else/#endifregions - Two brace styles (
legacy— K&R-ish,modern— Allman-ish), configurable viarender(node, style=...) - A
.markfield on every node: set it to any string and it shows up as a/*mark*/comment right before that node in the output — useful for tracing which part of your own compiler/generator produced which bit of C
What it doesn't do
No parsing, no semantic analysis, no type checking. It's a printer, not a compiler frontend — you build the tree, it hands you back text.
License
MIT
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 cshape-0.1.0.tar.gz.
File metadata
- Download URL: cshape-0.1.0.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54385c2f2beb21c025e38a0678be6d5c35c8e8ad3809edd83019f7cc03ff97b6
|
|
| MD5 |
2df0e1d0510723ab299d8ab8308ad905
|
|
| BLAKE2b-256 |
0c81db67f0dbf5d9c50397278e0cd1fd3e82f850a10ab8cdf1160d5a77782fab
|
Provenance
The following attestation bundles were made for cshape-0.1.0.tar.gz:
Publisher:
publish.yml on lexbalan/cshape
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cshape-0.1.0.tar.gz -
Subject digest:
54385c2f2beb21c025e38a0678be6d5c35c8e8ad3809edd83019f7cc03ff97b6 - Sigstore transparency entry: 2248751772
- Sigstore integration time:
-
Permalink:
lexbalan/cshape@f11811060e63a07fcc633f75fe2fef462e19a4eb -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/lexbalan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f11811060e63a07fcc633f75fe2fef462e19a4eb -
Trigger Event:
release
-
Statement type:
File details
Details for the file cshape-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cshape-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
582833d24b565c4a742215eec595d58ed23974c426dbbe0573bc22b7f897d578
|
|
| MD5 |
8e10d02410cb533002fb90af6cb921a9
|
|
| BLAKE2b-256 |
d34e6555f705d97ed7009cb778d12b8246c3d96d75c52ad593f6dcadddeac03a
|
Provenance
The following attestation bundles were made for cshape-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on lexbalan/cshape
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cshape-0.1.0-py3-none-any.whl -
Subject digest:
582833d24b565c4a742215eec595d58ed23974c426dbbe0573bc22b7f897d578 - Sigstore transparency entry: 2248752137
- Sigstore integration time:
-
Permalink:
lexbalan/cshape@f11811060e63a07fcc633f75fe2fef462e19a4eb -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/lexbalan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f11811060e63a07fcc633f75fe2fef462e19a4eb -
Trigger Event:
release
-
Statement type: