Skip to main content

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/#endif regions
  • Two brace styles (legacy — K&R-ish, modern — Allman-ish), configurable via render(node, style=...)
  • A .mark field 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

cshape-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cshape-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

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

Hashes for cshape-0.1.0.tar.gz
Algorithm Hash digest
SHA256 54385c2f2beb21c025e38a0678be6d5c35c8e8ad3809edd83019f7cc03ff97b6
MD5 2df0e1d0510723ab299d8ab8308ad905
BLAKE2b-256 0c81db67f0dbf5d9c50397278e0cd1fd3e82f850a10ab8cdf1160d5a77782fab

See more details on using hashes here.

Provenance

The following attestation bundles were made for cshape-0.1.0.tar.gz:

Publisher: publish.yml on lexbalan/cshape

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for cshape-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 582833d24b565c4a742215eec595d58ed23974c426dbbe0573bc22b7f897d578
MD5 8e10d02410cb533002fb90af6cb921a9
BLAKE2b-256 d34e6555f705d97ed7009cb778d12b8246c3d96d75c52ad593f6dcadddeac03a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cshape-0.1.0-py3-none-any.whl:

Publisher: publish.yml on lexbalan/cshape

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.2

2 files

0.2.1

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page