-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval.lua
59 lines (49 loc) · 1.15 KB
/
eval.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
---
-- The Lemma Evaluator
---
require 'env'
require 'class/List'
require 'class/Vector'
require 'class/PreHashMap'
require 'class/HashMap'
require 'class/Iter'
require 'interface/Seq'
---
-- This is just a workaround for circular module dependencies.
-- It will be replaced by the actual compile function before
-- it ever gets called.
---
lemma.compile = function(s) return "" end
function eval(t, env)
local undefined = false
if lemma.type(t) == 'List' then
local h = t:first()
if lemma.type(h) == 'Symbol' then
local undef = string.sub(h:string(), 1, 3)
if undef == 'def' or undef == 'set' or undef == 'ns' then
undefined = true
end
end
end
local code = lemma.compile(t)
if lemma.type(code) == 'Error' then
return code
end
if not undefined then
code = 'return '..code
end
if lemma['*debug*'] then
print('------'..tostring(undefined))
print(code)
print '------'
end
local f, err = loadstring(code)
if not f then
return error('compiler error: \n'..tostring(err))
end
local blarg = Vector(pcall(f))
if not blarg(1) then
return error('eval: '..tostring(blarg(2)))
end
return select(2, Seq.lib.unpack(blarg))
end