Skip to main content

HPL (H Programming Language) 是一种基于 YAML 格式的面向对象编程语言的运行器

Project description

HPL

一种基于YAML格式的面向对象的编程语言的运行器

安装

从 PyPI 安装(推荐)

pip install hpl-runtime

从源码安装

git clone https://github.com/TheSingularityStudio/HPL.git
cd hpl-runtime
pip install -e .

解释器架构

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 模块方式
python -m hpl_runtime <hpl_file>

# 例如
python -m hpl_runtime examples/example.hpl

或者使用命令行工具(安装后可用):

hpl examples/example.hpl
# 或
hpl-run 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
  • breakcontinue
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


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.0.tar.gz (42.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hpl_runtime-1.0.0-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file hpl_runtime-1.0.0.tar.gz.

File metadata

  • Download URL: hpl_runtime-1.0.0.tar.gz
  • Upload date:
  • Size: 42.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for hpl_runtime-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3926b7dd4a635bb9f5a81b1006f1512c9c9f6b88ecbba319671291898dbb8279
MD5 bd4efe956caa3e0e7c11f6d644d268fa
BLAKE2b-256 801408ee252451789863bee8fecc5ab21a496c350cf4ac8ee1f3b612d1a7e59d

See more details on using hashes here.

File details

Details for the file hpl_runtime-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: hpl_runtime-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 32.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for hpl_runtime-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 03f285b772a9262d823fdd09a23f9aff6172fd89ddad7f4f0043159b4e21d8b7
MD5 afc96d74b173e51a1552d3dd4d207926
BLAKE2b-256 829e521ee7fdf3b6f5bac3ba2dee720b2e958fff82a2d7116bd96af6affd6cd1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page