diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000000000000000000000000000000000000..51501caae8ec1261665e54d04a0f27242c04a465 --- /dev/null +++ b/README.en.md @@ -0,0 +1,85 @@ +# BF VM + +A Brainf**k virtual machine implementation based on MiniAsm, supporting interoperability with Python. + +## Project Overview + +BF VM is a lightweight Brainf**k virtual machine that uses a custom MiniAsm assembly language as its intermediate representation. It simulates basic hardware components (RAM, disk, serial port, etc.) and provides a bridge to the Python interpreter, enabling calls to Python modules and functions from within the BF environment. + +## Features + +- **MiniAsm Assembler**: A custom assembly language supporting variable assignment, arithmetic operations, and flow control +- **Built-in Object System**: Supports multiple data types (integers, strings, lists, dictionaries, sets, tuples) +- **Memory Tape**: Simulates the Brainf**k memory model with multi-level access support +- **Hardware Simulation**: Simulates hardware components such as RAM, disk, and COM1 serial port +- **Python Bridge**: Supports loading Python modules and calling Python functions from within BF VM +- **Import Mechanism**: Supports importing modular code from files + +## Project Structure + +- `asm.py`: Basic version of the MiniAsm interpreter +- `asm5.py`: Enhanced version supporting the object system, Python bridge, and module imports + +## Usage + +### Basic Usage + +```python +# Example using asm5.py +from asm5 import MiniAsmInterpreter + +code = """set a 10 +set b 20 +add a b +print a""" + +interp = MiniAsmInterpreter() +interp.load(code) +interp.run() +``` + +### Calling Python Modules + +```python +interp = MiniAsmInterpreter(search_paths=['/path/to/modules']) +interp.load("import os\ncall os.path.exists /path/to/file result\nprint result") +interp.run() +``` + +### Data Type Examples + +```python +# String operations +set s "Hello" +call s upper +print s + +# List operations +set lst [1, 2, 3] +call lst append 4 +print lst +``` + +## Brainf**k Code Examples + +``` +[?>] : Loop to send characters from RAM to COM1 Tx until encountering 0 +% : Execute command +[#.] : Loop to read from COM1 Rx and output until buffer is empty +``` + +## Execution Flow + +1. Initialize hardware components (RAM, disk, serial port, etc.) +2. Load program into memory +3. Execute RAM operations and control flow +4. Execute disk and COM1 operations +5. Terminate program and save disk state + +## Dependencies + +- Python 3.x + +## License + +See the LICENSE file for details. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..455246d85fc9956cf8114256a27d416582c1be95 --- /dev/null +++ b/README.md @@ -0,0 +1,87 @@ +# BF VM + +一个基于 MiniAsm 的 Brainf**k 虚拟机实现,支持与 Python 的互操作。 + +## 项目简介 + +BF VM 是一个轻量级的 Brainf**k 虚拟机,使用自定义的 MiniAsm 汇编语言作为中间表示。它模拟了基本的硬件组件(RAM、磁盘、串口等),并提供了与 Python 解释器的桥接功能,支持在 BF 环境中调用 Python 模块和方法。 + +## 功能特性 + +- **MiniAsm 汇编器**:自定义的汇编语言,支持变量赋值、算术运算、流程控制等 +- **内置对象系统**:支持多种数据类型(整数、字符串、列表、字典、集合、元组) +- **内存磁带**:模拟 Brainf**k 的内存模型,支持多层访问 +- **硬件模拟**:模拟 RAM、磁盘、COM1 串口等硬件组件 +- **Python 桥接**:支持加载 Python 模块并在 BF VM 中调用 Python 函数 +- **导入机制**:支持从文件导入模块化的代码 + +## 项目结构 + +- `asm.py`:基础版本的 MiniAsm 解释器 +- `asm5.py`:增强版本,支持对象系统、Python 桥接和模块导入 + +## 使用方法 + +### 基本用法 + +```python +# asm5.py 示例 +from asm5 import MiniAsmInterpreter + +code = """ +set a 10 +set b 20 +add a b +print a +""" + +interp = MiniAsmInterpreter() +interp.load(code) +interp.run() +``` + +### 调用 Python 模块 + +```python +interp = MiniAsmInterpreter(search_paths=['/path/to/modules']) +interp.load("import os\ncall os.path.exists /path/to/file result\nprint result") +interp.run() +``` + +### 数据类型示例 + +```python +# 字符串操作 +set s "Hello" +call s upper +print s + +# 列表操作 +set lst [1, 2, 3] +call lst append 4 +print lst +``` + +## Brainf**k 代码示例 + +``` +[?>] : 循环发送 RAM 中的字符到 COM1 Tx,直到遇到 0 +% : 执行命令 +[#.] : 循环从 COM1 Rx 读取并输出,直到缓冲区空 +``` + +## 运行流程 + +1. 初始化硬件组件(RAM、磁盘、串口等) +2. 加载程序到内存 +3. 执行 RAM 操作和控制流 +4. 执行磁盘和 COM1 操作 +5. 程序结束,保存磁盘状态 + +## 依赖 + +- Python 3.x + +## 许可证 + +请查看 LICENSE 文件了解详细信息。 \ No newline at end of file