HPL (H Programming Language) 是一种基于 YAML 格式的面向对象编程语言的运行器
Project description
HPL
HPL(H Programming Language)是一种基于 YAML 格式的面向对象编程语言,使用动态类型系统。它提供简洁的语法和强大的功能,适合快速原型开发和脚本编写。
特性
- 基于 YAML:使用人类可读的 YAML 格式编写代码
- 面向对象:支持类、继承和对象实例化
- 动态类型:无需显式类型声明
- 丰富的数据类型:整数、浮点数、字符串、布尔值、数组、字典
- 控制流:条件语句、循环(for、while)、异常处理
- 模块系统:支持文件包含和标准库导入
- 标准库:内置 math、io、json、os、time 等模块
- 错误处理:详细的错误信息和调用栈跟踪
安装
系统要求
- Python 3.6+
- PyYAML
安装步骤
-
克隆仓库:
git clone https://github.com/TheSingularityStudio/HPL.git cd hpl
-
安装依赖:
pip install -r requirements.txt
-
(可选)构建可执行文件:
python build_exe.py
快速开始
运行示例
运行内置示例:
python hpl_runtime/interpreter.py examples/example.hpl
编写第一个 HPL 程序
创建文件 hello.hpl:
main: () => {
echo "Hello, HPL!"
}
call: main()
运行程序:
python hpl_runtime/interpreter.py hello.hpl
语法文档
解释器架构
HPL 解释器采用模块化设计,使用 Python 实现。架构包括以下组件:
models.py: 定义数据模型,如 HPLClass、HPLObject、HPLFunction 等,用于表示类、对象和函数。lexer.py: 词法分析器,将源代码字符串转换为 Token 列表,支持行号和列号跟踪。parser.py: 使用 PyYAML 解析 YAML 文件,将其转换为内部表示形式。ast_parser.py: AST 解析器,将 Token 列表转换为抽象语法树。evaluator.py: 执行解析后的结构,处理方法调用、控制流和内置函数。interpreter.py: 主入口点,加载 YAML 文件,初始化组件并运行程序。
项目结构
hpl/
├── hpl_runtime/ # 运行时核心
│ ├── core/ # 核心组件
│ │ ├── models.py # 数据模型
│ │ ├── lexer.py # 词法分析器
│ │ ├── parser.py # 解析器
│ │ ├── ast_parser.py # AST 解析器
│ │ ├── evaluator.py # 执行器
│ │ └── __init__.py
│ ├── modules/ # 模块系统
│ │ ├── base.py # 基础模块
│ │ ├── loader.py # 模块加载器
│ │ └── package_manager.py # 包管理器
│ ├── stdlib/ # 标准库
│ │ ├── io.py # IO 模块
│ │ ├── json_mod.py # JSON 模块
│ │ ├── math.py # 数学模块
│ │ ├── os_mod.py # OS 模块
│ │ └── time_mod.py # 时间模块
│ └── utils/ # 工具函数
│ └── exceptions.py # 异常处理
├── examples/ # 示例程序
│ ├── example.hpl # 基础示例
│ ├── guess_number.hpl # 猜数字游戏
│ └── test_*.hpl # 测试文件
├── docs/ # 文档
│ ├── HPL语法概览.md # 语法概览
│ └── HPL语法手册.md # 详细手册
├── tests/ # 测试套件
├── build_exe.py # 可执行文件构建脚本
├── build_package.py # 包构建脚本
├── hpl_launcher.py # 启动器
├── pyproject.toml # 项目配置
├── requirements.txt # 依赖列表
├── LICENSE # 许可证
└── README.md # 项目说明
示例程序
基础示例
classes:
Calculator:
add: (a, b) => {
return a + b
}
objects:
calc: Calculator()
main: () => {
result = calc.add(10, 20)
echo "Result: " + result
}
call: main()
使用标准库
imports:
- math
- io
main: () => {
# 使用数学模块
pi = math.PI
sqrt_result = math.sqrt(16)
echo "PI: " + pi
echo "sqrt(16): " + sqrt_result
# 使用 IO 模块
io.write_file("output.txt", "Hello from HPL!")
content = io.read_file("output.txt")
echo "File content: " + content
}
call: main()
更多示例请查看 examples/ 目录。
贡献
欢迎贡献代码!请遵循以下步骤:
- Fork 本仓库
- 创建特性分支:
git checkout -b feature/new-feature - 提交更改:
git commit -am 'Add new feature' - 推送分支:
git push origin feature/new-feature - 提交 Pull Request
开发环境设置
# 安装开发依赖
pip install -r requirements-dev.txt
# 运行测试
python -m pytest tests/
# 运行特定测试
python tests/run_tests.py
许可证
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hpl_runtime-1.1.0.tar.gz.
File metadata
- Download URL: hpl_runtime-1.1.0.tar.gz
- Upload date:
- Size: 61.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3122b652ac40eeebfbdbfecffe9b76a41caf80449d26258e4d73f16fbe700da
|
|
| MD5 |
70b51999c096bb1f95d1a7f405f76fda
|
|
| BLAKE2b-256 |
e9b0a80c1a7416e9c1dfc170576af08758016981d72674d94beee5da8b44c975
|
File details
Details for the file hpl_runtime-1.1.0-py3-none-any.whl.
File metadata
- Download URL: hpl_runtime-1.1.0-py3-none-any.whl
- Upload date:
- Size: 49.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba0774408679ca28ff931b10067787f368e4239bc0fed99f06f1eb76b0d7ca9f
|
|
| MD5 |
59611bc920d7156976e4d18313eb8e53
|
|
| BLAKE2b-256 |
a050a13d13bba6adb2c91d55a19567141acf6168bcdb74ea8a96f95be1b22dbe
|