Commit f684a406

f684a406e399Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified

Description

Update PLAN.md — sp_String design: gradual introduction, UTF-8, pointer type

Design decisions:

  • Gradual: MONO strings stay const char *, sp_String only for POLY/GC
  • UTF-8 fixed (no encoding field)
  • Literals stay const char *, conversion on demand
  • Pointer type (GC heap allocated)
  • No COW initially
  • 4-step implementation plan

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
f684a406e399
Parent
695bdcc4ba56

PLAN.mdmodifiedView file

@@ -379,13 +379,68 @@
379379 - ✅ bimorphicダックタイピング (2クラス)
380380 - ✅ nilable変数
381381
382-### 次の3ステップ
382+### 完了した3ステップ
383+- ✅ 異種Hash (sp_RbHash)
384+- ✅ POLY算術 (sp_poly_add/sub/mul/div/gt/lt/eq)
385+- ✅ Megamorphic dispatch (3型以上 → dispatch関数)
386+
387+---
388+
389+## sp_String設計
390+
391+### 方針: 段階的導入
392+
393+MONO文字列は`const char *`のまま。POLY/GC必要な場面のみsp_Stringを使用。
394+
395+```
396+MONO文字列 (型確定) → const char * (現行通り、変更なし)
397+POLY/GC文字列 → sp_String * (新規、GC管理)
398+リテラル "hello" → const char * (変換は必要時のみ)
399+```
400+
401+### sp_String構造体
402+
403+```c
404+typedef struct {
405+ char *data; // ヒープ割り当てバッファ (NUL終端)
406+ int64_t len; // バイト長
407+ int64_t cap; // バッファ容量
408+} sp_String; // ポインタ型 (GCヒープ上)
409+```
383410
384-| # | 内容 | 設計 |
385-|---|------|------|
386-| 1 | **異種Hash** `{a: 1, b: "str"}` | sp_RbHashでキー/値をsp_RbValueで保持 |
387-| 2 | **POLY算術** (+, -, *, / on sp_RbValue) | タグcheckでInt/Float分岐、String連結対応 |
388-| 3 | **Megamorphic dispatch** (3型以上) | メソッド名ごとにdispatch関数を自動生成 |
411+- エンコーディング: UTF-8固定 (フィールド不要)
412+- COW: なし (初期段階)
413+- GC: sp_gc_alloc + ファイナライザでdata解放
414+
415+### 操作
416+
417+```c
418+sp_String *sp_String_new(const char *s); // const char * → sp_String
419+const char *sp_String_cstr(sp_String *s); // sp_String → const char * (読み出し)
420+sp_String *sp_String_concat(sp_String *a, sp_String *b);
421+sp_String *sp_String_from_cstr(const char *s); // コピー作成
422+void sp_String_append(sp_String *s, const char *t); // ミュータブル追加
423+int64_t sp_String_length(sp_String *s);
424+```
425+
426+### MONO↔sp_String境界
427+
428+```c
429+// const char * → sp_String (MONO→POLY境界で変換)
430+sp_String *sp_String_from_cstr("hello");
431+
432+// sp_String → const char * (puts等のstdio境界で変換)
433+fputs(sp_String_cstr(s), stdout);
434+```
435+
436+### 実装ステップ
437+
438+| # | 内容 |
439+|---|------|
440+| 1 | sp_String構造体 + new/cstr/concat/length + GC統合 |
441+| 2 | SPINEL_TYPE_MUTABLE_STRING + 型推論 (<<, replace等で昇格) |
442+| 3 | String#<< のミュータブル実装 (現在は再代入で代用) |
443+| 4 | 既存メソッドのsp_String対応版 (upcase, downcase等) |
389444
390445 ---
391446