Brainfuck+10

From Esolang
Jump to navigation Jump to search

Brainfuck+10 is a language created by User:Watermelyn. It is like Brainfuck but it has 10 more commands that may or may not be useful depending on how you are using them. Name is inspired by Brainfuck+3 because I didn't know what else to call it.

Commands

Command Description
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell under the pointer
- Decrement the memory cell under the pointer
. Output the character signified by the cell at the pointer
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell under the pointer is 0
] Jump back to the matching [ if the cell under the pointer is nonzero
$ Start and end a comment, no code between 2 of these will run.
e Exit the program.
r Reset the cell under the pointer to 0
# Output the value of the cell under the pointer
/ Input an integer and set the value of the cell under the pointer to that integer
A Set the cell under the pointer to 65 (Capital A).
a Set the cell under the pointer to 97 (Lowercase a).
Z Set the cell under the pointer to 90 (Capital Z).
z Set the cell under the pointer to 122 (Lowercase z).
5 Set the cell under the pointer to 53 (5).

Examples

Hello World

A+++++++.a++++.+++++++..+++.5---------------------.Z---.z-----------.+++.------.a+++.5--------------------.

Truth Machine

/[#]#

But if you would like, normal BF code would work too. Everything from BF should also work in BF+10.

Implementations

This is my implementation, created and (not very well) tested in python.

import sys

def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

def run(prg: str):
    comment = 0
    prgPos = 0
    mem = [0]
    memPos = 0
    while prgPos < len(prg):
        if prg[prgPos] == "$":
            if comment == 0:
                comment = 1
            else:
                comment = 0
        if comment == 0:
            if prg[prgPos] == ">":
                memPos += 1
                if len(mem) <= memPos:
                    mem.append(0)
            elif prg[prgPos] == "<":
                memPos -= 1
                if memPos < 0:
                    print("Moved off the tape! (memPos < 0)")
                    sys.exit(0)
            elif prg[prgPos] == "+":
                mem[memPos] += 1
                if mem[memPos] >= 255:
                    mem[memPos] = 0
            elif prg[prgPos] == "-":
                mem[memPos] -= 1
                if mem[memPos] <= -1:
                    mem[memPos] = 255
            elif prg[prgPos] == ".":
                print(chr(mem[memPos]), end="")
            elif prg[prgPos] == "#":
                print(mem[memPos], end="")
            elif prg[prgPos] == ",":
                inp = input("Input >")
                if inp == "":
                    mem[memPos] = 0
                else:
                    mem[memPos] = ord(inp[0])
            elif prg[prgPos] == "/":
                inp = input("Input >")
                if is_integer(inp):
                    if int(inp) > 255:
                        mem[memPos] = 255
                    elif int(inp) < 0:
                        mem[memPos] = 0
                    else:
                        mem[memPos] = int(inp)
            elif prg[prgPos] == "[":
                if mem[memPos] == 0:
                    countOpened = 0
                    prgPos+= 1
                    while prgPos<len(prg):
                        if prg[prgPos] == "]" and countOpened == 0:
                            break
                        elif prg[prgPos] == "[":
                            countOpened += 1
                        elif prg[prgPos] == "]":
                            countOpened -= 1
                        prgPos += 1
            elif prg[prgPos] == "]":
                if mem[memPos] != 0:
                    countClosed = 0
                    prgPos -= 1
                    while prgPos >= 0:
                        if prg[prgPos] == "[" and countClosed == 0:
                            break
                        elif prg[prgPos] == "]":
                            countClosed += 1
                        elif prg[prgPos] == "[":
                            countClosed -= 1
                        prgPos -= 1
            elif prg[prgPos] == "e":
                print(f"Exit Called\nprg[prgPos] = x\nprgPos = {prgPos}\nletter: {prgPos+1})")
                sys.exit(0)
            elif prg[prgPos] == "r":
                mem[memPos] = 0
            elif prg[prgPos] == "A":
                mem[memPos] = 65
            elif prg[prgPos] == "a":
                mem[memPos] = 97
            elif prg[prgPos] == "Z":
                mem[memPos] = 90
            elif prg[prgPos] == "z":
                mem[memPos] = 122
            elif prg[prgPos] == "5":
                mem[memPos] = 53

        prgPos += 1