-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlispparser.py
More file actions
71 lines (62 loc) · 1.58 KB
/
lispparser.py
File metadata and controls
71 lines (62 loc) · 1.58 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
from interpreter import *
def tryParse(n):
return tryParseQuote(tryParseFloat(tryParseInt(n)))
def tryParseInt(n):
try:
return Literal(int(n))
except Exception:
return n
def tryParseFloat(n):
try:
return Literal(float(n))
except Exception:
return n
def tryParseQuote(n):
try:
if(n[0] == "'"):
return ["quote",tryParse(n[1:])]
return n
except Exception:
return n
def parse_quote(st):
inesc = False
qt = ""
while len(st) > 0:
if st[0] == '"' and inesc:
qt += '"';
elif st[0] == '"':
return (Literal(qt),st)
else:
qt += st[0]
st = st[1:]
return (Literal(qt),st)
def whitespace(st):
return st == " " or st == "\n" or st == "\r" or st == "\t"
def read_expr(st,expr = None,word = ""):
if expr == None:
expr = []
inword = False
while len(st) > 0:
if(st[0] == "("):
(nexpr, st) = read_expr(st[1:],[],"")
expr.append(nexpr)
elif(st[0] == '"'):
(qt,st) = parse_quote(st[1:])
expr.append(qt)
elif(whitespace(st[0]) and inword):
expr.append(tryParse(word))
inword=False
word = ""
elif(whitespace(st[0])):
pass
elif(st[0]== ")"):
if(inword):
expr.append(tryParse(word))
return (expr,st)
else:
inword = True
word += st[0]
st = st[1:]
return (expr,st)
def read(st):
return read_expr(st)[0]