-
Notifications
You must be signed in to change notification settings - Fork 7
/
interp_2.py
289 lines (267 loc) · 11.3 KB
/
interp_2.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
"""
python drocta ~ATH interpreter
It interprets things written in drocta ~ATH
and is written in python.
Really, I thought the name was fairly self explanatory.
Build number:10
(note:build number might not be accurate, sometimes I forget to increment it.
But I dont decrement it so its still maybe somewhat useful.
or you could just check the github versions. w/e.)
"""
import bif
import re
import pdb
def bifurcate(valueA,valueB=False):
if(valueB):
return bif.unbifurcate(valueA, valueB)
else:
return bif.bifurcate(valueA)
def matchParens(text,start,openStr,closeStr):
count=0
charNum=start
firstChar=True
while(count>0 or firstChar):
if(charNum>=len(text)):
#print "err:could not find match!"
return -1
elif(text[charNum]==closeStr):
count=count-1
#print "close at "+str(charNum)
elif(text[charNum]==openStr):
count=count+1
#print "open at "+str(charNum)
if(firstChar):
firstChar=False
charNum=charNum+1
return charNum-1
def textToNextSemicolon(text,start=0):
semicolonOffset=text.find(';',start)
return text[start:semicolonOffset]
charObjs={}
def getCharObj(theChar):
if(theChar in charObjs):
return charObjs[theChar]
else:
theCharObj=bif.value_obj()
charObjs[theChar]=theCharObj
return theCharObj
def getStrObj(theStr):
if(len(theStr)==0):
return NULL_obj
else:
return bifurcate(getCharObj(theStr[0]),getStrObj(theStr[1:]))
def getObjStr(theObj):
outStr=""
theObj2=theObj
while(theObj2.living):
(leftObj,rightObj)=bifurcate(theObj2)
for char in charObjs:
if(charObjs[char]==leftObj):
outStr+=char
break
theObj2=rightObj
return outStr
funCodes={}
funCodes['HELLO']="""
print "Hello World.";"
THIS.DIE(THIS);"""
funCodes["ADD"]="""
import bluh BLAH;
BIFURCATE ARGS[A,B];
BIFURCATE [BLAH,A]ATEMP;
BIFURCATE [BLAH,B]BTEMP;
BIFURCATE ATEMP[JUNK,ATEMP];
BIFURCATE BTEMP[JUNK,BTEMP];
BIFURCATE [BLAH,NULL]C;
BIFURCATE C[JUNK,C];
~ATH(ATEMP){
BIFURCATE ATEMP[JUNK,ATEMP];
BIFURCATE [BLAH,C]C;
}
~ATH(BTEMP){
BIFURCATE BTEMP[JUNK,BTEMP];
BIFURCATE [BLAH,C]C;
}
/*
BIFURCATE [BLAH,C]CTEMP;
BIFURCATE CTEMP[JUNK,CTEMP];
~ATH(CTEMP){
BIFURCATE CTEMP[JUNK,CTEMP];
print some text;
}
print DONE!;
*/
THIS.DIE(C);
"""#NOTE:use a better addition algorithm.
NULL_obj=bif.value_obj()
NULL_obj.DIE()
def evalScript(script,inObj):
ATHVars={}
THIS=bif.value_obj()
ATHVars['THIS']=THIS
ATHVars['NULL']=NULL_obj
ATHVars['ARGS']=inObj
return_obj=NULL_obj
charNum=0
execStack=[]
while(THIS.living):
if(False):
pass
elif(script.startswith('import ',charNum)):
semicolonOffset=script[charNum:].index(';')
importStatementStr=script[charNum:charNum+semicolonOffset]
importStatementList=importStatementStr.split(' ')
if(importStatementList[-1] not in ATHVars):
ATHVars[importStatementList[-1]]=bif.value_obj()
charNum+=semicolonOffset
elif(re.match(r'importf ([^; ]+) as ([^; ]+);',script[charNum:])!=None):
matches=re.match(r'importf ([^; ]+) as ([^; ]+);',script[charNum:])
importfFilename=matches.group(1)
#print "the filename is "+importfFilename
try:
importfFilelink=open(importfFilename,'r')
newFunc=importfFilelink.read(-1)
#print newFunc
funCodes[matches.group(2)]=newFunc
except:
print "could not read file "+importfFilename
charNum=script.find(';',charNum)
elif(script.startswith('~ATH(',charNum)):
closeparenOffset=script[charNum:].index(')')
loopVar=script[charNum+5:charNum+closeparenOffset]
loopVar=loopVar.strip(' \t\n\r')
#print "reached ~ATH command, loopVar is "+loopVar
if(loopVar in ATHVars):
if(ATHVars[loopVar].living):
execStack.append((charNum,'{'))
charNum+=closeparenOffset
#print "loop on "+loopVar
else:
#print "parenmatch jump from "+str(charNum)
charNum=matchParens(script,charNum,'{','}')+2
#print "parenmatch jumped to char:"+str(charNum)+" which was"+script[charNum]
#print "loopVar was "+loopVar
else:
print('warning/error: \"{0}\" is undefined'.format(loopVar))
elif(script.startswith('}',charNum)):
openingTuple=execStack.pop()
if(openingTuple[1]=='{'):
charNum=openingTuple[0]
else:
print('what are you trying to do? \"(...}\" error')
elif(script.startswith('print ',charNum)):
#print "print..."
semicolonOffset=script[charNum:].index(';')
print(script[charNum+6:charNum+semicolonOffset])
charNum+=semicolonOffset#+6
elif(re.match(r'PRINT2 ([^\[\];]*);',script[charNum:])!=None):
matches=re.match(r'PRINT2 ([^\[\];]*);',script[charNum:])
print getObjStr(ATHVars[matches.group(1)])
charNum=script.find(';',charNum)
elif(script.startswith('INPUT',charNum)):
semicolonOffset=script[charNum:].index(';')
varname=script[charNum+6:charNum+semicolonOffset]
#print 'INPUT varname was "'+varname+'"'
ATHVars[varname]=getStrObj(raw_input(':'))
charNum+=semicolonOffset
elif(re.match(r'BIFURCATE ([^\[\];]*)\[([^\[\];]*),([^\[\];]*)\];',script[charNum:])!=None):
#print("binurcate the thing!")
matches=re.match(r'BIFURCATE ([^\[\];]*)\[([^\[\];]*),([^\[\];]*)\];',script[charNum:])
(ATHVars[matches.group(2)],ATHVars[matches.group(3)])=bifurcate(ATHVars[matches.group(1)])
charNum=script.find(';',charNum)
elif(re.match(r'BIFURCATE \[([^\[\];]*),([^\[\];]*)\]([^\[\];]*);',script[charNum:])!=None):
matches=re.match(r'BIFURCATE \[([^\[\];]*),([^\[\];]*)\]([^\[\];]*);',script[charNum:])
ATHVars[matches.group(3)]=bifurcate(ATHVars[matches.group(1)],ATHVars[matches.group(2)])
charNum=script.find(';',charNum)
elif(script.startswith('BIFFURCATE ',charNum)):
charNum+=10
semicolonOffset=script[charNum:].index(';')
openSquareOffset=script[charNum:].index('[')
closeSquareOffset=script[charNum:].index(']')
commaOffset=script[charNum:].index(',')
syntacticallyCorrect=True
for offset in [openSquareOffset,closeSquareOffset,commaOffset]:
if((offset==-1) or (offset>semicolonOffset)):
print("Bifurcate command malformed, char:"+str(charNum))
syntacticallyCorrect=False
break
if(syntacticallyCorrect):
if(openSquareOffset==0):
leftHalf=script[charNum+openSquareOffset+1:charNum+commaOffset]
rightHalf=script[charNum+commaOffset+1:charNum+closeSquareOffset]
combinedName=script[charNum+closeSquareOffset+1:charNum+semicolonOffset]
ATHVars[combinedName]=bifurcate(ATHVars[leftHalf],ATHVars[rightHalf])
else:
toSplitName=script[charNum:charNum+openSquareOffset]
leftHalf=script[charNum+openSquareOffset+1:charNum+commaOffset]
rightHalf=script[charNum+commaOffset+1:charNum+closeSquareOffset]
(ATHVars[leftHalf],ATHVars[rightHalf])=bifurcate(ATHVars[toSplitName])
elif(re.match(r'([0-9a-zA-Z]+)\.DIE\(([0-9a-zA-Z]*)\);',script[charNum:])!=None):#script[charNum:script[charNum:].find(';')].endswith('.DIE()')):
matches=re.match(r'([0-9a-zA-Z]+)\.DIE\(([0-9a-zA-Z]*)\);',script[charNum:])#.group(1)
varname=matches.group(1)
argvarname=matches.group(2)
if argvarname:
#print("argvarname is " +argvarname)
return_obj=ATHVars[argvarname]
#print "found .DIE(); statement! Variable name is "+varname
ATHVars[varname].DIE()
charNum=script.find(';',charNum)
#print varname+"killed"
elif(script.startswith('//',charNum)):
nextNewlinePos=script.find('\n',charNum)
if '\r' in script[charNum:nextNewlinePos]:
nextNewlinePos=script.find('\r',charNum)
charNum=nextNewlinePos
elif(script.startswith('/*',charNum)):
charNum=script.find('*/',charNum)
elif(script.startswith('PYDEBUG',charNum)):
pdb.set_trace()
charNum+=5
elif(re.match(r'([A-Z0-9_]+) \[([^\[\];]*),([^\[\];]*)\]([^\[\];]*);',script[charNum:])!=None):
try:
matches=re.match(r'([A-Z0-9_]+) \[([^\[\];]*),([^\[\];]*)\]([^\[\];]*);',script[charNum:])
funName=matches.group(1)
#print "the function called '"+funName + "' was called."
#print "yeah it works."
if funName in funCodes:
theFuncCode=funCodes[funName]
sentInObject=bifurcate(ATHVars[matches.group(2)],ATHVars[matches.group(3)])
ATHVars[matches.group(4)]=evalScript(theFuncCode,sentInObject)
else:
print "error: function called '"+funName+"' not recognized"
charNum+=len(funName)
except:
print "function not recognized/ a bug in the interpreter"
print matches#re.match(r'([A-Z0-9_]+) \[([^\[\];]*),([^\[\];]*)\]([^\[\];]*);',script[charNum:])
print "..."
charNum+=1
#charNum+=1
elif(re.match(r'([A-Z0-9_]+) ([^\[\];]*)\[([^\[\];]*),([^\[\];]*)\];',script[charNum:])!=None):
try:
matches=re.match(r'([A-Z0-9_]+) ([^\[\];]*)\[([^\[\];]*),([^\[\];]*)\];',script[charNum:])
funName=matches.group(1)
#print "the function called '"+funName + "' was called."
#print "yeah it works."
if funName in funCodes:
theFuncCode=funCodes[funName]
sentInObject=ATHVars[matches.group(2)]#bifurcate(ATHVars[matches.group(2)],ATHVars[matches.group(3)])
ATHVars[matches.group(3)],ATHVars[matches.group(4)]=bifurcate(evalScript(theFuncCode,sentInObject))
else:
print "error: function called '"+funName+"' not recognized"
charNum+=len(funName)
except:
print "function not recognized/ a bug in the interpreter"
print matches#re.match(r'([A-Z0-9_]+) \[([^\[\];]*),([^\[\];]*)\]([^\[\];]*);',script[charNum:])
print "..."
charNum+=1
else:
charNum+=1
if(charNum > len(script)):
THIS.DIE()
#print script[charNum]
return return_obj
filename=raw_input()
filelink=open(filename,'r')
script=filelink.read(-1)
result_obj=evalScript(script,NULL_obj)
raw_input("press enter to close")