forked from baguette/lemma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.lua
67 lines (56 loc) · 1.31 KB
/
exec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
lemma = {}
require 'class/FileStream'
require 'read'
require 'eval'
require 'compiled/std'
require 'compiled/compile'
require 'env'
require 'type'
---
-- Execute file f in the global environment.
-- If prompt is not nil, f should be stdin; starts a REPL.
---
function exec(f)
local done = false
if type(f) == 'string' then
f = io.open(f, 'r')
prompt = nil
end
f = FileStream(f)
lemma['*in-stream*'] = f
while not done do
if prompt then io.write(prompt) end
local t = read(f) -- read an expression!
if type(t) ~= 'Error' then
local val = Vector(eval(t, env)) -- evaluate the expression!
local err = false
for i = 1, val:length() do
v = val[i]
if v == Error'eof' then
done = true
f:close()
return
elseif type(v) == 'Error' then
io.stderr:write (f:lines() .. ': ' .. tostring(v) .. '\n')
err = true
end
end
if prompt and not err then
io.write';=> ' ; write(Seq.lib.unpack(val)) -- print the value!
end
else
if t == Error'eof' then
if prompt then io.write('\n') end
done = true
f:close()
return
else
io.stderr:write (f:lines() .. ': ' .. e:string() .. '\n')
if not prompt then
done = true
f:close()
end
end
end
end -- loop!
end