Files
hakorune/tools/pyc/PyCompiler.nyash
Moe Charm c13d9c045e 📚 Phase 12: Nyashスクリプトプラグインシステム設計と埋め込みVM構想
## 主な成果
- Nyashスクリプトでプラグイン作成可能という革命的発見
- C ABI制約の分析と埋め込みVMによる解決策
- MIR/VM/JIT層での箱引数サポートの詳細分析

## ドキュメント作成
- Phase 12基本構想(README.md)
- Gemini/Codex先生の技術分析
- C ABIとの整合性問題と解決策
- 埋め込みVM実装ロードマップ
- 箱引数サポートの技術詳細

## 重要な洞察
- 制約は「リンク時にC ABI必要」のみ
- 埋め込みVMでMIRバイトコード実行により解決可能
- Nyashスクリプト→C ABIプラグイン変換が実現可能

Everything is Box → Everything is Plugin → Everything is Possible!
2025-08-30 22:52:16 +09:00

52 lines
2.1 KiB
Plaintext

// Nyash-side Python compiler (Phase 10.7 C2 - minimal)
// Orchestrates: Parser(JSON) -> IR(JSON) -> Nyash source (string)
static box PyCompiler {
parseToJson() {
// Use Nyash-only parser via PyRuntimeBox (no Rust build)
local p
p = new PythonParserNy()
return p.parse("")
}
buildIRFromParse(json) {
// Minimal: analyze Python source from env and build IR with a constant return when possible
// Python snippet: parse code from os.getenv("NYASH_PY_CODE"), find first Return(Constant)
local os, getenv, src, ast, parsef, walkf, first_ret
local py = new PyRuntimeBox()
os = py.import("os")
getenv = os.getattr("getenv")
src = getenv.call("NYASH_PY_CODE").toString()
if (src == null || src == "") {
return new StringBox("{\"module\":{\"functions\":[{\"name\":\"main\",\"return_value\":0}]}}")
}
ast = py.import("ast")
parsef = ast.getattr("parse")
local tree
tree = parsef.call(src)
// Walk via Python: extract first constant return value if present
local res
res = py.eval("next((n.value.value for n in __import__('ast').walk(__import__('ast').parse(__import__('os').getenv('NYASH_PY_CODE') or '')) if isinstance(n, __import__('ast').Return) and isinstance(n.value, __import__('ast').Constant)), None)")
// If number -> return that number; if string -> quoted
local val
val = res.str()
if (val.matches("^[0-9]+$")) {
return new StringBox("{\"module\":{\"functions\":[{\"name\":\"main\",\"return_value\":" + val + "]}}")
} else if (val.length() > 0) {
return new StringBox("{\"module\":{\"functions\":[{\"name\":\"main\",\"return_value\":\"" + val + "\"}]}}")
} else {
return new StringBox("{\"module\":{\"functions\":[{\"name\":\"main\",\"return_value\":0}]}}")
}
}
irToNyashSource(irJson) {
// Build Nyash source from IR (minimal: assume IR has return_value embedded by PyIR.buildReturn)
local src
src = "static box Generated {\n main() {\n return 0\n }\n}"
// Future: parse irJson and extract return_value
return new StringBox(src)
}
}