Files
hakorune/apps/selfhost-vm/collect_literal_eval.nyash

31 lines
1.2 KiB
Plaintext

// Minimal self-contained eval (no using): collect the first Print(Literal int) and print it
static box Main {
// helper: find from position
index_of_from(hay, needle, pos) {
if pos < 0 { pos = 0 }
if pos >= hay.length() { return -1 }
local tail = hay.substring(pos, hay.length())
local rel = tail.indexOf(needle)
if rel < 0 { return -1 } else { return pos + rel }
}
main(args) {
local json = "{\"kind\":\"Program\",\"statements\":[{\"kind\":\"Print\",\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":42}}}]}"
// naive collect: first Print of Literal int
local ki = "\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
local p = json.indexOf(ki)
if p >= 0 {
local i = p + ki.length()
local j = i
loop (true) {
local ch = json.substring(j, j+1)
if ch == "" { break }
if ch == "0" || ch == "1" || ch == "2" || ch == "3" || ch == "4" || ch == "5" || ch == "6" || ch == "7" || ch == "8" || ch == "9" { j = j + 1 continue }
break
}
local digits = json.substring(i, j)
if digits { print(digits) }
}
return 0
}
}