2025-09-21 08:53:00 +09:00
|
|
|
// Mini-VM library (function-based) with a tiny JSON extractor
|
|
|
|
|
// Safe MVP: no real JSON parsing; string scan for first int literal only
|
|
|
|
|
|
|
|
|
|
static box MiniVm {
|
|
|
|
|
_is_digit(ch) { return ch == "0" || ch == "1" || ch == "2" || ch == "3" || ch == "4" || ch == "5" || ch == "6" || ch == "7" || ch == "8" || ch == "9" }
|
|
|
|
|
|
|
|
|
|
// Read consecutive digits starting at pos
|
|
|
|
|
read_digits(json, pos) {
|
2025-09-22 07:54:25 +09:00
|
|
|
@out = ""
|
2025-09-21 08:53:00 +09:00
|
|
|
loop (true) {
|
2025-09-22 07:54:25 +09:00
|
|
|
@s = json.substring(pos, pos+1)
|
2025-09-21 08:53:00 +09:00
|
|
|
if s == "" { break }
|
|
|
|
|
if _is_digit(s) { out = out + s pos = pos + 1 } else { break }
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract the first integer literal from our AST JSON v0 subset
|
|
|
|
|
parse_first_int(json) {
|
2025-09-22 07:54:25 +09:00
|
|
|
@key = "\"value\":{\"type\":\"int\",\"value\":"
|
|
|
|
|
@idx = json.lastIndexOf(key)
|
2025-09-21 08:53:00 +09:00
|
|
|
if idx < 0 { return "0" }
|
2025-09-22 07:54:25 +09:00
|
|
|
@start = idx + key.length()
|
2025-09-21 08:53:00 +09:00
|
|
|
return read_digits(json, start)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute a minimal program: print the extracted integer and exit code 0
|
|
|
|
|
run(json) {
|
2025-09-22 07:54:25 +09:00
|
|
|
@n = parse_first_int(json)
|
2025-09-21 08:53:00 +09:00
|
|
|
print(n)
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
}
|