##- # Author: Brian Tiffin # Dedicated to the public domain # # Date: September 2016 # Modified: 2016-10-10/04:47-0400 ##+ # # uval.icn, an eval function # $define base "/tmp/child-xyzzy" link ximage # # try an evaluation # global cache procedure main() cache := table() program := "# temporary file for eval, purge at will\n_ global var\n_ procedure main()\n_ var := 5\n_ suspend ![1,2,3] do var +:= 5\n_ end" while e := eval(program) do { v := variable("var", cache[program]) write("child var: ", v, " e: ", ximage(e)) } # BUG HERE, can't refresh the task space: ^cache[program] # test cache v := &null e := eval(program) v := variable("var", cache[program]) write("child var: ", v) write("e: ", ximage(e)) # eval and return a list program := "# temporary file for eval, purge at will\n_ procedure main()\n_ return [1,2,3]\n_ end" e := eval(program) write("e: ", ximage(e)) end # # eval, given string (either code or filename with isfile) # procedure eval(s, isfile) local f, code, status, child, result if \isfile then { f := open(s, "r") | fail code ||:= every(!read(f)) } else code := s # if cached, just refresh the co-expression # otherwise, compile and load the code if member(cache, code) then write("^cache[code]") else { codefile := open(base || ".icn", "w") | fail write(codefile, code) close(codefile) status := system("unicon -s -o " || base || " " || base || ".icn 2>/dev/null") if \status then cache[code] := load(base) } # if there is code, activate the co-expression if \cache[code] then result := @cache[code] remove(base || ".icn") remove(base) return \result | fail end