Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlip.lua
More file actions
Latest commit
384 lines (331 loc) · 9.51 KB
/
Copy pathlip.lua
File metadata and controls
384 lines (331 loc) · 9.51 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
--[[-------------------------------------
Lip
-----------------------------------------
Lip is a Lisp like mini language in Lua.
under GPL lisence.
copyright (c) 2006 hanzhao (abrash_han@hotmail.com)
http://luaforge.net/projects/lip/
--]]-------------------------------------
----------------------
-- key words of Lip --
Lisp= {
lambda=0,
ret=1,
cond=2,
}
-- verbose switch --
Lisp.verbose=false
-- a reg knows which function is lisp defined function --
Lisp.lisp_func_reg= {}
-- debug output --
functionlip_debug_print(val)
if(Lisp.verbose) then
print(val)
end
end
--------------------------------------------
-- core lib of Lip -------------------------
-- arithmetic ops --
functionadd(x, y)
lip_debug_print("> add " ..x..", " ..y)
returnx+y
end
functionsub(x, y)
lip_debug_print("> sub " ..x..", " ..y)
returnx-y
end
functionmut(x, y)
lip_debug_print("> mut " ..x..", " ..y)
returnx*y
end
functiondiv(x, y)
lip_debug_print("> div " ..x..", " ..y)
returnx/y;
end
functionmod(x, y)
lip_debug_print("> mod " ..x..", " ..y)
returnmath.mod(x, y)
end
-- compare ops --
functioneq(x, y)
lip_debug_print("> eq " ..tostring(x) ..", " ..tostring(y))
returnx==y
end
functiongt(x, y)
lip_debug_print("> gt " ..x..", " ..y)
returnx>y
end
functionge(x, y)
lip_debug_print("> ge " ..x..", " ..y)
returnx>=y
end
functionlt(x, y)
lip_debug_print("> lt " ..x..", " ..y)
returnx<y
end
functionle(x, y)
lip_debug_print("> le " ..x..", " ..y)
returnx<=y
end
-- logic ops --
function_or(x, y)
lip_debug_print("> or " ..tostring(x) ..", " ..tostring(y))
returnxory
end
function_and(x, y)
lip_debug_print("> and " ..tostring(x) ..", " ..tostring(y))
returnxandy
end
function_not(x)
lip_debug_print("> not " ..tostring(x))
returnnotx
end
-- util func --
-- print
functionlip_print(v)
print("[lip print] : " ..tostring(v))
end
-- asserts
functionlip_assert_eq(lhs, rhs)
if(lhs~=rhs) thenerror('[lip assert] failed : ' ..tostring(lhs) ..', ' ..tostring(rhs)) end
end
functionlip_assert_true(v)
lip_assert_eq(v, true)
end
functionlip_assert_false(v)
lip_assert_true(notv)
end
-- drop a message here
functionlip_for_whom()
print("------------------------------")
print("-- Lip\'s written for Xu Hui --")
print("------------------------------")
end
-- version
functionlip_version()
print('Lip 0.1 Alpha copyright(c) hanzhao(abrash_han@hotmail.com)')
end
-- end core lib ----------------------------
--------------------------------------------
-- utility functions for Lip interperator --
-- make args named for arguments bind --
functionpass_args(parm, arg)
fori=1, table.getn(parm) do
-- should here be an exec on each arg?
arg[parm[i]] =arg[i]
end
end
-- evaluate all args in stat to args before perform a call --
-- both to native lua function and lip defined function
functionbind_args(stat, upper)
localrslt= {}
localn=table.getn(stat)
if(type(stat[1]) =="function" andtype(stat[2]) =="table") then
-- native function call, if stat[2] is not table, it's just another
-- function arg
lip_debug_print('stat[1] is function')
rslt[1] =lisp_exec(stat, upper)
returnrslt
end
-- it's a lisp defined function call
if(type(stat[1]) =="string" andtype(stat[2]) =="table") then
lip_debug_print('stat[1] is string')
localval=upper[stat[1]]
if(type(val) =="function") then
-- func call
rslt[1] =lisp_exec(stat, upper)
returnrslt
end
end
--[[ it's an anonymous lambda
if(type(stat[1]) == "number" and stat[1] == Lisp.lambda) then
if(type(stat[2]) ~= "table") then
error("expect table as params for anonymous lambda but get " .. type(stat[2]))
end
rslt[1] = lisp_exec(stat, upper)
return rslt
end]]--
fori=1, ndo
if(type(stat[i]) =="table") then
rslt[i] =lisp_exec(stat[i], upper)
elseif(type(stat[i]) =="string") then
if(upper[stat[i]]) then
-- defined names, param or named lambda
localval=upper[stat[i]]
--[[if(type(val) == "function") then
-- func call
--rslt[1] = lisp_exec(stat, upper)
--return rslt
error("argument error, unexpected function.")
else--]]
-- normal arg
rslt[i] =val
--end
else
-- normal string
rslt[i] =stat[i]
end
else
rslt[i] =stat[i]
end
end
returnrslt
end
-- bind closure --
-- bind all params in stat from ctxt, but not from except
functionbind_closure(stat, ctxt, except)
lip_debug_print("bind closure")
localn=table.getn(stat)
fori=1, ndo
if(type(stat[i]) =='string') then
lip_debug_print(stat[i])
if(ctxt[stat[i]] andnotexcept[stat[i]]) then
stat[i] =ctxt[stat[i]]
end
elseif(type(stat[i]) =='table') then
bind_closure(stat[i], ctxt, except)
else
-- no other type need to be handled here
end
end
end
-- chain up the meta tables for name lookup --
functionchain_up(tbl, meta)
setmetatable(tbl, meta)
meta.__index=meta
end
-- return a copy of src tbl --
-- to copy an anonymous lambda's body
functioncpytbl(src)
localn=table.getn(src)
localdst= {}
fori=1, ndo
if(type(src[i]) =='table') then
dst[i] =cpytbl(src[i])
else
dst[i] =src[i]
end
end
returndst
end
-- end utility functions for interperator -----
-----------------------------------------------
-- Lip interperator ---------------------------
functionlisp_exec(stat, upper)
if(stat[1] ==nil) thenerror("error: unknown stat[1].") end
if(upper==nil) thenerror("error: no context passed in.") end
-- where to find names
setfenv(1, upper)
if(type(stat[1]) =="number") then
-- lisp key words
if(stat[1] ==Lisp.lambda) then
lip_debug_print("define lambda")
if(type(stat[2]) =="string") then-- named lambda
-- name, param, body
localfunc=function (arg)
-- bind
pass_args(stat[3], arg)
-- name lookup
chain_up(arg, upper)
returnlisp_exec(stat[4], arg)
end
-- a new func name defined(in upper context), so latter can be called
upper[stat[2]] =func
--table.foreach(upper, print)
Lisp.lisp_func_reg[func] =true
returnfunc
elseif(type(stat[2]) =="table") then-- anonymous lambda
-- param, body
lip_debug_print("anonymous lambda")
--table.foreach(upper, print)
-- closure need a copy
localstat3cpy=cpytbl(stat[3])
bind_closure(stat3cpy, upper, stat[2])
localfunc=function (arg)
-- bind
localtabled_arg=arg
pass_args(stat[2], tabled_arg)
-- name lookup
chain_up(tabled_arg, upper)
returnlisp_exec(stat3cpy, tabled_arg)
end
Lisp.lisp_func_reg[func] =true
returnfunc
else
error('unsupported lambda type' ..type(stat[2]))
end
elseif(stat[1] ==Lisp.ret) then
-- {ret, {vals}}
lip_debug_print("return")
--return lisp_exec(stat[2], upper)
localrslt=bind_args(stat[2], upper)
returnrslt[1] -- only one return val
elseif(stat[1] ==Lisp.cond) then
-- {Lisp.cond, bool-expression, true-first, false-second}
lip_debug_print("- cond")
--table.foreach(stat[2], print)
localrslt=lisp_exec(stat[2], upper)
if(rslt) then
lip_debug_print("-- cond true")
returnlisp_exec(stat[3], upper) -- true
else
lip_debug_print("-- cond false")
returnlisp_exec(stat[4], upper) -- false
end
elseif(stat[1] ==Lisp.arg) then
-- {Lisp.arg, {args}}
returnlisp_exec(stat[2], upper)
else
error('unrecognized key word : ' ..stat[1])
end
elseif(type(stat[1]) =="function") then
-- native lua function call
-- {func, {args}}
lip_debug_print("native function call")
-- bind args
localrslt=bind_args(stat[2], upper)
if(Lisp.lisp_func_reg[stat[1]]) then
-- lisp defined function need args be in table
returnstat[1](rslt)
else
-- unpack the result to call native lua func
returnstat[1](unpack(rslt))
end
elseif(type(stat[1]) =="table") then
-- {{closure}, {args}}
-- calc stat[1] first
lip_debug_print("- eval table")
-- it's a function call? defining a closure
--bind_closure(stat[1][2], upper, {})
--table.foreach(stat[1][2], print)
localfunc=lisp_exec(stat[1], upper)
--stat[1] = func
--lisp_exec(stat, upper)
returnfunc(stat[2])
elseif(type(stat[1]) =="string") then
-- lisp defined function
lip_debug_print("call lisp defined function : " ..stat[1])
-- it must be in upper context
-- local arg = lisp_exec(stat[2], upper)
localrslt=bind_args(stat[2], upper)
--table.foreach(rslt, print)
returnupper[stat[1]](rslt)
elseif(type(stat[1]) =="boolean") then
-- bool expression
lip_debug_print("boolean expression")
returnstat[1]
else
error('unsupported stat[1] type : ' ..type(stat[1]))
end
end
-----------------------------
-- lip program runner -------
-- run, prog, run -------
functionlip_runner(prog)
chain_up(prog, _G)
localn=table.getn(prog)
fori=1, ndo
lip_debug_print("exec " ..i)
lisp_exec(prog[i], prog)
end
end