-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlithp.py
289 lines (241 loc) · 9.92 KB
/
lithp.py
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
# Lithp - A interpreter for John McCarthy's original Lisp.
#
# The heavily documented code for [Lithp can be found on Github](http://github.com/fogus/lithp).
#
# It wasn't enough to write the Lisp interpreter -- I also wanted to share what I learned with *you*. Reading
# this source code provides a snapshot into the mind of John McCarthy, Steve Russell, Timothy P. Hart, and Mike Levin and
# as an added bonus, myself. The following source files are available for your reading:
#
# - [atom.py](atom.html)
# - [env.py](env.html)
# - [error.py](error.html)
# - [fun.py](fun.html)
# - [interface.py](interface.html)
# - [lisp.py](lisp.html)
# - [lithp.py](index.html) *(this file)*
# - [number.py](number.html)
# - [reader.py](reader.html)
# - [seq.py](seq.html)
# - [core.lisp](core.html)
#
# The Lithp interpreter requires Python 2.6.1+ to function.
# please add comments, report errors, annecdotes, etc. to the [Lithp Github project page](http://github.com/fogus/lithp)
#
import pdb
import getopt, sys, io
from env import Environment
from fun import Function
from atom import TRUE
from atom import FALSE
from lisp import Lisp
from reader import Reader
from error import Error
from fun import Lambda
from fun import Closure
NAME = "Lithp"
VERSION = "v1.1"
WWW = "http://fogus.me/fun/lithp/"
PROMPT = "lithp"
DEPTH_MARK = "."
class Lithp(Lisp):
""" The Lithper class is the interpreter driver. It does the following:
1. Initialize the global environment
2. Parse the cl arguments and act on them as appropriate
3. Initialize the base Lisp functions
4. Read input
5. Evaluate
6. Print
7. Loop back to #4
"""
def __init__( self):
iostreams=(sys.stdin, sys.stdout, sys.stderr)
(self.stdin, self.stdout, self.stderr) = iostreams
self.debug = False
self.verbose = True
self.core = True
self.closures = True
self.rdr = Reader()
self.environment = Environment()
self.init()
def init(self):
# Define core functions
self.environment.set("eq", Function(self.eq))
self.environment.set("quote", Function(self.quote))
self.environment.set("car", Function(self.car))
self.environment.set("cdr", Function(self.cdr))
self.environment.set("cons", Function(self.cons))
self.environment.set("atom", Function(self.atom))
self.environment.set("cond", Function(self.cond))
# Define utility function
self.environment.set("print", Function(self.println))
# Special forms
self.environment.set("lambda", Function(self.lambda_))
self.environment.set("label", Function(self.label))
# Define meta-elements
self.environment.set("__lithp__", self)
self.environment.set("__global__", self.environment)
def usage(self):
self.print_banner()
print()
print(NAME.lower(), " <options> [lithp files]\n")
def print_banner(self):
print("The", NAME, "programming shell", VERSION)
print(" by Fogus,", WWW)
print(" Type :help for more information")
print()
def print_help(self):
print("Help for Lithp v", VERSION)
print(" Type :help for more information")
print(" Type :env to see the bindings in the current environment")
print(" Type :load followed by one or more filenames to load source files")
print(" Type :quit to exit the interpreter")
def push(self, env=None):
if env:
self.environment = self.environment.push(env)
else:
self.environment = self.environment.push()
def pop(self):
self.environment = self.environment.pop()
def repl(self):
while True:
# Stealing the s-expression parsing approach from [CLIPS](http://clipsrules.sourceforge.net/)
source = self.get_complete_command()
# Check for any REPL directives
try:
if source in [":quit"]:
break
elif source in [":help"]:
self.print_help()
elif source.startswith(":load"):
files = source.split(" ")[1:]
self.process_files(files)
elif source in [":env"]:
print(self.environment)
else:
self.process(source)
except AttributeError as ae:
print("Could not process command: ", source)
raise ae
# Source is processed one s-expression at a time.
def process(self, source):
sexpr = self.rdr.get_sexpr(source)
while sexpr:
result = None
try:
result = self.eval(sexpr)
except Error as err:
print(err)
if self.verbose:
self.stdout.write(" %s\n" % result)
sexpr = self.rdr.get_sexpr()
# In the process of living my life I had always heard that closures and dynamic scope
# cannot co-exist. As a thought-experiment I can visualize why this is the case. That is,
# while a closure captures the contextual binding of a variable, lookups in dynamic scoping
# occur on the dynamic stack. This means that you may be able to close over a variable as
# long as it's unique, but the moment someone else defines a variable of the same name
# and attempt to look up the closed variable will resolve to the top-most binding on the
# dynamic stack. This assumes the the lookup occurs before the variable of the same name
# is popped. While this is conceptually easy to grasp, I still wanted to see what would
# happen in practice -- and it wasn't pretty.
def lambda_(self, env, args):
if self.environment != env.get("__global__") and self.closures:
return Closure(env, args[0], args[1:])
else:
return Lambda(args[0], args[1:])
# Delegate evaluation to the form.
def eval(self, sexpr):
try:
return sexpr.eval(self.environment)
except ValueError as err:
print(err)
return FALSE
# A complete command is defined as a complete s-expression. Simply put, this would be any
# atom or any list with a balanced set of parentheses.
def get_complete_command(self, line="", depth=0):
if line != "":
line = line + " "
if self.environment.level != 0:
prompt = PROMPT + " %i%s " % (self.environment.level, DEPTH_MARK * (depth+1))
else:
if depth == 0:
prompt = PROMPT + "> "
else:
prompt = PROMPT + "%s " % (DEPTH_MARK * (depth+1))
line = line + self.read_line(prompt)
# Used to balance the parens
balance = 0
for ch in line:
if ch == "(":
# This is not perfect, but will do for now
balance = balance + 1
elif ch == ")":
# Too many right parens is a problem
balance = balance - 1
if balance > 0:
# Balanced parens gives zero
return self.get_complete_command(line, depth+1)
elif balance < 0:
raise ValueError("Invalid paren pattern")
else:
return line
def read_line(self, prompt) :
if prompt and self.verbose:
self.stdout.write("%s" % prompt)
self.stdout.flush()
line = self.stdin.readline()
if(len(line) == 0):
return "EOF"
if line[-1] == "\n":
line = line[:-1]
return line
# Lithp also processes files using the reader plumbing.
def process_files(self, files):
self.verbose = False
for filename in files:
infile = open( filename, 'r')
self.stdin = infile
source = self.get_complete_command()
while(source not in ["EOF"]):
self.process(source)
source = self.get_complete_command()
infile.close()
self.stdin = sys.stdin
self.verbose = True
if __name__ == '__main__':
lithp = Lithp()
try:
opts, files = getopt.getopt(sys.argv[1:], "hd", ["help", "debug", "no-core", "no-closures"])
except getopt.GetoptError as err:
# Print help information and exit:
print(str( err)) # will print something like "option -a not recognized"
lithp.usage()
sys.exit(1)
for opt,arg in opts:
if opt in ("--help", "-h"):
lithp.usage()
sys.exit(0)
elif opt in ("--debug", "-d"):
lithp.verbose = True
elif opt in ("--no-core"):
lithp.core = False
elif opt in ("--no-closures"):
lithp.closures = False
else:
print("unknown option " + opt)
# Process the core lisp functions, if applicable
if lithp.core:
print("Loading core.lisp...")
lithp.process_files(["../core.lisp"])
if len(files) > 0:
lithp.process_files(files)
lithp.print_banner()
lithp.repl()
#### References
# - (McCarthy 1979) *History of Lisp* by John MaCarthy
# - (McCarthy 1960) *Recursive functions of symbolic expressions and their computation by machine, part I* by John McCarthy
# - (Church 1941) *The Calculi of Lambda-Conversion* by Alonzo Church
# - (Baker 1993) *Equal Rights for Functional Objects or, The More Things Change, The More They Are the Same* by Henry Baker
# - (Kleene 1952) *Introduction of Meta-Mathematics* by Stephen Kleene
# - (McCarthy 1962) *LISP 1.5 Programmer's Manual* by John McCarthy, Daniel Edwards, Timothy Hart, and Michael Levin
# - (IBM 1955) *IBM 704 Manual of Operation* [here](http://www.cs.virginia.edu/brochure/images/manuals/IBM_704/IBM_704.html)
# - (Hart 1963) *AIM-57: MACRO Definitions for LISP* by Timothy P. Hart