0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ブラウザ上で日本語をJavaScriptに変換して実行するランタイムを作った

0
Posted at

はじめに

ブラウザ上で、日本語の自然言語っぽい記述をJavaScriptに変換して実行するランタイムを作りました。

正確には「JavaScriptが日本語になった」というより、
日本語風のDSLをJavaScriptに変換して実行する仕組みです。


できること

例えば以下のように書くと動きます。

<script type="text/nl">
クリックしたら背景を赤くする
</script>

仕組み

内部では以下の流れになっています。

  1. <script type="text/nl"> を取得
  2. 日本語テキストを簡易パーサーで解析
  3. JavaScriptコードに変換
  4. new Function() で実行

実装イメージ

document.querySelectorAll('script[type="text/nl"]').forEach(el => {
  const nl = el.textContent;
  const js = compile(nl);
  new Function(js)();
});

対応している表現(現状)

  • クリックしたら〜する
  • ○秒ごとに〜する
  • 背景を色にする

かなり限定的なルールベース変換です。


なぜ作ったか

「コードを書く」というより
「意図をそのまま書く」UIの実験として作りました。

LLM時代の前段階として、
自然言語とコードの境界を雑に触ってみた感じです。


限界

  • 日本語の自由度は低い
  • 曖昧な表現には弱い
  • 本質的にはルールベース変換

つまり「言語」ではなく「変換器」です。


リポジトリ

こちらから実際のソースコードを見ることができます。
デモはGitHub Pagesでそのまま動くようにしています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?