📚 feat: docs/reference構造完成 + P2P実装Issue作成
🗂️ 重要ドキュメントをdocs/reference/に整理: - language-reference.md (言語仕様完全リファレンス) - override-delegation-syntax.md (デリゲーション構文仕様) - design-philosophy.md (明示的デリゲーション革命) - builtin-boxes.md (ビルトインBox型リファレンス) 🌐 P2P_IMPLEMENTATION_ISSUE.md作成: - AI大会議仕様完全準拠 - 既存実装との違い明確化 - 段階的実装計画 (IntentBox→MessageBus→P2PBox) - 包括的テスト要件 - Copilot実装用詳細仕様 🔄 docs/README.md更新: - reference/構造反映 - アクセシビリティ向上 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
254
P2P_IMPLEMENTATION_ISSUE.md
Normal file
254
P2P_IMPLEMENTATION_ISSUE.md
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
# 🌐 P2PBox完全実装 - AI大会議仕様準拠
|
||||||
|
|
||||||
|
## 📋 Issue概要
|
||||||
|
|
||||||
|
**目標**: NyaMeshP2Pライブラリ実現のためのP2P通信システムを、AI大会議で決定した最新仕様に従って完全実装する
|
||||||
|
|
||||||
|
**重要**: 既存の `src/boxes/intent_box.rs` と `src/boxes/p2p_box.rs` は**古い設計**のため、**完全に作り直し**が必要
|
||||||
|
|
||||||
|
## 🎯 AI大会議決定事項
|
||||||
|
|
||||||
|
### ✅ 採用仕様
|
||||||
|
- **構造化IntentBox**: `name` + `payload` 形式のメッセージBox
|
||||||
|
- **個別送信のみ**: `send(to, message)` 固定API
|
||||||
|
- **明示的デリゲーション**: `from Parent.method()` 統一構文
|
||||||
|
|
||||||
|
### ❌ 除外仕様
|
||||||
|
- **ブロードキャスト**: 安全性のため完全除外(無限ループリスク回避)
|
||||||
|
- **関数オーバーロード**: `send(a)` vs `send(a,b)` 分岐不採用
|
||||||
|
|
||||||
|
## 🏗️ 新アーキテクチャ設計
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
|
||||||
|
│ P2PBox │◄──►│ MessageBus │◄──►│ Transport │
|
||||||
|
│ (ユーザーAPI) │ │ (ローカル配送) │ │ (送受信層) │
|
||||||
|
└─────────────┘ └──────────────┘ └─────────────┘
|
||||||
|
▲ ▲ ▲
|
||||||
|
│ │ │
|
||||||
|
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
|
||||||
|
│ IntentBox │ │ ハンドラ管理 │ │ InProcess │
|
||||||
|
│ (構造化MSG) │ │ ノード登録 │ │ WebSocket │
|
||||||
|
└─────────────┘ └──────────────┘ │ WebRTC │
|
||||||
|
└─────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📦 段階的実装計画
|
||||||
|
|
||||||
|
### 🎯 **Phase 1: 基盤実装**
|
||||||
|
|
||||||
|
#### **Step 1: IntentBox(構造化メッセージ)**
|
||||||
|
**ファイル**: `src/boxes/intent_box.rs` (完全作り直し)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// 新しいIntentBox設計
|
||||||
|
pub struct IntentBoxData {
|
||||||
|
pub name: String, // "chat.message", "file.share"等
|
||||||
|
pub payload: serde_json::Value, // 任意のJSON data
|
||||||
|
}
|
||||||
|
pub type IntentBox = Arc<Mutex<IntentBoxData>>;
|
||||||
|
```
|
||||||
|
|
||||||
|
**実装要件**:
|
||||||
|
- Arc<Mutex>統一パターン準拠
|
||||||
|
- BoxCore + NyashBox実装
|
||||||
|
- serde_json::Value使用
|
||||||
|
|
||||||
|
**テストコード**:
|
||||||
|
```nyash
|
||||||
|
// tests/phase2/intent_box_test.nyash
|
||||||
|
local msg = new IntentBox("chat.message", { text: "Hello P2P!" })
|
||||||
|
local console = new ConsoleBox()
|
||||||
|
console.log("Name: " + msg.name) // "chat.message"
|
||||||
|
console.log("Text: " + msg.payload.text) // "Hello P2P!"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **Step 2: MessageBus(プロセス内シングルトン)**
|
||||||
|
**ファイル**: `src/messaging/message_bus.rs` (新規作成)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub struct MessageBusData {
|
||||||
|
nodes: HashMap<String, BusEndpoint>, // ノード登録
|
||||||
|
subscribers: HashMap<String, Vec<IntentHandler>>, // ハンドラー管理
|
||||||
|
}
|
||||||
|
pub type MessageBus = Arc<Mutex<MessageBusData>>;
|
||||||
|
|
||||||
|
impl MessageBusData {
|
||||||
|
pub fn global() -> MessageBus // シングルトンアクセス
|
||||||
|
pub fn register_node(&mut self, id: String, endpoint: BusEndpoint)
|
||||||
|
pub fn route(&self, to: &str, intent: IntentBox) -> Result<(), SendError>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **Step 3: Transport trait(送受信抽象化)**
|
||||||
|
**ファイル**: `src/transport/mod.rs` (新規作成)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub trait Transport: Send + Sync {
|
||||||
|
fn node_id(&self) -> &str;
|
||||||
|
fn send(&self, to: &str, intent: IntentBox, opts: SendOpts) -> Result<(), SendError>;
|
||||||
|
fn on_receive(&mut self, callback: Box<dyn Fn(IntentEnvelope) + Send + Sync>);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🎯 **Phase 2: InProcess実装**
|
||||||
|
|
||||||
|
#### **Step 4: InProcessTransport**
|
||||||
|
**ファイル**: `src/transport/inprocess.rs` (新規作成)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub struct InProcessTransport {
|
||||||
|
node_id: String,
|
||||||
|
bus: MessageBus, // MessageBus::global()を使用
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Transport for InProcessTransport {
|
||||||
|
// Bus経由の高速ローカル配送実装
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🎯 **Phase 3: P2PBox統合**
|
||||||
|
|
||||||
|
#### **Step 5: P2PBox基本実装**
|
||||||
|
**ファイル**: `src/boxes/p2p_box.rs` (完全作り直し)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub struct P2PBoxData {
|
||||||
|
node_id: String,
|
||||||
|
transport: Arc<dyn Transport>,
|
||||||
|
bus: MessageBus, // 全P2PBoxで共有
|
||||||
|
}
|
||||||
|
pub type P2PBox = Arc<Mutex<P2PBoxData>>;
|
||||||
|
|
||||||
|
impl P2PBoxData {
|
||||||
|
pub fn new(node_id: String, kind: TransportKind) -> P2PBox
|
||||||
|
pub fn on(&self, intent_name: &str, handler: IntentHandler) -> Result<(), P2PError>
|
||||||
|
pub fn send(&self, to: &str, intent: IntentBox) -> Result<(), SendError>
|
||||||
|
// ブロードキャストメソッドは実装しない
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🧪 包括的テスト要件
|
||||||
|
|
||||||
|
### **基本動作テスト**
|
||||||
|
**ファイル**: `test_p2p_basic_new.nyash`
|
||||||
|
|
||||||
|
```nyash
|
||||||
|
// 2つのノード作成
|
||||||
|
local node_a = new P2PBox("alice", transport: "inprocess")
|
||||||
|
local node_b = new P2PBox("bob", transport: "inprocess")
|
||||||
|
|
||||||
|
// 受信ハンドラ設定
|
||||||
|
node_b.on("chat.message", function(intent, from) {
|
||||||
|
local console = new ConsoleBox()
|
||||||
|
console.log("From " + from + ": " + intent.payload.text)
|
||||||
|
})
|
||||||
|
|
||||||
|
// メッセージ送信
|
||||||
|
local msg = new IntentBox("chat.message", { text: "Hello P2P!" })
|
||||||
|
node_a.send("bob", msg) // → "From alice: Hello P2P!"
|
||||||
|
```
|
||||||
|
|
||||||
|
### **エラーハンドリングテスト**
|
||||||
|
```nyash
|
||||||
|
// 存在しないノードへの送信
|
||||||
|
local result = node_a.send("nonexistent", msg)
|
||||||
|
// → SendError::NodeNotFound
|
||||||
|
|
||||||
|
// 不正なIntentBox
|
||||||
|
local invalid_msg = "not an IntentBox"
|
||||||
|
local result = node_a.send("bob", invalid_msg)
|
||||||
|
// → 型エラー
|
||||||
|
```
|
||||||
|
|
||||||
|
### **パフォーマンステスト**
|
||||||
|
```nyash
|
||||||
|
// 大量メッセージ送信テスト
|
||||||
|
local start_time = new TimeBox()
|
||||||
|
loop(i < 1000) {
|
||||||
|
local msg = new IntentBox("test.performance", { id: i })
|
||||||
|
node_a.send("bob", msg)
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
local end_time = new TimeBox()
|
||||||
|
// 実行時間計測
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📁 必要なディレクトリ構成
|
||||||
|
|
||||||
|
```
|
||||||
|
src/
|
||||||
|
├── boxes/
|
||||||
|
│ ├── intent_box.rs # 完全作り直し
|
||||||
|
│ └── p2p_box.rs # 完全作り直し
|
||||||
|
├── messaging/ # 新規作成
|
||||||
|
│ └── message_bus.rs # MessageBus実装
|
||||||
|
└── transport/ # 新規作成
|
||||||
|
├── mod.rs # Transport trait
|
||||||
|
└── inprocess.rs # InProcessTransport
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔧 実装時の重要注意点
|
||||||
|
|
||||||
|
### **Arc<Mutex>統一パターン厳守**
|
||||||
|
```rust
|
||||||
|
// ✅ 正しい統一パターン
|
||||||
|
pub type IntentBox = Arc<Mutex<IntentBoxData>>;
|
||||||
|
pub type MessageBus = Arc<Mutex<MessageBusData>>;
|
||||||
|
pub type P2PBox = Arc<Mutex<P2PBoxData>>;
|
||||||
|
|
||||||
|
// ❌ 避けるべき
|
||||||
|
pub struct IntentBox { ... } // Arcなし
|
||||||
|
```
|
||||||
|
|
||||||
|
### **BoxCore実装必須**
|
||||||
|
```rust
|
||||||
|
impl BoxCore for IntentBox {
|
||||||
|
fn box_id(&self) -> u64 { self.lock().unwrap().base.id }
|
||||||
|
fn parent_type_id(&self) -> Option<TypeId> { None }
|
||||||
|
fn fmt_box(&self, f: &mut fmt::Formatter) -> fmt::Result { ... }
|
||||||
|
fn as_any(&self) -> &dyn Any { self }
|
||||||
|
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### **エラーハンドリング設計**
|
||||||
|
```rust
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum SendError {
|
||||||
|
NodeNotFound(String), // 宛先ノードが見つからない
|
||||||
|
NetworkError(String), // ネットワークエラー
|
||||||
|
SerializationError(String), // JSON変換エラー
|
||||||
|
BusError(String), // MessageBusエラー
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🎯 成功の定義
|
||||||
|
|
||||||
|
以下のテストが全て通過すること:
|
||||||
|
|
||||||
|
1. **基本通信**: ノード間でIntentBoxメッセージ送受信
|
||||||
|
2. **ハンドラ登録**: `on()` でイベントリスナー正常動作
|
||||||
|
3. **エラーハンドリング**: 不正な送信先・データで適切エラー
|
||||||
|
4. **パフォーマンス**: 1000メッセージ/秒以上の送信性能
|
||||||
|
5. **メモリ安全性**: valgrind等でメモリリーク検出なし
|
||||||
|
|
||||||
|
## 📚 参考ドキュメント
|
||||||
|
|
||||||
|
- **[P2P_GUIDE.md](docs/P2P_GUIDE.md)** - 設計詳細・使用例
|
||||||
|
- **[CURRENT_TASK.md](CURRENT_TASK.md)** - 実装状況・優先順位
|
||||||
|
- **[ai_conference_overload_decision.md](ai_conference_overload_decision.md)** - AI大会議決定事項
|
||||||
|
- **[docs/reference/override-delegation-syntax.md](docs/reference/override-delegation-syntax.md)** - デリゲーション構文仕様
|
||||||
|
|
||||||
|
## 🚀 実装開始
|
||||||
|
|
||||||
|
**Priority**: High
|
||||||
|
**Assignee**: Copilot
|
||||||
|
**Labels**: enhancement, p2p, breaking-change
|
||||||
|
**Milestone**: P2P Phase 2 Complete
|
||||||
|
|
||||||
|
**最初に取り組むべき**: Step 1 IntentBox の完全作り直し
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
🎉 **この実装により、Nyashは本格的なP2P通信システムを持つ現代的プログラミング言語になります!**
|
||||||
294
docs/README.md
294
docs/README.md
@ -1,223 +1,97 @@
|
|||||||
# 📚 Nyash Programming Language - Documentation Index
|
# 📚 Nyash Documentation
|
||||||
|
|
||||||
**最終更新: 2025年8月8日**
|
**NyashプログラミングLexicalAnalyzer言語の公式ドキュメント** | 最終更新: 2025-08-12
|
||||||
|
|
||||||
## 🎯 ドキュメント概要
|
|
||||||
|
|
||||||
Nyash言語の包括的なドキュメントセットへようこそ!
|
|
||||||
「Everything is Box」哲学に基づく革新的なプログラミング言語の全貌を詳しく解説しています。
|
|
||||||
|
|
||||||
## 📖 読者別ガイド
|
|
||||||
|
|
||||||
### 🚀 **初心者・学習者向け**
|
|
||||||
```
|
|
||||||
1. GETTING_STARTED_2025.md ← まずはここから!
|
|
||||||
2. LANGUAGE_OVERVIEW_2025.md ← 言語全体を理解
|
|
||||||
3. サンプルプログラム実行 ← 実際に動かしてみる
|
|
||||||
```
|
|
||||||
|
|
||||||
### 💻 **開発者・エンジニア向け**
|
|
||||||
```
|
|
||||||
1. LANGUAGE_OVERVIEW_2025.md ← 機能全体把握
|
|
||||||
2. TECHNICAL_ARCHITECTURE_2025.md ← 内部実装理解
|
|
||||||
3. CLAUDE.md ← 開発者詳細情報
|
|
||||||
```
|
|
||||||
|
|
||||||
### 🔧 **言語開発・拡張者向け**
|
|
||||||
```
|
|
||||||
1. TECHNICAL_ARCHITECTURE_2025.md ← アーキテクチャ詳細
|
|
||||||
2. ../src/ ソースコード ← 実装コード確認
|
|
||||||
3. ../current_task ← 開発状況・TODO
|
|
||||||
```
|
|
||||||
|
|
||||||
## 📑 ドキュメント一覧
|
|
||||||
|
|
||||||
### 🌟 **メインドキュメント** (必読)
|
|
||||||
|
|
||||||
#### 1. **[Getting Started Guide](GETTING_STARTED_2025.md)** 🚀
|
|
||||||
- **対象**: プログラミング初心者〜中級者
|
|
||||||
- **内容**: 5分でNyashを理解、実践チュートリアル
|
|
||||||
- **特徴**:
|
|
||||||
- ステップバイステップの学習プログラム
|
|
||||||
- 実用的なサンプルコード(Calculator、並行処理等)
|
|
||||||
- ベストプラクティス・デバッグ技法
|
|
||||||
- **推奨**: 最初に読むべきドキュメント
|
|
||||||
|
|
||||||
#### 2. **[Language Overview](LANGUAGE_OVERVIEW_2025.md)** 📖
|
|
||||||
- **対象**: 言語仕様を理解したい全ユーザー
|
|
||||||
- **内容**: Nyash言語の完全仕様・機能概要
|
|
||||||
- **特徴**:
|
|
||||||
- 実装済み機能の包括的リスト
|
|
||||||
- 構文仕様書・サンプルコード
|
|
||||||
- 実用アプリケーション事例
|
|
||||||
- 他言語との比較・ベンチマーク
|
|
||||||
|
|
||||||
#### 3. **[Technical Architecture](TECHNICAL_ARCHITECTURE_2025.md)** 🔧
|
|
||||||
- **対象**: システム開発者・言語実装者
|
|
||||||
- **内容**: 内部アーキテクチャ・実装詳細
|
|
||||||
- **特徴**:
|
|
||||||
- コンポーネント設計・データ構造
|
|
||||||
- パフォーマンス特性・最適化戦略
|
|
||||||
- 拡張性・FFI設計
|
|
||||||
- 実装の技術的革新点
|
|
||||||
|
|
||||||
### 📋 **専門ドキュメント**
|
|
||||||
|
|
||||||
#### 4. **[CLAUDE.md](../CLAUDE.md)** 🤖
|
|
||||||
- **対象**: Claude Code開発セッション用
|
|
||||||
- **内容**: 開発者向け詳細情報・実装ガイドライン
|
|
||||||
- **特徴**:
|
|
||||||
- 最新実装状況・進行中タスク
|
|
||||||
- コーディング規約・注意事項
|
|
||||||
- 問題解決パターン・FAQ
|
|
||||||
|
|
||||||
#### 5. **[current_task](../current_task)** 📝
|
|
||||||
- **対象**: 言語開発継続者
|
|
||||||
- **内容**: 現在の開発状況・次期実装予定
|
|
||||||
- **特徴**:
|
|
||||||
- リアルタイムTODOリスト
|
|
||||||
- 実装完了項目の記録
|
|
||||||
- 技術課題・解決策
|
|
||||||
|
|
||||||
### 🗂️ **アーカイブドキュメント**
|
|
||||||
|
|
||||||
#### 6. **[archive/](archive/)** 📦
|
|
||||||
- **過去の開発記録**: モジュール化・スコープ革命等
|
|
||||||
- **歴史的文書**: 設計判断・実装過程の記録
|
|
||||||
- **参考資料**: 過去の試行錯誤・学習資料
|
|
||||||
|
|
||||||
## 🎯 学習パス推奨
|
|
||||||
|
|
||||||
### **パス 1: クイックスタート** (30分)
|
|
||||||
```
|
|
||||||
1. Getting Started → サンプル実行 → 基本構文理解
|
|
||||||
目標: Hello Worldから簡単なプログラムまで
|
|
||||||
```
|
|
||||||
|
|
||||||
### **パス 2: 実用マスター** (2-3時間)
|
|
||||||
```
|
|
||||||
1. Getting Started (完全版)
|
|
||||||
2. Language Overview (機能編)
|
|
||||||
3. 実際のアプリ開発
|
|
||||||
目標: Calculator・RPGゲーム等の開発
|
|
||||||
```
|
|
||||||
|
|
||||||
### **パス 3: エキスパート** (1-2日)
|
|
||||||
```
|
|
||||||
1. 全ドキュメント通読
|
|
||||||
2. Technical Architecture 詳細理解
|
|
||||||
3. ソースコード調査・拡張実装
|
|
||||||
目標: 言語拡張・新機能実装
|
|
||||||
```
|
|
||||||
|
|
||||||
## 📊 Nyash言語の現状 (2025-08-08時点)
|
|
||||||
|
|
||||||
### ✅ **完成済み機能** (Production Ready)
|
|
||||||
- **基本言語機能**: 変数・演算子・制御構造・関数
|
|
||||||
- **オブジェクト指向**: クラス・継承・インターフェース・コンストラクタ
|
|
||||||
- **並行処理**: nowait/await・マルチスレッド・SharedState
|
|
||||||
- **静的システム**: static box・シングルトン・名前空間
|
|
||||||
- **メモリ管理**: 自動解放・明示的デストラクタ・参照カウント
|
|
||||||
- **デバッグ**: DebugBox・プロファイリング・トレース機能
|
|
||||||
- **モジュール**: include文・ファイル分割・名前空間
|
|
||||||
|
|
||||||
### 🚧 **開発中機能**
|
|
||||||
- **ジェネリクス**: 基盤完成・実行時特殊化実装中
|
|
||||||
- **非同期Phase 3**: スレッドプール・タイムアウト機能
|
|
||||||
- **WebAssembly**: 出力対応準備完了
|
|
||||||
|
|
||||||
### 🌟 **実証済みアプリケーション**
|
|
||||||
- 🎲 **サイコロRPGバトル** - 複雑ゲームロジック
|
|
||||||
- 📊 **統計計算アプリ** - 数学関数活用
|
|
||||||
- 🧮 **LISPインタープリター** - メタプログラミング
|
|
||||||
- ⚡ **並行処理デモ** - マルチスレッド実行
|
|
||||||
|
|
||||||
### 📈 **開発実績**
|
|
||||||
- **開発期間**: 2025年8月6日-8日 (わずか3日!)
|
|
||||||
- **実装規模**: 30,000+ lines of code
|
|
||||||
- **Box種類**: 15+ specialized box types
|
|
||||||
- **テストカバレッジ**: 主要機能完全テスト済み
|
|
||||||
|
|
||||||
## 🤝 コミュニティ・貢献
|
|
||||||
|
|
||||||
### **フィードバック歓迎**
|
|
||||||
- 言語仕様への提案・改善案
|
|
||||||
- 実装バグレポート・修正提案
|
|
||||||
- 新機能アイデア・使用事例
|
|
||||||
|
|
||||||
### **貢献方法**
|
|
||||||
- サンプルプログラム作成
|
|
||||||
- ドキュメント改善・翻訳
|
|
||||||
- Box実装・ライブラリ開発
|
|
||||||
- パフォーマンステスト・ベンチマーク
|
|
||||||
|
|
||||||
## 🎯 Next Steps
|
|
||||||
|
|
||||||
### **すぐに始める**
|
|
||||||
1. `GETTING_STARTED_2025.md` を読む
|
|
||||||
2. サンプルプログラムを実行
|
|
||||||
3. 自分のプロジェクトを作成
|
|
||||||
|
|
||||||
### **深く学ぶ**
|
|
||||||
1. `LANGUAGE_OVERVIEW_2025.md` で全機能把握
|
|
||||||
2. `TECHNICAL_ARCHITECTURE_2025.md` で内部理解
|
|
||||||
3. ソースコード (`../src/`) を読む
|
|
||||||
|
|
||||||
### **貢献する**
|
|
||||||
1. 実際のアプリケーション開発
|
|
||||||
2. 機能拡張・新Box実装
|
|
||||||
3. ドキュメント・チュートリアル作成
|
|
||||||
|
|
||||||
## 🎉 Welcome to Nyash!
|
|
||||||
|
|
||||||
**Everything is Box** - シンプルな哲学から始まり、無限の可能性を秘めたプログラミング言語の世界へようこそ!
|
|
||||||
|
|
||||||
Nyashでプログラミングの新しい体験を始めましょう 🚀
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📞 クイックリファレンス
|
## 🚀 すぐ始める
|
||||||
|
|
||||||
**実行方法**:
|
### 👶 **初心者向け**
|
||||||
```bash
|
- **[Getting Started](GETTING_STARTED.md)** - 環境構築から最初のプログラムまで
|
||||||
cd nyash-rust
|
|
||||||
cargo build
|
|
||||||
./target/debug/nyash your_program.nyash
|
|
||||||
```
|
|
||||||
|
|
||||||
**サンプルプログラム**:
|
### 📖 **言語を学ぶ**
|
||||||
```bash
|
- **[Language Guide](LANGUAGE_GUIDE.md)** - 言語仕様・構文・完全ガイド
|
||||||
./target/debug/nyash test_local_init.nyash # 初期化付き変数宣言
|
|
||||||
./target/debug/nyash app_dice_rpg.nyash # RPGバトルゲーム
|
### 🌐 **P2P通信**
|
||||||
./target/debug/nyash app_statistics.nyash # 統計計算アプリ
|
- **[P2P Guide](P2P_GUIDE.md)** - P2P通信システム完全ガイド
|
||||||
./target/debug/nyash test_async_parallel.nyash # 並行処理デモ
|
|
||||||
```
|
---
|
||||||
|
|
||||||
|
## 📋 詳細リファレンス
|
||||||
|
|
||||||
|
### **[reference/](reference/)**
|
||||||
|
- **[language-reference.md](reference/language-reference.md)** - 言語仕様完全リファレンス
|
||||||
|
- **[override-delegation-syntax.md](reference/override-delegation-syntax.md)** - デリゲーション・override構文仕様
|
||||||
|
- **[design-philosophy.md](reference/design-philosophy.md)** - 明示的デリゲーション革命の設計思想
|
||||||
|
- **[builtin-boxes.md](reference/builtin-boxes.md)** - ビルトインBox型詳細リファレンス
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🗄️ 過去資料・開発履歴
|
||||||
|
|
||||||
|
### **[archive/](archive/)**
|
||||||
|
- **[development/](archive/development/)** - 過去のドキュメント・開発履歴
|
||||||
|
- **[p2p/](archive/p2p/)** - P2P詳細設計書・AI相談記録
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎯 Nyashとは
|
||||||
|
|
||||||
|
**「Everything is Box」哲学**に基づく革新的プログラミング言語
|
||||||
|
|
||||||
**基本構文**:
|
|
||||||
```nyash
|
```nyash
|
||||||
# 変数・初期化
|
// シンプルで強力な構文
|
||||||
local name = "Alice", age = 25
|
local greeting = "Hello, Nyash!"
|
||||||
|
print(greeting)
|
||||||
|
|
||||||
# 制御構造
|
// すべてがBox - 統一された美しい世界
|
||||||
if condition {
|
local numbers = new ArrayBox()
|
||||||
print("Hello!")
|
numbers.push(42)
|
||||||
}
|
numbers.push(3.14)
|
||||||
loop(i < 10) {
|
|
||||||
i = i + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# クラス定義
|
// P2P通信もBox!
|
||||||
box MyClass {
|
local node = new P2PBox("alice", transport: "inprocess")
|
||||||
init { field1, field2 }
|
node.send("bob", new IntentBox("chat", { text: "Hello P2P!" }))
|
||||||
MyClass(a, b) { me.field1 = a; me.field2 = b }
|
|
||||||
method() { return me.field1 + me.field2 }
|
|
||||||
}
|
|
||||||
|
|
||||||
# 並行処理
|
|
||||||
future = nowait heavyTask()
|
|
||||||
result = await future
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### ✨ **主な特徴**
|
||||||
|
- **🔧 Production Ready**: Phase 1完了、実用レベルの言語機能
|
||||||
|
- **🌐 P2P Native**: P2P通信がビルトイン (Phase 2実装中)
|
||||||
|
- **🛡️ Memory Safe**: Rust実装による完全メモリ安全性
|
||||||
|
- **📦 Everything is Box**: 統一されたオブジェクトモデル
|
||||||
|
- **⚡ Simple & Powerful**: 学習コストが低く、表現力が高い
|
||||||
|
|
||||||
|
### 📊 **実装状況 (2025-08-12)**
|
||||||
|
|
||||||
|
#### ✅ **Phase 1完了**
|
||||||
|
- FloatBox, ArrayBox改良, Cross-type演算子
|
||||||
|
- 包括的テストスイート (188行)
|
||||||
|
- デリゲーション革命 (`from`構文完成)
|
||||||
|
|
||||||
|
#### 🚧 **Phase 2実装中**
|
||||||
|
- IntentBox (構造化メッセージ)
|
||||||
|
- P2PBox (P2P通信ノード)
|
||||||
|
- MessageBus (プロセス内シングルトン)
|
||||||
|
|
||||||
|
#### 🎯 **最終目標**
|
||||||
|
**NyaMeshP2Pライブラリ実現** - Nyash言語による本格的P2P通信ライブラリ
|
||||||
|
|
||||||
---
|
---
|
||||||
*Documentation Index v1.0*
|
|
||||||
*Everything is Box - Documentation Complete*
|
## 🤝 コミュニティ
|
||||||
|
|
||||||
|
### 開発方針
|
||||||
|
- **ドキュメントファースト**: ソースより先にドキュメント確認
|
||||||
|
- **AI協働開発**: Gemini先生・ChatGPT先生・Copilot連携
|
||||||
|
- **段階的実装**: Phase 1→2→3の確実な進歩
|
||||||
|
|
||||||
|
### 貢献方法
|
||||||
|
1. **Issue報告**: バグ・要望をGitHub Issuesで報告
|
||||||
|
2. **ドキュメント改善**: typo修正・内容追加のPull Request歓迎
|
||||||
|
3. **コード貢献**: 新機能実装・バグ修正のPull Request歓迎
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**🎉 Welcome to the world of "Everything is Box"!**
|
||||||
|
|
||||||
|
*Nyashで新しいプログラミングの世界を体験しよう!*
|
||||||
182
docs/reference/builtin-boxes.md
Normal file
182
docs/reference/builtin-boxes.md
Normal file
@ -0,0 +1,182 @@
|
|||||||
|
# ビルトインBox型 API リファレンス
|
||||||
|
|
||||||
|
Nyashで利用できる全ビルトインBox型のAPI仕様書です。
|
||||||
|
|
||||||
|
## 📡 P2PBox - 通信ノードBox
|
||||||
|
|
||||||
|
P2P通信を行うノードを表すBox。通信世界(IntentBox)に参加してメッセージを送受信できます。
|
||||||
|
|
||||||
|
### コンストラクタ
|
||||||
|
```nyash
|
||||||
|
// 通信ノードを作成
|
||||||
|
local node = new P2PBox(node_id, world)
|
||||||
|
```
|
||||||
|
|
||||||
|
**パラメータ:**
|
||||||
|
- `node_id` (String): ノードの一意識別子
|
||||||
|
- `world` (IntentBox): 参加する通信世界
|
||||||
|
|
||||||
|
### メソッド
|
||||||
|
|
||||||
|
#### send(intent, data, target)
|
||||||
|
特定のノードにメッセージを送信します。
|
||||||
|
```nyash
|
||||||
|
local result = node.send("greeting", message_data, "target_node_id")
|
||||||
|
```
|
||||||
|
|
||||||
|
**パラメータ:**
|
||||||
|
- `intent` (String): メッセージの種類
|
||||||
|
- `data` (Box): 送信するデータ
|
||||||
|
- `target` (String): 送信先ノードID
|
||||||
|
|
||||||
|
**戻り値:** StringBox("sent")
|
||||||
|
|
||||||
|
#### on(intent, callback)
|
||||||
|
指定したintentのメッセージを受信した際のリスナーを登録します。
|
||||||
|
```nyash
|
||||||
|
node.on("chat", callback_function)
|
||||||
|
```
|
||||||
|
|
||||||
|
**パラメータ:**
|
||||||
|
- `intent` (String): 監視するメッセージ種類
|
||||||
|
- `callback` (MethodBox): 受信時に呼ばれる関数
|
||||||
|
|
||||||
|
**戻り値:** StringBox("listener added")
|
||||||
|
|
||||||
|
#### off(intent)
|
||||||
|
指定したintentのリスナーを解除します。
|
||||||
|
```nyash
|
||||||
|
node.off("chat")
|
||||||
|
```
|
||||||
|
|
||||||
|
**パラメータ:**
|
||||||
|
- `intent` (String): 解除するメッセージ種類
|
||||||
|
|
||||||
|
**戻り値:** StringBox("listener removed" / "no listener found")
|
||||||
|
|
||||||
|
#### get_node_id()
|
||||||
|
このノードのIDを取得します。
|
||||||
|
```nyash
|
||||||
|
local id = node.get_node_id()
|
||||||
|
```
|
||||||
|
|
||||||
|
**戻り値:** StringBox(ノードID)
|
||||||
|
|
||||||
|
### 使用例
|
||||||
|
```nyash
|
||||||
|
// 通信世界を作成
|
||||||
|
local world = new IntentBox()
|
||||||
|
|
||||||
|
// 2つのノードを作成
|
||||||
|
local alice = new P2PBox("alice", world)
|
||||||
|
local bob = new P2PBox("bob", world)
|
||||||
|
|
||||||
|
// Bobがgreetingを受信するリスナー設定
|
||||||
|
bob.on("greeting", greeting_handler)
|
||||||
|
|
||||||
|
// AliceからBobにメッセージ送信
|
||||||
|
local message = new MapBox()
|
||||||
|
message.set("text", "Hello Bob!")
|
||||||
|
alice.send("greeting", message, "bob")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📨 IntentBox - 通信世界Box
|
||||||
|
|
||||||
|
P2PBoxが通信を行うための世界(ネットワーク)を表すBox。複数のノードが同一のIntentBoxを共有して通信します。
|
||||||
|
|
||||||
|
### コンストラクタ
|
||||||
|
```nyash
|
||||||
|
// 通信世界を作成
|
||||||
|
local world = new IntentBox()
|
||||||
|
```
|
||||||
|
|
||||||
|
**パラメータ:** なし
|
||||||
|
|
||||||
|
### 特徴
|
||||||
|
- ローカル通信: 同一プロセス内のP2PBox間でメッセージをやり取り
|
||||||
|
- スレッドセーフ: Arc<Mutex>により並行アクセス対応
|
||||||
|
- 将来拡張: WebSocket版や分散版への拡張予定
|
||||||
|
|
||||||
|
### 使用例
|
||||||
|
```nyash
|
||||||
|
// 1つの通信世界に複数ノードが参加
|
||||||
|
local world = new IntentBox()
|
||||||
|
local node1 = new P2PBox("server", world)
|
||||||
|
local node2 = new P2PBox("client", world)
|
||||||
|
|
||||||
|
// 同一世界内での通信が可能
|
||||||
|
node1.send("data", payload, "client")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📝 StringBox - 文字列Box
|
||||||
|
|
||||||
|
文字列データを格納・操作するBox。
|
||||||
|
|
||||||
|
### コンストラクタ
|
||||||
|
```nyash
|
||||||
|
local text = new StringBox("Hello")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 基本メソッド
|
||||||
|
- `toString()`: 文字列表現を取得
|
||||||
|
- `length()`: 文字列長を取得
|
||||||
|
- `concat(other)`: 文字列結合
|
||||||
|
- `substring(start, end)`: 部分文字列取得
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔢 IntegerBox - 整数Box
|
||||||
|
|
||||||
|
整数データを格納・操作するBox。
|
||||||
|
|
||||||
|
### コンストラクタ
|
||||||
|
```nyash
|
||||||
|
local num = new IntegerBox(42)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 基本メソッド
|
||||||
|
- `toString()`: 文字列表現を取得
|
||||||
|
- `add(other)`: 加算
|
||||||
|
- `subtract(other)`: 減算
|
||||||
|
- `multiply(other)`: 乗算
|
||||||
|
- `divide(other)`: 除算
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📺 ConsoleBox - コンソール出力Box
|
||||||
|
|
||||||
|
コンソールへの出力を行うBox。
|
||||||
|
|
||||||
|
### コンストラクタ
|
||||||
|
```nyash
|
||||||
|
local console = new ConsoleBox()
|
||||||
|
```
|
||||||
|
|
||||||
|
### メソッド
|
||||||
|
- `log(message)`: メッセージをログ出力
|
||||||
|
- `error(message)`: エラーメッセージを出力
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🗂️ MapBox - 連想配列Box
|
||||||
|
|
||||||
|
キー・バリューペアでデータを格納するBox。
|
||||||
|
|
||||||
|
### コンストラクタ
|
||||||
|
```nyash
|
||||||
|
local map = new MapBox()
|
||||||
|
```
|
||||||
|
|
||||||
|
### メソッド
|
||||||
|
- `set(key, value)`: キー・バリューを設定
|
||||||
|
- `get(key)`: 値を取得
|
||||||
|
- `has(key)`: キーが存在するかチェック
|
||||||
|
- `remove(key)`: キー・バリューを削除
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
最終更新: 2025年8月11日
|
||||||
337
docs/reference/design-philosophy.md
Normal file
337
docs/reference/design-philosophy.md
Normal file
@ -0,0 +1,337 @@
|
|||||||
|
# 🌟 明示的デリゲーション革命:なぜNyashは世界初の完全明示デリゲーション言語になったのか
|
||||||
|
|
||||||
|
作成日: 2025年8月11日
|
||||||
|
著者: Nyashプロジェクトチーム
|
||||||
|
ステータス: 設計思想決定版
|
||||||
|
|
||||||
|
## 📜 はじめに:革命の始まり
|
||||||
|
|
||||||
|
2025年8月11日、Nyashプログラミング言語の開発において、言語設計史上最大級の発見がありました。それは、**暗黙のオーバーライド問題**の発見と、それを解決する**完全明示デリゲーション構文**の誕生です。
|
||||||
|
|
||||||
|
この文書は、なぜこの革命が必要だったのか、どのような思想の元に設計されたのかを詳しく解説します。
|
||||||
|
|
||||||
|
## 🚨 問題の発見:暗黙の悪魔
|
||||||
|
|
||||||
|
### HashMap::insert による意図しない上書き
|
||||||
|
|
||||||
|
Nyashの実装を詳しく調査した結果、恐ろしい問題が発見されました:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// instance.rs - add_method関数
|
||||||
|
pub fn add_method(&mut self, method_name: String, method_ast: ASTNode) {
|
||||||
|
let mut new_methods = (*self.methods).clone();
|
||||||
|
new_methods.insert(method_name, method_ast); // ← 暗黙の上書き!
|
||||||
|
self.methods = Arc::new(new_methods);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
この実装により、以下のような**暗黙のオーバーライド**が発生していました:
|
||||||
|
|
||||||
|
```nyash
|
||||||
|
box Node {
|
||||||
|
send(msg) { // 最初の定義
|
||||||
|
print("Version 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
send(msg) { // 暗黙に上書きされる
|
||||||
|
print("Version 2") // ← こちらだけが残る
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Nyash哲学との根本的矛盾
|
||||||
|
|
||||||
|
この問題は、Nyashの3つの核心哲学と完全に矛盾していました:
|
||||||
|
|
||||||
|
1. **明示性重視**: 「何が起きているかを隠さない」
|
||||||
|
2. **Everything is Box**: 「統一された世界観」
|
||||||
|
3. **初学者フレンドリー**: 「複雑な概念を分かりやすく表現」
|
||||||
|
|
||||||
|
暗黙のオーバーライドは、これらすべてを破壊する**言語設計上の致命的欠陥**だったのです。
|
||||||
|
|
||||||
|
## 💡 解決への道:3AI大会議
|
||||||
|
|
||||||
|
### AI専門家による徹底分析
|
||||||
|
|
||||||
|
この問題の解決策を求めて、言語設計の専門家であるGeminiとChatGPTに相談を行いました。結果は予想を上回る**圧倒的な支持**でした。
|
||||||
|
|
||||||
|
#### Gemini先生の評価
|
||||||
|
> **「全面的に賛成します」**
|
||||||
|
> **「極めて重要な一歩」**
|
||||||
|
> **「Nyashのアイデンティティを確立する」**
|
||||||
|
|
||||||
|
#### ChatGPT先生の評価
|
||||||
|
> **「強く整合する」**
|
||||||
|
> **「安全性と読みやすさを大幅に向上」**
|
||||||
|
> **「実装工数3-5日程度」**
|
||||||
|
|
||||||
|
### 専門的視点からの裏付け
|
||||||
|
|
||||||
|
両専門家から以下の重要な指摘がありました:
|
||||||
|
|
||||||
|
1. **哲学的整合性**: Nyashの明示性哲学と完全に合致
|
||||||
|
2. **技術的優位性**: 他言語の問題(Python MRO、Java super等)を根本解決
|
||||||
|
3. **学習効果**: 初学者にとってより理解しやすい設計
|
||||||
|
4. **実装可能性**: 技術的に十分実現可能
|
||||||
|
|
||||||
|
## 🌟 革命的解決策:Override + From 統一構文
|
||||||
|
|
||||||
|
### 4つの統一原則
|
||||||
|
|
||||||
|
この問題を解決するため、以下の4つの統一原則を確立しました:
|
||||||
|
|
||||||
|
#### 1. 宣言の統一
|
||||||
|
```nyash
|
||||||
|
box Child from Parent // デリゲーション関係の明示
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. 置換の統一
|
||||||
|
```nyash
|
||||||
|
override methodName() // オーバーライドの明示宣言
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 呼び出しの統一
|
||||||
|
```nyash
|
||||||
|
from Parent.methodName() // 親実装の明示呼び出し
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. 構築の統一
|
||||||
|
```nyash
|
||||||
|
from Parent.init() // コンストラクタも同じ構文
|
||||||
|
```
|
||||||
|
|
||||||
|
### 完全な例
|
||||||
|
|
||||||
|
```nyash
|
||||||
|
box MeshNode : P2PBox {
|
||||||
|
init routing = RoutingTable()
|
||||||
|
|
||||||
|
constructor(nodeId, world) {
|
||||||
|
from P2PBox.constructor(nodeId, world) // 統一構文
|
||||||
|
me.routing = RoutingTable()
|
||||||
|
}
|
||||||
|
|
||||||
|
override send(intent, data, target) { // 明示的置換
|
||||||
|
me.routing.log(target)
|
||||||
|
from P2PBox.send(intent, data, target) // 明示的呼び出し
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔥 革命的特徴
|
||||||
|
|
||||||
|
### 1. 完全な明示性
|
||||||
|
|
||||||
|
**従来の問題**:
|
||||||
|
- 何がオーバーライドされているかわからない
|
||||||
|
- 親のどのメソッドを呼んでいるかわからない
|
||||||
|
- 実行順序が不明確
|
||||||
|
|
||||||
|
**Nyashの解決**:
|
||||||
|
- `override` で置換を明示宣言
|
||||||
|
- `from Parent.method()` で呼び出し先を完全明示
|
||||||
|
- 上から下への直感的な実行順序
|
||||||
|
|
||||||
|
### 2. 曖昧性の完全排除
|
||||||
|
|
||||||
|
**多重デリゲーション時の曖昧性解消**:
|
||||||
|
```nyash
|
||||||
|
box SmartNode : P2PBox, Logger {
|
||||||
|
override send(intent, data, target) {
|
||||||
|
from Logger.debug("Sending: " + intent) // どのLoggerか明確
|
||||||
|
from P2PBox.send(intent, data, target) // どのP2PBoxか明確
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 競合時は更に明示的に
|
||||||
|
box ConflictNode from ParentA, ParentB {
|
||||||
|
override ParentA.process(data) { // ParentAのprocessを置換
|
||||||
|
from ParentA.process(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
override ParentB.process(data) { // ParentBのprocessを置換
|
||||||
|
from ParentB.process(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 学習コストの最小化
|
||||||
|
|
||||||
|
**覚えるべきルール**:
|
||||||
|
1. 親のメソッドを置換したい → `override`
|
||||||
|
2. 親のメソッドを呼びたい → `from Parent.method()`
|
||||||
|
3. 親のコンストラクタを呼びたい → `from Parent.init()`
|
||||||
|
|
||||||
|
たった3つのルールで、すべてのデリゲーション操作が表現できます。
|
||||||
|
|
||||||
|
## 🌍 他言語との比較:なぜNyashが優れているのか
|
||||||
|
|
||||||
|
### Python の問題
|
||||||
|
```python
|
||||||
|
# MRO(Method Resolution Order)地獄
|
||||||
|
class C(A, B):
|
||||||
|
def method(self):
|
||||||
|
super().method() # どっちのmethod?
|
||||||
|
```
|
||||||
|
|
||||||
|
**Nyash の解決**:
|
||||||
|
```nyash
|
||||||
|
box C : A, B {
|
||||||
|
override method() {
|
||||||
|
from A.method() // Aのmethodと明示
|
||||||
|
from B.method() // Bのmethodと明示
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Java/C# の問題
|
||||||
|
```java
|
||||||
|
// どの親のmethodを呼んでいるかコードから不明
|
||||||
|
@Override
|
||||||
|
public void method() {
|
||||||
|
super.method(); // 単一継承でも曖昧
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Nyash の解決**:
|
||||||
|
```nyash
|
||||||
|
override method() {
|
||||||
|
from Parent.method() // どのParentか完全に明確
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### TypeScript の問題
|
||||||
|
```typescript
|
||||||
|
// 暗黙のオーバーライドによる事故
|
||||||
|
class Child extends Parent {
|
||||||
|
method() { // うっかり同名メソッド → 意図しない上書き
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Nyash の解決**:
|
||||||
|
```nyash
|
||||||
|
// overrideなしで同名メソッド → コンパイルエラー
|
||||||
|
// 意図しない上書きは100%防止
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🎯 設計思想の深層
|
||||||
|
|
||||||
|
### Everything is Box との統合
|
||||||
|
|
||||||
|
この革命は、Nyashの根本思想「Everything is Box」と完全に統合されています:
|
||||||
|
|
||||||
|
- **Box同士のデリゲーション**: 階層ではなく、協力関係
|
||||||
|
- **Boxメソッドの明示的管理**: どのBoxのどのメソッドかが常に明確
|
||||||
|
- **Box構築の明示的制御**: コンストラクタも普通のメソッド
|
||||||
|
|
||||||
|
### 明示性の哲学
|
||||||
|
|
||||||
|
Nyashが目指すのは、**「魔法のない言語」**です:
|
||||||
|
|
||||||
|
- 隠れた処理は一切なし
|
||||||
|
- すべての動作がコードに現れる
|
||||||
|
- 初学者でも上級者でも同じように理解できる
|
||||||
|
|
||||||
|
### 初学者への配慮
|
||||||
|
|
||||||
|
複雑な概念を、シンプルな文法で表現:
|
||||||
|
|
||||||
|
- `override` = 「置き換えます」
|
||||||
|
- `from Parent.method()` = 「親の方法を使います」
|
||||||
|
- コンパイルエラー = 「間違いを素早く教える」
|
||||||
|
|
||||||
|
## 🚀 実装戦略
|
||||||
|
|
||||||
|
### 段階的導入
|
||||||
|
|
||||||
|
ChatGPT先生の提案による実装ロードマップ:
|
||||||
|
|
||||||
|
**Phase 1(0.5-1日)**:
|
||||||
|
- `override` キーワード追加
|
||||||
|
- 基本パーサー拡張
|
||||||
|
|
||||||
|
**Phase 2(1-2日)**:
|
||||||
|
- 暗黙オーバーライド検出
|
||||||
|
- コンストラクタ重複禁止
|
||||||
|
|
||||||
|
**Phase 3(1日)**:
|
||||||
|
- `from Parent.init()` 実装
|
||||||
|
- エラーメッセージ改善
|
||||||
|
|
||||||
|
### 移行支援
|
||||||
|
|
||||||
|
既存コードの安全な移行:
|
||||||
|
- 段階的警告システム
|
||||||
|
- 自動修正支援ツール
|
||||||
|
- 詳細な移行ガイド
|
||||||
|
|
||||||
|
## 🌟 期待される効果
|
||||||
|
|
||||||
|
### 1. 開発者体験の革命的向上
|
||||||
|
|
||||||
|
**Before(暗黙オーバーライド)**:
|
||||||
|
- バグの発見が困難
|
||||||
|
- 意図しない動作
|
||||||
|
- デバッグに多大な時間
|
||||||
|
|
||||||
|
**After(明示的オーバーライド)**:
|
||||||
|
- コンパイル時に間違いを検出
|
||||||
|
- 意図が明確に表現される
|
||||||
|
- デバッグ時間の劇的短縮
|
||||||
|
|
||||||
|
### 2. コードの可読性向上
|
||||||
|
|
||||||
|
**Before**:
|
||||||
|
```nyash
|
||||||
|
// これは何をオーバーライドしている?
|
||||||
|
send(msg) {
|
||||||
|
// 親を呼んでる?呼んでない?
|
||||||
|
processMessage(msg)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**After**:
|
||||||
|
```nyash
|
||||||
|
// P2PBoxのsendを明示的にオーバーライド
|
||||||
|
override send(msg) {
|
||||||
|
processMessage(msg)
|
||||||
|
from P2PBox.send(msg) // P2PBoxの実装も使用
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 保守性の向上
|
||||||
|
|
||||||
|
- 変更の影響範囲が明確
|
||||||
|
- リファクタリングが安全
|
||||||
|
- チーム開発での誤解を防止
|
||||||
|
|
||||||
|
## 🏆 結論:言語設計史に残る革命
|
||||||
|
|
||||||
|
この明示的デリゲーション革命により、Nyashは以下を達成しました:
|
||||||
|
|
||||||
|
### 世界初の完全明示デリゲーション言語
|
||||||
|
|
||||||
|
1. **完全な明示性**: すべての動作を明示
|
||||||
|
2. **曖昧性の完全排除**: どんな複雑なケースも明確
|
||||||
|
3. **統一構文**: デリゲーションとオーバーライドの完全統合
|
||||||
|
4. **初学者フレンドリー**: 学習しやすく、間違いにくい
|
||||||
|
|
||||||
|
### プログラミング言語設計への貢献
|
||||||
|
|
||||||
|
- **暗黙の悪魔**からの完全な解放
|
||||||
|
- **多重デリゲーション**の安全で明確な実現
|
||||||
|
- **コード可読性**の新しい基準の確立
|
||||||
|
|
||||||
|
### 未来への影響
|
||||||
|
|
||||||
|
Nyashのこの革命は、今後のプログラミング言語設計に大きな影響を与えるでしょう。「暗黙より明示」という哲学が、ついに技術的に完全実現されたのです。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**2025年8月11日は、プログラミング言語史において「明示的デリゲーション革命の日」として記憶されることでしょう。** 🎊
|
||||||
|
|
||||||
|
この革命により、Nyashは単なるプログラミング言語を超えて、**新しいプログラミングパラダイムの先駆者**となりました。
|
||||||
|
|
||||||
|
Everything is Box. Everything is Explicit. Everything is Beautiful. 🌟
|
||||||
525
docs/reference/language-reference.md
Normal file
525
docs/reference/language-reference.md
Normal file
@ -0,0 +1,525 @@
|
|||||||
|
# 🚀 Nyash Language Reference 2025
|
||||||
|
|
||||||
|
**最終更新: 2025年8月11日 - デリゲーション革命完了!`from`統一構文+`init`構文決定!**
|
||||||
|
|
||||||
|
## 📖 概要
|
||||||
|
|
||||||
|
Nyashは「Everything is Box」哲学に基づく革新的プログラミング言語です。
|
||||||
|
Rust製インタープリターによる高性能実行と、直感的な構文により、学習しやすく実用的な言語として完成しました。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔤 **1. 予約語・キーワード完全リスト**
|
||||||
|
|
||||||
|
### **コア言語**
|
||||||
|
| 予約語 | 用途 | 例 |
|
||||||
|
|-------|------|---|
|
||||||
|
| `box` | クラス定義 | `box MyClass { }` |
|
||||||
|
| `static` | 静的Box・関数定義 | `static box Main { }` |
|
||||||
|
| `interface` | インターフェース定義 | `interface Comparable { }` |
|
||||||
|
| `from` | デリゲーション指定 | `box Child from Parent { }` |
|
||||||
|
| `new` | オブジェクト生成 | `new ConsoleBox()` |
|
||||||
|
| `me`/`this` | 自己参照 | `me.field = value` |
|
||||||
|
|
||||||
|
### **変数・スコープ**
|
||||||
|
| 予約語 | 用途 | 例 |
|
||||||
|
|-------|------|---|
|
||||||
|
| `local` | ローカル変数宣言 | `local x, y = 10` |
|
||||||
|
| `outbox` | 所有権移転変数 | `outbox result = compute()` |
|
||||||
|
| `global` | グローバル変数 | `global CONFIG = "dev"` |
|
||||||
|
| `init` | フィールド初期化ブロック | `init { name, age }` |
|
||||||
|
|
||||||
|
### **制御構文**
|
||||||
|
| 予約語 | 用途 | 例 |
|
||||||
|
|-------|------|---|
|
||||||
|
| `if` | 条件分岐 | `if condition { }` |
|
||||||
|
| `else` | else節 | `else { }` |
|
||||||
|
| `loop` | ループ(唯一の形式) | `loop(condition) { }` |
|
||||||
|
| `break` | ループ脱出 | `break` |
|
||||||
|
| `return` | 関数リターン | `return value` |
|
||||||
|
|
||||||
|
### **論理・演算**
|
||||||
|
| 予約語 | 用途 | 例 |
|
||||||
|
|-------|------|---|
|
||||||
|
| `not` | 論理否定 | `not condition` |
|
||||||
|
| `and` | 論理積 | `a and b` |
|
||||||
|
| `or` | 論理和 | `a or b` |
|
||||||
|
| `true`/`false` | 真偽値 | `flag = true` |
|
||||||
|
|
||||||
|
### **非同期・並行**
|
||||||
|
| 予約語 | 用途 | 例 |
|
||||||
|
|-------|------|---|
|
||||||
|
| `nowait` | 非同期実行 | `nowait future = task()` |
|
||||||
|
| `await` | 待機・結果取得 | `result = await future` |
|
||||||
|
|
||||||
|
### **例外処理**
|
||||||
|
| 予約語 | 用途 | 例 |
|
||||||
|
|-------|------|---|
|
||||||
|
| `try` | 例外捕獲開始 | `try { }` |
|
||||||
|
| `catch` | 例外処理 | `catch (e) { }` |
|
||||||
|
| `finally` | 最終処理 | `finally { }` |
|
||||||
|
| `throw` | 例外発生 | `throw error` |
|
||||||
|
|
||||||
|
### **その他**
|
||||||
|
| 予約語 | 用途 | 例 |
|
||||||
|
|-------|------|---|
|
||||||
|
| `function` | 関数定義 | `function add(a,b) { }` |
|
||||||
|
| `print` | 出力 | `print("Hello")` |
|
||||||
|
| `include` | ファイル取り込み | `include "math.nyash"` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📝 **2. 文法・構文仕様**
|
||||||
|
|
||||||
|
### **2.1 Box定義文法**
|
||||||
|
|
||||||
|
#### **基本Box**
|
||||||
|
```nyash
|
||||||
|
box ClassName {
|
||||||
|
init { field1, field2, field3 } # カンマ必須!CPU暴走防止
|
||||||
|
|
||||||
|
# コンストラクタ
|
||||||
|
init(param1, param2) { # init構文に統一
|
||||||
|
me.field1 = param1
|
||||||
|
me.field2 = param2
|
||||||
|
me.field3 = defaultValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
# メソッド
|
||||||
|
methodName(arg1, arg2) {
|
||||||
|
return me.field1 + arg1
|
||||||
|
}
|
||||||
|
|
||||||
|
# デストラクタ
|
||||||
|
fini() {
|
||||||
|
print("Cleanup: " + me.field1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **デリゲーションBox**
|
||||||
|
```nyash
|
||||||
|
box Child from Parent interface Comparable {
|
||||||
|
init { childField }
|
||||||
|
|
||||||
|
init(parentParam, childParam) { # init構文に統一
|
||||||
|
from Parent.init(parentParam) # 親コンストラクタ明示呼び出し
|
||||||
|
me.childField = childParam
|
||||||
|
}
|
||||||
|
|
||||||
|
# メソッドオーバーライド
|
||||||
|
override process(data) { # override必須
|
||||||
|
local result = from Parent.process(data) # 親メソッド呼び出し
|
||||||
|
return result + " (Child processed)"
|
||||||
|
}
|
||||||
|
|
||||||
|
# インターフェース実装
|
||||||
|
compareTo(other) {
|
||||||
|
return me.value - other.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **Static Box(推奨エントリーポイント)**
|
||||||
|
```nyash
|
||||||
|
static box Main {
|
||||||
|
init { console, result }
|
||||||
|
|
||||||
|
main() {
|
||||||
|
me.console = new ConsoleBox()
|
||||||
|
me.console.log("🎉 Everything is Box!")
|
||||||
|
return "Success"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **ジェネリックBox**
|
||||||
|
```nyash
|
||||||
|
box Container<T> {
|
||||||
|
init { value }
|
||||||
|
|
||||||
|
Container(item) {
|
||||||
|
me.value = item
|
||||||
|
}
|
||||||
|
|
||||||
|
getValue() {
|
||||||
|
return me.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### **2.2 変数宣言**
|
||||||
|
|
||||||
|
#### **基本パターン**
|
||||||
|
```nyash
|
||||||
|
# 単一宣言
|
||||||
|
local x
|
||||||
|
local name = "初期値"
|
||||||
|
|
||||||
|
# 複数宣言
|
||||||
|
local a, b, c
|
||||||
|
local x = 10, y = 20, z # 混合初期化
|
||||||
|
|
||||||
|
# 所有権移転(static関数内)
|
||||||
|
static function Factory.create() {
|
||||||
|
outbox product # 呼び出し側に所有権移転
|
||||||
|
product = new Item()
|
||||||
|
return product
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **変数宣言厳密化システム(2025-08-09実装)**
|
||||||
|
```nyash
|
||||||
|
# ✅ 正しい - 明示宣言必須
|
||||||
|
local temp
|
||||||
|
temp = 42
|
||||||
|
|
||||||
|
# ❌ エラー - 未宣言変数への代入
|
||||||
|
x = 42 # RuntimeError: 未宣言変数 + 修正提案表示
|
||||||
|
```
|
||||||
|
|
||||||
|
### **2.3 制御構文**
|
||||||
|
|
||||||
|
#### **条件分岐**
|
||||||
|
```nyash
|
||||||
|
if condition {
|
||||||
|
# 処理
|
||||||
|
} else if condition2 {
|
||||||
|
# 処理2
|
||||||
|
} else {
|
||||||
|
# else処理
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **ループ(統一構文)**
|
||||||
|
```nyash
|
||||||
|
# ✅ 唯一の正しい形式
|
||||||
|
loop(condition) {
|
||||||
|
# ループ本体
|
||||||
|
if exitCondition {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ❌ 削除済み - 使用不可
|
||||||
|
while condition { } # パーサーエラー
|
||||||
|
loop() { } # パーサーエラー
|
||||||
|
```
|
||||||
|
|
||||||
|
### **2.4 演算子・式**
|
||||||
|
|
||||||
|
#### **🚀 新実装: 関数オーバーロードシステム**
|
||||||
|
```nyash
|
||||||
|
# Rust風トレイトベース演算子(2025-08-10実装完了)
|
||||||
|
sum = 10 + 20 # IntegerBox + IntegerBox = IntegerBox
|
||||||
|
concat = "Hi" + " !" # StringBox + StringBox = StringBox
|
||||||
|
repeat = "Ha" * 3 # StringBox * IntegerBox = "HaHaHa"
|
||||||
|
mixed = 42 + " answer" # 混合型 → 自動文字列結合フォールバック
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **演算子優先順位**
|
||||||
|
```nyash
|
||||||
|
result = a + b * c / d - e # 算術演算子は標準的優先順位
|
||||||
|
logic = not a and b or c # not > and > or
|
||||||
|
compare = (x > y) and (z <= w) # 比較は括弧推奨
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **論理演算子**
|
||||||
|
```nyash
|
||||||
|
# キーワード版(推奨)
|
||||||
|
canAccess = level >= 5 and hasKey
|
||||||
|
isValid = not (isEmpty or hasError)
|
||||||
|
|
||||||
|
# シンボル版(互換)
|
||||||
|
result = condition && other || fallback # 利用可能だが非推奨
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🏗️ **3. Box構文詳細ガイド**
|
||||||
|
|
||||||
|
### **3.1 Everything is Box 原則**
|
||||||
|
|
||||||
|
```nyash
|
||||||
|
# すべての値がBox
|
||||||
|
number = 42 # IntegerBox
|
||||||
|
text = "hello" # StringBox
|
||||||
|
flag = true # BoolBox
|
||||||
|
array = new ArrayBox() # ArrayBox
|
||||||
|
console = new ConsoleBox() # ConsoleBox
|
||||||
|
|
||||||
|
# 統一的なメソッド呼び出し
|
||||||
|
print(number.to_string_box().value) # "42"
|
||||||
|
print(array.length()) # 配列長
|
||||||
|
console.log("Everything is Box!") # コンソール出力
|
||||||
|
```
|
||||||
|
|
||||||
|
### **3.2 コンストラクタパターン**
|
||||||
|
|
||||||
|
#### **パラメータ付きコンストラクタ**
|
||||||
|
```nyash
|
||||||
|
box Person {
|
||||||
|
init { name, age, email }
|
||||||
|
|
||||||
|
init(personName, personAge) { # init構文に統一
|
||||||
|
me.name = personName
|
||||||
|
me.age = personAge
|
||||||
|
me.email = me.name + "@example.com" # 計算フィールド
|
||||||
|
}
|
||||||
|
|
||||||
|
# ファクトリーメソッド
|
||||||
|
static createGuest() {
|
||||||
|
outbox guest
|
||||||
|
guest = new Person("Guest", 0)
|
||||||
|
return guest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 使用例
|
||||||
|
person = new Person("Alice", 25)
|
||||||
|
guest = Person.createGuest()
|
||||||
|
```
|
||||||
|
|
||||||
|
### **3.3 継承とインターフェース**
|
||||||
|
|
||||||
|
#### **デリゲーションチェーン**
|
||||||
|
```nyash
|
||||||
|
# 基底Box
|
||||||
|
box Animal {
|
||||||
|
init { name, species }
|
||||||
|
|
||||||
|
init(animalName, animalSpecies) {
|
||||||
|
me.name = animalName
|
||||||
|
me.species = animalSpecies
|
||||||
|
}
|
||||||
|
|
||||||
|
speak() {
|
||||||
|
return me.name + " makes a sound"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# デリゲーション
|
||||||
|
box Dog from Animal {
|
||||||
|
init { breed } # 追加フィールド
|
||||||
|
|
||||||
|
init(dogName, dogBreed) {
|
||||||
|
from Animal.init(dogName, "Canine") # 親コンストラクタ呼び出し
|
||||||
|
me.breed = dogBreed
|
||||||
|
}
|
||||||
|
|
||||||
|
override speak() { # 明示的オーバーライド
|
||||||
|
return me.name + " barks: Woof!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# インターフェース実装
|
||||||
|
box Cat from Animal interface Playful {
|
||||||
|
# Playfulインターフェースの実装必須
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### **3.4 Static Boxパターン**
|
||||||
|
|
||||||
|
#### **名前空間・ユーティリティ**
|
||||||
|
```nyash
|
||||||
|
static box MathUtils {
|
||||||
|
init { PI, E }
|
||||||
|
|
||||||
|
static {
|
||||||
|
me.PI = 3.14159265
|
||||||
|
me.E = 2.71828182
|
||||||
|
}
|
||||||
|
|
||||||
|
add(a, b) {
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
circleArea(radius) {
|
||||||
|
return me.PI * radius * radius
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 使用法
|
||||||
|
area = MathUtils.circleArea(5)
|
||||||
|
sum = MathUtils.add(10, 20)
|
||||||
|
pi = MathUtils.PI
|
||||||
|
```
|
||||||
|
|
||||||
|
#### **アプリケーションエントリーポイント**
|
||||||
|
```nyash
|
||||||
|
# 🎯 推奨: Static Box Main パターン
|
||||||
|
static box Main {
|
||||||
|
init { console, result }
|
||||||
|
|
||||||
|
main() {
|
||||||
|
me.console = new ConsoleBox()
|
||||||
|
me.console.log("🚀 Starting application...")
|
||||||
|
|
||||||
|
# アプリケーションロジック
|
||||||
|
me.result = processData()
|
||||||
|
|
||||||
|
return "Application completed successfully"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🚀 **4. 最新機能・革新技術**
|
||||||
|
|
||||||
|
### **4.1 Arc<Mutex> Revolution(2025-08-10)**
|
||||||
|
```nyash
|
||||||
|
# 全16種類のBox型が統一Arc<Mutex>パターンで実装
|
||||||
|
# 完全なスレッドセーフティと高性能を両立
|
||||||
|
|
||||||
|
array = new ArrayBox()
|
||||||
|
array.push(10) # スレッドセーフな追加
|
||||||
|
array.push(20)
|
||||||
|
item = array.get(0) # スレッドセーフな取得
|
||||||
|
|
||||||
|
json = new JSONBox()
|
||||||
|
json.set("name", "Alice") # 並行安全な操作
|
||||||
|
data = json.stringify() # JSON文字列化
|
||||||
|
```
|
||||||
|
|
||||||
|
### **4.2 Rust風トレイトベース演算子(2025-08-10)**
|
||||||
|
```nyash
|
||||||
|
# AI大相談会で決定された最適設計
|
||||||
|
# 静的・動的ハイブリッドディスパッチによる高性能実現
|
||||||
|
|
||||||
|
# 整数演算
|
||||||
|
result = 100 - 25 # IntegerBox間演算 → IntegerBox
|
||||||
|
product = 6 * 7 # 高速静的ディスパッチ
|
||||||
|
|
||||||
|
# 文字列操作
|
||||||
|
greeting = "Hello" + " World" # 文字列結合
|
||||||
|
repeated = "Echo" * 3 # "EchoEchoEcho"
|
||||||
|
|
||||||
|
# 混合型フォールバック
|
||||||
|
message = "Answer: " + 42 # "Answer: 42"
|
||||||
|
|
||||||
|
# Boolean演算
|
||||||
|
boolSum = true + false # 1 (IntegerBox)
|
||||||
|
```
|
||||||
|
|
||||||
|
### **4.3 変数宣言厳密化(2025-08-09)**
|
||||||
|
```nyash
|
||||||
|
# メモリ安全性・非同期安全性保証システム
|
||||||
|
|
||||||
|
static box Calculator {
|
||||||
|
init { memory } # 必須フィールド宣言
|
||||||
|
|
||||||
|
calculate() {
|
||||||
|
local temp # 必須ローカル変数宣言
|
||||||
|
temp = me.memory * 2
|
||||||
|
return temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ⚡ **5. 実装済みBox型ライブラリ**
|
||||||
|
|
||||||
|
### **5.1 基本型**
|
||||||
|
- `StringBox` - 文字列(split, find, replace, trim等)
|
||||||
|
- `IntegerBox` - 64bit整数
|
||||||
|
- `BoolBox` - 真偽値
|
||||||
|
- `VoidBox` - null/void値
|
||||||
|
|
||||||
|
### **5.2 コレクション**
|
||||||
|
- `ArrayBox` - 動的配列(push, pop, get, set, join等)
|
||||||
|
- `MapBox` - 連想配列・辞書
|
||||||
|
|
||||||
|
### **5.3 システム・I/O**
|
||||||
|
- `ConsoleBox` - コンソール入出力
|
||||||
|
- `DebugBox` - デバッグ支援・メモリ追跡
|
||||||
|
- `FileBox` - ファイルシステム操作
|
||||||
|
|
||||||
|
### **5.4 数学・時間**
|
||||||
|
- `MathBox` - 数学関数(sin, cos, log, sqrt等)
|
||||||
|
- `TimeBox` - 時刻操作・タイマー
|
||||||
|
- `RandomBox` - 乱数生成・選択・シャッフル
|
||||||
|
|
||||||
|
### **5.5 データ処理**
|
||||||
|
- `JSONBox` - JSON解析・生成(parse, stringify, get, set)
|
||||||
|
- `RegexBox` - 正規表現(test, find, replace, split)
|
||||||
|
- `BufferBox` - バイナリデータ処理
|
||||||
|
- `StreamBox` - ストリーム処理
|
||||||
|
|
||||||
|
### **5.6 ネットワーク・Web**
|
||||||
|
- `HttpClientBox` - HTTP通信
|
||||||
|
- `WebDisplayBox` - HTML表示(WASM)
|
||||||
|
- `WebConsoleBox` - ブラウザコンソール(WASM)
|
||||||
|
- `WebCanvasBox` - Canvas描画(WASM)
|
||||||
|
|
||||||
|
### **5.7 GUI・マルチメディア**
|
||||||
|
- `EguiBox` - デスクトップGUI(Windows/Linux)
|
||||||
|
- `SoundBox` - 音声再生
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎯 **6. パフォーマンス・デザイン原則**
|
||||||
|
|
||||||
|
### **6.1 メモリ安全性**
|
||||||
|
- Rust所有権システムによる完全なメモリ安全性
|
||||||
|
- Arc<Mutex>によるスレッドセーフな共有状態管理
|
||||||
|
- 自動参照カウント + 明示的デストラクタ(fini)
|
||||||
|
|
||||||
|
### **6.2 実行効率**
|
||||||
|
- 統一されたBox型システムによる最適化
|
||||||
|
- 静的・動的ハイブリッドディスパッチで高速演算
|
||||||
|
- パーサー無限ループ対策(--debug-fuel)
|
||||||
|
|
||||||
|
### **6.3 開発効率**
|
||||||
|
- 変数宣言厳密化による早期エラー検出
|
||||||
|
- 包括的デバッグ機能(DebugBox)
|
||||||
|
- 直感的な"Everything is Box"概念
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📚 **7. 学習パス・ベストプラクティス**
|
||||||
|
|
||||||
|
### **7.1 初心者向け学習順序**
|
||||||
|
1. **基本概念**: Everything is Box哲学理解
|
||||||
|
2. **基本構文**: 変数宣言・制御構文・演算子
|
||||||
|
3. **Box定義**: 基本的なクラス作成
|
||||||
|
4. **Static Box Main**: アプリケーションエントリーポイント
|
||||||
|
5. **継承・インターフェース**: オブジェクト指向機能
|
||||||
|
|
||||||
|
### **7.2 推奨コーディングスタイル**
|
||||||
|
```nyash
|
||||||
|
# ✅ 推奨スタイル
|
||||||
|
static box Main {
|
||||||
|
init { console, result } # フィールド明示
|
||||||
|
|
||||||
|
main() {
|
||||||
|
me.console = new ConsoleBox()
|
||||||
|
|
||||||
|
local data # 変数事前宣言
|
||||||
|
data = processInput()
|
||||||
|
|
||||||
|
me.result = data # 明確な代入
|
||||||
|
return "Success"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### **7.3 よくある間違いと対策**
|
||||||
|
```nyash
|
||||||
|
# ❌ よくある間違い
|
||||||
|
init { field1 field2 } # カンマなし → CPU暴走
|
||||||
|
x = 42 # 変数未宣言 → ランタイムエラー
|
||||||
|
while condition { } # 非対応構文 → パーサーエラー
|
||||||
|
|
||||||
|
# ✅ 正しい書き方
|
||||||
|
init { field1, field2 } # カンマ必須
|
||||||
|
local x = 42 # 事前宣言
|
||||||
|
loop(condition) { } # 統一ループ構文
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**🎉 Nyash 2025は、AI協働設計による最先端言語システムとして、シンプルさと強力さを完全に両立しました。**
|
||||||
|
|
||||||
|
*最終更新: 2025年8月10日 - Arc<Mutex> Revolution + AI大相談会成功記念*
|
||||||
338
docs/reference/override-delegation-syntax.md
Normal file
338
docs/reference/override-delegation-syntax.md
Normal file
@ -0,0 +1,338 @@
|
|||||||
|
# 🌟 Nyash Override + Delegation 統一構文仕様
|
||||||
|
|
||||||
|
バージョン: 2.0
|
||||||
|
作成日: 2025年8月11日
|
||||||
|
ステータス: 正式決定
|
||||||
|
|
||||||
|
## 📋 概要
|
||||||
|
|
||||||
|
Nyashプログラミング言語における明示的オーバーライドとデリゲーション構文の完全仕様。世界初の**完全明示デリゲーション言語**としてのNyashの核心機能を定義する。
|
||||||
|
|
||||||
|
## 🎯 設計哲学
|
||||||
|
|
||||||
|
### 基本原則
|
||||||
|
1. **完全明示性**: すべての動作を明示的に宣言
|
||||||
|
2. **曖昧性の完全排除**: 暗黙の動作は一切許可しない
|
||||||
|
3. **統一構文**: デリゲーションとオーバーライドの完全統合
|
||||||
|
4. **初学者フレンドリー**: 直感的で理解しやすい構文
|
||||||
|
|
||||||
|
### Everything is Box との整合性
|
||||||
|
- すべてのオブジェクトがBox
|
||||||
|
- デリゲーション先もBox
|
||||||
|
- オーバーライドもBoxメソッドの置換
|
||||||
|
|
||||||
|
## 🔥 基本構文
|
||||||
|
|
||||||
|
### デリゲーション宣言
|
||||||
|
```nyash
|
||||||
|
box Child from Parent {
|
||||||
|
// 親Boxからの機能デリゲーション
|
||||||
|
}
|
||||||
|
|
||||||
|
// 多重デリゲーション
|
||||||
|
box Child from Parent1, Parent2 {
|
||||||
|
// 複数のBoxからの機能デリゲーション
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### メソッドオーバーライド
|
||||||
|
```nyash
|
||||||
|
box Child from Parent {
|
||||||
|
// 必須: overrideキーワードによる明示的宣言
|
||||||
|
override methodName(params) {
|
||||||
|
// オーバーライド実装
|
||||||
|
from Parent.methodName(params) // 親実装呼び出し(任意)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### コンストラクタデリゲーション
|
||||||
|
```nyash
|
||||||
|
box Child from Parent {
|
||||||
|
init(params) { # init構文に統一
|
||||||
|
from Parent.init(params) # 必須: 親コンストラクタ明示呼び出し
|
||||||
|
me.childSpecificField = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📚 詳細仕様
|
||||||
|
|
||||||
|
### 1. Override キーワード
|
||||||
|
|
||||||
|
#### 必須条件
|
||||||
|
- 親Box(デリゲーション先)に同名メソッドが存在する場合のみ使用可能
|
||||||
|
- 同名メソッドが存在しない場合は**コンパイルエラー**
|
||||||
|
|
||||||
|
#### 禁止事項
|
||||||
|
- 同一Box内での同名メソッド重複定義は**すべてエラー**
|
||||||
|
- 暗黙のオーバーライドは**完全禁止**
|
||||||
|
|
||||||
|
#### 構文例
|
||||||
|
```nyash
|
||||||
|
box MeshNode from P2PBox {
|
||||||
|
// ✅ 正しい使用法
|
||||||
|
override send(intent, data, target) {
|
||||||
|
me.routing.log(target)
|
||||||
|
from P2PBox.send(intent, data, target)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ❌ エラー: P2PBoxに存在しないメソッド
|
||||||
|
override nonExistentMethod() {
|
||||||
|
// Error: Method 'nonExistentMethod' does not exist in parent P2PBox
|
||||||
|
}
|
||||||
|
|
||||||
|
// ❌ エラー: overrideなしで親メソッドと同名
|
||||||
|
send(intent, data, target) {
|
||||||
|
// Error: Method 'send' overrides parent method. Add 'override' keyword.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. From デリゲーション構文
|
||||||
|
|
||||||
|
#### 基本構文
|
||||||
|
```nyash
|
||||||
|
from ParentBox.methodName(args)
|
||||||
|
from ParentBox.fieldName
|
||||||
|
```
|
||||||
|
|
||||||
|
#### メソッド呼び出し
|
||||||
|
```nyash
|
||||||
|
// 親の特定メソッドを明示的に呼び出し
|
||||||
|
from P2PBox.send(intent, data, target)
|
||||||
|
|
||||||
|
// 複数親からの呼び出し
|
||||||
|
from Logger.log("Starting operation")
|
||||||
|
from P2PBox.send(intent, data, target)
|
||||||
|
from Cache.store(data)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### フィールドアクセス
|
||||||
|
```nyash
|
||||||
|
// 親のフィールドへのアクセス
|
||||||
|
local status = from P2PBox.connectionStatus
|
||||||
|
from Logger.logLevel = "DEBUG"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### コンストラクタ呼び出し
|
||||||
|
```nyash
|
||||||
|
init(nodeId, world) { # init構文に統一
|
||||||
|
from P2PBox.init(nodeId, world) # 完全統一構文
|
||||||
|
me.routing = RoutingTable()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 多重デリゲーション
|
||||||
|
|
||||||
|
#### 基本形式
|
||||||
|
```nyash
|
||||||
|
box ComplexNode from P2PBox, Logger, Cache {
|
||||||
|
override send(intent, data, target) {
|
||||||
|
from Logger.debug("Sending: " + intent) // Logger親から
|
||||||
|
from Cache.store(intent, data) // Cache親から
|
||||||
|
from P2PBox.send(intent, data, target) // P2PBox親から
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 曖昧性の解消
|
||||||
|
```nyash
|
||||||
|
// 複数親に同名メソッドが存在する場合
|
||||||
|
box ConflictNode from ParentA, ParentB {
|
||||||
|
// ❌ エラー: どちらのprocessを置換するか不明
|
||||||
|
override process(data) {
|
||||||
|
// Error: Method 'process' exists in multiple parents. Use specific parent.
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ 正しい解決法: 親を明示指定
|
||||||
|
override ParentA.process(data) {
|
||||||
|
// ParentAのprocessをオーバーライド
|
||||||
|
from ParentA.process(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
override ParentB.process(data) {
|
||||||
|
// ParentBのprocessをオーバーライド
|
||||||
|
from ParentB.process(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. エラーメッセージ仕様
|
||||||
|
|
||||||
|
#### 重複定義エラー
|
||||||
|
```
|
||||||
|
Error: Method 'send' is already defined in this box at line 15.
|
||||||
|
--> box.nyash:20:5
|
||||||
|
|
|
||||||
|
20 | send(msg) {
|
||||||
|
| ^^^^ duplicate method definition
|
||||||
|
|
|
||||||
|
Help: Remove duplicate definition or rename method.
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Missing Override エラー
|
||||||
|
```
|
||||||
|
Error: Method 'send' overrides a parent method. Add 'override' keyword.
|
||||||
|
--> box.nyash:18:5
|
||||||
|
|
|
||||||
|
18 | send(intent, data, target) {
|
||||||
|
| ^^^^ missing 'override' keyword
|
||||||
|
|
|
||||||
|
Help: Change to 'override send(intent, data, target) {'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Wrong Override エラー
|
||||||
|
```
|
||||||
|
Error: Method 'newMethod' does not exist in any parent. Remove 'override' keyword.
|
||||||
|
--> box.nyash:22:5
|
||||||
|
|
|
||||||
|
22 | override newMethod() {
|
||||||
|
| ^^^^^^^^ unnecessary 'override'
|
||||||
|
|
|
||||||
|
Help: Remove 'override' or verify parent method name.
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 曖昧Override エラー
|
||||||
|
```
|
||||||
|
Error: Method 'process' exists in multiple parents. Specify which parent to override.
|
||||||
|
--> box.nyash:25:5
|
||||||
|
|
|
||||||
|
25 | override process(data) {
|
||||||
|
| ^^^^^^^^ ambiguous override
|
||||||
|
|
|
||||||
|
Help: Use 'override ParentA.process' or 'override ParentB.process'
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🚫 禁止事項
|
||||||
|
|
||||||
|
### 1. 暗黙のオーバーライド
|
||||||
|
```nyash
|
||||||
|
box Child from Parent {
|
||||||
|
send(msg) { // ❌ エラー: overrideキーワードなし
|
||||||
|
print("Child implementation")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. コンストラクタオーバーロード
|
||||||
|
```nyash
|
||||||
|
box Node {
|
||||||
|
init(id) { // 最初の定義
|
||||||
|
me.id = id
|
||||||
|
}
|
||||||
|
|
||||||
|
init(id, name) { // ❌ エラー: 重複定義
|
||||||
|
me.id = id
|
||||||
|
me.name = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 同名メソッド重複定義
|
||||||
|
```nyash
|
||||||
|
box Example {
|
||||||
|
process(data) { // 最初の定義
|
||||||
|
print("Version 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
process(data) { // ❌ エラー: 重複定義
|
||||||
|
print("Version 2")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## ✅ ベストプラクティス
|
||||||
|
|
||||||
|
### 1. 明示的な親呼び出し
|
||||||
|
```nyash
|
||||||
|
box MeshNode from P2PBox {
|
||||||
|
override send(intent, data, target) {
|
||||||
|
// 前処理
|
||||||
|
me.routing.logOutgoing(target)
|
||||||
|
|
||||||
|
// 親実装呼び出し(明示的)
|
||||||
|
from P2PBox.send(intent, data, target)
|
||||||
|
|
||||||
|
// 後処理
|
||||||
|
me.statistics.incrementSentCount()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 多重デリゲーションでの順序指定
|
||||||
|
```nyash
|
||||||
|
box SmartNode from P2PBox, Logger, Cache {
|
||||||
|
override send(intent, data, target) {
|
||||||
|
// 1. ログ記録
|
||||||
|
from Logger.info("Sending to: " + target)
|
||||||
|
|
||||||
|
// 2. キャッシュ保存
|
||||||
|
from Cache.store(intent + ":" + target, data)
|
||||||
|
|
||||||
|
// 3. 実際の送信
|
||||||
|
from P2PBox.send(intent, data, target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. コンストラクタチェーン
|
||||||
|
```nyash
|
||||||
|
box SecureNode from P2PBox {
|
||||||
|
init security = SecurityManager()
|
||||||
|
|
||||||
|
init(nodeId, world, keyFile) { # init構文に統一
|
||||||
|
// 1. 親初期化(必須)
|
||||||
|
from P2PBox.init(nodeId, world)
|
||||||
|
|
||||||
|
// 2. 子固有の初期化
|
||||||
|
me.security = SecurityManager()
|
||||||
|
me.security.loadKeys(keyFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔮 将来の拡張
|
||||||
|
|
||||||
|
### 1. Final メソッド(検討中)
|
||||||
|
```nyash
|
||||||
|
box Parent {
|
||||||
|
final criticalMethod() { // オーバーライド禁止
|
||||||
|
// 重要な処理
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Abstract メソッド(検討中)
|
||||||
|
```nyash
|
||||||
|
box AbstractParent {
|
||||||
|
abstract process(data) // 子でのoverride必須
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Override チェック強化(検討中)
|
||||||
|
```nyash
|
||||||
|
override! send(data) { // 親呼び出し必須チェック
|
||||||
|
// from Parent.send(data) がないとエラー
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📊 他言語との比較
|
||||||
|
|
||||||
|
| 言語 | 継承方式 | オーバーライド | 親呼び出し | 多重継承 |
|
||||||
|
|------|----------|---------------|-----------|----------|
|
||||||
|
| **Nyash** | デリゲーション | `override` 必須 | `from Parent.method()` | 明示的解消 |
|
||||||
|
| Java | クラス継承 | `@Override` 注釈 | `super.method()` | 不可 |
|
||||||
|
| Python | クラス継承 | 暗黙 | `super().method()` | MRO(複雑) |
|
||||||
|
| C# | クラス継承 | `override` 必須 | `base.method()` | 不可 |
|
||||||
|
| TypeScript | プロトタイプ | 暗黙 | `super.method()` | 不可 |
|
||||||
|
|
||||||
|
### Nyashの優位性
|
||||||
|
1. **完全な明示性**: すべての動作が明確
|
||||||
|
2. **曖昧性の完全排除**: 多重デリゲーションでも安全
|
||||||
|
3. **統一構文**: デリゲーションとオーバーライドが統合
|
||||||
|
4. **初学者フレンドリー**: 分かりやすいエラーメッセージ
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**この仕様により、Nyashは世界初の「完全明示デリゲーション言語」として、プログラミング言語史に新たな1ページを刻むことになります。** 🌟
|
||||||
Reference in New Issue
Block a user