--- lam.load local m = {} local core = require("core") local eval = require("eval") local port = require("port") local read = require("read") local type = require("type") local function schemeprint (x) -- possibly a candidate to put in a `write' library if x == true then print("#t") elseif x == false then print("#f") elseif x == nil then return -- print("#") else print(x) end end local function handle_error (e) local start = e:find(": ") return e:sub(start + 2) end function m.load (filename, interactive) -- interactive = { out = file/handle, prompt = string, } local inport = port.input_port(filename) if interactive then io.output(interactive.out) io.output():setvbuf("line") else io.output():setvbuf("no") end repeat if interactive then io.stderr:write(interactive.prompt or "") io.stderr:flush() end -- read local read_ok, form = xpcall( function () return read.read(inport) end, handle_error) if form == port.eof then break end if not read_ok then io.stderr:write("error (read): ", form, "\n") -- when interactive, errors should not be fatal, but -- they should be in batch mode inport:flush() -- avoid endless loop if not interactive then return nil end else -- eval local eval_ok, value = xpcall( function () return eval.eval(form, core.environment) end, handle_error) if not eval_ok then io.stderr:write("error (eval): ", value, "\n") if not interactive then return nil end else -- print if interactive then schemeprint(value) end end end until value == port.eof -- loop end -------- return m