Skip to content

feat: support Ruby symbol literals (:symbol) with $_symbols_ global list#314

Merged
takaokouji merged 11 commits intodevelopfrom
feature/ruby-symbol-support
Mar 16, 2026
Merged

feat: support Ruby symbol literals (:symbol) with $_symbols_ global list#314
takaokouji merged 11 commits intodevelopfrom
feature/ruby-symbol-support

Conversation

@takaokouji
Copy link

@takaokouji takaokouji commented Mar 15, 2026

Summary

Ruby のシンボルリテラル :symbol を Smalruby で扱えるようにする。シンボルはグローバルリスト $_symbols_ で管理し、data_itemnumoflist ブロックでインデックスを参照することで高速な比較を実現する。

Closes #313

Implementation Steps

  • Phase 1: $_symbols_ リスト管理基盤 — パーザー内でシンボル収集、Stage 上にグローバルリスト作成
  • Phase 2: シンボル参照 → data_itemnumoflist ブロック — visitSymbolNode 拡張、@ruby:symbol:foo コメント付与
  • Phase 3: :symbol.to_s サポート — operators.js'symbol' レシーバ追加
  • Phase 4: say/puts/p/print でシンボル暗黙変換 — 各ハンドラでシンボル引数の暗黙文字列変換
  • Phase 5: シンボル非対応メソッドのエラーメッセージ — symbolNeedsToS エラー追加、ロケール追加
  • Phase 6: Blocks → Ruby ジェネレーター — @ruby:symbol コメント検出、シンボルリテラル・$_symbols_ 出力
  • Phase 7: ふりがな対応 — _handleSymbolNodeシンボル「foo」 表示
  • Phase 8: Integration Tests — round-trip、$_symbols_ 管理、エラーケース

Definition of Done

  • ユニットテスト pass
  • Integration テスト pass
  • lint pass
  • CI green
  • ブラウザ確認(Playwright MCP):
    • :foo.to_s を Ruby タブで入力 → エラーなしで変換、ブロック表示される
    • say(:foo) → 正常にブロック変換、「foo と言う」ブロック表示
    • $a = :foodata_itemnumoflist ブロックで変数にインデックス格納
    • :foo == :baroperator_equals で比較ブロック生成
    • move(:foo) → 「.to_s を付けてください」エラーメッセージ表示
    • self.when(:flag_clicked) → 既存動作が壊れていない
    • ブロック → Ruby 変換で :foo リテラルが正しく出力される
    • :foo にふりがな シンボル「foo」 が表示される

Test Plan

Type Timing Target
Unit tests (TDD) 各Phase で RED → GREEN シンボル収集、ブロック変換、エラー、ジェネレーター、ふりがな
Integration tests Phase 8 round-trip、$_symbols_ 管理
Browser verification CI green 後 Playwright MCP で DoD 確認

Add symbol collection mechanism (_collectSymbol) and $_symbols_ list
creation (_createSymbolsList) as foundation for Ruby symbol support.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link

takaokouji and others added 10 commits March 15, 2026 23:30
Symbols used as values (variable assignment, comparison) are converted
to data_itemnumoflist blocks referencing the $_symbols_ global list.
Add @ruby:symbol:name comments for round-trip conversion.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add symbol receiver handler for .to_s that creates operator_join("foo", "")
with @ruby:symbol:foo comment. Call _createSymbolsList at end of conversion.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Symbol arguments in display methods are implicitly converted to their
string name. E.g., say(:foo) displays "foo" with @ruby:symbol:foo comment.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When a symbol is used where .to_s is needed (e.g., move(:foo)),
show a specific error suggesting to add .to_s instead of generic
"wrong instruction" error.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Detect @ruby:symbol: comments on blocks and generate :foo (reference),
:foo.to_s (conversion), say(:foo)/think(:foo) (implicit conversion).

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
:foo displays as シンボル「foo」 with furigana rendering.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Test Ruby → Blocks → Ruby conversion for :symbol.to_s, variable
assignment, comparison, and implicit say/think conversion.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When a variable with dataType='symbol' is used in say/think/puts/p/print,
wrap it in data_itemoflist to look up the symbol name from $_symbols_ list.
Also change $_symbols_ to store names without colon prefix.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Symbols cannot be used in arithmetic (+, -, *, /, %, **) or compared
with non-symbols using >, <, >=, <=. Add specific error messages with
.to_s suggestion. Matches Ruby's NoMethodError/ArgumentError behavior.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
These files are entirely Smalruby-specific, so inline Start/End
markers are redundant. File-level markers at the top are sufficient.

Refs #313

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@takaokouji takaokouji merged commit cc3a42e into develop Mar 16, 2026
9 checks passed
@takaokouji takaokouji deleted the feature/ruby-symbol-support branch March 16, 2026 00:02
github-actions bot pushed a commit that referenced this pull request Mar 16, 2026
…symbol-support

feat: support Ruby symbol literals (:symbol) with $_symbols_ global list
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.

feat: support Ruby symbol literals (:symbol) with $_symbols_ global list

1 participant