Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.95 KB | None | 0 0
  1. import custom_types, datetime, itertools, math, random, re, regex, time, sys
  2.  
  3. CMD_REGEX = r"Kk|(E[^\d ]([\da-f]+)?)|([?SmFvW][^\d]([\da-f]+)?)|([gr].)|([tU]..)|(o.)|('[^']+')|([^\da-f? ]([\da-f]+)?)"
  4. NESTED = [1, 3, 9]
  5.  
  6. ALPHABETU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  7. ALPHABETL = 'abcdefghijklmnopqrstuvwxyz'
  8. ALPHABETB = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  9. CONSONANTSU = 'BCDFGHJKLMNPQRSTVWXYZ'
  10. CONSONANTSL = 'bcdfghjklmnpqrstvwxyz'
  11. CONSONANTSB = 'BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz'
  12. VOWELU = 'AEIOU'
  13. VOWELL = 'aeiou'
  14. VOWELB = 'AEIOUaeiou'
  15. VOWELYU = 'AEIOUY'
  16. VOWELYL = 'aeiouy'
  17. VOWELYB = 'AEIOUYaeiouy'
  18. QWERTYU = ['QWERTYUIOP','ASDFGHJKL','ZXCVBNM']
  19. QWERTYL = ['qwertyuiop','asdfghjkl','zxcvbnm']
  20. KEYBOARDML = ['§1234567890-=','qwertyuiop[]',"asdfghjkl;'\\",'`zxcvbnm,./']
  21. KEYBOARDMU = ['±!@#$%^&*()_+','QWERTYUIOP{}','ASDFGHJKL:"|','~ZXCVBNM<>?']
  22. KEYBOARDWU = ['¬!"£$%^&*()_+','QWERTYUIOP{}','ASDFGHJKL:@~','|ZXCVBNM<>?']
  23. KEYBOARDWL = ['`1234567890-=','qwertyuiop[]',"asdfghjkl;'#",'\\zxcvbnm,./']
  24. DIGIT = '0123456789'
  25. HEX = '0123456789abcdef'
  26. QUINE = 'EqO'
  27. WORD = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_'
  28. PRINTABLE = '''!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'''
  29. PI = math.pi
  30. E = math.e
  31. PHI = (1 + math.sqrt(5)) / 2
  32.  
  33. def binary(value):
  34. if isinstance(value, (int, float, bool)):
  35. return bin(int(value)[2:])
  36. return ''.join(map(lambda c: bin(ord(c))[2:], value))
  37.  
  38. def bin_(value):
  39. if isinstance(value, list):
  40. return list(map(bin_, value))
  41. return bin(value)[2:]
  42.  
  43. def choice(value):
  44. if isinstance(value, (int, float)):
  45. return random.randrange(0, value)
  46. return random.choice(value)
  47.  
  48. def combinations(n, r):
  49. return list(itertools.combinations(n, r))
  50.  
  51. def deltas(value):
  52. if isinstance(value, str):
  53. value = list(map(ord, value))
  54. if isinstance(value, (int, float)):
  55. value = list(range(abs(int(value))))
  56. if isinstance(value, list):
  57. value = list(filter(lambda a: isinstance(a, int), value))
  58. final = []
  59. for i in range(1,len(value)):
  60. final.append(value[i] - value[i-1])
  61. return final
  62.  
  63. def divisors(x):
  64. if isinstance(x, list):
  65. return list(map(divisors, x))
  66. return list(filter(lambda a: int(x) % a == 0, range(1,int(x)+1)))
  67.  
  68. def divmod_(x, y):
  69. if isinstance(x, list):
  70. return list(map(lambda a: divmod_(a, y), x))
  71. if isinstance(y, list):
  72. return list(map(lambda a: divmod_(x, a), y))
  73. if isinstance(x, list) and isinstance(y, list):
  74. return list(map(divmod_, x, y))
  75. return [x//y, x%y]
  76.  
  77. def elementwise(left, right, mode):
  78. left, right = str(left), str(right)
  79. length = min(len(left), len(right))
  80. left = left[:length]
  81. right = right[:length]
  82.  
  83. SWITCHES = {
  84. 1:lambda lc, rc: chr(ord(lc) + ord(rc)),
  85. 2:lambda lc, rc: chr(ord(lc) - ord(rc)),
  86. 3:lambda lc, rc: chr(ord(lc) * ord(rc)),
  87. 4:lambda lc, rc: chr(ord(lc) // ord(rc)),
  88. 5:lambda lc, rc: chr(min(ord(lc), ord(rc))),
  89. 6:lambda lc, rc: chr(max(ord(lc), ord(rc))),
  90.  
  91. 7:lambda lc, rc: chr(((ord(lc)-32) + (ord(rc)-32)) % 95 + 32),
  92. 8:lambda lc, rc: chr(((ord(lc)-32) * (ord(rc)-32)) % 95 + 32),
  93. 9:lambda lc, rc: chr(((ord(lc)-32) ** (ord(rc)-32)) % 95 + 32),
  94.  
  95. 10:lambda lc, rc: random.choice([lc, rc]),
  96. }
  97.  
  98. try:
  99. switch = SWITCHES[int(mode)]
  100. except:
  101. switch = lambda lc, rc: lc
  102.  
  103. return ''.join(map(switch, left, right))
  104.  
  105. def eval_(value):
  106. if isinstance(value, list):
  107. return list(map(eval_, value))
  108. try:
  109. return float(value)
  110. except:
  111. try:
  112. return int(value)
  113. except:
  114. try:
  115. return eval(value)
  116. except:
  117. return str(value)
  118.  
  119. def flatten(array):
  120. array = str(array)
  121. array = '['+array.replace('[','').replace(']','')+']'
  122. return eval(array)
  123.  
  124. def hexadecimal(value):
  125. if isinstance(value, (int, float, bool)):
  126. return hex(int(value)[2:])
  127. return ''.join(map(lambda c: hex(ord(c))[2:], value))
  128.  
  129. def hex_(value):
  130. if isinstance(value, list):
  131. return list(map(hex_, value))
  132. return hex(value)[2:]
  133.  
  134. def indexes(var, val):
  135. zipped = zip(str(var), str(val))
  136. return list(map(lambda a: int(a[0] == a[1]), zipped))
  137.  
  138. def isprime(val):
  139. if isinstance(val, list):
  140. return list(map(isprime, val))
  141. for i in range(2,val):
  142. if val % i == 0:
  143. return False
  144. return True
  145.  
  146. def length(value):
  147. try:
  148. return len(value)
  149. except:
  150. return len(str(value))
  151.  
  152. def print_(*values, sep=' ', end='\n'):
  153. print(*values, sep=sep, end=str(end), file=open('stdout.txt','a'))
  154. return sep.join(map(str, values))+end
  155.  
  156. def permutations(value):
  157. try:
  158. return list(itertools.permutations(value))
  159. except:
  160. return list(itertools.permutations(str(value)))
  161.  
  162. def repeat(iterable, repetitions):
  163. final = []
  164. for i in range(len(repetitions)):
  165. final.append(iterable)
  166. if isinstance(iterable, str):
  167. return ''.join(final)
  168. return final
  169.  
  170. def reverse(value):
  171. try:
  172. return value[::-1]
  173. except:
  174. return eval(str(value)[::-1])
  175.  
  176. def set_(string):
  177. final = []
  178. for char in string:
  179. if char not in final:
  180. final.append(char)
  181. return final
  182.  
  183. def shuffle(string):
  184. random.shuffle(string)
  185. return string
  186.  
  187. def swap(stack):
  188. stack[-1], stack[-2] = stack[-2], stack[-1]
  189.  
  190. class Stack(list):
  191. def __getitem__(self, index_slice):
  192. try:
  193. return super().__getitem__(index_slice)
  194. except IndexError:
  195. return 1
  196.  
  197. def init(self):
  198. self.sort = True
  199. self.time = time.time()
  200.  
  201. def flatten(self):
  202. copy = self.copy()
  203. copy = flatten(copy)
  204. self.clear()
  205. self.push(*copy)
  206.  
  207. def deduplicate(self):
  208. copy = self.copy()
  209. copy = set_(copy)
  210. self.clear()
  211. self.push(*copy)
  212.  
  213. def push(self, *values):
  214. for v in values:
  215. self.append(v)
  216. if self.sort:
  217. copy = self.sort_self(self.copy())
  218. self.clear()
  219. for value in copy:
  220. self.append(value)
  221.  
  222. def peek(self,index=-1):
  223. return self[index]
  224.  
  225. def pop(self, index=-1):
  226. try:
  227. return super().pop(index)
  228. except:
  229. return 1
  230.  
  231. def sort_self(self, seq):
  232. for i in range(len(seq)):
  233. if type(seq[i]) == str:
  234. seq[i] = custom_types.Str(seq[i])
  235. if type(seq[i]) == int:
  236. seq[i] = custom_types.Int(seq[i])
  237. if type(seq[i]) == list:
  238. seq[i] = custom_types.List(seq[i])
  239. if type(seq[i]) == bool:
  240. seq[i] = custom_types.Bool(seq[i])
  241. if type(seq[i]) == float:
  242. seq[i] = custom_types.Float(seq[i])
  243.  
  244. for i in reversed(range(len(seq))):
  245. finished = True
  246. for j in range(i):
  247. if seq[j] > seq[j + 1]:
  248. seq[j], seq[j + 1] = seq[j + 1], seq[j]
  249. finished = False
  250. if finished:
  251. break
  252. return seq
  253.  
  254. COMMANDS = {
  255.  
  256. '!':lambda i,s: 'For loop',
  257. '"':lambda i,s: 'If statement',
  258. '#':lambda i,s: 'While loop',
  259. '$':lambda i,s: None,
  260. '%':lambda i,s: s.push(s.pop(i) % s.pop()),
  261. '&':lambda i,s: s.push(s.pop(i) & s.pop()),
  262. "'":lambda i,s: 'String terminator',
  263. '(':lambda i,s: s.push(s.pop(i) + 1),
  264. ')':lambda i,s: s.push(s.pop(i) - 1),
  265. '*':lambda i,s: s.push(s.pop(i) * s.pop()),
  266. '+':lambda i,s: s.push(s.pop(i) + s.pop()),
  267. ',':lambda i,s: s.push(s.pop(i) // s.pop()),
  268. '-':lambda i,s: s.push(s.pop(i) - s.pop()),
  269. '.':lambda i,s: s.push(s.pop(i) ** s.pop()),
  270. '/':lambda i,s: s.push(s.pop(i) / s.pop()),
  271.  
  272. '0':lambda i,s: s.push(0),
  273. '1':lambda i,s: s.push(1),
  274. '2':lambda i,s: s.push(2),
  275. '3':lambda i,s: s.push(3),
  276. '4':lambda i,s: s.push(4),
  277. '5':lambda i,s: s.push(5),
  278. '6':lambda i,s: s.push(6),
  279. '7':lambda i,s: s.push(7),
  280. '8':lambda i,s: s.push(8),
  281. '9':lambda i,s: s.push(9),
  282.  
  283. ':':lambda i,s: s.push(s.peek(i)),
  284. ';':lambda i,s: s.push(s.pop(i)),
  285. '<':lambda i,s: s.push(s.pop(i) < s.pop()),
  286. '=':lambda i,s: s.push(s.pop(i) == s.pop()),
  287. '>':lambda i,s: s.push(s.pop(i) > s.pop()),
  288. '?':lambda i,s: 'Boolean prefix',
  289. '@':lambda i,s: swap(s),
  290.  
  291. 'A':lambda i,s: s.push(abs(s.pop(i))),
  292. 'B':lambda i,s: s.push(s.pop(i)%2),
  293. 'C':lambda i,s: s.push(combinations(s.pop(i), s.peek())),
  294. 'D':lambda i,s: s.push(divmod_(s.pop(i), s.pop())),
  295. 'E':lambda i,s: 'Extension prefix',
  296. 'F':lambda i,s: 'Filter prefix',
  297. 'G':lambda i,s: s.push(math.gcd(s.pop(i), s.pop())),
  298. 'H':lambda i,s: s.push(s.pop(i)/2),
  299. 'I':lambda i,s: s.push(indexes(s.pop(i), s.pop())),
  300. 'J':lambda i,s: s.push(list(range(s.pop(i)))),
  301. 'K':lambda i,s: 'Sort determinate',
  302. 'L':lambda i,s: s.push(len(s.pop(i))),
  303. 'M':lambda i,s: s.push(elementwise(s.pop(i), s.pop(), s.pop())),
  304. 'N':lambda i,s: s.push(not s.pop(i)),
  305. 'O':lambda i,s: print_(s.peek(i)),
  306. 'P':lambda i,s: s.push(permutations(s.pop(i))),
  307. 'Q':lambda i,s: s.push(''.join(map(str, set_(s.pop(i))))),
  308. 'R':lambda i,s: s.push(s.pop(i).replace(s.pop(), s.pop())),
  309. 'S':lambda i,s: 'Sort control prefix',
  310. 'T':lambda i,s: s.push(deltas(s.pop(i))),
  311. 'U':lambda i,s: 'Unpacked 2 character literal',
  312. 'V':lambda i,s: s.push(reverse(s.pop(i))),
  313. 'W':lambda i,s: 'Datetime prefix',
  314. 'X':lambda i,s: s.push(choice(s.pop(i))),
  315. 'Y':lambda i,s: s.push('\n'.join(map(str, s.pop(i)))),
  316. 'Z':lambda i,s: s.push(''.join(map(''.join, zip(map(str, s.pop(i)), map(str, s.pop()))))),
  317.  
  318. '[':lambda i,s: s.push([s.pop(i)]),
  319. '\\':lambda i,s: s.push(s.pop(i)//1),
  320. ']':lambda i,s: s.push(flatten(s.pop(i))),
  321. '^':lambda i,s: s.push(s.pop(i) ^ s.pop()),
  322. '_':lambda i,s: print_(*s),
  323. '`':lambda i,s: s.push(s.pop(i).split(s.pop())),
  324.  
  325. 'a':lambda i,s: s.push(10),
  326. 'b':lambda i,s: s.push(11),
  327. 'c':lambda i,s: s.push(12),
  328. 'd':lambda i,s: s.push(13),
  329. 'e':lambda i,s: s.push(14),
  330. 'f':lambda i,s: s.push(15),
  331. 'g':lambda i,s: 'Case sensitive regex prefix',
  332. 'h':lambda i,s: None,
  333. 'i':lambda i,s: s.push(int(s.pop(i))),
  334. 'j':lambda i,s: s.push(chr(s.pop(i))),
  335. 'k':lambda i,s: 'Inverse sort determinate',
  336. 'l':lambda i,s: s.push(-i),
  337. 'm':lambda i,s: 'Apply-to-each prefix',
  338. 'n':lambda i,s: s.push('\n'.join(map(str, s))),
  339. 'o':lambda i,s: 'One character literal',
  340. 'p':lambda i,s: s.pop(i),
  341. 'q':lambda i,s: s.push(ord(s.pop(i))),
  342. 'r':lambda i,s: 'Case insensitive regex prefix',
  343. 's':lambda i,s: s.push(shuffle(s.pop(i))),
  344. 't':lambda i,s: 'Two character literal',
  345. 'u':lambda i,s: s.push(*s.pop(i)),
  346. 'v':lambda i,s: 'Vectorise prefix',
  347. 'w':lambda i,s: s[-1].pop(i),
  348. 'x':lambda i,s: s.push(random.randint(s.pop(i), s.pop())),
  349. 'y':lambda i,s: print_('\n'.join(map(str, s))),
  350. 'z':lambda i,s: 'Retrieve input',
  351.  
  352. '{':lambda i,s: s.push(s.pop(i) <= s.pop()),
  353. '|':lambda i,s: s.push(s.pop(i) | s.pop()),
  354. '}':lambda i,s: s.push(s.pop(i) >= s.pop()),
  355. '~':lambda i,s: s.push(~s.pop(i)),
  356.  
  357. }
  358.  
  359. BOOLEANS = {
  360.  
  361. 'A':lambda s, i: all(s),
  362. 'B':lambda s, i: bool(s.pop(i)),
  363. 'D':lambda s, i: bool(open('stdout.txt').read()),
  364. 'E':lambda s, i: all(s[i] == s[i+1] for i in range(len(s)-1)),
  365. 'F':lambda s, i: isfib(s.pop(i)),
  366. 'I':lambda s, i: isinstance(s.pop(i), int),
  367. 'K':lambda s, i: s.sort,
  368. 'L':lambda s, i: all(c.islower() for c in s.pop(i)),
  369. 'O':lambda s, i: list(s) in [sorted(s), sorted(s,reverse=True)],
  370. 'P':lambda s, i: isprime(s.pop(i)),
  371. 'Q':lambda s, i: sorted(set(s)) == sorted(s),
  372. 'R':lambda s, i: s == s[::-1],
  373. 'S':lambda s, i: isinstance(s.pop(i), str),
  374. 'U':lambda s, i: all(c.isupper() for c in s.pop(i)),
  375. 'X':lambda s, i: isinstance(s.pop(i), list),
  376. 'Z':lambda s, i: s.pop(i) > 0,
  377.  
  378. }
  379.  
  380. SORTS = {
  381.  
  382. 'L':length,
  383. 'M':max,
  384. 'I':min,
  385. 'B':binary,
  386. 'H':hexadecimal,
  387. 'E':eval_,
  388.  
  389. }
  390.  
  391. MAPPINGS = {
  392.  
  393. 'a':abs,
  394. 'b':bin_,
  395. 'c':chr,
  396. 'e':eval_,
  397. 'h':hex_,
  398. 'i':int,
  399. 'j':length,
  400. 'l':list,
  401. 'o':ord,
  402. 'p':print_,
  403. 'q':set_,
  404. 'r':reverse,
  405. 's':str,
  406. 'z':sorted,
  407.  
  408. }
  409.  
  410. FILTERS = {
  411.  
  412. 'A':lambda s: list(filter(lambda a: a.isalpha(), s)),
  413. 'a':lambda s: list(filter(lambda a: not a.isalpha(), s)),
  414. 'B':lambda s: list(filter(lambda a: bool(a), s)),
  415. 'b':lambda s: list(filter(lambda a: not a, s)),
  416. 'C':lambda s: list(filter(lambda a: a in s[-1], s)),
  417. 'c':lambda s: list(filter(lambda a: a not in s[-1], s)),
  418. 'D':lambda s: list(filter(lambda a: a.isdigit(), s)),
  419. 'd':lambda s: list(filter(lambda a: not a.isdigit(), s)),
  420. 'E':lambda s: list(filter(lambda a: a == s[-1], s)),
  421. 'e':lambda s: list(filter(lambda a: a != s[-1], s)),
  422. 'I':lambda s: list(filter(lambda a: isinstance(a, int), s)),
  423. 'i':lambda s: list(filter(lambda a: not isinstance(a, int), s)),
  424. 'J':lambda s: list(filter(lambda a: length(a) == s[-1], s)),
  425. 'j':lambda s: list(filter(lambda a: length(a) != s[-1], s)),
  426. 'L':lambda s: list(filter(lambda a: isinstance(a, list), s)),
  427. 'l':lambda s: list(filter(lambda a: not isinstance(a, list), s)),
  428. 'O':lambda s: list(filter(lambda a: list(a) in [sorted(a), sorted(a,reverse=True)], s)),
  429. 'o':lambda s: list(filter(lambda a: list(a) not in [sorted(a), sorted(a,reverse=True)], s)),
  430. 'P':lambda s: list(filter(lambda a: isprime(a), s)),
  431. 'p':lambda s: list(filter(lambda a: not isprime(a), s)),
  432. 'S':lambda s: list(filter(lambda a: isinstance(a, str), s)),
  433. 's':lambda s: list(filter(lambda a: not isinstance(a, str), s)),
  434. 'V':lambda s: list(filter(lambda a: a == a[::-1], s)),
  435. 'v':lambda s: list(filter(lambda a: a != a[::-1], s)),
  436. }
  437.  
  438. REGEX = {
  439.  
  440. 'c':(3, lambda p, s, c: regex.contains(p, s, c)),
  441. 'f':(3, lambda p, s, c: regex.findall(p, s, c)),
  442. 'g':(3, lambda p, s, c: regex.group(p, s, c)),
  443. 'm':(3, lambda p, s, c: regex.match(p, s, c)),
  444. 'b':(3, lambda p, s, c: regex.start(p, s, c)),
  445. 'p':(3, lambda p, s, c: regex.split(p, s, c)),
  446. 'o':(3, lambda p, s, c: regex.findor(p, s, c)),
  447. 's':(4, lambda p, s, r, c: regex.sub(p, s, r, c)),
  448. 'u':(4, lambda p, s, r, c: regex.unsub(p, s, r, c)),
  449.  
  450. 'C':(3, lambda p, s, c: flatten(regex.contains(p, s, c))),
  451. 'F':(3, lambda p, s, c: flatten(regex.findall(p, s, c))),
  452. 'G':(3, lambda p, s, c: flatten(regex.group(p, s, c))),
  453. 'M':(3, lambda p, s, c: flatten(regex.match(p, s, c))),
  454. 'B':(3, lambda p, s, c: flatten(regex.start(p, s, c))),
  455. 'P':(3, lambda p, s, c: flatten(regex.split(p, s, c))),
  456. 'O':(3, lambda p, s, c: flatten(regex.findor(p, s, c))),
  457. 'S':(4, lambda p, s, r, c: flatten(regex.sub(p, s, r, c))),
  458. 'U':(4, lambda p, s, r, c: flatten(regex.unsub(p, s, r, c))),
  459.  
  460. }
  461.  
  462. EXTENSIONS = {
  463.  
  464. 'A':lambda i, s: s.push(s[i]),
  465. 'C':lambda i, s: s.clear(),
  466. 'D':lambda i, s: s.push(divisors(s.pop(i))),
  467. 'E':lambda i, s: s.push(enumerate(s.pop(i))),
  468. 'F':lambda i, s: s.push(fib(s.pop(i))),
  469. 'I':lambda i, s: s.push(int(s.pop(i))),
  470. 'J':lambda i, s: s.push(''.join(map(str, s.pop(i)))),
  471. 'L':lambda i, s: s.push(str(s.pop(i)).lower()),
  472. 'N':lambda i, s: s.push(str(s.pop(i)).count(str(s.pop()))),
  473. 'O':lambda i, s: print_(end=str(s.peek(i))),
  474. 'P':lambda i, s: s.push(nprime(s.pop(i))),
  475. 'Q':lambda i, s: s.deduplicate(),
  476. 'R':lambda i, s: s.reverse(),
  477. 'S':lambda i, s: s.push(sorted(s.pop(i))),
  478. 'T':lambda i, s: s.push(str(s.pop(i)).title()),
  479. 'U':lambda i, s: s.push(str(s.pop(i)).upper()),
  480. 'V':lambda i, s: s.push(eval_(s.pop(i))),
  481. 'W':lambda i, s: s.push(str(s.pop(i)).swapcase()),
  482. 'Y':lambda i, s: exec(str(s.pop(i))),
  483. '"':lambda i, s: s.push(str(s.pop(i))),
  484. '[':lambda i, s: s.push(list(s.pop(i))),
  485. ']':lambda i, s: s.flatten(),
  486. '.':lambda i, s: s.push(list(s.pop(i)).index(s.pop())),
  487. '{':lambda i, s: s.push(s),
  488. '}':lambda i, s: repeat(s, s.pop(i)),
  489. '>':lambda i, s: repeat(s.pop(i), s.pop()),
  490. '_':lambda i, s: print_(s),
  491. ';':lambda i, s: s.push(''.join(map(str, s))),
  492.  
  493. 'a':lambda i, s: s.push(PRINTABLE),
  494. 'b':lambda i, s: s.push(ALPHABETU),
  495. 'c':lambda i, s: s.push(CONSONANTSB),
  496. 'd':lambda i, s: s.push(DIGIT),
  497. 'e':lambda i, s: s.push(E),
  498. 'f':lambda i, s: s.push(ALPHABETL),
  499. 'g':lambda i, s: s.push(ALPHABETB),
  500. 'h':lambda i, s: s.push(HEX),
  501. 'i':lambda i, s: s.push(PHI),
  502. 'j':lambda i, s: s.push(CONSONANTSU),
  503. 'k':lambda i, s: s.push(KEYBOARDWU),
  504. 'l':lambda i, s: s.push(KEYBOARDMU),
  505. 'm':lambda i, s: s.push(CONSONANTSL),
  506. 'n':lambda i, s: s.push(VOWELU),
  507. 'o':lambda i, s: s.push(VOWELL),
  508. 'p':lambda i, s: s.push(PI),
  509. 'q':lambda i, s: s.push(QUINE),
  510. 'r':lambda i, s: s.push(QWERTYU),
  511. 's':lambda i, s: s.push(VOWELYU),
  512. 't':lambda i, s: s.push(VOWELYL),
  513. 'u':lambda i, s: s.push(QWERTYL),
  514. 'v':lambda i, s: s.push(VOWELB),
  515. 'w':lambda i, s: s.push(WORD),
  516. 'x':lambda i, s: s.push(KEYBOARDWL),
  517. 'y':lambda i, s: s.push(VOWELYB),
  518. 'z':lambda i, s: s.push(KEYBOARDML),
  519.  
  520. ' ':lambda i, s: s.push(100),
  521. '!':lambda i, s: s.push(128),
  522. '#':lambda i, s: s.push(256),
  523. '$':lambda i, s: s.push(512),
  524. '%':lambda i, s: s.push(1000),
  525. '&':lambda i, s: s.push(1024),
  526. "'":lambda i, s: s.push(2048),
  527. '(':lambda i, s: s.push(4096),
  528. ')':lambda i, s: s.push(8192),
  529. '*':lambda i, s: s.push(16384),
  530. '+':lambda i, s: s.push(32768),
  531. ',':lambda i, s: s.push(65536),
  532. '-':lambda i, s: s.push(1234567890),
  533.  
  534. }
  535.  
  536. DATETIME = {
  537.  
  538. 'H':lambda s: s.push(datetime.datetime.today().hour),
  539. 'M':lambda s: s.push(datetime.datetime.today().minute),
  540. 'S':lambda s: s.push(datetime.datetime.today().second),
  541. 'd':lambda s: s.push(datetime.datetime.today().day),
  542. 'm':lambda s: s.push(datetime.datetime.today().month),
  543. 'y':lambda s: s.push(datetime.datetime.today().year),
  544. 'R':lambda s: s.push(time.time() - s.time),
  545. 'D':lambda s: s.push(datetime.datetime(s.pop(), s.pop(), s.pop())),
  546. 'N':lambda s: s.push(datetime.datetime.now()),
  547. 'W':lambda s: time.sleep(s.pop()),
  548.  
  549. }
  550.  
  551. def stack_sort(stack, command):
  552. if command.isupper():
  553. key, reverse = SORTS[command], False
  554. else:
  555. command = command.upper()
  556. key, reverse = SORTS[command], True
  557. return sorted(stack, key=key, reverse=reverse)
  558.  
  559. def cond_sort(value):
  560. return value[0] not in """tUo'!"#$"""
  561.  
  562. def sort(line):
  563. y = iter(sorted((w for w in line if cond_sort(w)), key=lambda a: a[0]))
  564. return [w if not cond_sort(w) else next(y) for w in line]
  565.  
  566. def parser(code, sort_lines):
  567. code = code.split('\n')
  568. for i in range(len(code)):
  569. group = regex.findor(CMD_REGEX, code[i], 0)
  570. for j in range(len(group)):
  571. enum = enumerate(group[j])
  572. group[j] = list(filter(lambda a: a[1] and a[0] not in NESTED, enum))
  573. group[j] = list(map(lambda a: a[1], group[j]))
  574. if sort_lines:
  575. group = sort(group)
  576. code[i] = list(map(lambda a: a[0], group))
  577. return code
  578.  
  579. def arg(arg_value):
  580. try:return -int(arg_value, 16)
  581. except:return -1
  582.  
  583. def execute_line(code, stack, inputs):
  584. for char in code:
  585.  
  586. if char == 'K':
  587. stack.sort = True
  588. elif char == 'k':
  589. stack.sort = False
  590.  
  591. elif char.startswith("'"):
  592. stack.push(char[1:-1])
  593.  
  594. elif char.startswith('o'):
  595. stack.push(char[1])
  596.  
  597. elif char.startswith('t'):
  598. stack.push(char[1:3])
  599.  
  600. elif char.startswith('U'):
  601. stack.push(*char[1:3])
  602.  
  603. elif char[0] == '?':
  604. cmd = char[1]
  605. command = BOOLEANS[cmd.upper()]
  606. if index != -1:
  607. result = command(stack, -index)
  608. else:
  609. result = command(stack, arg(char[2:]))
  610. if cmd.islower():
  611. result = not result
  612. stack.push(result)
  613.  
  614. elif char[0] == 'S':
  615. cmd = char[1]
  616. stack = Stack(stack_sort(stack, cmd))
  617.  
  618. elif char[0] == 'm':
  619. cmd = char[1]
  620. command = MAPPINGS[cmd.lower()]
  621. for i in range(len(stack)):
  622. if cmd.islower():
  623. stack[i] = command(stack[i])
  624. else:
  625. stack[i] = [list(map(command, i)) for i in stack[i]]
  626.  
  627. elif char[0] == 'F':
  628. cmd = char[1]
  629. command = FILTERS[cmd]
  630. stack = Stack(command(stack))
  631.  
  632. elif char[0] == 'v':
  633. cmd = MAPPINGS[char[1]]
  634. value = stack.pop()
  635. try:list(value)
  636. except:value = list(range(abs(int(value))))
  637. value = list(map(cmd, value))
  638. stack.push(value)
  639.  
  640. elif char[0] == 'E':
  641. ext = EXTENSIONS[char[1]]
  642. argument = arg(char[2:])
  643. ext(argument, stack)
  644.  
  645. elif char[0] == 'W':
  646. dtm = DATETIME[char[1]]
  647. dtm(stack)
  648.  
  649. elif char[0] in 'gr':
  650. if char[0] == 'r':
  651. case = re.IGNORECASE
  652. else:
  653. case = 0
  654. args, cmd = REGEX[char[1]]
  655. pattern = str(stack.pop())
  656. string = str(stack.pop())
  657. if args == 4:
  658. other = stack.pop()
  659. result = cmd(pattern, string, other, case)
  660. else:
  661. result = cmd(pattern, string, case)
  662.  
  663. stack.push(result)
  664.  
  665. else:
  666. if char[0] == 'z':
  667. stack.push(inputs)
  668. else:
  669. cmd = char[0]
  670. command = COMMANDS[cmd]
  671. if len(char) == 1:
  672. command(-1, stack)
  673. else:
  674. command(arg(char[1:]), stack)
  675.  
  676. return stack
  677.  
  678. def interpreter(code, input_file, argv, stack, flags):
  679. STDIN = list(map(eval_, map(str.strip,open(input_file, 'r').readlines())))
  680. argv = list(map(eval_, argv))
  681.  
  682. stack.init()
  683. stack.push(*STDIN)
  684. stack.push(*argv)
  685. inputs = STDIN.copy()
  686. inputs.extend(argv)
  687.  
  688. for line in parser(code, '-c' in flags):
  689.  
  690. if line[0] == '!':
  691. x = stack.peek()
  692. if isinstance(x, (int, bool, float)):
  693. x = abs(int(x))
  694. else:
  695. x = len(x)
  696. for i in range(x):
  697. stack = execute_line(line[1:], stack, inputs)
  698.  
  699. elif line[0] == '"':
  700. if stack.peek():
  701. stack = execute_line(line[1:], stack, inputs)
  702.  
  703. elif line[0] == '#':
  704. while stack.peek():
  705. stack = execute_line(line[1:], stack, inputs)
  706.  
  707. else:
  708. stack = execute_line(line, stack, inputs)
  709.  
  710. if not open('stdout.txt').read():
  711. if '-s' in flags:
  712. print_(stack)
  713. else:
  714. print_(stack.pop())
  715.  
  716. def run(program, inputs):
  717. open('stdout.txt','w').close()
  718. flags = list(filter(lambda a: str(a)[0] == '-', inputs))
  719. inputs = list(filter(lambda a: a not in flags, inputs))
  720. interpreter(program.strip(), 'stdin.txt', inputs, Stack(), flags)
  721.  
  722. if __name__ == '__main__':
  723.  
  724. prog = sys.argv[1]
  725. argv = sys.argv[2:]
  726. try:
  727. prog = open(prog).read()
  728. except:
  729. pass
  730.  
  731. run(prog, argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement