+.*

From Esolang
Jump to navigation Jump to search

+.* is a Brainfuck derivative where the [ and ] commands are replaced with *, which moves execution back to the beginning of the program if the current cell's value is 0. It was created by User:3xcpy.

Overview

Instructions
Command Description
> Move the tape head to the right (like in Brainfuck)
< Move the tape head to the left (like in Brainfuck)
+ Increment the value in the current cell by 1 (like in Brainfuck)
- Decrement the value in the current cell by 1 (like in Brainfuck)
* Move execution to the beginning of the program if the current cell's value is 0
. Print the current cell's value as an ASCII character (like in Brainfuck)
, Read a byte from stdin into the current cell (like in Brainfuck)

Turing-completeness

+.* is Turing-complete, because Brainpocalypse can be compiled into it. +, <, and > can be compiled directly, and - can become *-.

Notes

  • I came up with this while making +-.%*, where I noticed that the * instruction could be used for flow control on its own, if it was conditional (kind of like Fractran). 3xcpy (talk)
  • The title was originally meant to be a little program that would loop through all the ASCII codes and print them, over and over again. It later occurred to me that the program would not work like that because the * instruction is conditional. Oh well, now I have to live with the consequences of knowing I have made such a terrible mistake. 3xcpy (talk)
  • This seems pretty fundamental and simple, so it is possible that somebody else has already made something like this. If so, please let me know on my discussion page or something. 3xcpy (talk)

Python Interpreter

If there is something wrong with this interpreter, I hope someone can point it out or fix it.

def run_plus_period_star(code: str, starting_input=''):
    pointer = 0  # for instructions
    location = 0  # for tape
    tape = [0]
    input_string = starting_input  # allows multiple inputs to happen easily

    while pointer < len(code):
        if code[pointer] == '>':
            location += 1
            if location == len(tape):
                tape.append(0)
        elif code[pointer] == '<':
            if location <= 0:
                raise ValueError('Cannot move left from position 0')
            location -= 1
        elif code[pointer] == '+':
            tape[location] = (tape[location] + 1) % 256
        elif code[pointer] == '-':
            tape[location] = (tape[location] - 1) % 256
        elif code[pointer] == '.':
            print(chr(tape[location]), end='')
        elif code[pointer] == ',':
            if input_string == '':
                input_string = input(">>")
            if len(input_string) > 0:
                tape[location] = ord(input_string[0])
                input_string = input_string[1:]
        elif code[pointer] == '*':
            if tape[location] == 0:
                pointer = -1
        pointer += 1
    return tape