60 lines
1.9 KiB
Plaintext
60 lines
1.9 KiB
Plaintext
|
|
// Mini-VM: function-based entry for branching
|
||
|
|
// Local static box (duplicated from mini_vm_lib for now to avoid include gate issues)
|
||
|
|
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_digits(json, pos) {
|
||
|
|
local out = ""
|
||
|
|
loop (true) {
|
||
|
|
local s = json.substring(pos, pos+1)
|
||
|
|
if s == "" { break }
|
||
|
|
if _is_digit(s) { out = out + s pos = pos + 1 } else { break }
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
parse_first_int(json) {
|
||
|
|
local key = "\"value\":{\"type\":\"int\",\"value\":"
|
||
|
|
local idx = json.lastIndexOf(key)
|
||
|
|
if idx < 0 { return "0" }
|
||
|
|
local start = idx + key.length()
|
||
|
|
return read_digits(json, start)
|
||
|
|
}
|
||
|
|
run_branch(json) {
|
||
|
|
local n = parse_first_int(json)
|
||
|
|
if n == "0" || n == "1" || n == "2" || n == "3" || n == "4" { print("10") return 0 }
|
||
|
|
print("20")
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Program entry: embedded JSON (value=1 → print 10; else → 20)
|
||
|
|
static box Main {
|
||
|
|
main(args) {
|
||
|
|
local vm = new MiniVm()
|
||
|
|
local json = "{\"kind\":\"Program\",\"statements\":[{\"kind\":\"Print\",\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":1}}}]}"
|
||
|
|
// If provided, override by argv[0]
|
||
|
|
if args {
|
||
|
|
if args.size() > 0 {
|
||
|
|
local s = args.get(0)
|
||
|
|
if s { json = s }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return vm.run_branch(json)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Top-level fallback entry for current runner
|
||
|
|
function main(args) {
|
||
|
|
local vm = new MiniVm()
|
||
|
|
local json = "{\"kind\":\"Program\",\"statements\":[{\"kind\":\"Print\",\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":1}}}]}"
|
||
|
|
if args {
|
||
|
|
if args.size() > 0 {
|
||
|
|
local s = args.get(0)
|
||
|
|
if s { json = s }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
local n = vm.parse_first_int(json)
|
||
|
|
if n == "0" || n == "1" || n == "2" || n == "3" || n == "4" { print("10") return 0 }
|
||
|
|
print("20")
|
||
|
|
return 0
|
||
|
|
}
|