Skip to main content

The Loosey Goosey C Parser and Interpreter

Rationale

I love programming with a REPL (Read-Eval-Print-Loop).

So for instance, I'm one of those people who's always at the CLI, instead of in an IDE.

And I love Python, and iPython; my favourite style of Python programming is where I'm typing stuff into the REPL, seeing what works, and when I find it, I copy-paste it into a .py file as code and/or tests.

Now, I'm also a C programmer, and C is in many ways a terrible language to work with. It certainly doesn't lend itself well to programming in a REPL. I can read through a large C codebase, like SQLite or PostgreSQL or CPython or the Linux kernel, and come to some understanding of what it's doing, but I can't easily try it out. It's not like Python, where I'm at the REPL, and I can import a class, instantiate it, and poke at it.

So, that's the goal here: what if you could "import" (or even just copy-paste) C code, and play with it at a REPL?

The idea will be to map the runtime semantics of C onto some Python classes, then have a C parser which is intentionally loosey-goosey, so it can parse even fragments of C code without having to #include all the proper stuff, and then you can instantiate the types and call the functions and so on, and have things fail at runtime instead of compile-time, for instance if you call some function which attempts to call another function which hasn't been defined. And you should be able to define missing functions and things quickly in Python, so you can re-run the thing you were actually interested in, and have it succeed this time.

In order for this to work, we're not gonna worry too much about e.g. whether f(x) is a preprocessor macro or a function call. And we're not gonna worry too much about whether every statement ends in a semicolon, because maybe MY_MACRO(X, Y) is supposed to expand to a bunch of statements with semicolons already at the end... but we don't care if we've actually got the macro definition, we'll just call that a "function call", and if the function's defined in Python, so be it!

So, we're going to support some invalid C code, and we're probably also going to fail on some valid C code, because C allows for a lot of weird stuff which is handy if you're writing for the Obfuscated C Contest, but I don't think we need to worry about if we're just trying to e.g. play with nicely formatted stuff we find in the Linux kernel.

Last thing in this rationale, I'm going to breathe to you a secret word... doctests. Imagine doctests for your C code.

Installation

pip install loosey-goosey-c

See also:

The C preprocessor

Implemented in pure Python! It's actually fairly complete.

See its documentation here:

Example usage:

$ echo -e '#define M(X) [X]\nM(M(1))' | python -m loosey.pp
[ [ 1 ] ]

Mini C Interpreter

Work in progress.

Allows C code to use Python values and functions, and allows definition of C functions usable from Python.

Example usage:

>>> from loosey.minic import MiniC
>>> minic = MiniC()

>>> minic.eval('1 + 2')
3

>>> minic.eval('1 + 2 * 10 + 3')
24

>>> add = minic.eval('int add(int x, int y) { int z = x + y; return z; }')
>>> add
add(x, y)

Using a mini-C function directly from Python:
>>> add(3, 4)
7

>>> minic.eval('int x = 3, total = add(x, 5);')
{'x': 3, 'total': 8}

>>> minic.globals
{'add': add(x, y), 'x': 3, 'total': 8}

Using a Python function from within mini-C:
>>> import math
>>> minic.globals['sqrt'] = math.sqrt
>>> minic.eval('sqrt(20 + 5)')
5.0

Metadata

Release files for loosey-goosey-c 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for loosey-goosey-c 0.0.2
File Size Uploaded Provenance Actions
loosey_goosey_c-0.0.2.tar.gz 36.8 kB Without provenance Details for loosey_goosey_c-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for loosey-goosey-c 0.0.2
File Interpreter ABI Platform Actions
loosey_goosey_c-0.0.2-py3-none-any.whl Python 3 none any Details for loosey_goosey_c-0.0.2-py3-none-any.whl

Total release size: 75.6 kB

Release files / loosey_goosey_c-0.0.2.tar.gz

Download URL loosey_goosey_c-0.0.2.tar.gz
Size 36.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f3b38333f927fae2750a5414e7a0f8e1902879ae3bf8bbc80de0884ac9ac0995
BLAKE2b-256 checksum
How to use checksums
ea7f24058407d953a987836263148e2e4ebaed3cdd825a5be2f35dc862140efb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / loosey_goosey_c-0.0.2-py3-none-any.whl

Download URL loosey_goosey_c-0.0.2-py3-none-any.whl
Size 38.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99a46c3815e827fa1af8a68f8767799c317644541d21c4c732b3971a248b0084
BLAKE2b-256 checksum
How to use checksums
c4d2e544d2eb8e7a93c8e6ff7069d63318bb1cc8a4802f8776228fc9894724bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for loosey-goosey-c 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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