phase: 20.49 COMPLETE; 20.50 Flow+String minimal reps; 20.51 selfhost v0/v1 minimal (Option A/B); hv1-inline binop/unop/copy; docs + run_all + CURRENT_TASK -> 21.0
This commit is contained in:
32
tools/pyc/PythonParserNy.hako
Normal file
32
tools/pyc/PythonParserNy.hako
Normal file
@ -0,0 +1,32 @@
|
||||
// Nyash-only Python parser using PyRuntimeBox (no Rust build)
|
||||
static box PythonParserNy {
|
||||
parse(code) {
|
||||
local src
|
||||
if (code == null || code.length() == 0) {
|
||||
// Fallback: read from env via Python os.getenv
|
||||
local os, getenv
|
||||
local py = new PyRuntimeBox()
|
||||
os = py.import("os")
|
||||
getenv = os.getattr("getenv")
|
||||
src = getenv.call("NYASH_PY_CODE").str()
|
||||
} else {
|
||||
src = code.str()
|
||||
}
|
||||
|
||||
// ast.dump(ast.parse(src))
|
||||
local ast, parsef, dumpf, tree, dump, dump_str
|
||||
local py2 = new PyRuntimeBox()
|
||||
ast = py2.import("ast")
|
||||
parsef = ast.getattr("parse")
|
||||
tree = parsef.call(src)
|
||||
dumpf = ast.getattr("dump")
|
||||
dump = dumpf.call(tree)
|
||||
dump_str = dump.str()
|
||||
|
||||
// Minimal JSON (escape quotes minimally by replacing \" -> \\\" )
|
||||
// Note: for robust escaping, prefer Python json.dumps in a later pass
|
||||
local esc
|
||||
esc = dump_str.replace("\"", "\\\"")
|
||||
return new StringBox("{\"ast\":\"Module\",\"dump\":\"" + esc + "\"}")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user