You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Two intertwined changes:
- Reorganize the bf-dialect doc with a "Reading the compiled Python"
section that frames bf as a transpiler (pedagogic value lives in the
output text) and documents two ways to actually see the compiled
Python: programmatically via bf.compile, and live via mcpyrate.debug
StepExpansion. The second method goes through the import machinery,
which on first import requires path_stats to handle source-level
dialect files — fixed in mcpyrate 4.1.1.
- Bump unpythonic's mcpyrate dependency floor from >=4.1.0 to >=4.1.1
to make the StepExpansion path actually work (and to keep the bf
docstring's "running the file under macropython" claim honest, which
was technically broken until the mcpyrate hotfix even though we
hadn't noticed).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- A leading `# ` in the BF source is passed through cleanly, so both `# real comment` and bare `real comment` come out as `# real comment` in the compiled Python.
57
58
-**`reset`**: a line whose stripped content is exactly `reset` compiles to `tape.clear(); ptr = 0`. This lets several BF programs share one file.
58
59
59
-
The same compiler is available as a plain function:
60
+
## Reading the compiled Python
61
+
62
+
BF is a *transpiler*: it takes a BF program and emits human-readable Python that does the same thing. The compiled output is intentionally legible — `+++` becomes `tape[ptr] += 3`, `[…]` becomes `while tape[ptr]: …`, comments are preserved as Python comments — so reading the Python is a perfectly good way to understand a non-trivial BF program. The pedagogic value of the dialect lives in the output text, not in any stored knowledge of the input.
63
+
64
+
Two ways to actually see the compiled Python:
65
+
66
+
**1. Programmatically, via `bf.compile`.** Useful for offline inspection, ad-hoc experiments, and printing the compiled form into a notebook or a paper:
60
67
61
68
```python
62
69
from unpythonic.dialects import bf
63
-
print(bf.compile(bf_program_str))
64
-
```
65
70
66
-
`bf.compile(src)` returns self-contained runnable Python — useful for reading a non-trivial BF program by rewriting it in a language a human can actually read. The qualified `bf.compile` form is recommended over `from … import compile` to avoid shadowing `builtins.compile` in the importer's namespace.
71
+
src ="+++++++++++++[>+++++<-]>."# 'A' via a 5×13 multiplication loop
72
+
print(bf.compile(src))
73
+
```
67
74
68
-
For example, the program above compiles to:
75
+
For the program above, this prints:
69
76
70
77
```python
71
78
from sys import stdin, stdout
@@ -83,6 +90,21 @@ ptr += 1
83
90
stdout.write(chr(tape[ptr])); stdout.flush()
84
91
```
85
92
93
+
The qualified `bf.compile` form is recommended over `from … import compile` to avoid shadowing `builtins.compile` in the importer's namespace.
94
+
95
+
**2. Live, while running the dialect file, via `mcpyrate.debug.StepExpansion`.** When `StepExpansion` is the *first* dialect in the import chain, the dialect expander prints the source after each transformer pass — so for a BF file it shows the BF body before transformation and the generated Python afterward, then runs the result:
96
+
97
+
```python
98
+
from mcpyrate.debug import dialects, StepExpansion
99
+
from unpythonic.dialects.bf import dialects, BF
100
+
101
+
+++++++++++++[>+++++<-]>.
102
+
```
103
+
104
+
Run via `macropython` (or by `import`-ing the file) and the BF→Python translation is printed to stderr alongside the program's normal execution. Useful when the BF source already lives inside a `.py` file and you'd rather not retype it as a string for `bf.compile`.
105
+
106
+
`StepExpansion` is documented in [`mcpyrate`'s troubleshooting guide](https://github.com/Technologicat/mcpyrate/blob/master/doc/troubleshooting.md). It works for any dialect, not just BF.
107
+
86
108
## What BF is
87
109
88
110
BF is a dialect ~of Python~ implemented as a whole-module *source-to-source* transform. The dialect definition lives in [`unpythonic.dialects.bf`](../../unpythonic/dialects/bf.py). Usage examples can be found in [the unit tests](../../unpythonic/dialects/tests/test_bf.py).
0 commit comments