Skip to content
26 changes: 13 additions & 13 deletions docs/dncl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
| ファイル | 役割 |
|---|---|
| `dncl-to-ruby.js` | 日本語 → Ruby 変換のエントリポイント(`dnclToRuby`)。ja-notation(下の「日本語 → Ruby の変換(ja-notation)」)で読んで Ruby と行の対応を作り、誤りを `ja-notation-errors.js` で日本語モードの文言にする |
| `ja-notation-table.js` | ja-notation に渡すスモウルビーの表(`表示する` → `console.puts`、大文字の名前 → `@_var_X_` / `@_array_X_`、入力 → `console.gets` など。`console` はコンソールの拡張機能のブロック)と、その逆向きの表(`SMALRUBY_RUBY_TO_JA`: `console.puts` / `puts` / `say` / `p` / `print` → `表示する`、`.to_i` → `整数` など、`@_array_X_` → `X`、`console.gets` の代入と、`ask("")` と次の行の `answer` → 入力)。組み込み関数の一覧 `DNCL_BUILTIN_NAMES`(生成AI に相談の質問文の命令の例にも使う) |
| `ja-notation-table.js` | ja-notation に渡すスモウルビーの表(`表示する` → `console.puts`、大文字の名前 → `@_var_X_` / `@_array_X_`、入力 → `console.gets` など。`console` はコンソールの拡張機能のブロック)と、その逆向きの表(`SMALRUBY_RUBY_TO_JA`: `console.puts` → `表示する`、`.to_i` → `整数` など、`@_array_X_` → `X`、`console.gets` の代入 → 入力。「言う」の `say` / `puts` / `p` / `print` と「聞いて待つ」の `ask` / `answer` は持たない)。組み込み関数の一覧 `DNCL_BUILTIN_NAMES`(生成AI に相談の質問文の命令の例にも使う) |
| `ja-notation-errors.js` | **ja-notation の誤りを、日本語モードで見せる文言にする対応表**(日本語 → Ruby の `jaNotationErrors`、Ruby → 日本語の `rubyErrorWords`)。下の「誤りの文言」 |
| `dncl-indent.js` | **字下げの幅の数え方**(`indentWidth`: タブは 4 桁・全角空白は 2 桁、`indentOffset`: ある幅の位置の字下げの文字)。読むモードが使う(ja-notation の読み方も同じ数え方) |
| `dncl-problem-form.js` | **読むモードの配置**(`layoutProblemForm`: 行ごとの行番号 `(01)` と罫線の位置・種類、`problemFormText`: 行番号と罫線を文字で付けた形)。エディタの描画(`dncl-problem-form-view.js`)と「行番号と罫線つきでコピー」が使う |
Expand Down Expand Up @@ -459,22 +459,22 @@ Smalruby のルビータブ(Ruby / ふりがな)と日本語タブ(DNCL)
| **DNCL** `x = 【外部からの入力】` → Ruby / Block | `@x = console.gets` / `x を (入力する) にする` | `【整数を入力】` は `console.gets.to_i` / `整数を入力する`(全角の数字も数として読む) |
| Block `smalrubyConsole_print` / `smalrubyConsole_input` / `smalrubyConsole_inputInteger` → **DNCL** | `表示する(...)` / `【外部からの入力】` / `【整数を入力】` | 前の作品の `((入力する) の切り捨て)` も `【整数を入力】` |
| Block `looks_sayforsecs(msg, n)` → **Ruby** | `say(msg, n)` | 秒数を保持 |
| Block `looks_sayforsecs(msg, n)`(Ruby の `puts` / `say`)→ **DNCL** | `表示する(msg)` | **秒数 n は無視**。もう一度日本語から変換すると、コンソールのブロックになる |
| Block `sensing_askandwait("")` + 次の `答え` の代入 → **DNCL** | `【外部からの入力】` | コンソールの前の日本語のプログラム。もう一度日本語から変換すると、コンソールのブロックになる |
| Block「言う」(Ruby の `say` / `puts` / `print` / `p`)→ **DNCL** | 誤り | 日本語タブでは使えないブロック。ルビータブのその行に「「言う」は日本語タブでは使えないブロックです。コンソールの「(…) を表示する」を使ってください。」を出す |
| Block「聞いて待つ」「答え」(Ruby の `ask` / `answer`)→ **DNCL** | 誤り | 同じく「「聞いて待つ」と「答え」は日本語タブでは使えないブロックです。コンソールの「入力する」を使ってください。」 |

### 不可逆な変換とその理由
### 「言う」「聞いて待つ」を日本語にしない理由

#### 1. Block `say(msg, n)` → DNCL `表示する(msg)` (秒数喪失)
**理由**: 日本語の `表示する` はキャラクターの吹き出しではなく、コンソールへの出力を意味します。「言う」も `表示する` と書くと、
同じ `表示する(...)` が「ステージの吹き出し」と「コンソール」の 2 つの意味を持ち、生徒が混乱します(日本語タブではステージが
コンソールに隠れているので、「言う」は出ないように見える)。`【外部からの入力】` と「聞いて待つ」「答え」も同じです。
また、日本語タブで書き直すと「言う」がコンソールのブロックに変わり、人間が作ったブロックのプログラムを黙って変えてしまいます。

**理由**: 日本語の `表示する` は「コンソールに値を表示する」という操作で、秒数 (Scratch の吹き出し表示時間) は概念として持っていません。
**結果**: 日本語モードでは見た目と調べるのカテゴリは使えません。コードタブのパレットにも出しません(`dncl-block-filter.js`)。
「言う」「聞いて待つ」「答え」を使ったプログラムで日本語タブを開くと、`move(10)` などと同じ「日本語にできない命令」として、
ルビータブのその行に、代わりに使うコンソールのブロックを書いた誤りを出します。ブロックは変えません。
日本語タブの中に誤りの行として出す形にはしない(読めない行があると、タブの移動と旗が止まり、生徒が抜け出せなくなるため)。
日本語タブに `say(...)` などと書いたときも、同じ文を出します。

**結果**: Block で秒数を 2 にしても DNCL では `表示する(msg)` (秒数情報なし) になります。

#### 2. DNCL `表示する` → コンソールのブロック(`say` / `puts` には戻さない)

**理由**: 日本語の `表示する` はキャラクターの吹き出しではなく、コンソールへの出力を意味します。`say` は Scratch の吹き出し演出を含むため、日本語の「表示」とは概念的に異なります。

**結果**: 「言う」のブロック(Ruby の `say` / `puts`)や「聞いて待つ」と「答え」で作ったプログラムを日本語タブで編集すると、**コンソールのブロックに変わったまま戻らない**。秒数情報も失われます。
Ruby の `puts` / `gets` は、いままでどおりコンソールのブロックにはしません(ルビータブは Scratch の部分集合。`design.md` の 5 章)。

### ブロックを通ったときの並び
Expand Down
3 changes: 3 additions & 0 deletions docs/dncl/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ Ruby や日本語のプログラムの `表示する` は、キャラクター
- 画面: **ステージの上にコンソールボタン**を置き、押すとステージがコンソールに切りかわる。
- 日本語のプログラムではキャラクターをそもそも動かさないので、日本語タブではステージがコンソールになるのが最善。
- 「聞いて待つ」もコンソールの入力として振る舞う。
- 日本語タブでは「言う」「聞いて待つ」「答え」を使えない(見た目と調べるのカテゴリをパレットに出さない)。
`表示する` と `【外部からの入力】` はコンソールだけの意味にして、同じ言葉が 2 つの意味を持たないようにする。
それを使ったプログラムは、日本語にせずにルビータブの行に誤りを出す(ブロックは変えない。[README](./README.md) の変換マトリクス)。
- **コンソールでのやりとりを主にするのは日本語タブだけ**。Ruby の `puts` / `gets` はコンソールのブロックに対応させない。
Ruby タブは Scratch の部分集合(Scratch でできることを Ruby で書く)なので、コンソールのブロックを Ruby で書く形は足さない。
- 将来の案: Ruby タブに「コンソールモード」のような切りかえを置き、そのときだけ `puts` / `gets` がコンソールを使う。いまは作らない。
Expand Down
12 changes: 6 additions & 6 deletions docs/smalruby-dncl-spec.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ DNCL 風の形の `返す 式` も読みます。
表示する(not x, y) # → console.puts((!@x).to_s + @y.to_s)
```

> **重要 (不可逆性)**: `表示する(...)` と `【外部からの入力】` は、**コンソールの拡張機能**のブロック([`docs/extension-smalruby-console/`](extension-smalruby-console/README.md))になり、Ruby では `console.puts(...)` / `console.gets` と書きます(「言う」`say` / `puts` や「聞いて待つ」ではない)。`表示する` はキャラクターの吹き出しではなくコンソールへの出力だからです。「言う」や「聞いて待つ」で作ったプログラムも `表示する` / `【外部からの入力】` として読みますが、日本語から変換し直すとコンソールのブロックになります。ステージの上のコンソールのボタンで、ステージとコンソールを切りかえます。詳細は [Block ↔ Ruby ↔ DNCL 変換マトリクス](dncl/README.md#block--ruby--dncl-の変換マトリクス) を参照。
> **重要 (不可逆性)**: `表示する(...)` と `【外部からの入力】` は、**コンソールの拡張機能**のブロック([`docs/extension-smalruby-console/`](extension-smalruby-console/README.md))になり、Ruby では `console.puts(...)` / `console.gets` と書きます(「言う」`say` / `puts` や「聞いて待つ」ではない)。`表示する` はキャラクターの吹き出しではなくコンソールへの出力だからです。「言う」「聞いて待つ」「答え」は日本語タブでは使えないブロックで、それを使ったプログラムを日本語タブで開くと、ルビータブのその行に誤り(コンソールのブロックを使うように)を出します(ブロックは変えません)。ステージの上のコンソールのボタンで、ステージとコンソールを切りかえます。詳細は [Block ↔ Ruby ↔ DNCL 変換マトリクス](dncl/README.md#block--ruby--dncl-の変換マトリクス) を参照。

`【外部からの入力】` / `【整数を入力】` は代入文の右辺で使用します:

Expand Down Expand Up @@ -581,8 +581,10 @@ kamoku を 1 から 3 まで 1 ずつ増やしながら繰り返す:
### 完全に非表示のカテゴリ

- **動き(Motion)** — 非表示
- **見た目(Looks)** — 非表示(「言う」も使えない。表示はコンソールの `〜を表示する`)
- **音(Sound)** — 非表示
- **イベント(Events)** — 非表示(共通テスト用プログラム表記にはイベント概念がない)
- **調べる(Sensing)** — 非表示(「聞いて待つ」「答え」も使えない。入力はコンソールの `入力する`)

### 使用可能なブロック

Expand All @@ -591,8 +593,6 @@ kamoku を 1 から 3 まで 1 ずつ増やしながら繰り返す:
| **制御** | `〜回繰り返す`, `もし〜なら`, `もし〜でなければ`, `〜まで繰り返す` | 増やしながら繰り返す, もし, もし〜そうでなければ, 〜の間繰り返す |
| **演算** | すべて | 算術・比較・論理演算子 |
| **コンソール** | `〜を表示する`, `入力する` | `表示する()`, `【外部からの入力】` |
| **見た目** | `〜と〜秒言う`(`say for N secs`)のみ | `表示する()`(読むだけ) |
| **調べる** | `〜と聞いて待つ`(`ask`), `答え`(`answer`)のみ | `【外部からの入力】`(読むだけ) |
| **変数** | すべて(変数の作成、代入、変化、表示/非表示) | 変数代入 |
| **リスト** | すべて(追加、削除、挿入、置換、取得、検索、長さ、含む、空判定) | 配列操作(`A に 値 を追加する` など) |
| **ブロック定義** | すべて(関数定義、関数呼び出し、引数) | `関数 名前(引数):` |
Expand Down Expand Up @@ -709,7 +709,7 @@ Monaco エディタで以下の要素がハイライトされます:
| 入力 | `【外部からの入力】` → コンソールで答えを受け取る |
| スコープ | すべて Scratch のスプライト変数に変換 |
| 配列操作 | `要素数(A)`、要素の追加 `A に 値 を追加する`。Scratch のリストブロックも使用可能 |
| ブロック | コードタブでは、日本語で書けるブロック(と「〜と〜秒言う」など読むだけのもの)に絞る |
| ブロック | コードタブでは、日本語で書けるブロックに絞る(見た目・調べるのカテゴリは出さない) |
| 誤りのあるとき | 読めない行があるあいだは、ほかのタブやスプライトへ移らず、保存・提出・実行(旗・[実行])・ルビーのバージョンの切りかえもしない(書いた行が消えないように) |

### 内部実装の特徴
Expand All @@ -729,8 +729,8 @@ smalruby3-editor の日本語モードは、以下のアーキテクチャで動
- イベントハンドラ(`旗が押されたとき` 以外)
- 動き関連メソッド(`move`, `turn_right` 等)
- 音関連メソッド(`play`, `stop_all_sounds` 等)
- 見た目の大部分(`say` 以外の `think`, `show`, `hide` 等)
- 調べるの大部分(`ask`/`answer` 以外の `touching?`, `mouse.x` 等)
- 見た目(`say` / `puts` / `print` / `p` の「言う」、`think`, `show`, `hide` 等)
- 調べる(`ask` / `answer` の「聞いて待つ」「答え」、`touching?`, `mouse.x` 等)
- `self.属性 = 値` 形式のプロパティ設定
- 正規表現
- `for` / `each` ループ
Expand Down
16 changes: 16 additions & 0 deletions packages/scratch-gui/src/containers/ruby-tab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
FORBIDDEN_PREFIX_ERROR_ID,
isRubyCommandName,
POST_TEST_LOOP_ERROR_ID,
stageBlockOf,
STAGE_VARIABLE_ERROR_ID,
UNSUPPORTED_COMMAND_ERROR_ID,
UNSUPPORTED_NOTATION_ERROR_ID,
Expand Down Expand Up @@ -939,6 +940,21 @@ const RubyTab = (props) => {
{ name: err.name },
);
}
// The say block and ask and answer of the stage tell the console block to use (#2623).
if (err.id === UNSUPPORTED_COMMAND_ERROR_ID && stageBlockOf(err.name) === 'say') {
return intl.formatMessage({
id: 'gui.rubyTab.dnclSayBlockError',
defaultMessage:
'The "say" block cannot be used in the Japanese tab. Use "print (…)" of the console instead.',
});
}
if (err.id === UNSUPPORTED_COMMAND_ERROR_ID && stageBlockOf(err.name) === 'ask') {
return intl.formatMessage({
id: 'gui.rubyTab.dnclAskBlockError',
defaultMessage:
'The "ask and wait" and "answer" blocks cannot be used in the Japanese tab. Use "input" of the console instead.',
});
}
if (err.id === UNSUPPORTED_COMMAND_ERROR_ID) {
return intl.formatMessage(
{
Expand Down
10 changes: 3 additions & 7 deletions packages/scratch-gui/src/lib/dncl/dncl-block-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ const DNCL_ALLOWED_BLOCKS = [
// Operators: all
'operator_',

// Looks: say with duration (表示する of the programs before the console)
'looks_sayforsecs',

// Sensing: ask/answer (【外部からの入力】 of the programs before the console)
'sensing_askandwait',
'sensing_answer',
// No looks and sensing blocks: 表示する and 【外部からの入力】 are the console only, not
// "say" and "ask and wait" of the stage (#2623).

// Console: 表示する and 【外部からの入力】 (docs/dncl/design.md, chapter 5)
'smalrubyConsole_',
Expand Down Expand Up @@ -72,7 +68,7 @@ const isDnclAllowedBlock = (opcode) =>
/**
* Categories that are completely hidden in DNCL mode.
*/
const DNCL_HIDDEN_CATEGORIES = ['motion', 'sound', 'events'];
const DNCL_HIDDEN_CATEGORIES = ['motion', 'looks', 'sound', 'events', 'sensing'];

/**
* Check if a category is hidden in DNCL mode.
Expand Down
8 changes: 2 additions & 6 deletions packages/scratch-gui/src/lib/dncl/dncl-keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ const RUBY_LITERALS = new Set([
'true',
'false',
'nil',
'answer',
'say',
'puts',
'print',
'ask',
// `say` / `puts` / `print` and `ask` / `answer` are the blocks of the stage, which the
// Japanese tab does not have: they are told as commands, not passed through (#2623).
'rand',
'ask_and_wait',
'return',
'if',
'elsif',
Expand Down
41 changes: 38 additions & 3 deletions packages/scratch-gui/src/lib/dncl/dncl-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,49 @@ const UNSUPPORTED_COMMAND_ERROR_ID = 'unsupportedCommand';
*/
const isRubyCommandName = (name) => /^[A-Za-z_]/.test(name);

/**
* The Ruby commands of the blocks of the stage that have a console block for the
* same thing: the "say" block (`say`, and `puts` / `print` / `p`, which the Ruby
* tab makes the say block) and "ask and wait" with "answer". The Japanese tab
* does not have them, so `表示する` and `【外部からの入力】` mean only the console,
* and their errors tell the console block to use (#2623).
*/
const STAGE_BLOCK_COMMANDS = {
say: 'say',
puts: 'say',
print: 'say',
p: 'say',
ask: 'ask',
ask_and_wait: 'ask',
answer: 'ask',
};

/**
* The block of the stage a Ruby command is (`say` or `ask`), or null.
* @param {string} name - The name of the command.
* @returns {?string} `say`, `ask` or null.
*/
const stageBlockOf = (name) =>
Object.prototype.hasOwnProperty.call(STAGE_BLOCK_COMMANDS, name) ? STAGE_BLOCK_COMMANDS[name] : null;

/** The messages of the blocks of the stage the Japanese tab does not have, by `stageBlockOf`. */
const STAGE_BLOCK_MESSAGES = {
say: '「言う」は日本語タブでは使えないブロックです。コンソールの「(…) を表示する」を使ってください。',
ask: '「聞いて待つ」と「答え」は日本語タブでは使えないブロックです。コンソールの「入力する」を使ってください。',
};

/**
* The message of the unsupported command error.
* @param {string} name - The name of the command.
* @returns {string} The message.
*/
const unsupportedCommandMessage = (name) =>
isRubyCommandName(name)
const unsupportedCommandMessage = (name) => {
const stageBlock = stageBlockOf(name);
if (stageBlock) return STAGE_BLOCK_MESSAGES[stageBlock];
return isRubyCommandName(name)
? `「${name}」は日本語タブでは書けない命令です。ルビータブでは書けます。`
: `「${name}」は定義されていない関数です。「関数 ${name}(…):」で定義してください。`;
};

/**
* A line that starts with a Ruby keyword that jumps in a loop (`break`,
Expand Down Expand Up @@ -268,7 +302,7 @@ const findDeclaredNames = (source) => {
* command of a `do` block, and a keyword that jumps in a loop at the head of a
* line (`break if i == 5`, issue #1716). The names the Japanese mode has
* are the DNCL keywords and built-in functions, the Ruby names the converter
* passes as they are (`puts`, `say`, `rand`, ...) and the functions the
* passes as they are (`rand`, `to_i`, ...) and the functions the
* source defines. A variable (a name not called) is not taken, so a
* variable may have the name of a command.
* @param {string} source - The Japanese (its line numbers and `|` marks blanked out).
Expand Down Expand Up @@ -344,6 +378,7 @@ export {
isRubyCommandName,
POST_TEST_LOOP_ERROR_ID,
POST_TEST_LOOP_MESSAGE,
stageBlockOf,
STAGE_VARIABLE_ERROR_ID,
stageVariableMessage,
UNSUPPORTED_COMMAND_ERROR_ID,
Expand Down
Loading
Loading