Pyline

From Esolang
Jump to navigation Jump to search

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")

Truth machine

(
	count:=__import__("itertools").count,
	i:=input(":"),
	[
		print('1',end="") for _ in count()
	] if i == '1'
	else (
		print('0')
	)
)

Rule 110 (Turing-completeness 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 !.

(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("!"))