-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlisp.py
More file actions
268 lines (211 loc) · 7.57 KB
/
Copy pathlisp.py
File metadata and controls
268 lines (211 loc) · 7.57 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
# -*- coding: utf-8 -*-
import logging
import re
__all__ = ['BOOL_PATTERN', 'NUMBER_PATTERN', 'cons', 'lbool', 'llist',
'lstring', 'quoted', 'symbol', 'DOT_OPERATOR', 'LispReader',
'LispWritter', 'read_lisp', 'write_lisp']
BOOL_PATTERN = re.compile(r"^('?t|'?nil)\b")
NUMBER_PATTERN = re.compile(r"^([0-9]+(\.[0-9]+)?)\b[^.]")
class cons(object):
"""Simple type representing a cons cell."""
def __init__(self, car, cdr):
self.car = car
self.cdr = cdr
def __repr__(self):
return "cons({0}, {1})".format(self.car, self.cdr)
def __str__(self):
return "({0} . {1})".format(self.car, self.cdr)
class lbool(object):
"""Simple list type representing a cons cell."""
def __init__(self, value):
self.value = bool(value)
def __repr__(self):
return "lbool(" + str(self.value) + ")"
def __str__(self):
if self.value:
return "t"
else:
return "nil"
class llist(list):
"""Simple list type representing a lisp list."""
def __repr__(self):
return "llist(" + super(llist, self).__repr__() + ")"
def __str__(self):
parts = []
for part in self:
parts.append(str(part))
return "(" + ' '.join(parts) + ")"
class lstring(str):
"""Simple string type representing a lisp string."""
def __repr__(self):
return "lstring(" + super(lstring, self).__repr__() + ")"
def __str__(self):
return '"' + self + '"'
def unquote(self):
"""Unquote lisp string so it's python formatted."""
return (r'' + self).replace(r'\"', r'"')
class quoted(llist):
"""Simple list type representing a quoted lisp list."""
def __repr__(self):
return "quoted(" + super(quoted, self).__repr__() + ")"
def __str__(self):
return "'" + super(quoted, self).__str__()
class symbol(str):
"""Simple string type representing a lisp symbol."""
def __repr__(self):
return "symbol(" + super(symbol, self).__repr__() + ")"
def __str__(self):
return self
DOT_OPERATOR = symbol(".")
class LispReader(object):
def __init__(self, code):
self.code = code
self.char_pos = 0
def char_at_pos(self, pos):
try:
return self.code[pos]
except IndexError:
return ""
def prev_char(self):
return self.char_at_pos(self.char_pos - 1)
def current_char(self):
return self.char_at_pos(self.char_pos)
def next_char(self):
return self.char_at_pos(self.char_pos + 1)
def remaining_code(self):
return self.code[self.char_pos:]
def read(self):
try:
self.skip_whitespace()
char = self.current_char()
next_char = self.next_char()
if char == "(" or (char == "'" and next_char == "("):
return self.read_list()
elif char == '"':
return self.read_string()
elif char == ";":
self.skip_comment()
return self.read()
elif re.search(BOOL_PATTERN, self.remaining_code()):
return self.read_bool()
elif re.search(NUMBER_PATTERN, self.remaining_code()):
return self.read_number()
else:
return self.read_symbol()
except Exception as e:
logging.debug("Parsing failed at: %s", self.remaining_code())
raise
def read_bool(self):
starting_pos = self.char_pos
if self.current_char() == "'":
self.char_pos += 1
if self.current_char() == "t":
self.char_pos += 1
return lbool(True)
else:
self.char_pos += 3
return lbool(False)
def read_list(self):
char = self.current_char()
quote = False
if char == "'":
collect = quoted()
self.char_pos += 1
else:
collect = llist()
self.char_pos += 1
while True:
if char != ")" and len(char):
collect.append(self.read())
char = self.current_char()
else:
break
self.char_pos += 1
# If this is a cons cell, return it.
if DOT_OPERATOR in collect and len(collect) == 3:
return cons(collect[0], collect[2])
return collect
def read_number(self):
starting_pos = self.char_pos
allowed = ["0", "1","2","3","4","5","6","7","8","9","."]
char = self.current_char()
is_float = False
while char in allowed:
if char == ".":
is_float = True
self.char_pos += 1
char = self.current_char()
number = self.code[starting_pos:self.char_pos]
if is_float:
return float(number)
else:
return int(number)
def skip_whitespace(self):
"""Move char_pos to first non-whitespace char."""
while self.current_char().isspace() and self.remaining_code():
self.char_pos += 1
def skip_comment(self):
"""Move char_pos to first non-whitespace char."""
while self.current_char() != "\n" and self.remaining_code():
self.char_pos += 1
def read_string(self):
starting_pos = self.char_pos + 1
self.char_pos = starting_pos + 1
char = self.current_char()
prev_char = self.prev_char()
if (self.prev_char() == '"' and
self.char_at_pos(self.char_pos - 2) == '"'):
return lstring("")
while char != '"' or (char == '"' and prev_char == '\\'):
try:
self.char_pos += 1
char = self.code[self.char_pos]
prev_char = self.prev_char()
except IndexError:
raise ValueError(
"Unterminated string literal at {0}".format(self.char_pos))
string = self.code[starting_pos:self.char_pos]
self.char_pos += 1
return lstring(string)
def read_symbol(self):
starting_pos = self.char_pos
char = self.current_char()
while char and not char.isspace() and char != ")":
self.char_pos += 1
char = self.current_char()
string = self.code[starting_pos:self.char_pos]
return symbol(string)
class LispWritter(object):
def __init__(self, value):
self.value = value
def to_lisp_string(self, obj):
if isinstance(obj, (lbool, llist, lstring, quoted, symbol)):
return str(obj)
elif isinstance(obj, cons):
return ''.join(["(", self.to_lisp_string(obj.car), " . ",
self.to_lisp_string(obj.cdr), ")"])
elif isinstance(obj, str):
return '"' + str(obj) + '"'
elif isinstance(obj, (float, int)):
return str(obj)
elif obj is None or isinstance(obj, bool):
return str(lbool(obj))
elif isinstance(obj, (list, tuple)):
parts = []
for part in obj:
parts.append(self.to_lisp_string(part))
return "(" + ' '.join(parts) + ")"
elif isinstance(obj, dict):
parts = []
for key, value in obj.items():
parts.append(self.to_lisp_string(symbol(key)))
parts.append(self.to_lisp_string(value))
return "(" + ' '.join(parts) + ")"
else:
return str(obj)
def write(self):
return self.to_lisp_string(self.value)
def read_lisp(code):
return LispReader(code).read()
def write_lisp(value):
return LispWritter(value).write()