Skip to main content

johnnybt_dsl

一台装好的 mruby 虚拟机。跑几段 Ruby,取回它产出的那个字符串 —— 就这些。

from johnnybt_dsl import compile, run

words = compile(open("dsl.rb").read(), "dsl.rb")     # 一个进程编一次
ir = run([(words, "dsl.rb"), (source, "策略.rb")], answer="MyDSL.result")

词是你自己的。 这个包不知道什么是策略、什么是因子,也不规定 IR 长什么样: 那都是拿它写 DSL 的项目说了算。写死一套词就成了只服务一个项目的库。

装上就能用:wheel 里 mruby 已经编好了,用的人不需要 GCC,也不需要 Ruby。 那两样只有我们构建 wheel 时才要。

安装

pip install johnnybt-dsl

预编译轮子,一个平台一个,abi3 绑 Python 3.11、覆盖 3.11–3.14:

x86_64 arm64
Linux manylinux_2_28(glibc 2.28 及以上) manylinux_2_28(glibc 2.28 及以上)
macOS 10.12 及以上 11.0 及以上
Windows 有 有

linux 的门槛比同系的两个 polars 插件(2.17)高一档,是因为这个包要编 C: mruby 的构建会跑它刚编出来的 mrbc,所以每一格都得在自己的架构上原生构建, 而 pypa 提供原生镜像的最老的一档是 glibc 2.28。

API

compile(source, name) -> bytes 编成字节码。字节码和解释器无关,编一次到处 load
run(chunks, answer, context) -> str 按顺序跑几段(源码或字节码),再跑 answer,返回它的字符串。context 进全局变量 $johnny_context(惯例是 JSON,基类懒解析成 JohnnyBtDSL.context)
run_file(path, before, answer) 同上,最后一段读自文件

一次 run 一个解释器:一份文件定义的东西不会漏进下一份。几段之间共用一个 编译上下文,所以局部变量跨得过去 —— 它们像一段程序,不像几份互不相干的文件。

通用能力也由本包提供,不需要宿主框架注入:

  • float64("…"):裸 VM 内置的精确标量声明 helper。
  • loads(text) / decode_float64(value):JSON IR 的保留叶子解码与有限值检查。
  • export_schema(sources):有超时的隔离词库元信息导出,不运行使用方配置。
  • builtin_sources():已安装内置词源码,供文档与源码跳转使用。

words/float64.rb 同一份文件编译进 VM 并随 wheel 发布;无两份 Ruby 定义。 这些通用能力不依赖 johnnybt。宿主只负责自己的词库发现、声明绑定和图接口。

里面有什么

件 做什么
build.rs 取 mruby(钉住某个 commit)、按 build_config.rb 编、链接 libmruby.a
src/sys.rs mruby 的 C API,手写的 extern 声明。十来个函数,不用生成器
src/shim.c mruby 里是宏的那几个(mrb_test / mrb->exc)和编译入口的跳板
src/engine.rs 解释器的生命周期,以及「跑几段、取一个字符串」
mrbgems/mruby-json/ 我们自己的 gem:JSON.generate / #to_json / JSON.parse,双向都在 C 里(解析错误带字节位)
mrbgems/mruby-dsl/ 我们自己的 gem:JohnnyBtDSL::Base —— DSL 的基类(注册、declare、逐语句 trouble、manifest/ir 信封、JohnnyBtDSL.context)。词不在这里,词归定义语言的项目

一处 mruby 的实情

mruby 开发线上的 Prism 编译器带全局状态(mrc_init_presym 写一个文件级的 offset),两个解释器同时编译会互相踩。我们钉的是稳定版,它的解析器状态都在 mrb_parser_state 里、没有这个全局 —— 但 run / compile 那把锁留着:一次求值 不到一毫秒,串行的代价是零,而下次挪版本时谁都不用再想起这件事。

构建(只有我们要)

  • Rust、C 编译器
  • Ruby —— mruby 自己的 rake 要它(brew install ruby)。解析器由 parse.y 生成,但生成好的 y.tab.c 随发布版一起来,不要 bison

构建走真 rake -m(并行):mruby 自带的 minirake 完全串行,316 个目标文件 一个一个编,32 核机器上要几分钟;rake -m 同一棵树 4 秒。没有 rake 时退回 minirake。vendor/mruby 不进 git,第一次 cargo build 自己取。

测试:vendor/mruby/build/host/bin/mruby mrbgems/run_tests.rb(gem 那侧)与 cargo test(引擎那侧)。

许可

本包 MIT(见 LICENSE)。wheel 里静态链接了 mruby(MIT,见 LICENSE-mruby, 版权归 mruby developers);两个 mrbgem(mruby-json、mruby-dsl)是本仓库自己写的, 同样 MIT。

配置元编程基类

JohnnyBtDSL::Config 是市场中立的配置对象基类,不会被注册为一门语言。 字段/嵌套/自定义词的声明同时生成行为、校验与可反射 schema。 JohnnyBtDSL::Base.entry 负责绑定入口、采集显式 IR 和定位失败语句。

class FactorConfig < JohnnyBtDSL::Config
  field :reference, type: String, positional: true, required: true,
    doc: "Factor reference"
  field :weight, type: Numeric, default: 1,
    doc: "Scoring weight", impact: :strategy
end

class ResearchConfig < JohnnyBtDSL::Config
  field :name, type: String, positional: true, required: true,
    doc: "Declaration title"
  many :factors, of: FactorConfig, item: :use, doc: "Ordered factors"
end

class ResearchDSL < JohnnyBtDSL::Base
  sample "research.rb"
  entry :research, of: ResearchConfig, doc: "Declare a research configuration"
end

research "example" do
  factors do
    use("volume.bias").weight(0.7)
  end
end

field 支持原始类型、required、nullable、默认值、min/max/enum、doc、impact、 invariants;one / many 声明嵌套;word 同时定义参数和实现; validates 的任意 Ruby 谓词在 schema 中明确标为 runtime-only。 默认字段只影响访问器,to_h 默认不写入省略值;整数不转换成浮点。 继承不共享实例默认容器,显式字段重定义要求 override: true。 word 实现块的参数默认值由 schema 注入;兼容绑定已有方法时保留其原默认行为, 不要复制一套原实现/Python 模板拥有的默认值。

Config.schema 导出配置类型;JohnnyBtDSL.manifest 导出语言入口与可达配置树, 含字段/参数说明、约束、来源、返回类型和 block 类型。JSON 是自动导出格式, 不是需要作者手动维护的另一份定义。现有收集器可使用 entry(existing: true) 和同文件 word 绑定保持原 IR,逐步迁入元编程 API。

浮点契约(0.2.0 起)

源码字面量、String#to_f/Float(...) 和 JSON 读取均使用 C locale 的 正确舍入 binary64 转换。构建时使用本仓库的 src/readfloat.c 替换上游的 近似 reader 对象,不修改 vendor 源码;禁止编译为 float32。 JSON 输出使用 17 位有效数字保证往返,保留浮点类型与负零,拒绝 NaN/Infinity。 不依赖 mruby to_s 或 sprintf 的展示文本来传输数值。

这修复配置解析和传输,不承诺 Ruby 与 Python 的不同计算表达式逐位等价, 也不会修复已经用旧引擎编译的字节码。升级引擎后重新编译 DSL,清理自己的 字节码缓存;不要未经验收改写历史配置或放宽对齐标准。

Rust 测试同时检查源码、预编译字节码和 JSON 往返,包括有符号零、subnormal、 相邻浮点数和最大有限值。发布流程也应从 wheel 安装后运行 Python 位模式测试。

Metadata

Release files for johnnybt-dsl 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for johnnybt-dsl 0.2.1
File Size Uploaded Provenance Actions
johnnybt_dsl-0.2.1.tar.gz 51.1 kB With provenance Details for johnnybt_dsl-0.2.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for johnnybt-dsl 0.2.1
File Actions
johnnybt_dsl-0.2.1-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details for johnnybt_dsl-0.2.1-cp311-abi3-win_arm64.whl
johnnybt_dsl-0.2.1-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for johnnybt_dsl-0.2.1-cp311-abi3-win_amd64.whl
johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_x86_64.whl CPython 3.11 abi3 Linux glibc 2.28+ x86-64 Details for johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_x86_64.whl
johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_aarch64.whl CPython 3.11 abi3 Linux glibc 2.28+ ARM64 Details for johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_aarch64.whl
johnnybt_dsl-0.2.1-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for johnnybt_dsl-0.2.1-cp311-abi3-macosx_11_0_arm64.whl
johnnybt_dsl-0.2.1-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details for johnnybt_dsl-0.2.1-cp311-abi3-macosx_10_12_x86_64.whl

Total release size: 4.7 MB

Release files / johnnybt_dsl-0.2.1.tar.gz

Download URL johnnybt_dsl-0.2.1.tar.gz
Size 51.1 kB
Tags Source
SHA-256 checksum
How to use checksums
210faa3d48a06fce3507ebdc7286939a74d34dfc00b418e13f87efbcd0d1aa29
BLAKE2b-256 checksum
How to use checksums
baf5bfe8c24e1ff928c7ac5a587f337cb34822ff5176ab837beb49fef7983087
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / johnnybt_dsl-0.2.1-cp311-abi3-win_arm64.whl

Download URL johnnybt_dsl-0.2.1-cp311-abi3-win_arm64.whl
Size 610.1 kB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
853ae300b3d0e93baf90fcb61319578f32552d96a1af8ce18a0c46753ecf3025
BLAKE2b-256 checksum
How to use checksums
f29915b0b452871c1d9fabf4774dfb79737adb1f415420ae098127ee25292e92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / johnnybt_dsl-0.2.1-cp311-abi3-win_amd64.whl

Download URL johnnybt_dsl-0.2.1-cp311-abi3-win_amd64.whl
Size 653.1 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
0f36bb0cd8aa7b917dd2ced393f83fc1e615ac214094d98d16a4586bb65ef477
BLAKE2b-256 checksum
How to use checksums
3e1092ef20ca573736a4a0cdaadd66ed4db0552323dc56ce0090cd1b18051205
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_x86_64.whl

Download URL johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_x86_64.whl
Size 856.5 kB
Tags CPython 3.11 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
535b6e6793c52dd0a640bb011a356ce5966b7db7e863584c257901c1f42fc8cf
BLAKE2b-256 checksum
How to use checksums
53701db307b1a5caf178916ab9e6f22a4bd02c5840f36d37a93c9e3dd8791fd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_aarch64.whl

Download URL johnnybt_dsl-0.2.1-cp311-abi3-manylinux_2_28_aarch64.whl
Size 864.4 kB
Tags CPython 3.11 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
2e4af6a7b95663baf26db1cf3e555c9d2c31c4a1f19a76a89e410db9c1430172
BLAKE2b-256 checksum
How to use checksums
0fdc5074ff46e4ea4aeced1bdf7152be935efb83ad4c0fe9c89f2d41f2d5922f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / johnnybt_dsl-0.2.1-cp311-abi3-macosx_11_0_arm64.whl

Download URL johnnybt_dsl-0.2.1-cp311-abi3-macosx_11_0_arm64.whl
Size 826.3 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a90990c594604c14df9d498dc6428c205f9267e1a9dd6976f32b3e17debad076
BLAKE2b-256 checksum
How to use checksums
4fb9a593c997afc8f2697b9414d674f9b4b353d73a3ef1f81745882782890e82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / johnnybt_dsl-0.2.1-cp311-abi3-macosx_10_12_x86_64.whl

Download URL johnnybt_dsl-0.2.1-cp311-abi3-macosx_10_12_x86_64.whl
Size 802.9 kB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
360ed05b48d7c6df7551032833b67af16b61695b67b9eb1d747ede09e728de88
BLAKE2b-256 checksum
How to use checksums
b53b914c718773b50dfd5f7f0af3eb449540c200351adf426b0e4ec3318057ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Security for johnnybt-dsl 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for johnnybt_dsl-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page