binary safety
This commit is contained in:
+38
-10
@@ -19,13 +19,18 @@ replpath = _fpath(f'{scriptdir}/clyx_repl.clx', f'{scriptdir}/../../clyx_repl.cl
|
||||
|
||||
class Sock:
|
||||
def __init__(self, s): self.s = s
|
||||
def write(self, d): return self.s.write(d) if hasattr(self.s, 'write') else self.send(d.encode('utf-8') if isinstance(d, str) else d)
|
||||
def send(self, d): return self.s.send(d) if hasattr(self.s, 'send') else getattr(self.s, 'write', lambda *a: 0)(d)
|
||||
def write(self, d):
|
||||
data = d.encode('utf-8') if isinstance(d, str) else d
|
||||
return self.s.write(data) if hasattr(self.s, 'write') else self.send(data)
|
||||
def send(self, d):
|
||||
data = d.encode('utf-8') if isinstance(d, str) else d
|
||||
return self.s.send(data) if hasattr(self.s, 'send') else getattr(self.s, 'write', lambda *a: 0)(data)
|
||||
def recv(self, n):
|
||||
if hasattr(self.s, 'recv'): return self.s.recv(n)
|
||||
b = bytearray(n); return bytes(b[:self.s.recv_into(b)]) if hasattr(self.s, 'recv_into') else getattr(self.s, 'read', lambda *a: b'')(n)
|
||||
def readline(self):
|
||||
if hasattr(self.s, 'readline'): return self.s.readline()
|
||||
if hasattr(self.s, 'readline'):
|
||||
r = self.s.readline()
|
||||
return r.decode('utf-8') if isinstance(r, bytes) else r
|
||||
res = b''
|
||||
while not res.endswith(b'\n'):
|
||||
c = self.recv(1)
|
||||
@@ -54,24 +59,47 @@ class Clyx:
|
||||
'divmod': lambda: (lambda y: (lambda x: self._stack.extend(divmod(x, y)))(self._stack.pop()))(self._stack.pop()), 'shl': lambda: (lambda f, x: self._stack.append(x << f if f >= 0 else x >> -f))(self._stack.pop(), self._stack.pop()), '^': lambda: (lambda y: self._stack.append(self._stack.pop() ** y))(self._stack.pop()),
|
||||
'0=': lambda: self._stack.append(1 if self._stack.pop() == 0 else 0), '0<': lambda: self._stack.append(1 if self._stack.pop() < 0 else 0), 'nand': lambda: self._stack.append(~(self._stack.pop() & self._stack.pop())), 'defw': self._cmd_defw, 'type': lambda: self._stack.append(2 if isinstance(self._stack[-1], list) else (1 if isinstance(self._stack[-1], str) else 0)),
|
||||
'next': lambda: self._stack.append(self._pop_caller_token()), '.': lambda: self._out_num(self._stack.pop()), 'emit': lambda: self._out_char(self._stack.pop()),
|
||||
'fopen': lambda: (lambda m, f: self._stack.append(type('FD0', (), {'readline': lambda s: self._in_str() + '\n', 'read': lambda s: self._in_str(), 'fileno': lambda s: 0})() if f in (0, '0') else open(f, m)))(self._stack.pop(), self._stack.pop()),
|
||||
'nopen': lambda: (lambda p, h: (lambda s: (lambda addr: (s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if hasattr(s, 'setsockopt') else None, s.bind(addr) if h == '0.0.0.0' else s.connect(addr), s.listen(1) if h == '0.0.0.0' else None, self._stack.append(s if h == '0.0.0.0' else Sock(getattr(s, 'makefile', lambda *a: s)('rw'))))[3])(socket.getaddrinfo(h, int(p))[0][-1] if hasattr(socket, 'getaddrinfo') else (h, int(p))))(socket.socket()))(self._stack.pop(), self._stack.pop()),
|
||||
'write': lambda: (lambda d, fd: (self._stack.append(fd.write(d)), self._flush(fd)))(self._stack.pop(), self._stack.pop()),
|
||||
'read': lambda: (lambda fd: self._stack.append((lambda c: Sock(getattr(c, 'makefile', lambda *a: c)('rw')))(fd.accept()[0]) if hasattr(fd, 'accept') else (lambda r: r.rstrip(b'\r\n' if isinstance(r, bytes) else '\r\n'))(fd.readline() if (fd is sys.stdin or fd.__class__.__name__ in ('FD0', 'Sock')) else fd.read())))(self._stack.pop()),
|
||||
'fopen': lambda: (lambda m, f: self._stack.append(type('FD0', (), {'readline': lambda s: self._in_str() + '\n', 'read': lambda s: self._in_str(), 'fileno': lambda s: 0})() if f in (0, '0') else open(f, m if 'b' in m else m + 'b')))(self._stack.pop(), self._stack.pop()),
|
||||
'nopen': lambda: (lambda p, h: (lambda s: (lambda addr: (s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if hasattr(s, 'setsockopt') else None, s.bind(addr) if h == '0.0.0.0' else s.connect(addr), s.listen(1) if h == '0.0.0.0' else None, self._stack.append(s if h == '0.0.0.0' else Sock(getattr(s, 'makefile', lambda *a: s)('rwb'))))[3])(socket.getaddrinfo(h, int(p))[0][-1] if hasattr(socket, 'getaddrinfo') else (h, int(p))))(socket.socket()))(self._stack.pop(), self._stack.pop()),
|
||||
'write': self._cmd_write,
|
||||
'read': self._cmd_read,
|
||||
'close': lambda: (lambda fd: fd.close() if not (fd in (sys.stdin, sys.stdout, sys.stderr) or fd.__class__.__name__ == 'FD0') else None)(self._stack.pop()),
|
||||
'delf': lambda: os.remove(self._stack.pop()), 'maked': lambda: os.mkdir(self._stack.pop()), 'listd': lambda: self._stack.append(os.listdir(self._stack.pop())), 'deld': lambda: os.rmdir(self._stack.pop()),
|
||||
'rand': lambda: self._stack.append(randrange(int(self._stack.pop()))), '.s': lambda: self._out_num(repr(self._stack)), 'dip': lambda: (lambda q, y: (self._exec_quot(q), self._stack.append(y)))(self._stack.pop(), self._stack.pop()),
|
||||
'loop': lambda: (lambda body, cond: self._exec_loop(cond, body))(self._stack.pop(), self._stack.pop()),
|
||||
'hsargs': lambda: self._stack.append(list(sys.argv)),
|
||||
'hsexit': lambda: sys.exit(int(self._stack.pop())),
|
||||
'l2s': lambda: self._stack.append("".join(chr(int(x)) for x in self._stack.pop())),
|
||||
'hseval': lambda: self._stack.append(eval(self._stack.pop())),
|
||||
'chr': lambda: self._stack.append(chr(int(self._stack.pop()))), 'asc': lambda: self._stack.append(ord(self._stack.pop())),
|
||||
'chr': lambda: self._stack.append(chr(int(self._stack.pop()))),
|
||||
'sin': lambda: self._stack.append(math.sin(float(self._stack.pop()))), 'cos': lambda: self._stack.append(math.cos(float(self._stack.pop()))), 'atan': lambda: self._stack.append(math.atan(float(self._stack.pop()))),
|
||||
'exp': lambda: self._stack.append(math.exp(float(self._stack.pop()))), 'log': lambda: self._stack.append(math.log(float(self._stack.pop())))
|
||||
}
|
||||
self.run('"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf i ]')
|
||||
self.run('"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf l2s i ]')
|
||||
self.run(f'"{libpath}" src' if libfname is None else (libfname + ' src'))
|
||||
|
||||
def _cmd_write(self):
|
||||
d, fd = self._stack.pop(), self._stack.pop()
|
||||
data = bytes(int(x) for x in d) if isinstance(d, list) else (d.encode('utf-8') if isinstance(d, str) else d)
|
||||
if hasattr(fd, 'write'):
|
||||
n = fd.write(data)
|
||||
self._flush(fd)
|
||||
self._stack.append(n)
|
||||
elif hasattr(fd, 'send'):
|
||||
n = fd.send(data)
|
||||
self._stack.append(n)
|
||||
else:
|
||||
self._stack.append(0)
|
||||
def _cmd_read(self):
|
||||
fd = self._stack.pop()
|
||||
if hasattr(fd, 'accept'):
|
||||
c, addr = fd.accept()
|
||||
self._stack.append(Sock(getattr(c, 'makefile', lambda *a: c)('rwb')))
|
||||
else:
|
||||
r = fd.readline() if (fd is sys.stdin or fd.__class__.__name__ in ('FD0', 'Sock')) else fd.read()
|
||||
r = r.rstrip(b'\r\n' if isinstance(r, bytes) else '\r\n')
|
||||
res = list(r) if isinstance(r, (bytes, bytearray)) else [ord(c) for c in r]
|
||||
self._stack.append(res)
|
||||
def _cmd_qset(self): i, v, x = self._stack.pop(), self._stack.pop(), self._stack.pop(); (v.pop(int(i)), v.insert(int(i), x)) if isinstance(v, list) else None; self._stack.append(v if isinstance(v, list) else None)
|
||||
def _exec_loop(self, c, b):
|
||||
while (self._exec_quot(c), self._stack.pop())[1]: self._exec_quot(b)
|
||||
|
||||
Reference in New Issue
Block a user