dscribt ~codex-rjjjm5

A lightweight embeddable scripting language runtime implemented in D.


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Dscribt

Dscribt は、D言語で実装され、Dアプリケーションへ組み込みやすい軽量スクリプト言語ランタイムです。

今回強化した点

  • D配列をそのまま受け渡し: int[] などの D の配列を Value.from で Script 配列へ変換
  • D構造体 / Dクラスを扱える: Value.reflect で公開フィールドを Script 側のテーブルとして公開
  • Script側の関数定義: fn name(args) { ... } と無名関数 fn(args) { ... } に対応
  • D風の連結演算子: 文字列/配列の連結は + ではなく ~
  • Luaライクなテーブル: { key = value } リテラル、table.keytable["key"]、代入に対応
  • テーブルの演算子オーバーロード: opBinary<演算子> / opBinaryRight<演算子> を定義可能
  • UFCS + ダックタイピング + メソッドチェーン: obj.method(x) はメソッドか同名関数に委譲
  • 制御構文: if/elsewhileforforeachswitch/case/defaultbreakcontinue
  • 三項演算子: condition ? truthyValue : falsyValue
  • 実行時エラー情報強化: runSafe で失敗時メッセージと簡易スタックトレースを取得
  • モジュール機構: ホスト側 registerModule と script 側 require("name") で読み込み/キャッシュ
  • 最小標準ライブラリ: string, math, table, time, json を同梱
  • 軽量実装: lexer / parser / AST / interpreter のシンプルな構成

サポートする構文

fn buildProfile(label, total) {
    return {
        label = label,
        total = total,
        name = label,
        opBinary~ = fn(self, rhs) {
            return buildProfile(self.label ~ rhs.label, self.total + rhs.total);
        }
    };
}

let numbers = [4, 6, 8];
let profile = buildProfile(stats.name, sum(numbers));
let chained = (profile ~ buildProfile("-elite", 2)).suffix("-v1").suffix("-final");
let grade = chained.total > 10 ? "high" : "normal";

for (let i = 0; i < 3; i = i + 1) {
    if (i == 1) { continue; }
}

switch (grade) {
    case "high":
        return chained.name;
    default:
        return "fallback";
}

使える主な要素

  • let
  • return
  • fn name(...) { ... }
  • fn(...) { ... } 無名関数
  • if (...) ... else ...
  • while (...) ...
  • for (init; condition; increment) ...
  • foreach (item; iterable) ...
  • switch (...) { case ...: ... default: ... }
  • break / continue
  • 三項演算子 condition ? a : b
  • 文字列/配列連結 a ~ b
  • 配列リテラル []
  • テーブルリテラル { key = value }
  • プロパティ参照 table.key
  • 添字参照 array[0], table["name"]
  • UFCS 呼び出し value.method(arg)(同名関数への委譲を含む)
  • メソッドチェーン value.f().g().h()
  • テーブル演算子オーバーロード opBinary~ など
  • 変数 / プロパティ / 添字への代入
  • 算術演算 / 比較演算 / 等価比較

埋め込み例

auto engine = new ScriptEngine();
engine.bind("baseDamage", Value.from([4, 6, 8]));
engine.bind("stats", Value.reflect(stats));
engine.bind("player", Value.reflect(player));
engine.bindNative("sum", (scope const(Value)[] args) {
    long total;
    foreach (value; args[0].arrayValue) {
        total += value.toInt();
    }
    return Value.from(total);
});
auto outcome = engine.runSafe(scriptSource);
if (!outcome.ok) {
    // outcome.errorMessage
    // outcome.stackTrace
}
engine.registerModule("combat", q{
    let stats = { base = 40 };
    return stats;
});

auto value = engine.run(q{
    let combat = require("combat");
    return combat.base + math.abs(-2);
});

設計方針

  1. D 側が主役: 重い標準ライブラリより、ホストアプリの値や関数を公開する設計を優先
  2. Value を境界型にする: D と Script の間で型変換の責務を明確化
  3. 小さく拡張しやすい: 将来的にクロージャ、最適化、メタテーブル的拡張へ育てやすい構造

今後の拡張候補

  • reflected class/struct への書き戻し
  • メソッド呼び出しの自動ブリッジ
  • エラーメッセージの改善
  • bytecode / VM 化による高速化
Authors:
  • runner
Dependencies:
none
Versions:
~xhthfp-codex/evaluate-dscribt-for-d-language-lua-alternative 2026-Mar-25
~x6xc68-codex/enable-method-access-for-d-structs-in-dscript 2026-Mar-24
~so9w2e-codex/identify-missing-features-compared-to-lua 2026-Mar-25
~push-zmwzqutlnpll 2026-Mar-24
~o9annz-codex/list-three-features-to-add 2026-Mar-24
Show all 15 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 0 downloads total

Score:
0.0
Short URL:
dscribt.dub.pm