Hanzifuck

From Esolang
Jump to navigation Jump to search

Hanzifuck is a esolang made by Mihai Popa. It's Brainfuck, but you only use Chinese characters (Hanzi) instead of symbols to make programs.

What to do? Just speak Chinese!

Idea by None1 and PrySigneToFry!

Table

Command Brainfuck equivalent Description
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell at the pointer
- Decrement the memory cell at 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 at the pointer is 0
] Jump back to the matching if the cell at the pointer is nonzero

Everything other than "乐","蓝","音","你","还","行","咖" and "啡" is a comment and it's ignored. But if you comment with these, it's executed as a command and it's a error.

Examples

Hello, World!

音音音音音音音音咖乐音音音音音音音音蓝你啡乐音音音音音音音音还乐音音音音音音音音咖乐音音音音音音音音音音音音蓝你啡乐音音音音音还音音音音音音音还还音音音还乐音音音音音音音音咖乐音音音音音蓝你啡乐音音音音还你你你你你你你你你你你你还蓝蓝蓝蓝音音音音音音音音音音音音音音音还乐乐还音音音还你你你你你你还你你你你你你你你还乐乐音还

Implementer

Python

Note: edited by myself. See the Numberfuck interpreter, coded by ChatGPT. Still is optimized for everything, including printing the full lyrics of the song Never Gonna Give You Up.

def interpret():
    code = input("Enter Hanzifuck code: ")
    memory = [0] * 30000
    pointer = 0
    output_string = ""
    code_length = len(code)
    code_pointer = 0

    loop_stack = []

    commands = {
        '乐': lambda: nonlocal_pointer('+'),
        '蓝': lambda: nonlocal_pointer('-'),
        '音': lambda: increment_memory(),
        '你': lambda: decrement_memory(),
        '还': lambda: output_to_string(),
        '行': lambda: take_input(),
        '咖': lambda: start_loop(),
        '啡': lambda: end_loop()
    }

    def nonlocal_pointer(op):
        nonlocal pointer
        if op == '+':
            pointer += 1
        else:
            pointer -= 1

    def increment_memory():
        nonlocal memory, pointer
        memory[pointer] = (memory[pointer] + 1) % 256

    def decrement_memory():
        nonlocal memory, pointer
        memory[pointer] = (memory[pointer] - 1) % 256

    def output_to_string():
        nonlocal memory, pointer, output_string
        output_string += chr(memory[pointer])

    def take_input():
        nonlocal memory, pointer
        user_input = input("Enter input (1 character): ")
        memory[pointer] = ord(user_input[0])

    def start_loop():
        nonlocal memory, pointer, code_pointer, loop_stack
        if memory[pointer] == 0:
            count = 1
            while count != 0 and code_pointer < code_length - 1:
                code_pointer += 1
                if code[code_pointer] == '咖':
                    count += 1
                elif code[code_pointer] == '啡':
                    count -= 1
        else:
            loop_stack.append(code_pointer)

    def end_loop():
        nonlocal code_pointer, loop_stack
        if memory[pointer] != 0:
            code_pointer = loop_stack[-1]
        else:
            loop_stack.pop()

    while code_pointer < code_length:
        command = code[code_pointer]
        if command in commands:
            commands[command]()
        code_pointer += 1

    print("Output:", output_string)

interpret()

And easier version by PrySigneToFry:

import sys
def hzfsck(code):
    s=[]
    matches={}
    tape=[0]*1000000
    for i,j in enumerate(code):
        if j=='咖':
            s.append(i)
        if j=='啡':
            m=s.pop()
            matches[m]=i
            matches[i]=m
    cp=0
    p=0
    while cp<len(code):
        if code[cp]=='音':
            tape[p]=(tape[p]+1)%256
        if code[cp]=='你':
            tape[p]=(tape[p]-1)%256
        if code[cp]=='还':
            c=sys.stdin.read(1)
            tape[p]=(ord(c) if c else 0)%256
        if code[cp]=='行':
            print(chr(tape[p]),end='')
        if code[cp]=='乐':
            p+=1
        if code[cp]=='蓝':
            p-=1
        if code[cp]=='咖':
            if not tape[p]:
                cp=matches[cp]
        if code[cp]=='啡':
            if tape[p]:
                cp=matches[cp]
        cp+=1
hzfsck(sys.stdin.read())

Computational class

Hanzifuck is turing-complete because Brainfuck, Mariofuck and Numberfuck are also turing-complete!

See also

Categories