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="add",
	type=CTypeFunction(
		params=[CField(id="a", type=CTypeIdentifier("int")),
		        CField(id="b", type=CTypeIdentifier("int"))],
		to=CTypeIdentifier("int"),
	),
	block=CStmtBlock([
		CStmtReturn(CValueAdd(CValueIdentifier("a"), CValueIdentifier("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.2.1.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.2.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file cshape-0.2.1.tar.gz.

File metadata

  • Download URL: cshape-0.2.1.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.2.1.tar.gz
Algorithm Hash digest
SHA256 b25b956ba45e85337f94d008e6d4b7799effd5450c9e70bb551881eff3535a0a
MD5 b86464874ed19c9cbd16f1f00ff07940
BLAKE2b-256 dfe0612b6b8e19d15df5e5590f1d0c4f5d65ed91a294c1dd0379e142ccf4f4c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for cshape-0.2.1.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.2.1-py3-none-any.whl.

File metadata

  • Download URL: cshape-0.2.1-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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 307b9682344aee677734cf2a01b6fb61491fa45a2087ea46212e728ebf23cabd
MD5 e241e7644bfd7d7ff1de6b672270a8f2
BLAKE2b-256 9cfca1e76c4756bbca379facef233a86b4241d6b815cf140ee923067d821f482

See more details on using hashes here.

Provenance

The following attestation bundles were made for cshape-0.2.1-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

This release

0.2.1 This release

2 files

0.1.0

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