Files
hakorune/docs/guides/tutorials/2_language_guide.md
Moe Charm cc2a820af7 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>
2025-08-21 00:41:26 +09:00

5.9 KiB
Raw Blame History

📚 Nyash Programming Language - 完全ガイド

最終更新: 2025年8月12日 - Phase 1完了, P2P実装準備完了

📖 概要

Nyashは「Everything is Box」哲学に基づく革新的なプログラミング言語です。 Rust製インタープリターによる高性能実行と、直感的な構文により、学習しやすく実用的な言語として完成しました。

🎯 核心哲学: "Everything is Box"

# すべてのデータがBoxとして統一的に表現される
number = 42              # IntegerBox
text = "hello"           # StringBox  
flag = true              # BoolBox
array = new ArrayBox()   # ArrayBox
debug = new DebugBox()   # DebugBox

重要な利点:

  • 統一性: すべてのデータが共通インターフェース
  • メモリ安全性: Arcパターンで完全スレッドセーフ
  • 拡張性: 新しいBox型を容易に追加可能

🔤 言語構文リファレンス

🏷️ 変数宣言・スコープ

// ローカル変数宣言
local x, y
local name = "Alice"

// 所有権移転変数(関数戻り値用)
outbox result = compute()

// グローバル変数
global CONFIG = "dev"

🧮 演算子

// 算術演算子
a + b, a - b, a * b, a / b

// 比較演算子  
a == b, a != b, a < b, a > b, a <= b, a >= b

// 論理演算子
not condition, a and b, a or b

// Cross-type演算 (Phase 1で完全実装)
10 + 3.14              // → 13.14 (型変換)
"Value: " + 42         // → "Value: 42" (文字列連結)

🏗️ Box定義・デリゲーション

基本Box定義

box User {
    init { name, email }  // フィールド宣言
    
    pack(userName, userEmail) {  // 🎁 Box哲学の具現化
        me.name = userName
        me.email = userEmail  
    }
    
    greet() {
        print("Hello, " + me.name)
    }
}

デリゲーション (2025-08 革命)

box AdminUser from User {  // 🔥 from構文でデリゲーション
    init { permissions }
    
    pack(adminName, adminEmail, perms) {
        from User.pack(adminName, adminEmail)  // 親のpack呼び出し
        me.permissions = perms
    }
    
    override greet() {  // 明示的オーバーライド
        from User.greet()  // 親メソッド呼び出し
        print("Admin privileges: " + me.permissions)
    }
}

Static Box Main パターン

static box Main {
    init { console, result }
    
    main() {
        me.console = new ConsoleBox()
        me.console.log("🎉 Everything is Box!")
        return "Success!"
    }
}

🔄 制御構造

// 条件分岐
if condition {
    // 処理
} else {
    // 別処理
}

// ループ(唯一の正しい形式)
loop(condition) {
    // 処理
    if breakCondition {
        break
    }
}

📦 ビルトインBox型

基本型

  • StringBox: 文字列 ("hello")
  • IntegerBox: 整数 (42)
  • FloatBox: 浮動小数点数 (new FloatBox(3.14)) Phase 1完了
  • BoolBox: 真偽値 (true, false)
  • NullBox: NULL値 (null)

データ構造

  • ArrayBox: 配列 (new ArrayBox()) Phase 1で sort/reverse/indexOf/slice 実装
  • MapBox: 連想配列 (new MapBox())

ユーティリティ

  • ConsoleBox: コンソール出力 (new ConsoleBox())
  • DebugBox: デバッグ機能 (new DebugBox())
  • MathBox: 数学関数 (new MathBox())
  • TimeBox: 時刻処理 (new TimeBox())
  • DateTimeBox: 日時処理 (new DateTimeBox()) Phase 1で完全動作

高度機能

  • RandomBox: 乱数生成
  • BufferBox: バッファ操作
  • RegexBox: 正規表現
  • JSONBox: JSON処理
  • StreamBox: ストリーム処理

P2P通信 (Phase 2実装中)

  • IntentBox: 構造化メッセージ (実装予定)
  • P2PBox: P2P通信ード (実装予定)

🚀 実用例

データ処理例

// 配列操作
local numbers = new ArrayBox()
numbers.push(3)
numbers.push(1) 
numbers.push(2)
numbers.sort()  // [1, 2, 3]

// 型変換・演算
local result = 10 + new FloatBox(3.14)  // 13.14
print("Result: " + result.toString())

P2P通信例 (将来実装)

// P2Pード作成
local node_a = new P2PBox("alice", transport: "inprocess") 
local node_b = new P2PBox("bob", transport: "inprocess")

// メッセージ受信ハンドラ
node_b.on("chat.message", function(intent, from) {
    print("From " + from + ": " + intent.payload.text)
})

// 構造化メッセージ送信
local msg = new IntentBox("chat.message", { text: "Hello P2P!" })
node_a.send("bob", msg)  // → "From alice: Hello P2P!"

⚠️ 重要な注意点

必須のコンマ

// ✅ 正しい
init { field1, field2 }

// ❌ 間違いCPU暴走の原因
init { field1 field2 }

変数宣言厳密化

// ✅ 明示宣言必須
local x
x = 42

// ❌ 未宣言変数への代入はエラー
y = 42  // Runtime Error + 修正提案

ループ構文統一

// ✅ 唯一の正しい形式
loop(condition) { }

// ❌ 削除済み構文
while condition { }  // 使用不可

📈 実装状況 (2025-08-12)

Phase 1完了

  • FloatBox toString() メソッド
  • ArrayBox 改良 (sort/reverse/indexOf/slice)
  • Cross-type演算子システム
  • 包括的テストスイート (188行)

🚧 Phase 2実装中

  • IntentBox (構造化メッセージ)
  • MessageBus (プロセス内シングルトン)
  • P2PBox (P2P通信ード)

📋 将来予定

  • WebSocket/WebRTC通信
  • 非同期処理 (async/await)
  • 追加のBox型拡張

🎉 Nyashで「Everything is Box」の世界を体験しよう

📚 関連ドキュメント: