Files
hakorune/docs/guides/macro-box.md

1.7 KiB
Raw Blame History

MacroBox — User Extensible Macro Units

Status: Stable for user macros via Nyash runner routeselfhosting優先

Goals

  • Allow users to implement procedural macro-like expansions in pure Nyash (future) and Rust (prototype) with a clean, deterministic interface.
  • Preserve “Box independence” (loose coupling). Operate on public interfaces; avoid relying on private internals.

Enabling

  • Macro engine is ON by default. Disable with NYASH_MACRO_DISABLE=1.
  • Register user macro files via NYASH_MACRO_PATHS=path1,path2.
  • Runner routeNyashでの実行が既定。内部子ルートは非推奨NYASH_MACRO_BOX_CHILD_RUNNER=0 でのみ強制)。

APINyash, 推奨)

  • Box: static box MacroBoxSpec { static function expand(json[, ctx]) -> json }
  • Contract: AST JSON v0docs/reference/ir/ast-json-v0.md。ctxはJSON文字列MVP: {caps:{io,net,env}})。
  • 実行: エンジンは登録されたファイルをNyashランナールートで呼び出し、展開JSONを受け取る。

APIRust, 内部/開発者向け)

  • Trait: MacroBox { name() -> &'static str; expand(&ASTNode) -> ASTNode }
  • 用途: 最小のビルトイン(例: deriveやプロトタイピング。一般ユーザーはNyash版を使う。

Constraints (philosophy)

  • Deterministic; side-effect free by defaultio/net/envはcapabilitiesで明示
  • Respect “Box independence”: 公開インターフェースを基本に、疎結合を維持

Built-in example

  • UppercasePrintMacro開発者向けの内蔵例。通常はNyashで実装されたユーザーマクロを使用。

Future plan

  • nyash.toml による登録/パッケージ管理
  • Attribute-style macros@derive/@lintの整理