braintuck
Jump to navigation
Jump to search
| Paradigm(s) | imperative |
|---|---|
| Designed by | Xradiation |
| Appeared in | 2021 |
| Memory system | Cell-based |
| Dimensions | one-dimensional |
| Computational class | Turing complete |
| Reference implementation | See below. |
| Influenced by | Brainfuck |
| File extension(s) | .bt |
Braintuck or bt for short, is a derivative of brainfuck made by Xradiation (User:Xradiation) on 27-11-2021 with the purpose of learning. Braintuck is very similar to brainfuck, but its operations are decided based on two characters instead of one.
Workings
The interpreter goes over every character and reads it as well as its neighbor. So the code ABCD is read as AB BC CD. The two characters read by the interpreter determines what is executed. We can name these two characters A and B. Character A determines the handling, and B the handler for some of the handlings (see table 1)
| A | handling |
|---|---|
+ |
current cell += B
|
- |
current cell -= B
|
> |
cell pointer += B
|
< |
cell pointer -= B
|
[ |
Jump past the matching ] if the referenced cell is 0 |
] |
Jump back to the matching [ if the referenced cell is nonzero |
. |
Output the character signified by B
|
, |
Input a character and store it in the current cell |
| B | handler |
|---|---|
+- |
current cell |
>< |
cell pointer |
, |
Input |
[]. |
0 |
However when A is the same as B, B becomes one of the default values:
| B | Default |
|---|---|
+- |
1 |
>< |
1 |
,[] |
Not applicable (ignored) |
. |
current cell |
Instruction list
| Instruction | meaning |
|---|---|
| >+/>- | pointer+= current cell |
| >> | pointer += 1 |
| >< | pointer += pointer |
| >[ / >]/ >. | nothing |
| >, | pointer += input, inputptr +=1 |
Interpreter
Python
import sys
print('input:')
inputStr = input()
R = {
'cell': 0,
'codeptr': 0,
'cellptr': 0,
'input': 0,
'zero': 0
}
T = {
'>': 'cellptr',
'<': 'cellptr',
'+': 'cell',
'-': 'cell',
'[': 'zero',
']': 'zero',
',': 'input',
'.': 'zero'
}
def RT(A, B):
if A == B:
return 1
RTAB = R[T[B]]
if RTAB == 'empty':
RTAB = 0
return RTAB
def execute(filename):
f = open(filename, "r")
evaluate(f.read())
f.close()
def evaluate(code):
code = cleanup(list(code))
bracemap = buildbracemap(code)
cells, codeptr, cellptr, inputptr = [0], 0, 0, 0
while codeptr < len(code)-1:
try:
R['cell'], R['codeptr'], R['cellptr'] = cells[cellptr], codeptr, cellptr
if inputptr<=len(inputStr)-1:
R['input'] = ord(inputStr[inputptr])
else:
R['input'] = 'empty';
command = code[codeptr:codeptr+2]
A = command[0]
B = command[1]
RTAB = RT(A, B)
if A == ">":
cellptr += RTAB
if cellptr >= len(cells):
for i in range(cellptr-len(cells)+1):
cells.append(0)
if A == "<":
cellptr = (cellptr - RTAB) % len(cells)
elif A == "+":
cells[cellptr] = (cells[cellptr] + RTAB) % 255
elif A == "-":
cells[cellptr] = (cells[cellptr] - RTAB) % 255
elif A == "[" and cells[cellptr] == 0:
codeptr = bracemap[codeptr]
elif A == "]" and cells[cellptr] != 0:
codeptr = bracemap[codeptr]
elif A == ".":
if B == A:
#sys.stdout.write(chr(cells[cellptr]))
print(chr(cells[cellptr]), cells[cellptr])
else:
#sys.stdout.write(chr(RTAB))
print(chr(RTAB), RTAB)
elif A == ",":
if R['input'] != 'empty':
cells[cellptr] = R['input']
inputptr += 1
else:
cells[cellptr] = 0
codeptr += 1
except Exception as e:
print(code)
print(' '*codeptr+'^')
print("Error at char "+str(codeptr+1))
print()
print('Interpeter:')
raise e
def cleanup(code):
return .join(filter(lambda x: x in ['.', ',', '[', ']', '<', '>', '+', '-'], code))
def buildbracemap(code):
temp_bracestack, bracemap = [], {}
for position, command in enumerate(code):
if command == "[":
temp_bracestack.append(position)
if command == "]":
start = temp_bracestack.pop()
bracemap[start] = position
bracemap[position] = start
return bracemap
def main():
if len(sys.argv) == 2:
execute(sys.argv[1])
else:
print("Usage:", sys.argv[0], "filename")
if __name__ == "__main__":
main()
Examples
Hello, World!
++++->>[]<<+++->>[]<<++->>[]<<+++-. H ++++++++++++++++++++++++++++++. e ++++++++.. ll ++++. o -+++++++++++++++++++++++-. , -------------. space +++++++++++++--. W +++++++++++++++++++++++++. o ++++. r -------. l ---------. d -++++++++++++++++++--.. !
Truth machine
,[.+]..
Cat
,[.+,]+