Files
hakorune/docs/development/roadmap/phases/phase-12.7/extreme-sugar-proposals.txt

302 lines
8.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
================================================================================
【目標】
自己ホスティングコンパイラを8万行→2万行に圧縮75%削減)
================================================================================
🎯 最優先実装(削減効果最大)
================================================================================
1. 暗黙変数 + パイプライン強化
================================================================================
【統一案】
- 暗黙変数: $_ Perlスタイルまたは単に _
- パイプライン最後引数自動注入
- プロパティ/メソッドアクセス短縮
// 現在48文字
local result = trim(uppercase(replace(input, "cat", "nyan")))
// 提案1: 暗黙変数32文字、-33%
local result = input |> replace(_, "cat", "nyan") |> uppercase() |> trim()
// 提案2: 最後引数自動28文字、-42%
local result = input |> replace("cat", "nyan") |> uppercase |> trim
// 提案3: プロパティアクセスAST処理で威力発揮
ast |> .left |> .value // ast.left.value と同じ
【実装コスト】
- パーサー: 最小限の変更
- 脱糖規則: x |> f(args) → f(args, x)
================================================================================
2. 高階関数専用演算子
================================================================================
【Gemini案 + Codex案の融合】
| 機能 | 演算子 | 例 | 削減率 |
|------|--------|---|---------|
| map | /: | list /: {$_*2} | -40% |
| filter | \: | list \: {$_>0} | -35% |
| reduce | // | nums // {$1+$2} | -45% |
// 現在72文字
local evens = list.filter(|x| x % 2 == 0).map(|x| x * x).reduce(|a,b| a + b)
// 提案38文字、-47%
local evens = list \: {$_%2==0} /: {$_*$_} // {$1+$2}
================================================================================
3. 演算子セクション(部分適用)
================================================================================
// 現在
list.map(|x| x + 1)
list.filter(|x| x > 0)
sorted_by(|a,b| a.key.cmp(b.key))
// 提案
list /: (+1)
list \: (>0)
sorted_by(by key) // byマクロ
【削減例】
- (+1) は |x| x+1 の短縮(-60%
- (>0) は |x| x>0 の短縮(-55%
================================================================================
4. 極短キーワードエイリアス
================================================================================
【必須短縮1文字化
| 元 | 新 | 例 |
|----|----|----|
| local | l | l x = 42 |
| return | ^ | ^ result |
| function | fn | fn add(a,b) |
【頻出Box操作】
| 元 | 新 | 例 |
|----|----|----|
| new | @ | @ StringBox("hello") |
| me. | $ | $.count = $.count + 1 |
// 現在128文字
function calculate(x, y) {
local temp = x + y
local result = temp * 2
return result
}
// 提案58文字、-55%
fn calculate(x,y) {
l t = x+y
^ t*2
}
================================================================================
5. リスト内包 + 分割代入の統合
================================================================================
// 現在(複数行)
local names = new ArrayBox()
for user in users {
if user.active {
names.push(user.name)
}
}
// 提案1: 基本内包27文字、-70%
l names = [u.name for u in users if u.active]
// 提案2: 暗黙変数版24文字、-75%
l names = [$.name for users if $.active]
// 提案3: 分割代入併用
l [{name,age}] = users \: {$.age>18} // 18歳以上の名前と年齢
================================================================================
🚀 革新的提案(更なる短縮)
================================================================================
6. シジルモードGemini案
================================================================================
【@モード: パイプライン特化】
@ input |> trim |> replace("a","b") |> upper
【$モード: プロパティチェーン】
$ user.profile.settings.theme.color
【効果】
- 特定文脈で暗黙ルール適用
- パーサーモード切り替えで実現
================================================================================
7. Unicode演算子オプション
================================================================================
| ASCII | Unicode | 意味 |
|-------|---------|------|
| -> | → | ラムダ |
| compose | ∘ | 関数合成 |
| in | ∈ | 所属判定 |
| != | ≠ | 不等号 |
// ASCIIフォールバック必須
l double = λx → x*2 // または x -> x*2
================================================================================
8. deriveマクロボイラープレート削減
================================================================================
// 現在60-120行/ノード)
impl Visitor for AstNode {
fn visit_expr(&mut self, e: &Expr) {
match e {
Expr::Call(f, args) => {
self.visit_expr(f);
for a in args { self.visit_expr(a) }
}
// ... 各ケース実装
}
}
}
// 提案1行、-99%
derive visit for AstNode
================================================================================
9. peek式パターン強化
================================================================================
// 基本
peek ast {
BinaryOp(l,op,r) => compile(l) + op + compile(r)
UnaryOp(op,e) => op + compile(e)
Lit(v) => v
}
// ガード付き
peek n {
_ if _ > 0 => "positive"
_ if _ < 0 => "negative"
0 => "zero"
}
// 範囲
peek score {
0..60 => "F"
90..100 => "A"
}
================================================================================
10. 関数合成 + ポイントフリー
================================================================================
// 現在
fn process(x) {
return format(validate(parse(clean(x))))
}
// 提案1: 合成演算子
l process = clean >> parse >> validate >> format
// 提案2: 逆合成
l process = format ∘ validate ∘ parse ∘ clean
================================================================================
実装優先順位と削減見積もり
================================================================================
【Phase 12.7-A即実装】削減効果: -25%
1. 暗黙変数 $_
2. パイプライン強化(最後引数注入)
3. 高階関数演算子(/:, \:, //
4. 1文字エイリアスl, ^, fn
【Phase 12.7-B次段階】削減効果: -20%
5. リスト内包 + 分割代入
6. 演算子セクション(部分適用)
7. deriveマクロvisit, display, eq
【Phase 12.7-C将来】削減効果: -10%
8. シジルモード
9. Unicode演算子
10. 関数合成演算子
================================================================================
具体例:コンパイラのコア部分
================================================================================
// 現在のコンパイラ(擬似コード、~200行
fn compile(source: String) -> Result<ByteCode, Error> {
let tokens = match tokenize(source) {
Ok(t) => t,
Err(e) => return Err(e),
};
let ast = match parse(tokens) {
Ok(a) => a,
Err(e) => return Err(e),
};
let typed_ast = match type_check(ast) {
Ok(ta) => ta,
Err(e) => return Err(e),
};
let mir = match lower_to_mir(typed_ast) {
Ok(m) => m,
Err(e) => return Err(e),
};
let optimized = optimize(mir);
let bytecode = codegen(optimized);
Ok(bytecode)
}
// 極限短縮版(~10行、-95%
fn compile(src) =
src |> tokenize
?. parse
?. type_check
?. lower_to_mir
/: optimize
/: codegen
// または関数合成版
l compile = tokenize >> parse >> type_check >> lower_to_mir >> optimize >> codegen
================================================================================
総合削減見積もり
================================================================================
【コンパイラ本体】
- パーサー: derive + 内包で -3000行
- 型検査: 暗黙変数 + HOF演算子で -2500行
- 最適化パス: 合成 + パイプで -2000行
- コード生成: テンプレート + マクロで -1500行
【標準ライブラリ】
- コレクション操作: -2000行
- エラー処理: -1000行
- ユーティリティ: -1000行
【合計】
現在: 80,000行
削減: -13,000行
目標: 67,000行 → さらなる削減が必要
【追加施策】
- ANCPとの併用で更に-40%
- 不要機能の削除
- アーキテクチャ簡素化
================================================================================