-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisp_cmd.lua
112 lines (79 loc) · 2.11 KB
/
lisp_cmd.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
local quote = require "quote"
local eval = require "eval_cont"
local eval_reg = require "eval"
local lisp = require "lisp"
package.cpath = package.cpath .. ";./lib/target/lib?.dylib"
local readline = require "luaReadline"
lisp.set_cons_metatable = true
local cmd_line
local env = eval.Environment.initEnv()
local function func_noop() end
local function is_incomplete(str)
if str == "" then
return true
end
local str_end = string.byte(str, string.len(str))
if str_end == string.byte("\\") then
return true, string.sub(str, 1, string.len(str) - 1)
else
return false, str
end
end
print("Lua Scheme 0.1")
print("Copyright 2015 Dong Feng")
local arg = select(1, ...)
local nocallcc = (arg == "no-callcc")
local wascallcc = (not nocallcc)
if nocallcc then
print("No first-class continuation mode.")
end
local code = ""
repeat
if wascallcc ~= (not nocallcc) then
if nocallcc then
print("Turn off first-class continuation.")
else
print("Turn on first-class continuation.")
end
wascallcc = (not nocallcc)
end
local prompt = code ~= "" and ">> " or "> "
if readline then
cmd_line = readline.readline(prompt)
else
io.write("> ")
cmd_line = io.read("l")
end
local incomplete, cmd_line = is_incomplete(cmd_line)
code = code .. (cmd_line or "")
if (not incomplete) and code ~= "exit" and code ~= "no-callcc" and code ~= "with-callcc" then
local bottom_cont = eval.ContinuationBottom:new(func_noop)
local env_reg = eval_reg.Enviornment.initEnviornment()
local s_exp
local r, err = pcall(function ()
s_exp = quote.quote(code)
end)
if not r then
print("Syntax Error: " .. err)
end
if not nocallcc then
r, err = pcall(function ()
local value = eval.eval_begin(s_exp, env, bottom_cont)
print(value)
end)
else
r, err = pcall(function ()
local value = env_reg:evalSequence(s_exp)
print(value)
end)
end
if not r then
print("Execution Error: " .. err)
end
code = ""
end
if code == "no-callcc" or code == "with-callcc" then
nocallcc = (code == "no-callcc")
code = ""
end
until cmd_line == "exit"