mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 22:34:56 +00:00
Extracted from modular repo and reorganized: Compiler Implementation: - 21 compiler source files (frontend, semantic, IR, codegen, runtime) - 15 comprehensive test files (lexer, parser, type checker, backend, etc.) - 9 compiler usage example programs Architecture (5 phases): - Frontend: Lexer, parser, AST generation (lexer.mojo, parser.mojo, ast.mojo) - Semantic: Type system, checking, symbol resolution (3 files) - IR: MLIR code generation (mlir_gen.mojo, mojo_dialect.mojo) - Codegen: LLVM backend, optimization passes (llvm_backend.mojo, optimizer.mojo) - Runtime: Memory mgmt, reflection, async support (3 files) File Organization: - mojo/compiler/src/: Compiler implementation (21 files, 952K) - mojo/compiler/tests/: Test suite (15 files) - mojo/compiler/examples/: Usage examples (9 files) - mojo/samples/: Mojo language examples (37 files, moved from examples/) Documentation: - mojo/CLAUDE.md: Project-level guide - mojo/compiler/CLAUDE.md: Detailed architecture documentation - mojo/compiler/README.md: Quick start guide - mojo/samples/README.md: Example programs guide Status: - Compiler architecture complete (Phase 4) - Full test coverage included - Ready for continued development and integration Files tracked: - 45 new compiler files (21 src + 15 tests + 9 examples) - 1 moved existing directory (examples → samples) - 3 documentation files created - 1 root CLAUDE.md updated Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
43 lines
1.7 KiB
Markdown
Executable File
43 lines
1.7 KiB
Markdown
Executable File
# Calling Mojo functions from Python
|
|
|
|
Mojo now has the ability to be called from Python, with Mojo functions
|
|
representing themselves as if they were native Python functions. When a Python
|
|
script is run that references a Mojo module, that module is transparently
|
|
compiled to present an interface that can be used from Python. At present,
|
|
the interface that is presented to Python must be manually defined in Mojo
|
|
code.
|
|
|
|
A full description of this functionality can be found
|
|
[within the Mojo manual](https://docs.modular.com/mojo/manual/python/mojo-from-python/),
|
|
including current [known limitations](https://docs.modular.com/mojo/manual/python/mojo-from-python/#known-limitations).
|
|
|
|
These examples show how to perform basic to more advanced use of Mojo code from
|
|
Python in order to progressively replace hotspots in Python code with fast
|
|
Mojo. This includes using Mojo to drive calculations on
|
|
[MAX-compatible GPUs](https://docs.modular.com/max/faq/#gpu-requirements).
|
|
|
|
The two examples of Mojo functions being called from Python consist of:
|
|
|
|
- **hello.py**: A literal "hello world" example, where a string is passed in
|
|
from Python to Mojo, concatenated with a string in Mojo code, and the result
|
|
passed back to Python to be printed.
|
|
- **mandelbrot.py**: A parallel calculation of the number of iterations to
|
|
escape in the Mandelbrot set, performed on a MAX-compatible GPU in Mojo. The
|
|
Python code calls into the Mojo GPU calculation and gets the results back as
|
|
ASCII art in a string to be printed.
|
|
|
|
You can run these examples via [Pixi](https://pixi.sh):
|
|
|
|
```sh
|
|
pixi run hello
|
|
pixi run mandelbrot
|
|
```
|
|
|
|
or directly in a Python virtual environment where the `max` PyPI package has
|
|
been installed:
|
|
|
|
```sh
|
|
python hello.py
|
|
python mandelbrot.py
|
|
```
|