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.)

Documentation

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
  • A .note field on every value, printed as a /* note */ comment after the value — for saying where a value came from once it no longer shows: 3.125 /* totalVolume / elementVolume */

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.2.tar.gz (10.6 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.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cshape-0.2.2.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.3

File hashes

Hashes for cshape-0.2.2.tar.gz
Algorithm Hash digest
SHA256 198bf48fc17f74f679483cb0ae871052d8dbf69a40d924355ccf1797c892bf7e
MD5 50f719b48f6577acfd5bb15e664a6a76
BLAKE2b-256 48c4a79109066dc20cf2abcedc2d2b5122bc396805d982c8e2bdf56aa7e6ae5a

See more details on using hashes here.

File details

Details for the file cshape-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: cshape-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.3

File hashes

Hashes for cshape-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a62c28f36ae8d319d99c77bcea72596205c7f9041a27b3457b926094c7a8040d
MD5 f1c687c097330bd98e2bbbf4b853e67f
BLAKE2b-256 0a4dfe9dfd2f675779896b8304e39f19f8b4c7e2d9f12507d4cdcb30ff72ae9b

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 files

0.2.1

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