bf-codegen
所属分类:自动编程
开发工具:Python
文件大小:7KB
下载次数:0
上传日期:2023-02-22 03:27:49
上 传 者:
sh-1993
说明: 作为一种中间语言发出brainstfuck的编译器后端
(A compiler backend that emits brainfuck as an intermediate language)
文件列表:
LICENSE (1068, 2023-02-22)
pyproject.toml (254, 2023-02-22)
requirements.txt (0, 2023-02-22)
setup.py (69, 2023-02-22)
src (0, 2023-02-22)
src\bf_codegen (0, 2023-02-22)
src\bf_codegen\__init__.py (22, 2023-02-22)
src\bf_codegen\context.py (5937, 2023-02-22)
src\bf_codegen\interpreter.py (1720, 2023-02-22)
tests (0, 2023-02-22)
tests\__init__.py (0, 2023-02-22)
tests\test_helloworld.py (299, 2023-02-22)
# bf-codegen
A compiler backend that emits [Brain***](https://en.wikipedia.org/wiki/Brain***) as an intermediate language
This shouldn't exist. I'm sorry.
## Examples
### Hello example
```python
import bf_codegen
from bf_codegen import interpreter
context = bf_codegen.Context()
hello_str = context.build_store("Hello, Sailor!\n")
context.build_print(hello_str)
prog = context.code()
assert interpreter.run(prog) == "Hello, Sailor!\n"
```
### Loop example
```python
## prints numbers counting down from 9 to 1
import bf_codegen
from bf_codegen import interpreter
context = bf_codegen.Context()
newline = context.build_store(["\n"])
def body():
# add 48 to get the ascii value of the digit
sum = context.build_add(loop_times, 48)
context.build_print(sum)
context.build_assign(sum, 0)
context.build_print(newline)
# 9 is the number of times to loop
# the loop implicitly decrements the value of loop_times and stops when it reaches 0
loop_times = context.build_store([9])
context.build_loop(
times = loop_times,
body = body
)
prog = context.code()
assert interpreter.run(prog) == "9\n8\n7\n6\n5\n4\n3\n2\n1\n"
```
近期下载者:
相关文件:
收藏者: