-
Notifications
You must be signed in to change notification settings - Fork 0
/
eggeggegg.py
303 lines (281 loc) · 8.26 KB
/
eggeggegg.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import time
# EGGEGGEGG
# at the start of the program you have 1 egg (variable) available, and every n (5?) commands turtle lays an egg (eggs stored in a list, their index is used to refer to them)
# pseudo truth machine
'''
set $0 n
is $0 0
out 0
is $0 1
🐢 5
while $0 1
out 1
'''
# value - if its a number then its a number,
# if it starts with $ then 1) if its followed by a number its $egg; 2) 'c' without quotes will return total amount of available eggs; 3) 'n' will return amount of nonzero variables; 4) 'z' will return amount of zeros (int only)
# if starts with " then everything until the end of the line is saved as a string (\n = newline, \s = space),
# if its 'n' without quotes then it asks for input (int), if its 'i' without quotes it asks for input (str)
# -----
# set $egg value - sets value of $egg to value
# is value1 value2 - executes next line only if value1 is equal to value2
# isnot value1 value2 - executes next line only if value1 is NOT equal to value2
# while value1 value2 - while value1 is equal to value2 do the next line
# whilenot value1 value2 - while value1 is NOT equal to value2 execute the next line
# out value - output value
# turtle value - goto line number value (count from 0)
# add value1 value2 $egg - adds value1 to value2 and saves into $egg
# sub value1 value2 $egg - subtracts value2 from value1 and saves into $egg
# mult value1 value2 $egg - multiplies value1 by value2 and saves into $egg
# div value1 value2 $egg - divides value1 by value2 (integer division) and saves into $egg
# conc value1 value2 $egg - concatenate value1 and value2 and save into $egg
# toint value $egg - convert value to int if possible and save into $egg
# tostr value $egg - convert value to str and save into $egg
# free - you free the turtle
#TODO:
eggs = [0]
turtle = 0
eggcount = 1
def reset():
global eggs,turtle,eggcount
eggs = [0]
turtle = 0
eggcount = 1
def toval(xx): #xxv = xx value
if xx[0] == '"':
xxv = xx[1:].replace('\\n','\n').replace('\s',' ')
elif xx[0] == '$':
if xx[1] in ['0','1','2','3','4','5','6','7','8','9']:
xxv = eggs[int(xx[1:])]
elif xx[1:3] in ['-0','-1','-2','-3','-4','-5','-6','-7','-8','-9']:
xxv = eggs[abs(int(xx[1:]))]
elif xx[1] == 'c': #c = count
xxv = len(eggs)
elif xx[1] == 'n':
xxv = 0
for n in eggs:
if n != 0:
xxv += 1
elif xx[1] == 'z':
xxv = 0
for n in eggs:
if n == 0:
xxv += 1
elif xx.rstrip() == 'n':
xxv = int(input('n>'))
elif xx.rstrip() == 'i':
xxv = input('i>')
elif xx[0] in ['0','1','2','3','4','5','6','7','8','9']:
xxv = int(xx)
elif xx[0:2] in ['-0','-1','-2','-3','-4','-5','-6','-7','-8','-9']:
xxv = int(xx)
else:
xxv = 0
return xxv
def eee(abc,ln=0):
global eggs,turtle,eggcount
abcl = abc.split('\n')
#comments
win = 0
for w in abcl:
w2in = 0
for w2 in w:
if w2 == '`':
abcl[win]=abcl[win][:w2in]
w2in += 1
win += 1
#print(abcl)
line = ln
while line < len(abcl):
if abcl[line].strip(' ') == '':
line += 1
continue
time.sleep(turtle)
#print(eggcount,eggs,line,abcl[line])
i = abcl[line]
isp = i.lstrip(' ').split(' ')
if eggcount == 5:
eggs.append(0)
eggcount = 0
# set
if isp[0] == 'set':
if isp[1][0] == '$':
eggs[abs(int(isp[1][1:]))] = toval(' '.join(isp[2:]))
# out
elif isp[0] == 'out':
print(toval(' '.join(isp[1:])),end='',flush=True)
# funny goto
elif isp[0] == 'turtle':
if line > abs(int(isp[1])):
while line > abs(int(isp[1]))-1:
line -= 1
elif line < abs(int(isp[1]))-1:
while line < abs(int(isp[1]))-1:
line += 1
elif line == abs(int(isp[1])):
line -= 1
# if ==
elif isp[0] == 'is':
add = 1
if toval(isp[1]) != toval(' '.join(isp[2:])):
while abcl[line+add].lstrip(' ').split(' ')[0] in ['is','while']:
add += 1
line += add
# if !=
elif isp[0] == 'isnot':
add = 1
if toval(isp[1]) == toval(' '.join(isp[2:])):
while abcl[line+add].lstrip(' ').split(' ')[0] in ['is','while']:
add += 1
line += add
# while ==
elif isp[0] == 'while':
add = 1
op1 = toval(isp[1])
op2 = toval(' '.join(isp[2:]))
if op1 != op2:
while abcl[line+add].lstrip(' ').split(' ')[0] in ['is','while']:
add += 1
line += add
else:
while abcl[line+add].lstrip(' ').split(' ')[0] in ['is','while']:
add += 1
end = line+add
while op1 == op2:
if isp[1] not in ['n','i']:
op1 = toval(isp[1])
if isp[2] not in ['n','i']:
op2 = toval(' '.join(isp[2:]))
if op1 == op2:
break
#eee('\n'.join(abcl[line+1:end+1]))
if abcl[end].split(' ')[0] == 'turtle':
eee(abc,end)
else:
eee('\n'.join(abcl[line+1:end+1]))
line += 1
# while !=
elif isp[0] == 'whilenot':
add = 1
op1 = toval(isp[1])
op2 = toval(' '.join(isp[2:]))
if op1 == op2:
while abcl[line+add].lstrip(' ').split(' ')[0] in ['is','while']:
add += 1
line += add
else:
while abcl[line+add].lstrip(' ').split(' ')[0] in ['is','while']:
add += 1
end = line+add
while op1 != op2:
if isp[1] not in ['n','i']:
op1 = toval(isp[1])
if isp[2] not in ['n','i']:
op2 = toval(' '.join(isp[2:]))
if op1 == op2:
break
#eee('\n'.join(abcl[line+1:end+1]))
if abcl[end].split(' ')[0] == 'turtle':
eee(abc,end)
else:
eee('\n'.join(abcl[line+1:end+1]))
line += 1
# ---- MATH ----
# add
elif isp[0] == 'add':
op1 = toval(isp[1]) #op1 = operation1
op2 = toval(isp[2])
if type(op1) == int and type(op2) == int:
if isp[3][0] == '$':
eggs[abs(int(isp[3][1:]))] = op1+op2
else:
print('\nerror (line {0}): cant add strs'.format(line),flush=True)
break
# sub
elif isp[0] == 'sub':
op1 = toval(isp[1])
op2 = toval(isp[2])
if type(op1) == int and type(op2) == int:
if isp[3][0] == '$':
eggs[abs(int(isp[3][1:]))] = op1-op2
else:
print('\nerror (line {0}): cant subtract strs'.format(line),flush=True)
break
# mult
elif isp[0] == 'mult':
op1 = toval(isp[1])
op2 = toval(isp[2])
if type(op1) == int and type(op2) == int:
if isp[3][0] == '$':
eggs[abs(int(isp[3][1:]))] = op1*op2
else:
print('\nerror (line {0}): cant multiply strs'.format(line),flush=True)
break
# div
elif isp[0] == 'div':
op1 = toval(isp[1])
op2 = toval(isp[2])
if type(op1) == int and type(op2) == int:
if isp[3][0] == '$':
eggs[abs(int(isp[3][1:]))] = op1//op2
else:
print('\nerror (line {0}): cant divide strs'.format(line),flush=True)
break
# conc
elif isp[0] == 'conc':
op1 = toval(isp[1])
op2 = toval(isp[2])
if type(op1) == str and type(op2) == str:
if isp[3][0] == '$':
eggs[abs(int(isp[3][1:]))] = op1+op2
else:
print('\nerror (line {0}): cant concatenate ints'.format(line),flush=True)
break
# toint
elif isp[0] == 'toint':
if isp[2][0] == '$':
eggs[abs(int(isp[2][1:]))] = int(toval(isp[1]))
else:
print('\nerror (line {0}): cant save result of toint'.format(line),flush=True)
break
# tostr
elif isp[0] == 'tostr':
if isp[2][0] == '$':
eggs[abs(int(isp[2][1:]))] = str(toval(isp[1]))
else:
print('\nerror (line {0}): cant save result of tostr'.format(line),flush=True)
break
# get
elif isp[0] == 'get':
if isp[2][0] == '$':
eggs[abs(int(isp[2][1:]))] = eggs[abs(toval(isp[1]))]
else:
print('\nerror (line {0}): cant save result of get'.format(line),flush=True)
break
# free
elif isp[0] == 'free':
break
line += 1
eggcount += 1
turtle += 0.1
#print('end',eggs,line)
print('Welcome to the EGGEGGEGG interpreter')
while 1:
print('Type "c" to code - use ; for newlines (then type c again to exit); "o" to open a file')
og = input()
if og == 'c':
cinp = input()
while cinp != 'c':
eee(cinp.replace(';','\n'))
print('/---\\')
reset()
cinp = input()
elif og == 'o':
print('Enter file path')
oinp = input()
oopen = open(oinp,'r')
oread = oopen.read()
oopen.close()
eee(oread)
#eee(open(input(),'r').read())
print('/---\\')
reset()