-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisp.py
More file actions
269 lines (240 loc) · 6.15 KB
/
lisp.py
File metadata and controls
269 lines (240 loc) · 6.15 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
import os
__all__ = ["Lisp","Function","double","times"]
class Function():
name = None
args = []
argcount = 0
body = None #right now this should just be a string of lisp code
agent = None #the agent which represents the function
def __init__(self, name, args, body):
self.name = name
self.args = args
self.argcount = len(args)
self.body = body
def defun(self):
ret = '(defun '
ret += self.name
ret += ' '
ret += '('
for i in self.args:
ret += i
ret += ' '
ret += ')\n'
ret += ' '
ret += self.body
ret += ')\n'
return ret
def __repr__(self):
string = '<'
string += 'lisp.function: '
string += self.name + ': '
for i in self.args:
string += i
string += ' '
string += '-->'
string += self.body
string += '>'
return string
def __str__(self):
return self.name
"""Class to interact with lisp and to create lisp source files"""
class Lisp():
#command used to invoke lisp
invoke = "clisp"
command = None
source = None
stream = 'pylisp' #this is the symbol that will be used in streams
scratch = 'pylispscratch.lisp'
flag_compile = True #compile on load, slower load but faster exec"
#by defualt the lisp command is clisp, but you can change it
#depending on the system
#returns the present state of the lisp API
def state(self):
print "Invoke command:", self.invoke
print "Present command string:", self.command
print "Writing source to file:", self.source
print "Compile Flag:", self.flag_compile
def clear(self):
self.command = None
self.source = None
def delete_scratch(self):
os.system('rm ' + self.scratch)
def go(self):
os.system(self.command)
def run(self, file, *out):
self.command = self.invoke
self.space()
self.command += file
self.space()
if (None):
self.command += '>'
self.command += out
self.go()
self.clear()
def compile(self, input, *output):
self.command = self.invoke
self.command += ' -i '
self.command += input
self.command += ' -c '
self.command += input
if(output):
self.command += ' -o '
self.command += output
self.go()
def space(self):
self.command += " "
def start_writing(self,filename):
self.source = open(filename, "w")
#flushes command when called
def stop_writing(self):
if (self.command != None):
self.write()
self.source.close()
self.source = None
def write(self): #writes the present command to the source file
if(self.source == None): #some sanity checking
raise IOError, 'No source file specified'
else:
self.source.write(self.command)
self.command = None
def write_string(self): #returns what would have been written as a string
temp = self.command
self.command = None
return temp
def list(self,list, *as_string):
self.command = '(list '
for i in list:
self.command += i + ' '
self.command += ')'
if(as_string):
return self.write_string()
else:
self.write()
#calls to defun have the side effect of clearing the command at
#the start and end, as well as writing to the source file
def defun(self, function, *as_string):
self.command = function.defun()
if(as_string):
return self.write_string()
else:
self.write()
def setq(self, symbol, value, *as_string):
self.command = '('
self.command += 'setq'
self.space()
self.command += symbol
self.space()
self.command += str(value)
self.command += ')\n'
if(as_string):
return self.write_string()
else:
self.write()
def lprint(self, message, *as_string):
self.command = '('
self.command += 'print '
self.command += '"' + message + '"'
self.command += ')\n'
if(as_string):
return self.write_string()
else:
self.write()
def call(self, func, args, *as_string):
self.command = '('
if(isinstance(func, Function)):
self.command += func.name
else:
self.command += func
self.space()
for i in args:
self.command += str(i)
self.space()
self.command += ') '
if(as_string):
return self.write_string()
else:
self.write()
def complist(self, list):
self.command = '(list '
for f_or_none in list:
if f_or_none == None:
self.command += 'None '
else:
self.command += ('(safe-function ' + f_or_none.name + ')')
self.command += ')'
return self.command
def pinchlist(self, list):
self.command = '(list '
for argument in list:
self.command += "'" + argument + ' '
self.command += ')'
return self.command
def load(self, file, *as_string):
self.command = '('
self.command += 'load '
self.command += ('"' + file + '"')
self.command += ')\n'
if(as_string):
return self.write_string()
else:
self.write()
def open_stream(self, file, *as_string):
self.command = '(setq '
self.command += 'pylisp '
self.command += '(open '
self.command += ('"' + file + '" ')
self.command += ':if-does-not-exist :create '
self.command += ':direction :output'
self.command += '))\n'
if(as_string):
return self.write_string()
else:
self.write()
def streamprint(self, expression, *as_string):
self.command = '(print '
self.command += (expression + ' ')
self.command += ('pylisp' + ')\n')
if(as_string):
return self.write_string()
else:
self.write()
def close_stream(self, *as_string):
self.command = '(close '
self.command += ('pylisp' + ')\n')
if(as_string):
return self.write_string()
else:
self.write
self.stream = None
def comment(self, comment):
self.command = ';;'
self.command += comment
self.command += '\n'
self.write()
_inst = Lisp()
stream = _inst.stream
state = _inst.state
clear = _inst.clear
go = _inst.go
run = _inst.run
compile = _inst.compile
start_writing = _inst.start_writing
stop_writing = _inst.stop_writing
write = _inst.write
write_string = _inst.write_string
list = _inst.list
defun = _inst.defun
setq = _inst.setq
lprint = _inst.lprint
call = _inst.call
complist = _inst.complist
pinchlist = _inst.pinchlist
load = _inst.load
open_stream = _inst.open_stream
streamprint = _inst.streamprint
close_stream = _inst.close_stream
comment = _inst.comment
#here are some nice test functions for debugging purposes
#these should all be compileable lisp functions
double = Function('double', ['x'], '(* x 2)')
times = Function('timess', ['x','y'], '(* x y)')