Skip to content

fix(ja-notation): walk the syntax trees with a stack of their own - #2271

Merged
smalruby3-editor-bot[bot] merged 10 commits into
topic/japanese-mode-1731from
fix/ja-notation-iterative-walk
Nov 19, 2026
Merged

smalruby3-editor-bot[bot] merged 10 commits into
topic/japanese-mode-1731from
fix/ja-notation-iterative-walk

Conversation

@smalruby3-editor-bot

Copy link
Copy Markdown

Summary

日本語モード EPIC #1731 の確認ページの答え L4(J3・J4 の申し送り)。ja-notation の構文木をたどる処理を再帰でなくし、数千項の式・数千重ねたかっこ・深いブロックでも呼び出しのスタックが尽きないようにする。

重ねて作っています: #2269(W3)のブランチの上に作ったので、#2263 → #2267 → #2269 → この PR の順にマージしてください。

Changes Made

  • src/run.js(新規): 構文木をたどる処理をジェネレーターで書き、子をたどるところで yield すると、run が自前のスタック(配列)で動かす。誤りの throw は待っている親に投げ戻す。each は並び(引数・要素)を順にたどる。
  • ジェネレーターにしたもの: parse.js の式の読み方(ExpressionReader の再帰下降すべて)、build-ruby.js(式・文・ブロック)、write-ruby.js(式・文)、write-ja.js(式・文・ブロック)、read-ruby.js(式・文・ブロック・raw の中の読み)。build-ruby.js の walkStatements / walkExpressions と、settings.js の添字のずらし(dropsToRuby / dropsToJa の相互再帰)はくり返しにした。
  • 表(calls など)に渡す helpers.expression は、run を通す同期の関数のまま(表の書き方は変えない)。
  • scratch-gui の dncl-to-ruby.js / ruby-to-dncl.js の RangeError の受け止めは残し、説明を直した(Ruby → 日本語では prism 自身が構文木を JavaScript に読み出すところで再帰するため、数千項の + は prism が解析できない)。
  • docs: docs/dncl/README.md(構文木をたどる処理・ファイル表・テスト一覧)、packages/ja-notation/README.md(守ること)。
日本語 → Ruby(これまで) N = 3000 N = 10000
+ を N 項 RangeError → 通る
かっこを N 重 RangeError(1000 でも) → 通る
もし を N 重(標準の形・DNCL 風の形) RangeError → 通る

(この PR の後は、手元の確かめで N = 10000 まで全部通った。)

Test Coverage

  • packages/ja-notation/test/deep.test.js(新規): 5000 項の + / and、5000 重のかっこ・右に深い式・符号・not・呼び出し・添字、1000 重のブロック(両方の形)、writeJa の書き戻し、Ruby → 日本語の 1000 項の +・3000 個の &&・1000 個の elsif(prism が解析できる範囲)。直す前のソースでは 13 件中 9 件が落ちることを確かめた。繰り返しで作るプログラムなので JSON データには入れていない。
  • test/unit/lib/dncl/dncl-long-program.test.js(新規): スモウルビーの表を通して同じことを確かめる。
  • ゴールデン(UPDATE_JA_GOLDEN=1): 差なし(動きは変えていない)。

Closes #2270

🤖 Generated with Claude Code

smalruby3-editor-bot Bot and others added 10 commits November 19, 2026 07:54
…them

The Japanese mode is written for a program that runs alone, as on a
terminal, and has no stage. Ruby → Japanese tells each `$a` (a variable or
a list of the stage) at its name as `stage-variable`, which the editor says
as "ステージの変数($a)は日本語モードでは書けません。Ruby モードでは書けます。",
in place of the no-ja-form error of the whole statement.

Closes #1706

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Out of the strings and the comments, the lexer makes the full-width
letters and digits half-width before it reads them (`x1` is `x1`, `and`
is `and`), as the auto-correct of the Ruby mode does. A full-width letter is
no longer a name of its own (the decision of #1714 is taken back). The
strings and the comments keep what is written.

Closes #2266

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A block of the DNCL-like form closed by the closing word of another kind
(a もし closed by を繰り返す, a counting loop by を実行する) is now an error
(`closer-kind`) that says the word of the block, even when the Ruby could
be made: the students are told as much as can be. A block forgotten inside
the one the word closes is told as not closed. `end` still closes any.

Closes #2268

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The walks of the syntax trees (the reading of the Japanese, the building
and writing of the Ruby, the reading of the prism tree and the writing of
the Japanese) were recursive, so an expression of some thousands of terms,
a chain of parentheses or signs, or blocks deep in blocks ran out of the
call stack. They are generators now, run by `run` (src/run.js) with a
stack of its own; the index shift of the settings counts in a loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…an error

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

🚀 Preview deployed (stg API): https://preview.smalruby.app/fix-ja-notation-iterative-walk-77b6d97c/

This branch was previously deployed

1 inactive deployment
stg — fae6e324 Deployed Nov 19, 2026 by smalruby3-editor-bot[bot] via cleanup #444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants