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.1.tar.gz
(50.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.1.tar.gz.
File metadata
- Download URL: hpl_runtime-1.0.1.tar.gz
- Upload date:
- Size: 50.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 |
f8ff19e1c557688483ea9f3026af11d042dfde9e6f3594d4ecca89952a2e8205
|
|
| MD5 |
cba893c6fa11945df616e84f442357fa
|
|
| BLAKE2b-256 |
f2d6684f6cf364aa28d15cfd7ca18cb69bc78520fbe3ee0c70fbed4b513f937e
|
File details
Details for the file hpl_runtime-1.0.1-py3-none-any.whl.
File metadata
- Download URL: hpl_runtime-1.0.1-py3-none-any.whl
- Upload date:
- Size: 37.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 |
178a43cc83b5623c73cb4c856c901db24743003e11322f7b25c529b497a5e59b
|
|
| MD5 |
5fbe8114ad1b1f061cc2389c76728252
|
|
| BLAKE2b-256 |
ed87196ff44d68743e2c959dbe3104f98702e0adb1dfa63fc6a68e2609e29fb0
|