-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval_common.lua
161 lines (108 loc) · 2.74 KB
/
eval_common.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
local quote = require "quote"
local lisp = require "lisp"
do
local old_select = select
local old_io = io
local old_type = type
local old_table = table
_ENV = {}
_ENV.select = old_select
_ENV.io = old_io
_ENV.type = old_type
_ENV.table = {}
_ENV.table.concat = old_table.concat
end
--------------------------------------------------------------------------------------------
------------------------------ Primitive Helper ------------------------------
function primitive_null(exp)
return exp == lisp.empty_list
end
function primitive_atom(exp)
return (not lisp.is_pair(exp)) and (exp ~= lisp.empty_list)
end
function primitive_add(...)
local result = 0
for i = 1, select('#', ...) do
result = result + select(i, ...)
end
return result
end
function primitive_sub(...)
local result = select(1, ...)
for i = 2, select('#', ...) do
result = result - select(i, ...)
end
return result
end
function primitive_mul(...)
local result = 1
for i = 1, select('#', ...) do
result = result * select(i, ...)
end
return result
end
function primitive_div(...)
local result = select(1, ...)
for i = 2, select('#', ...) do
result = result / select(i, ...)
end
return result
end
function primitive_algebra(oper)
if oper == '+' then
return primitive_add
elseif oper == '-' then
return primitive_sub
elseif oper == '*' then
return primitive_mul
elseif oper == "/" then
return primitive_div
elseif oper == "==" then
return function(v1, v2) return v1 == v2 end
elseif oper == "<" then
return function(v1, v2) return v1 < v2 end
elseif oper == ">" then
return function(v1, v2) return v1 > v2 end
end
end
--
-- policy: format: indented, wrap
-- option: level of indention
--
function primitive_tostring(list, policy, option)
if lisp.is_pair(list) then
if policy == "indented" then
return lisp.list_tostring_indented(list, option, true)
else
local wrap = (policy == "wrap");
return lisp.list_tostring(list, wrap)
end
end
return lisp.tostring(list)
end
function primitive_string_append(...)
local strings = {...}
return table.concat(strings)
end
function primitive_open(mode)
return function(filename)
return io.open(filename, mode)
end
end
function primitive_close(port)
return io.close(port)
end
function primitive_read_string(inport)
local content = inport:read("a")
inport:seek("set", 0)
return content
end
function primitive_read(instream)
if type(instream) ~= 'string' then
instream = primitive_read_string(instream)
end
return quote.quote(instream)
end
------------------------------ Primitive Helper ------------------------------
--------------------------------------------------------------------------------------------
return _ENV