Skip to content
/ Py2c Public

A lightweight Python-to-C transpiler based on AST, supporting most Python 3.10 features. MVP version.

License

Notifications You must be signed in to change notification settings

lixiasky/Py2c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Py2C — Python to C Transpiler (MVP)

Py2C is a lightweight MVP transpiler that converts Python 3.10 source code into valid C code using AST transformation. It is designed to demonstrate the feasibility of converting high-level language constructs to C while maintaining correctness and basic structure.

Note: Source code comments are primarily in Chinese, as this is an MVP stage prototype.

*Note: This project was built by a first-year undergraduate student as an initial attempt to explore compiler design.

Features

Supported

  • Variables and expressions

    • Arithmetic: +, -, *, /, %, **
    • Comparison and logical operators
    • Automatic type inference: int, double, char
  • Control flow

    • if / elif / else
    • while, for x in range(n)
    • break, continue, pass
  • Functions

    • Definition and invocation
    • Return values
    • Type inference for parameters and return types
  • print()

    • Supports multi-argument
    • Automatically chooses format specifier (%d, %f, %s)
  • Classes and objects

    • class converted to struct
    • init, methods, attribute access
    • self mapped to struct pointer
  • Lists

    • Simple list converted to C array (no slicing or append)
  • Global code

    • All top-level code placed inside main()

Not supported (output as comments in generated C code)

  • try/except/finally
  • with
  • import, from ... import
  • dict, set, tuple
  • lambda, decorators, yield, async/await

Usage

python3 py2ast.py example.py > example_ast.json go run ast2c.go example_ast.json > example.c gcc -o example example.c ./example

Example

The included example.py demonstrates support for:

  • Functions
  • Class definition and usage
  • Control structures
  • Print output

The corresponding C output compiles via GCC and executes partially with correct output.

Notes

  • Some generated C files may require manually adding missing #include lines.
  • Output structure is formatted with readable indentation and fallback comments.
  • Only one test file (example.py) has been verified to compile and partially execute correctly.

Contact

I'd be glad to receive feedback if you'd like.

Email:[email protected]

License

MIT License. See LICENSE for details.

About

A lightweight Python-to-C transpiler based on AST, supporting most Python 3.10 features. MVP version.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published