Old page wikitext, before the edit (old_wikitext) | ''''Pyline''' is an esoteric subset of python, where the whole source file has to be a single expression. Python 3.10 or newer is recommended due to the addition of the walrus operator.
==Examples==
===Hello World===
print("hello world")
===Quine===
(lambda s:print(f'{s}({chr(34)}{s}{chr(34)})'))("(lambda s:print(f'{s}({chr(34)}{s}{chr(34)})'))")
===Truth machine===
(
count:=__import__("itertools").count,
i:=input(":"),
[
print('1',end="") for _ in count()
] if i == '1'
else (
print('0')
)
)
The walrus operator is only used here for readability; the entire expression can be given without it:
[print("1", end="") for _ in __import__("itertools").count()] if input(":") == "1" else print("0")
A version that doesn't use __import__
[print(1,end="")for _ in iter(lambda:0,1)]if input(":")=="1"else print(0)
===Rule 110 ([[Turing-complete]]ness proof)===
(
count:=__import__("itertools").count,
grb:=__import__("random").getrandbits,
SIZE:=150,
board:=[[bool(grb(1)) for _ in range(SIZE)],],
[
(
old_board:=board[0].copy(),
print("".join('.#'[i] for i in board[0])),
board.__setitem__(0,[
(
l:=old_board[(i-1)%SIZE],
r:=old_board[(i+1)%SIZE],
(
bool(110 & 2**(4*l+2*t+r))
)
)[-1]
for i,t in enumerate(board[0])
])
)
for _ in count()
]
)
===[[Brainfuck]] interpreter===
(
brainfuck_inpreter := lambda code: (
imports := __import__,
whiler := (type("whiler", (object,), {
"__init__": lambda s,f: setattr(s,"f",f) if callable(f) else \
(_ for _ in ()).throw(TypeError(f"{f.__class__.__name__} object is not callable")),
"__iter__": lambda s: s,
"__next__": lambda s: None if s.f() else next(iter(())),
"__repr__": lambda s: f"<whiler object with {s.f}>"
})),
code_index := [0],
tape := [0],
tape_index := [0],
input_store := [""],
[(
{
"+":lambda: tape.insert(tape_index[0], (tape.pop(tape_index[0]) + 1) % 256),
"-":lambda: tape.insert(tape_index[0], (tape.pop(tape_index[0]) - 1) % 256),
">":lambda: (
tape_index.append(tape_index.pop() + 1),
None if tape_index[0] < len(tape) else tape.append(0)
),
"<":lambda: tape_index.append(tape_index.pop() - 1) \
if tape_index[0] > 0 else tape.insert(0, 0),
".":lambda: print(chr(tape[tape_index[0]]), end="", flush=True),
",":(lambda: (
tape.pop(tape_index),
tape.insert(tape_index, int.from_bytes(imports("msvcrt").getche()))
)) if "-getch" in imports("sys").argv else (lambda: (
(
input_store.pop(),
input_store.append(input())
) if input_store == [""] else None,
tape.pop(tape_index[0]),
tape.insert(tape_index[0], ord(input_store[0][0]) % 256),
input_store.append(input_store.pop()[1:]),
)),
"[":lambda: (
sml_index := code_index[0],
brackets := 1,
[(
sml_index := sml_index + 1,
brackets := brackets + {"[":1,"]":-1}.get(code[sml_index],0)
) for __ in whiler(lambda: brackets != 0)],
code_index.pop(),
code_index.append(sml_index)
) if not tape[tape_index[0]] else None,
"]":lambda: (
sml_index := code_index[0],
brackets := -1,
[(
sml_index := sml_index - 1,
brackets := brackets + {"[":1,"]":-1}.get(code[sml_index],0)
) for __ in whiler(lambda: brackets != 0)],
code_index.pop(),
code_index.append(sml_index)
) if tape[tape_index[0]] else None
}.get(code[code_index[0]], lambda: None)(),
code_index.append(code_index.pop()+1),
) for _ in whiler(lambda: len(code) > code_index[0])]
),
None
)
Another written by [[User:DGCK81LNN]]; takes both the program and the input from standard input, with the start of the input indicated by a <code>!</code>.
<pre>
(lambda cod, _, inp:
(lambda *, _cp = 0, _mp = 0, _ip = 0, _mem = {}, _inp = inp .encode(), _out = b"": next(
__import__("sys").stdout.buffer.write(_out) and None or None
for _ in __import__("itertools").count()
if not (
_mem.__setitem__(
_mp,
((_mem[_mp] if _mp in _mem else 0) + (1 if cod[_cp] == "+" else -1 if cod[_cp] == "-" else 0)) & 255
) and 0 or
(_mp := _mp + (1 if cod[_cp] == ">" else -1 if cod[ _cp] == "<" else 0)) and 0 or
(
(_cp := (lambda si, *, _d = 0:
next(i
for i in (range(si, len(cod)) if cod[si] == "[" else range(si, -1, -1))
if cod[i] in "[]" and ((_d := _d + (1 if cod[i] == "[" else -1)) == 0)
)
)(_cp))
if cod[_cp] in "[]" and bool(_mem[_mp] if _mp in _mem else 0) == bool(cod[_cp] == "]")
else 0
) and 0 or
(
(_out := _out + bytes((_mem[_mp],))) if cod[_cp] == "."
else _mem.__setitem__(_mp, _inp[_ip]) and 0 or (_ip := _ip + 1) if cod[_cp] == "," and _ip < len(_inp)
else 0
) and 0 or
(_cp := _cp + 1) < len(cod)
)
))()
)(*__import__("sys").stdin.read().partition("!"))
</pre>
Another written by [[User: Jan jelo]]
<pre>
(
program:=list(input())+['@'],
ltape:=[0],rtape:=[0],
pc:=0,i:=0,state:=0,
INC:=lambda:ltape.append((ltape.pop()+1)%256),
DEC:=lambda:ltape.append((ltape.pop()-1)%256),
IN:=lambda:ltape.append(ord(input())%256),
LS:=lambda:(a:=ltape[-1],
ltape.pop()
if len(ltape)>1 else ...,
rtape.append(a)
),
RS:=lambda:(a:=rtape[-1],
rtape.pop()
if len(rtape)>1 else ...,
ltape.append(a)
),
[(
(
(LS(),
state:=1)
if program[pc]=='<' else
(RS(),
state:=1)
if program[pc]=='>' else
(INC(),state:=1)
if program[pc]=='+' else
(DEC(),state:=1)
if program[pc]=='-' else
(IN(),state:=1)
if program[pc]==',' else
(print(chr(ltape[-1]),end=''),state:=1)
if program[pc]=='.' else
(
(i:=0,state:=2)
if ltape[-1]==0 else
(state:=1)
)
if program[pc]=='[' else
(
(i:=0,state:=3)
if ltape[-1]!=0 else
(state:=1)
)
if program[pc]==']' else
(state:=4)
if program[pc]=='@' else...
)
if state==0 else
(
state:=0,
pc:=pc+1
)
if state==1 else
(
(
i:=i-1,
(state:=0) if i==0 else (pc:=pc+1)
)
if program[pc]=='[' else
(
i:=i+1,
(state:=0) if i==0 else (pc:=pc+1)
)
if program[pc]==']' else (pc:=pc+1)
)
if state==2 else
(
(
i:=i-1,
(state:=0) if i==0 else (pc:=pc-1)
)
if program[pc]=='[' else
(
i:=i+1,
(state:=0) if i==0 else (pc:=pc-1)
)
if program[pc]==']' else (pc:=pc-1)
)
if state==3 else ...
)
for _ in iter(lambda:state!=4,0)]
)
</pre>
[[Category:Esoteric subset]]
[[Category:Languages]]
[[Category:High-level]]
[[Category:Implemented]]
[[Category:Turing complete]]
[[Category:2022]]' |
New page wikitext, after the edit (new_wikitext) | ''''Pyline''' is an esoteric subset of python, where the whole source file has to be a single expression. Python 3.10 or newer is recommended due to the addition of the walrus operator.
==Examples==
===Hello World===
print("hello world")
===Quine===
(lambda s:print(f'{s}({chr(34)}{s}{chr(34)})'))("(lambda s:print(f'{s}({chr(34)}{s}{chr(34)})'))")
===Truth machine===
(
count:=__import__("itertools").count,
i:=input(":"),
[
print('1',end="") for _ in count()
] if i == '1'
else (
print('0')
)
)
The walrus operator is only used here for readability; the entire expression can be given without it:
[print("1", end="") for _ in __import__("itertools").count()] if input(":") == "1" else print("0")
A version that doesn't use __import__
[print(1,end="")for _ in iter(lambda:0,1)]if input(":")=="1"else print(0)
===Rule 110 ([[Turing-complete]]ness proof)===
(
count:=__import__("itertools").count,
grb:=__import__("random").getrandbits,
SIZE:=150,
board:=[[bool(grb(1)) for _ in range(SIZE)],],
[
(
old_board:=board[0].copy(),
print("".join('.#'[i] for i in board[0])),
board.__setitem__(0,[
(
l:=old_board[(i-1)%SIZE],
r:=old_board[(i+1)%SIZE],
(
bool(110 & 2**(4*l+2*t+r))
)
)[-1]
for i,t in enumerate(board[0])
])
)
for _ in count()
]
)
===[[Brainfuck]] interpreter===
(
brainfuck_inpreter := lambda code: (
imports := __import__,
whiler := (type("whiler", (object,), {
"__init__": lambda s,f: setattr(s,"f",f) if callable(f) else \
(_ for _ in ()).throw(TypeError(f"{f.__class__.__name__} object is not callable")),
"__iter__": lambda s: s,
"__next__": lambda s: None if s.f() else next(iter(())),
"__repr__": lambda s: f"<whiler object with {s.f}>"
})),
code_index := [0],
tape := [0],
tape_index := [0],
input_store := [""],
[(
{
"+":lambda: tape.insert(tape_index[0], (tape.pop(tape_index[0]) + 1) % 256),
"-":lambda: tape.insert(tape_index[0], (tape.pop(tape_index[0]) - 1) % 256),
">":lambda: (
tape_index.append(tape_index.pop() + 1),
None if tape_index[0] < len(tape) else tape.append(0)
),
"<":lambda: tape_index.append(tape_index.pop() - 1) \
if tape_index[0] > 0 else tape.insert(0, 0),
".":lambda: print(chr(tape[tape_index[0]]), end="", flush=True),
",":(lambda: (
tape.pop(tape_index),
tape.insert(tape_index, int.from_bytes(imports("msvcrt").getche()))
)) if "-getch" in imports("sys").argv else (lambda: (
(
input_store.pop(),
input_store.append(input())
) if input_store == [""] else None,
tape.pop(tape_index[0]),
tape.insert(tape_index[0], ord(input_store[0][0]) % 256),
input_store.append(input_store.pop()[1:]),
)),
"[":lambda: (
sml_index := code_index[0],
brackets := 1,
[(
sml_index := sml_index + 1,
brackets := brackets + {"[":1,"]":-1}.get(code[sml_index],0)
) for __ in whiler(lambda: brackets != 0)],
code_index.pop(),
code_index.append(sml_index)
) if not tape[tape_index[0]] else None,
"]":lambda: (
sml_index := code_index[0],
brackets := -1,
[(
sml_index := sml_index - 1,
brackets := brackets + {"[":1,"]":-1}.get(code[sml_index],0)
) for __ in whiler(lambda: brackets != 0)],
code_index.pop(),
code_index.append(sml_index)
) if tape[tape_index[0]] else None
}.get(code[code_index[0]], lambda: None)(),
code_index.append(code_index.pop()+1),
) for _ in whiler(lambda: len(code) > code_index[0])]
),
None
)
Another written by [[User:DGCK81LNN|DGCK8LNN]]; takes both the program and the input from standard input, with the start of the input indicated by a <code>!</code>.
<pre>
(lambda cod, _, inp:
(lambda *, _cp = 0, _mp = 0, _ip = 0, _mem = {}, _inp = inp .encode(), _out = b"": next(
__import__("sys").stdout.buffer.write(_out) and None or None
for _ in __import__("itertools").count()
if not (
_mem.__setitem__(
_mp,
((_mem[_mp] if _mp in _mem else 0) + (1 if cod[_cp] == "+" else -1 if cod[_cp] == "-" else 0)) & 255
) and 0 or
(_mp := _mp + (1 if cod[_cp] == ">" else -1 if cod[ _cp] == "<" else 0)) and 0 or
(
(_cp := (lambda si, *, _d = 0:
next(i
for i in (range(si, len(cod)) if cod[si] == "[" else range(si, -1, -1))
if cod[i] in "[]" and ((_d := _d + (1 if cod[i] == "[" else -1)) == 0)
)
)(_cp))
if cod[_cp] in "[]" and bool(_mem[_mp] if _mp in _mem else 0) == bool(cod[_cp] == "]")
else 0
) and 0 or
(
(_out := _out + bytes((_mem[_mp],))) if cod[_cp] == "."
else _mem.__setitem__(_mp, _inp[_ip]) and 0 or (_ip := _ip + 1) if cod[_cp] == "," and _ip < len(_inp)
else 0
) and 0 or
(_cp := _cp + 1) < len(cod)
)
))()
)(*__import__("sys").stdin.read().partition("!"))
</pre>
Another written by [[User: Jan jelo|Jan jelo]]
<pre>
(
program:=list(input())+['@'],
ltape:=[0],rtape:=[0],
pc:=0,i:=0,state:=0,
INC:=lambda:ltape.append((ltape.pop()+1)%256),
DEC:=lambda:ltape.append((ltape.pop()-1)%256),
IN:=lambda:ltape.append(ord(input())%256),
LS:=lambda:(a:=ltape[-1],
ltape.pop()
if len(ltape)>1 else ...,
rtape.append(a)
),
RS:=lambda:(a:=rtape[-1],
rtape.pop()
if len(rtape)>1 else ...,
ltape.append(a)
),
[(
(
(LS(),
state:=1)
if program[pc]=='<' else
(RS(),
state:=1)
if program[pc]=='>' else
(INC(),state:=1)
if program[pc]=='+' else
(DEC(),state:=1)
if program[pc]=='-' else
(IN(),state:=1)
if program[pc]==',' else
(print(chr(ltape[-1]),end=''),state:=1)
if program[pc]=='.' else
(
(i:=0,state:=2)
if ltape[-1]==0 else
(state:=1)
)
if program[pc]=='[' else
(
(i:=0,state:=3)
if ltape[-1]!=0 else
(state:=1)
)
if program[pc]==']' else
(state:=4)
if program[pc]=='@' else...
)
if state==0 else
(
state:=0,
pc:=pc+1
)
if state==1 else
(
(
i:=i-1,
(state:=0) if i==0 else (pc:=pc+1)
)
if program[pc]=='[' else
(
i:=i+1,
(state:=0) if i==0 else (pc:=pc+1)
)
if program[pc]==']' else (pc:=pc+1)
)
if state==2 else
(
(
i:=i-1,
(state:=0) if i==0 else (pc:=pc-1)
)
if program[pc]=='[' else
(
i:=i+1,
(state:=0) if i==0 else (pc:=pc-1)
)
if program[pc]==']' else (pc:=pc-1)
)
if state==3 else ...
)
for _ in iter(lambda:state!=4,0)]
)
</pre>
[[Category:Esoteric subset]]
[[Category:Languages]]
[[Category:High-level]]
[[Category:Implemented]]
[[Category:Turing complete]]
[[Category:2022]]' |