HPL (H Programming Language) 是一种基于 YAML 格式的面向对象编程语言的运行器
Project description
HPL
一种基于YAML格式的面向对象的编程语言的运行器
解释器架构
HPL 解释器采用模块化设计,使用 Python 实现。架构包括以下组件:
models.py: 定义数据模型,如 HPLClass、HPLObject、HPLFunction 等,用于表示类、对象和函数。lexer.py: 词法分析器,将源代码字符串转换为 Token 列表,支持行号和列号跟踪。parser.py: 使用 PyYAML 解析 YAML 文件,将其转换为内部表示形式。ast_parser.py: AST 解析器,将 Token 列表转换为抽象语法树。evaluator.py: 执行解析后的结构,处理方法调用、控制流和内置函数。interpreter.py: 主入口点,加载 YAML 文件,初始化组件并运行程序。
依赖
- PyYAML: 用于解析 YAML 文件。
使用
运行解释器:python hpl_runtime/interpreter.py <hpl_file>
例如:python hpl_runtime/interpreter.py examples/example.hpl
支持的语法特性
1. 基本数据类型
- 整数:
42,0,-10 - 浮点数:
3.14,-0.5 - 字符串:
"Hello World" - 布尔值:
true,false - 数组:
[1, 2, 3]
2. 变量和赋值
x = 10
name = "HPL"
flag = true
3. 控制流
- if-else 条件语句:
if (condition) :
# then block
else :
# else block
- for 循环:
for (i = 0; i < count; i++) :
# loop body
- while 循环:
while (condition) :
# loop body
- break 和 continue:
while (true) :
if (condition) :
break
if (other_condition) :
continue
4. 逻辑运算符
&&:逻辑与||:逻辑或!:逻辑非
5. 类和对象
classes:
MyClass:
parent: BaseClass
method: (param) => {
# method body
}
objects:
myObj: MyClass()
6. 异常处理
try :
# try block
catch (error) :
# catch block
7. 内置函数
echo(message): 输出消息len(array_or_string): 获取长度int(value): 转换为整数str(value): 转换为字符串type(value): 获取类型abs(number): 绝对值max(a, b, ...): 最大值min(a, b, ...): 最小值
8. 数组操作
arr = [1, 2, 3]
first = arr[0] # 数组访问
错误处理
HPL 解释器提供详细的错误信息,包括:
- 行号和列号信息
- 调用栈跟踪
- 类型检查错误
- 未定义变量检测
- 除零错误保护
示例程序
见 examples/ 目录:
example.hpl: 主示例程序test_new_features.hpl: 新特性测试(while、逻辑运算符、break/continue)test_for_loop.hpl: 循环测试test_comment_comprehensive.hpl: 注释测试
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
hpl_runtime-1.0.2.tar.gz
(37.4 kB
view details)
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.0.2.tar.gz.
File metadata
- Download URL: hpl_runtime-1.0.2.tar.gz
- Upload date:
- Size: 37.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c0fa6023fe64505088df342b7104578b306152fff759695e9af20866fa1bb12
|
|
| MD5 |
0c191be11e9a08ab9d7abf8b6715cfdb
|
|
| BLAKE2b-256 |
0e83b6f3631afc11a2b5699377af0fbdc255a9ff3addf34b16c05f1a3089bb52
|
File details
Details for the file hpl_runtime-1.0.2-py3-none-any.whl.
File metadata
- Download URL: hpl_runtime-1.0.2-py3-none-any.whl
- Upload date:
- Size: 38.4 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 |
f24d6917aa012081445dd777cafa511427faeceef6276cdff1fcf108cdcd9d80
|
|
| MD5 |
254e8f700a8551bdeefe3dd1d6bed6ce
|
|
| BLAKE2b-256 |
5934828aa52584bd8463e48a8417b8980d26b5c6d0b5b5edbdc4ab73f247e785
|