-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLRNIS.py
363 lines (363 loc) · 11.8 KB
/
NLRNIS.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# read file of first agrument
import sys, os
try:
data = open(sys.argv[1]).read()
except:
print("ERR")
exit()
dct = {"@": data, "E": "0"}
iteration = 0
RESET = False
define = False
printf = False
replace = False
inp = False
join = False
Revert = False
getascii = False
getfirstchar = False
removeallexceptcertainchar = False
readfile = False
comment = False
multilinecomment = False
takeagrument = False
definebutvar = False
getfilesandfolders = False
################################################################################
variable = ""
variable2 = ""
variable3 = ""
variable4 = ""
variable5 = ""
variable6 = ""
string = ""
escape = False
isready = False
isset = False
isgo = False
def erroring(errtype, errreason, tb):
print(end=errreason, file=sys.stderr)
sys.excepthook = erroring
def dctremoveallexceptcertain(string, variable):
# removes all except certain characters
# string = string to be modified
# variable = characters to be kept
# returns modified string
global newstring
newstring = ""
for i in string:
if i in variable:
newstring += i
return newstring
while True:
# Note you should use try and except to catch errors, not if x in y:
for i in data:
if printf:
# print the variable of dct variable
if i in dct:
print(dct[i], end = "")
printf = False
else:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
elif define:
if isready:
if escape:
# check if i is quote or backslash add to string else add backslash and i
if i == "\"" or i == "\\":
string += i
escape = False
else:
string += "\\" + i
escape = False
elif i == "\"":
dct[variable] = string
string = ""
isready = False
define = False
# check if the variable is "@" change the data
if variable == "@":
RESET = True
break
elif i == "\\":
escape = True
continue
else:
string += i
else:
variable = i
isready = True
elif replace:
# get 3 variables
if isgo:
try:
dct[variable] = dct[variable2].replace(variable3, i)
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
replace = isset = isready = isgo = False
# check if the variable is "@" change the data
if variable == "@":
RESET = True
break
elif isset:
variable3 = i
isgo = True
elif isready:
variable2 = i
isset = True
else:
variable = i
isready = True
elif inp:
try:
dct[i] = input()
dct["E"] = "0"
except EOFError:
dct["E"] = "1"
inp = False
if i == "@":
RESET = True
break
elif join:
if isset:
try:
dct[variable] = dct[variable2] + dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
join = isset = isready = isgo = False
# check if the variable is "@" change the data
if variable == "@":
RESET = True
break
elif isready:
variable2 = i
isset = True
else:
variable = i
isready = True
elif Revert:
# this is equivalent to pseudocode var = var2.reverse()
if isready:
try:
dct[variable] = dct[i][::-1]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
isready = False
Revert = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif getfirstchar:
# gets the first character of the variable
if isready:
try:
dct[variable] = dct[i][0]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
getfirstchar = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif getascii:
# gets ascii value of the variable
if isready:
try:
dct[variable]
dct[i]
try:
dct[variable] = ord(dct[i])
except:
raise Exception("ERROR IN FILE", sys.argv[1], "INVALID NUMBER IN ITERATION", iteration)
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
getascii = False
isready = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif removeallexceptcertainchar:
# removes all except certain character
if isready:
try:
dct[variable] = dctremoveallexceptcertain(dct[variable], dct[i])
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
removeallexceptcertainchar = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif readfile:
if isready:
try:
dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
try:
dct[variable] = open(dct[i], "r").read()
dct["E"] = "0"
except:
dct["E"] = "1"
isready = False
readfile = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif comment:
# ignore until a newline is found
if i == "\n":
comment = False
elif multilinecomment:
if i == "}":
multilinecomment = False
elif takeagrument:
# checks if the variable is defined and the variable is a number
if isready:
try:
dct[i]
try:
int(dct[i])
try:
dct[variable] = int(dct[i]) + 2
dct["E"] = "0"
except:
dct["E"] = "1"
except:
raise Exception("ERROR IN FILE", sys.argv[1], "INVALID NUMBER", iteration)
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
takeagrument = False
if variable == "@":
RESET = True
else:
variable = i
isready = True
elif definebutvar:
# like > but it defines the variable
if isready:
try:
dct[variable] = dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
definebutvar = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif getfilesandfolders:
# gets all files and folders in a variable if folder not found the variable "E" is equal to "1" else "0"
if isready:
# check if variable is defined
try:
dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
try:
files = os.listdir(dct[i])
dct[variable] = files
dct["E"] = "0"
except:
dct["E"] = "1"
isready = False
getfilesandfolders = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif i == "@": #############################################################################################################
RESET = True
break
elif i == "H":
break
elif i == ">":
define = True
elif i == "*":
printf = True
elif i == "$":
replace = True
elif i == "&":
inp = True
elif i == "+":
join = True
elif i == "/":
revert = True
elif i == "?":
getfirstchar = True
elif i == ".":
getascii = True
elif i == "!":
removeallexceptcertainchar = True
elif i == ";":
readfile = True
elif i == ":":
comment = True
elif i == "{":
multilinecomment = True
elif i == "|":
takeagrument = True
elif i == "=":
definebutvar = True
elif i == "%":
getfilesandfolders = True
# its not necessary to raise error invalid command
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
iteration += 1
# check if string still contains something put it in dct
if string != "":
dct[variable] = string
string = ""
define = False
isready = False
isset = False
isgo = False
printf = False
replace = False
inp = False
join, revert = False, False
getfirstchar = False
getascii = False
removeallexceptcertainchar = False
readfile = False
comment = False
multilinecomment = False
takeagrument = False
definebutvar = False
getfilesandfolders = False
# check if dct is "@" change the data
if variable == "@":
RESET = True
if RESET:
RESET = False
data = dct["@"]
iteration = 0
continue
break
try:
print(dct["#"], end = "")
except:
pass