fix(dncl): treat / and ÷ as integer division#666
Merged
Conversation
…/int) Background: DNCL is taught alongside Python in 共通テスト curricula where \`/\` is integer division for integer operands. Previously DNCL \`/\` mapped to Ruby's float-returning \`/\`, so \`(hidari + migi) / 2\` produced 4.5 instead of 4 — breaking the canonical binary-search example. scratch-vm cannot be modified; the runtime \`/\` always returns float. Fix this entirely on the conversion layer: - \`.to_i\` now compiles to \`operator_mathop(floor, x)\` (was a no-op \`operator_add(x, 0)\`). The \`@ruby:method:to_i\` comment marker preserves the source method name on round-trip. Legacy projects saved with the old \`operator_add(x, 0) + @ruby:method:to_i\` shape still emit \`.to_i\` via the existing compat path in operators-math-gen.js (one-way migration). - DNCL \`/\` and \`÷\` (and existing \`//\`) all wrap their division as \`(a / b).to_i\` so the runtime truncates via the floor mathop. A new \`wrapIntegerDivisions\` helper walks the converted Ruby line and wraps each top-level \`/\` while skipping operands inside strings, balanced parens, and already-wrapped \`(... / ...).to_i\` shapes. - Ruby → DNCL strips \`(EXPR / EXPR).to_i\` back to \`EXPR / EXPR\` so the \`.to_i\` doesn't surface as redundant \`整数(...)\` in the round-trip. - Drop hardcoded (200, 0) on say/puts comments — the natural-anchor capture from PR #665 places them correctly now. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
🚀 Preview deployed: https://smalruby.jp/smalruby3-editor/fix/dncl-integer-division/ |
The two occurrences of '+ 6' computed offsets past the literal strings ').to_i' (in wrapIntegerDivisions, where the cursor was at ')') and '.to_i' (in stripIntegerDivisionToI, where the cursor was past ')'). Replace both with .length references to the explicit string literals so the intent is obvious.
github-actions Bot
pushed a commit
that referenced
this pull request
May 10, 2026
…ger-division fix(dncl): treat / and ÷ as integer division
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DNCL の
/と÷を「整数同士の割り算は整数を返す (Ruby/Python の慣習)」に修正します。共通テスト DNCL 例題のようなaida = (hidari + migi) / 2が4.5ではなく4になります。scratch-vm の
/は常に float を返す仕様 (変更不可) なので、変換層で対応します。実装方針 (Issue 議論の E 案)
.to_iのブロックマッピングをoperator_mathop(floor)に変更 (旧:operator_add(x, 0)で truncate せず実質 no-op)。@ruby:method:to_iコメントマーカーで round-trip 時に.floorではなく.to_iとして復元。/と÷を Ruby(a / b).to_iにラップ。新ヘルパーwrapIntegerDivisionsがトップレベル/を検出して括弧バランスを取りながら左右オペランドを特定。(EXPR / EXPR).to_iをEXPR / EXPRに戻す (stripIntegerDivisionToI)。operator_add(x, 0)+@ruby:method:to_i) を読み込み → ブロック→Ruby.to_i(既存 generator で対応)。Ruby.to_i→ 新ブロック (operator_mathop(floor))。一方向 migration。例
aida = (hidari + migi) / 2@aida = ((@hidari + @migi) / 2).to_iaida = (hidari + migi) / 2a = 10 / 3@a = (10 / 3).to_ia = 10 / 3a = b / c * d@a = (@b / @c).to_i * @da = b / c * da = b + c / d@a = @b + (@c / @d).to_ia = b + c / da = Data[i + 1] / 2@a = (@_array_Data_[@i + 1] / 2).to_ia = Data[i + 1] / 2Test plan
operator_divideの親がoperator_mathop(floor)+@ruby:method:to_iに/,÷,//がすべて(a / b).to_iにラップされる.to_iラッパーが除去される🤖 Generated with Claude Code