Exemplo n.º 1
0
def equal_fn(srs):
    a, b = srs.pop(), srs.pop()
    if isinstance(a, collections.Iterable) and isinstance(
            b, collections.Iterable):
        srs.push(int(as_list(a) == as_list(b)))
    else:
        srs.push(int(a == b))
Exemplo n.º 2
0
 def test_utils(self):
     self.assertEqual(as_list(range(5)), [0, 1, 2, 3, 4])
     self.assertEqual(as_list((1,2,3,4) for x in range(3)), [[1, 2, 3, 4]]*3)
     self.assertEqual(as_list(2), [2])
     self.assertEqual([chr_cp437(x) for x in range(256)], [x for x in CP437.table])
     self.assertEqual([ord_cp437(x) for x in CP437.table], [x for x in range(256)])
     with self.assertRaises(ValueError):
         chr_cp437(257)
     self.assertEqual(CP437.from_Unicode(chr_cp437(0x8D)+'\u2266'), [0x8D, 0xE2, 0x89, 0xA6])
Exemplo n.º 3
0
 def eval(self, code):
     if self.debug_mode:
         print(code)
     i = 0
     if all(x not in remove_lists_and_strings(code)
            for x in (',', chr_cp437(0xCA), chr_cp437(0x09),
                      chr_cp437(0x15))):
         for line in sys.stdin.read().splitlines():
             self.push(literal_eval(line))
             self.inputs.append(literal_eval(line))
     self.code = code
     while i < len(code):
         old_stack = self.stack.copy()
         try:
             c = code[i]
             if c == '"':
                 s = ""
                 i += 1
                 while i < len(code) and code[i] != '"':
                     s += code[i]
                     i += 1
                 self.push(s)
             elif c == "'":
                 i += 1
                 self.push(code[i])
             elif c == ':':
                 v = ""
                 i += 1
                 while i < len(code) and code[i] in '0123456789.ij+-':
                     v += code[i]
                     i += 1
                 i -= 1
                 val = 0
                 v = v.replace('i', 'j')
                 if self.debug_mode:
                     print(v)
                 try:
                     val = literal_eval(v)
                     if self.debug_mode:
                         print(val)
                 except:
                     while not val and v:
                         v = v[:-1]
                         i -= 1
                         try:
                             val = literal_eval(v)
                             break
                         except:
                             continue
                     if self.debug_mode:
                         print("Failed to eval numeric")
                 val = val if anytype(val, int, float, complex) else 0
                 self.push(val)
             elif c == 'W':
                 inner = ''
                 i += 1
                 while i < len(code) and code[i] != 'W':
                     inner += code[i]
                     i += 1
                 if self.debug_mode:
                     print("while loop code: {}".format(inner))
                 while self.peek():
                     self.eval(inner)
             elif c == '[':
                 l = ''
                 i += 1
                 nest = 1
                 while i < len(code):
                     if code[i] == '[':
                         nest += 1
                     elif code[i] == ']':
                         nest -= 1
                         if nest == 0:
                             break
                     l += code[i]
                     i += 1
                 self.push(literal_eval('[{}]'.format(l)))
                 if self.debug_mode:
                     print("list: [{}]".format(l))
                     print(self.stack)
             elif c == '⌠':
                 fn = ''
                 i += 1
                 nest = 1
                 while i < len(code):
                     if code[i] == '⌠':
                         nest += 1
                     elif code[i] == '⌡':
                         nest -= 1
                         if nest == 0:
                             break
                     fn += code[i]
                     i += 1
                 self.push(SeriouslyCommands.SeriousFunction(fn))
                 if self.debug_mode:
                     print("fn: {}".format(fn))
                     print(self.stack)
             elif c == '`':
                 i += 1
                 self.push(SeriouslyCommands.SeriousFunction(code[i]))
             elif ord(c) in range(48, 58):
                 self.push(int(c))
             elif ord_cp437(c) == 0x0B:
                 i += 1
                 self.push(SeriouslyCommands.SeriousFunction(code[i]))
                 self.fn_table.get(ord_cp437('M'))(self)
             elif ord_cp437(c) == 0x0C:
                 i += 1
                 a, b = self.pop(), self.pop()
                 if not isinstance(a, collections.Iterable):
                     a = [
                         a for _ in
                         (b if isinstance(b, collections.Iterable) else [1])
                     ]
                 if not isinstance(b, collections.Iterable):
                     b = [b for _ in a]
                 self.push(b)
                 self.push(a)
                 self.fn_table.get(ord_cp437('Z'))(self)
                 self.push(SeriouslyCommands.SeriousFunction('i' + code[i]))
                 self.fn_table.get(ord_cp437('M'))(self)
             elif ord_cp437(c) == 0x14:
                 i += 1
                 cmd = code[i]
                 a = self.pop()
                 for _ in range(a):
                     self.eval(cmd)
             elif ord_cp437(c) == 0x0E:
                 cmd1, cmd2 = code[i + 1], code[i + 2]
                 temp_stack = self.stack.copy()
                 self.eval(cmd1)
                 res1 = self.stack.copy()
                 self.stack = temp_stack
                 self.eval(cmd2)
                 res2 = self.stack.copy()
                 self.stack = deque()
                 self.push([
                     res1 if len(res1) > 1 else res1[0],
                     res2 if len(res2) > 1 else res2[0]
                 ])
                 i += 2
             else:
                 if self.debug_mode:
                     print("{:2X}".format(ord_cp437(c)))
                 self.fn_table.get(ord_cp437(c), lambda x: x)(self)
                 if self.debug_mode:
                     print(self.stack)
         except SystemExit:
             exit()
         except KeyboardInterrupt:  # pragma: no cover
             exit()
         except:
             if self.debug_mode:
                 traceback.print_exc()
             self.stack = old_stack
         finally:
             i += 1
     return as_list(self.stack)[::-1]
Exemplo n.º 4
0
def set_reg(i, val):
    global registers
    if isinstance(val, collections.Iterable) and not isinstance(val, str):
        val = as_list(val)
    registers[i] = val
Exemplo n.º 5
0
def copy(a):
    if isinstance(a, collections.Iterable) and not isinstance(a, str):
        a = as_list(a)
    return a, _copy(a)
Exemplo n.º 6
0
def to_list_fn(srs):
    srs.stack = deque([as_list(srs.stack)])