Files
hakorune/docs/development/roadmap/phases/phase-12.7/grammar-reform-final-decision.txt

231 lines
6.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

================================================================================
Nyash文法改革 - 最終決定事項(実装向け)
2025-09-03
================================================================================
【Phase 12.7: 文法改革の完全仕様】
================================================================================
1. 予約語リスト(最終版)
================================================================================
合計15個の予約語
1. box - Box定義
2. new - インスタンス生成
3. me - 自己参照
4. public - 公開指定(デフォルトは非公開)
5. if - 条件分岐
6. else - else節
7. loop - ループ(唯一の形式)
8. break - ループ脱出
9. continue - 次の反復へスキップ
10. peek - 分岐構文旧when※※※新決定※※※
11. return - 関数リターン
12. import - モジュール読み込み
13. from - デリゲーション/親メソッド呼び出し
14. birth - コンストラクタEverything is Box哲学
15. fn - 関数定義/関数Box生成
================================================================================
2. peek構文分岐構文
================================================================================
【基本構文】
peek <expression> {
<pattern> => <expression-or-block>
<pattern> => <expression-or-block>
else => <expression-or-block> // else必須
}
【3つの形式】
// 単一式
peek animal {
"dog" => bark()
"cat" => meow()
else => silent()
}
// ブロック式(最後の式が値)
peek type {
"error" => {
log("Error occurred")
local message = getErrorMessage()
notifyAdmin(message)
message // これがpeekの値
}
else => "ok"
}
// 関数Box新しいスコープ
peek operation {
"factorial" => fn(n) {
if n <= 1 { return 1 }
return n * me(n - 1) // meは関数Box自身
}(5)
else => fn() { return 1 }()
}
【重要な仕様】
- peekは「式」値を返す
- else節は必須非網羅エラーを防ぐ
- returnはpeekではなく関数から脱出
- =>の右側は式、ブロック、関数Boxのいずれか
- パターンは当初「リテラルの等値比較」のみ
================================================================================
3. フィールド宣言Box内
================================================================================
【基本形式】
box ClassName {
// フィールド宣言(最上部に配置)
fieldName: TypeBox // デフォルト非公開
public fieldName: TypeBox // 公開フィールド
// コンストラクタ
birth(params) {
// 初期化処理
}
// メソッド
methodName(params) {
// 処理
}
}
【具体例】
box Counter {
count: IntegerBox
public name: StringBox
cache: MapBox = new MapBox() // デフォルト値も可能
birth(name) {
me.name = name
me.count = 0
// cacheは既に初期化済み
}
}
================================================================================
4. デリゲーション構文
================================================================================
box Child from Parent {
additionalField: StringBox
birth(name, extra) {
from Parent.birth(name) // 親のコンストラクタ呼び出し
me.additionalField = extra
}
// メソッドオーバーライド(@overrideは将来検討
process() {
Parent::process() // 親メソッド呼び出し(::記法)
// または
from Parent.process() // 従来記法も可
}
}
================================================================================
5. fn関数Boxの拡張
================================================================================
【用途】
1. トップレベル関数定義
2. インライン関数Box作成クロージャ
【例】
// 通常の関数定義
fn add(a, b) {
return a + b
}
// インライン関数Box
local counter = fn() {
local count = 0
return {
increment: fn() { count = count + 1 },
get: fn() { return count }
}
}()
// peek内での使用
peek operation {
"make_adder" => fn(x) {
return fn(y) { return x + y } // クロージャ
}
else => fn() { return null }
}
【重要】
- fn{} は新しいスコープ関数Boxを作る
- {} だけは単なるブロック(スコープ共有)
- meの意味が変わる関数Box内では関数自身
================================================================================
6. その他の重要事項
================================================================================
【セミコロン】
- 基本的に不要(改行が文の区切り)
- 1行に複数文を書く場合のみ使用可
【変数宣言】
- local x = 42 // ローカル変数
- 未宣言変数への代入はエラー
【論理演算子】
- not, and, or を使用(!, &&, || は非推奨)
【型チェック】
- typeof()関数 + peek構文で実現
- 例: peek typeof(value) { "StringBox" => ... }
================================================================================
7. 実装優先順位
================================================================================
P0即実装:
1. パーサーにpeek構文追加
2. continue追加
3. フィールド宣言の name: Type 形式
4. birth統一
P1次フェーズ:
1. Parent::method() 記法
2. fn{} クロージャ完全実装
3. OptionBox/ResultBox標準化
P2将来検討:
1. パターンマッチング拡張
2. @override等の属性
3. 構文糖衣nyan等のエイリアス
================================================================================
8. パーサー実装への注意点
================================================================================
- peek <expr> { の識別
- => トークンの追加
- else必須チェック
- ブロックと関数Boxの区別fnキーワードの有無
- returnのスコープ最も内側の関数から脱出
================================================================================
9. MIR/VM/LLVM実装指針
================================================================================
【MIR】
- PeekExpr { scrutinee, arms: [(Pattern, Expr)], else_expr }
- Pattern は当初 Literal のみ
- 将来的に Pattern を拡張可能な設計に
【VM】
- 小規模: if-else連鎖
- 大規模: ジャンプテーブル最適化
【LLVM】
- 整数: switch命令
- 文字列: ハッシュテーブル or 二分探索
================================================================================