feat(plugin): Fix plugin BoxRef return and Box argument support
- Fixed deadlock in FileBox plugin copyFrom implementation (single lock) - Added TLV Handle (tag=8) parsing in calls.rs for returned BoxRefs - Improved plugin loader with config path consistency and detailed logging - Fixed loader routing for proper Handle type_id/fini_method_id resolution - Added detailed logging for TLV encoding/decoding in plugin_loader_v2 Test docs/examples/plugin_boxref_return.nyash now works correctly: - cloneSelf() returns FileBox Handle properly - copyFrom(Box) accepts plugin Box arguments - Both FileBox instances close and fini correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
337
docs/reference/core-language/design-philosophy.md
Normal file
337
docs/reference/core-language/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/core-language/language-reference.md
Normal file
525
docs/reference/core-language/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大相談会成功記念*
|
||||
525
docs/reference/core-language/language_spec.md
Normal file
525
docs/reference/core-language/language_spec.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/core-language/override-delegation-syntax.md
Normal file
338
docs/reference/core-language/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ページを刻むことになります。** 🌟
|
||||
295
docs/reference/core-language/portability-contract.md
Normal file
295
docs/reference/core-language/portability-contract.md
Normal file
@ -0,0 +1,295 @@
|
||||
# 🤝 Nyash Portability Contract v0
|
||||
|
||||
*ChatGPT5アドバイス・全バックエンド互換性保証仕様*
|
||||
|
||||
## 🎯 目的
|
||||
|
||||
**「nyash --target= interp / vm / wasm / aot-rust / jit-cranelift」で同一プログラムが同一結果を保証**
|
||||
|
||||
全バックエンドでNyashプログラムが確実に動作し、最適化レベルに関係なく**決定的で予測可能な実行**を実現。
|
||||
|
||||
## 🔧 **Contract v0 仕様**
|
||||
|
||||
### 1️⃣ **決定的破棄(Deterministic Finalization)**
|
||||
|
||||
#### **強参照のみ伝播保証**
|
||||
```rust
|
||||
// ✅ 保証される動作
|
||||
box Parent {
|
||||
child_strong: ChildBox // 強参照→破棄連鎖
|
||||
}
|
||||
|
||||
parent.fini() // 必ずchild_strong.fini()も呼ばれる
|
||||
```
|
||||
|
||||
#### **破棄順序の決定性**
|
||||
```nyash
|
||||
// 破棄順序: 最新→最古(スタック順序)
|
||||
box Child from Parent {
|
||||
init { data }
|
||||
pack() {
|
||||
from Parent.pack() // 1. Parent初期化
|
||||
me.data = "child" // 2. Child初期化
|
||||
}
|
||||
// fini順序: 2→1(逆順破棄)
|
||||
}
|
||||
```
|
||||
|
||||
#### **例外安全性**
|
||||
```rust
|
||||
pub enum FinalizationGuarantee {
|
||||
AlwaysExecuted, // fini()は例外時も必ず実行
|
||||
NoDoubleDestroy, // 同一オブジェクトの二重破棄禁止
|
||||
OrderPreserved, // 初期化と逆順での破棄保証
|
||||
}
|
||||
```
|
||||
|
||||
### 2️⃣ **weak参照の非伝播+生存チェック**
|
||||
|
||||
#### **非伝播保証**
|
||||
```nyash
|
||||
box Parent {
|
||||
init { child_weak }
|
||||
|
||||
pack() {
|
||||
local child = new Child()
|
||||
me.child_weak = weak(child) // weak参照生成
|
||||
// child がfini()されても Parent は影響なし
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### **生存チェック必須**
|
||||
```mir
|
||||
// MIR レベルでの生存チェック
|
||||
%alive = weak_load %weak_ref
|
||||
br %alive -> %use_bb, %null_bb
|
||||
|
||||
%use_bb:
|
||||
// weak参照が有効な場合の処理
|
||||
%value = /* weak_refの値使用 */
|
||||
jmp %continue_bb
|
||||
|
||||
%null_bb:
|
||||
// weak参照が無効な場合の処理
|
||||
%value = const null
|
||||
jmp %continue_bb
|
||||
|
||||
%continue_bb:
|
||||
// 合流地点(Phi必須)
|
||||
%result = phi [%value from %use_bb, %value from %null_bb]
|
||||
```
|
||||
|
||||
#### **自動null化契約**
|
||||
```rust
|
||||
pub struct WeakContract {
|
||||
auto_nullification: true, // 参照先fini()時に自動null
|
||||
no_dangling_pointers: true, // ダングリングポインタ禁止
|
||||
thread_safe_access: true, // マルチスレッド安全アクセス
|
||||
}
|
||||
```
|
||||
|
||||
### 3️⃣ **Effect意味論(最適化可能性)**
|
||||
|
||||
#### **Effect分類契約**
|
||||
```rust
|
||||
pub enum EffectLevel {
|
||||
Pure, // 副作用なし→並び替え・除去・重複実行可能
|
||||
Mut, // メモリ変更→順序保証必要・並列化制限
|
||||
Io, // I/O操作→実行順序厳密保証・キャッシュ禁止
|
||||
Bus, // 分散通信→elision対象・ネットワーク最適化可能
|
||||
}
|
||||
```
|
||||
|
||||
#### **最適化契約**
|
||||
```mir
|
||||
// Pure関数→最適化可能
|
||||
%result1 = call @pure_function(%arg) effects=[PURE]
|
||||
%result2 = call @pure_function(%arg) effects=[PURE]
|
||||
// → 最適化: %result2 = copy %result1
|
||||
|
||||
// Mut操作→順序保証
|
||||
store %value1 -> %ptr effects=[MUT]
|
||||
store %value2 -> %ptr effects=[MUT]
|
||||
// → 順序維持必須
|
||||
|
||||
// Bus操作→elision対象
|
||||
send %bus, %message effects=[BUS]
|
||||
// → ネットワーク最適化・バッチ化可能
|
||||
```
|
||||
|
||||
### 4️⃣ **Bus-elision基盤契約**
|
||||
|
||||
#### **elision ON/OFF同一結果保証**
|
||||
```bash
|
||||
# 最適化ON→高速実行
|
||||
nyash --elide-bus --target wasm program.nyash
|
||||
|
||||
# 最適化OFF→完全分散実行
|
||||
nyash --no-elide-bus --target vm program.nyash
|
||||
|
||||
# 結果は必ず同一(契約保証)
|
||||
```
|
||||
|
||||
#### **Bus操作の意味保証**
|
||||
```mir
|
||||
// Bus送信の意味論
|
||||
send %bus, %message effects=[BUS] {
|
||||
// elision OFF: 実際のネットワーク送信
|
||||
// elision ON: ローカル最適化(結果同一)
|
||||
}
|
||||
|
||||
// Bus受信の意味論
|
||||
%msg = recv %bus effects=[BUS] {
|
||||
// elision OFF: ネットワーク受信待ち
|
||||
// elision ON: ローカル値返却(結果同一)
|
||||
}
|
||||
```
|
||||
|
||||
## 🧪 **Contract検証システム**
|
||||
|
||||
### **互換テストスイート**
|
||||
```rust
|
||||
// tests/portability_contract_tests.rs
|
||||
#[test]
|
||||
fn test_deterministic_finalization() {
|
||||
let program = "/* fini順序テスト */";
|
||||
|
||||
let interp_result = run_interpreter(program);
|
||||
let vm_result = run_vm(program);
|
||||
let wasm_result = run_wasm(program);
|
||||
|
||||
// 破棄順序・タイミングが全バックエンドで同一
|
||||
assert_eq!(interp_result.finalization_order, vm_result.finalization_order);
|
||||
assert_eq!(vm_result.finalization_order, wasm_result.finalization_order);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_weak_reference_semantics() {
|
||||
let program = "/* weak参照テスト */";
|
||||
|
||||
// 生存チェック・null化が全バックエンドで同一動作
|
||||
let results = run_all_backends(program);
|
||||
assert_all_equal(results.map(|r| r.weak_behavior));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_effect_optimization_equivalence() {
|
||||
let program = "/* Effect最適化テスト */";
|
||||
|
||||
// PURE関数の最適化結果が同一
|
||||
let optimized = run_with_optimization(program);
|
||||
let reference = run_without_optimization(program);
|
||||
assert_eq!(optimized.output, reference.output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bus_elision_equivalence() {
|
||||
let program = "/* Bus通信テスト */";
|
||||
|
||||
let elision_on = run_with_flag(program, "--elide-bus");
|
||||
let elision_off = run_with_flag(program, "--no-elide-bus");
|
||||
|
||||
// Bus最適化ON/OFFで結果同一
|
||||
assert_eq!(elision_on.output, elision_off.output);
|
||||
}
|
||||
```
|
||||
|
||||
### **Golden Dump検証**
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# scripts/verify_portability_contract.sh
|
||||
|
||||
echo "🧪 Portability Contract v0 検証中..."
|
||||
|
||||
# 1. MIR出力一致検証
|
||||
nyash --dump-mir test.nyash > golden.mir
|
||||
nyash --dump-mir test.nyash > current.mir
|
||||
if ! diff golden.mir current.mir; then
|
||||
echo "❌ MIR回帰エラー検出"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 2. 全バックエンド同一出力
|
||||
declare -a backends=("interp" "vm" "wasm")
|
||||
for backend in "${backends[@]}"; do
|
||||
nyash --target $backend test.nyash > ${backend}.out
|
||||
done
|
||||
|
||||
# 出力一致確認
|
||||
if diff interp.out vm.out && diff vm.out wasm.out; then
|
||||
echo "✅ 全バックエンド出力一致"
|
||||
else
|
||||
echo "❌ バックエンド出力差異検出"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 3. Bus-elision検証
|
||||
nyash --elide-bus test.nyash > elision_on.out
|
||||
nyash --no-elide-bus test.nyash > elision_off.out
|
||||
if diff elision_on.out elision_off.out; then
|
||||
echo "✅ Bus-elision同一結果"
|
||||
else
|
||||
echo "❌ Bus-elision結果差異"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "🎉 Portability Contract v0 検証完了"
|
||||
```
|
||||
|
||||
## 📊 **Contract適合レベル**
|
||||
|
||||
### **Tier-0: 基本互換性**
|
||||
- [ ] **決定的破棄**: fini()順序がバックエンド間で同一
|
||||
- [ ] **weak非伝播**: weak参照が親破棄に影響しない
|
||||
- [ ] **基本Effect**: PURE/MUT/IO の意味論統一
|
||||
- [ ] **出力一致**: 同一プログラム→同一標準出力
|
||||
|
||||
### **Tier-1: 最適化互換性**
|
||||
- [ ] **PURE最適化**: 純粋関数の除去・移動がバックエンド間で同等
|
||||
- [ ] **weak生存チェック**: 全バックエンドで同一タイミング
|
||||
- [ ] **Bus-elision**: ON/OFF切り替えで結果同一
|
||||
- [ ] **性能予測**: 最適化レベル差が定量的
|
||||
|
||||
### **Tier-2: 高度互換性**
|
||||
- [ ] **メモリレイアウト**: Box構造がバックエンド間で互換
|
||||
- [ ] **エラー処理**: 例外・パニックが同一動作
|
||||
- [ ] **並行性**: Future/awaitが同一意味論
|
||||
- [ ] **デバッグ**: スタックトレース・診断情報が同等
|
||||
|
||||
## ⚡ **実装優先順位**
|
||||
|
||||
### **Phase 8.4(今すぐ)**
|
||||
1. **Tier-0契約実装**: 基本互換性確保
|
||||
2. **Golden dump自動化**: CI/CDで回帰検出
|
||||
3. **Bus命令設計**: elision基盤構築
|
||||
|
||||
### **Phase 8.5(短期)**
|
||||
1. **Tier-1契約実装**: 最適化互換性
|
||||
2. **性能ベンチマーク**: 契約準拠性測定
|
||||
3. **エラー契約**: 例外処理統一
|
||||
|
||||
### **Phase 9+(中長期)**
|
||||
1. **Tier-2契約実装**: 高度互換性
|
||||
2. **形式検証**: 契約の数学的証明
|
||||
3. **認証システム**: 契約適合認定
|
||||
|
||||
---
|
||||
|
||||
## 🎯 **期待効果**
|
||||
|
||||
### **開発者体験**
|
||||
- **予測可能性**: どのバックエンドでも同一動作保証
|
||||
- **デバッグ容易性**: バックエンド切り替えで問題切り分け
|
||||
- **最適化信頼性**: 高速化しても結果不変保証
|
||||
|
||||
### **Nyash言語価値**
|
||||
- **差別化**: 「全バックエンド互換」言語として独自性
|
||||
- **信頼性**: エンタープライズ採用の技術的根拠
|
||||
- **拡張性**: 新バックエンド追加時の品質保証
|
||||
|
||||
---
|
||||
|
||||
*最終更新: 2025-08-14 - ChatGPT5アドバイス完全実装*
|
||||
|
||||
*「Everything is Box」×「全バックエンド互換」= Nyashの技術的優位性*
|
||||
Reference in New Issue
Block a user