33 lines
1.3 KiB
Plaintext
33 lines
1.3 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 extract first constant return as JSON scalar string
|
|
// 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()
|
|
// 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)")
|
|
// JSON-encode result (number or None) and return as StringBox directly
|
|
local ret_json
|
|
ret_json = py.import("json").getattr("dumps").call(res).str()
|
|
return new StringBox(ret_json)
|
|
}
|
|
|
|
irToNyashSource(retJson) {
|
|
// Build Nyash source directly from the scalar JSON value string
|
|
local src
|
|
src = "static box Generated {\n main() {\n return " + retJson + "\n }\n}"
|
|
return new StringBox(src)
|
|
}
|
|
}
|