-
Notifications
You must be signed in to change notification settings - Fork 0
/
old-parser.py
executable file
·212 lines (179 loc) · 5.54 KB
/
old-parser.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
#!/usr/bin/env python
## FILE: parser.py
# An interpreter of a language
import sys
import time
class Context:
token = ""
tokentype = "?"
queue = []
insymbol = False
innumber = False
intext = False
pass
def processtoken(context):
if context.intext:
context.intext = False
context.tokentype = "T"
elif context.insymbol:
context.insymbol = False
context.tokentype = "S"
elif context.innumber:
context.innumber = False
context.tokentype = "N"
else:
# must be an operator?
context.tokentype = "X"
evaltoken(context)
def cleartoken(context):
context.tokentype = "?"
context.token = ""
context.intext = False
context.insymbol = False
context.innumber = False
return
def evaltoken(context):
print("Eval: ", context.tokentype, context.token)
if context.tokentype != "X":
# evaluates to itself
context.queue.append((context.tokentype, context.token))
elif context.token == "@":
context.queue = [("Q", context.queue)]
elif context.token == "*" and len(context.queue) > 0 and context.queue[-1][0] == "Q":
v = context.queue.pop()
context.queue.extend(v[1])
print("Exploded: ", v)
elif context.token == "*" and len(context.queue) == 2 and context.queue[-1][0] == "S":
print("Symbol dereference not implemented")
elif context.token == "~" and len(context.queue) > 0:
v = context.queue.pop()
print("Deleted: ", v)
return
else:
# mark leftovers an error
# context.append(('E', context.token))
print("Meaningless token discarded: ", context.token)
#endif
cleartoken(context)
###################################
def parse(context=Context(), code=''):
for char in code:
# check for valid continuation
if context.intext:
context.token += char
if char in "\"":
processtoken(context)
continue
elif context.insymbol and (char.isidentifier() or char.isdecimal() or char in "_'"):
context.token += char
continue
elif context.innumber and char.isdecimal():
context.token += char
continue
# token not continuing
if len(context.token) > 0:
processtoken(context)
# skip spaces
if char.isspace():
continue
# are we starting a new long token or text string
if char.isidentifier() or char in "'":
context.insymbol = True
context.token += char
continue
elif char.isdecimal() or char in "+-":
context.innumber = True
context.token += char
continue
elif char in "\"":
context.intext = True
context.token += char
continue
# skip spaces
if char.isspace():
continue
# all other chars are singleton
context.token += char
processtoken(context)
#endfor
if len(context.token) > 0:
if context.intext:
context.token += "\n"
else:
processtoken(context)
pass
# end of input line reached
return context
################################
def printcontext(context):
if not context:
#print("Current Context: ", context)
return
#print("Current Context:")
if len(context.token) > 0:
print("Token Type: ", context.tokentype)
print("Token: ", context.token)
if context.intext:
print("Partial Text: ", context.token.encode())
printqueue(context.queue)
return
def printqueue(queue=[], depth=0):
if not queue:
print(queue)
return
for qtup in queue:
if qtup[0] == 'Q':
for i in range(depth):
print(" ", end="")
print("[")
printqueue(qtup[1], depth + 1)
for i in range(depth):
print(" ", end="")
print("]")
else:
for i in range(depth):
print(" ", end="")
print(" ", qtup)
return
###############################
def repl(*args, **kwargs):
prompt = "*> "
textprompt = "*> \""
context = Context()
while True:
if not context.intext:
printcontext(context)
try:
code = input(prompt if not context.intext else textprompt)
except EOFError:
print('EOF')
cleartoken(context)
break
#cleartoken(context)
except KeyboardInterrupt:
print('Interrupted, Continuing...')
print("Use control-D or 'x' to exit...")
cleartoken(context)
# skip parsing
continue
#try:
# newcontext = parse(context=context, code=code)
#except:
# print("Parse Error: ", sys.exc_info())
newcontext = parse(context=context, code=code)
if not newcontext:
print("Failed to rebuild context")
continue
context = newcontext
#end repl loop
if len(context.token) > 0:
if context.intext:
print("Text terminated")
context.token += "\""
processtoken(context)
input("Hit Enter to Reload...")
########## MAIN ##################
if (__name__ == "__main__"):
repl(sys.argv)
else:
print("parser loaded", time.time())