We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

樺木

From Esolang
Jump to navigation Jump to search
樺木
Paradigm(s) Imperative, tree-based
Designed by Gas
Appeared in 2026
Computational class Unknown
Reference implementation Unimplemented

樺木 (Japanese kabaki, Chinese huàmù, "birch wood") is an esoteric programming language whose only data structure is a growing tree with a single cursor. It is named after the birch, a tree that sheds its bark in layers: the program can only grow new branches or peel old ones away.

Overview

Memory is a rooted ordered tree. Every node holds one integer (initially 0, unbounded in both directions). The tree starts as a single root node, and the cursor starts on the root. A program is a string of instructions, and all other characters are comments.

Think of Brainfuck's tape bent into branches. Going "right" means going deeper, and there is also a sideways dimension that Brainfuck lacks.

Commands

Command Meaning
芽 (bud) Move to the first child of the current node. If it has no children, create one with value 0 first.
根 (root) Move to the parent. Does nothing at the root.
右 Move to the next sibling, creating it with value 0 if the current node is the last child. Does nothing at the root.
左 Move to the previous sibling. Does nothing on the first child or at the root.
+ Increment the current node.
- Decrement the current node.
剥 (peel) Delete the current node together with its whole subtree and move to its parent. At the root, set the root to 0 and delete all its children.
出 Output the current value as a Unicode character.
入 Read one character into the current node (0 on EOF).
「 If the current node is 0, jump past the matching 」.
」 If the current node is nonzero, jump back to the matching 「.

Computational class

樺木 is Turing complete by reduction from Brainfuck, using the chain of first children as the tape:

Brainfuck 樺木
> 芽
< 根
+ - . , + - 出 入
[ ] 「 」

The tape is unbounded to the right, and cells are unbounded integers. The sibling commands (右, 左) and 剥 are not needed for completeness. They give the language a second dimension and a built-in way to clear memory.

Examples

Cat

入「出入」

Print "A"

++++++++「芽++++++++根-」芽+出

The loop stores 8×8 = 64 in a child node, then + makes it 65.

Clear a branch

芽+++右++++根

This creates two sibling children holding 3 and 4. Adding 剥 before the final 根 would delete the second child and return to the root.

Implementation

Reference interpreter in Python:

class N:
    def __init__(s, p): s.v = 0; s.k = []; s.p = p

def run(src, inp=""):
    jump, st = {}, []
    for i, c in enumerate(src):
        if c == "「": st.append(i)
        elif c == "」": j = st.pop(); jump[i] = j; jump[j] = i
    cur = N(None); pc = 0; out = []; inp = list(inp)
    while pc < len(src):
        c = src[pc]
        if c == "芽":
            if not cur.k: cur.k.append(N(cur))
            cur = cur.k[0]
        elif c == "根": cur = cur.p or cur
        elif c in "右左" and cur.p:
            s = cur.p.k; i = s.index(cur)
            if c == "右":
                if i + 1 == len(s): s.append(N(cur.p))
                cur = s[i + 1]
            elif i > 0: cur = s[i - 1]
        elif c == "+": cur.v += 1
        elif c == "-": cur.v -= 1
        elif c == "剥":
            if cur.p: p = cur.p; p.k.remove(cur); cur = p
            else: cur.v = 0; cur.k = []
        elif c == "出": out.append(chr(cur.v))
        elif c == "入": cur.v = ord(inp.pop(0)) if inp else 0
        elif c == "「" and cur.v == 0: pc = jump[pc]
        elif c == "」" and cur.v != 0: pc = jump[pc]
        pc += 1
    return "".join(out)

print(run("++++++++「芽++++++++根-」芽+出"))  # A

C++ interpreter

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

using u32 = char32_t;

struct Node {
    long long v = 0;
    Node* parent;
    std::vector<std::unique_ptr<Node>> kids;
    explicit Node(Node* p) : parent(p) {}
};

static std::vector<u32> decode(const std::string& s) {
    std::vector<u32> out;
    size_t i = 0;
    while (i < s.size()) {
        unsigned char c = s[i];
        u32 cp; int n;
        if (c < 0x80)       { cp = c;        n = 0; }
        else if (c >= 0xF0) { cp = c & 0x07; n = 3; }
        else if (c >= 0xE0) { cp = c & 0x0F; n = 2; }
        else if (c >= 0xC0) { cp = c & 0x1F; n = 1; }
        else { i++; continue; }
        i++;
        while (n-- > 0 && i < s.size() && (s[i] & 0xC0) == 0x80)
            cp = (cp << 6) | (s[i++] & 0x3F);
        out.push_back(cp);
    }
    return out;
}

static void encode(long long v, std::string& out) {
    if (v < 0 || v > 0x10FFFF || (v >= 0xD800 && v <= 0xDFFF)) return;
    u32 c = (u32)v;
    if (c < 0x80) out += (char)c;
    else if (c < 0x800) {
        out += (char)(0xC0 | (c >> 6));
        out += (char)(0x80 | (c & 0x3F));
    } else if (c < 0x10000) {
        out += (char)(0xE0 | (c >> 12));
        out += (char)(0x80 | ((c >> 6) & 0x3F));
        out += (char)(0x80 | (c & 0x3F));
    } else {
        out += (char)(0xF0 | (c >> 18));
        out += (char)(0x80 | ((c >> 12) & 0x3F));
        out += (char)(0x80 | ((c >> 6) & 0x3F));
        out += (char)(0x80 | (c & 0x3F));
    }
}

int main(int argc, char** argv) {
    if (argc < 2) { std::cerr << "usage: kabaki program.kb\n"; return 2; }
    std::ifstream f(argv[1], std::ios::binary);
    if (!f) { std::cerr << "cannot open file\n"; return 2; }
    std::string raw((std::istreambuf_iterator<char>(f)), {});
    std::vector<u32> src = decode(raw);

    std::string inRaw((std::istreambuf_iterator<char>(std::cin)), {});
    std::vector<u32> in = decode(inRaw);
    size_t inPos = 0;

    std::unordered_map<size_t, size_t> jump;
    std::vector<size_t> st;
    for (size_t i = 0; i < src.size(); i++) {
        if (src[i] == U'「') st.push_back(i);
        else if (src[i] == U'」') {
            if (st.empty()) { std::cerr << "unmatched 」\n"; return 1; }
            size_t j = st.back(); st.pop_back();
            jump[i] = j; jump[j] = i;
        }
    }
    if (!st.empty()) { std::cerr << "unmatched 「\n"; return 1; }

    auto root = std::make_unique<Node>(nullptr);
    Node* cur = root.get();
    std::string out;

    auto indexOf = [](Node* n) {
        auto& s = n->parent->kids;
        return (size_t)(std::find_if(s.begin(), s.end(),
            [&](auto& p) { return p.get() == n; }) - s.begin());
    };

    for (size_t pc = 0; pc < src.size(); pc++) {
        switch (src[pc]) {
        case U'芽':
            if (cur->kids.empty()) cur->kids.push_back(std::make_unique<Node>(cur));
            cur = cur->kids[0].get();
            break;
        case U'根':
            if (cur->parent) cur = cur->parent;
            break;
        case U'右':
            if (cur->parent) {
                auto& s = cur->parent->kids;
                size_t i = indexOf(cur);
                if (i + 1 == s.size()) s.push_back(std::make_unique<Node>(cur->parent));
                cur = s[i + 1].get();
            }
            break;
        case U'左':
            if (cur->parent) {
                size_t i = indexOf(cur);
                if (i > 0) cur = cur->parent->kids[i - 1].get();
            }
            break;
        case U'+': cur->v++; break;
        case U'-': cur->v--; break;
        case U'剥':
            if (cur->parent) {
                Node* p = cur->parent;
                p->kids.erase(p->kids.begin() + indexOf(cur));
                cur = p;
            } else {
                cur->v = 0;
                cur->kids.clear();
            }
            break;
        case U'出': encode(cur->v, out); break;
        case U'入': cur->v = inPos < in.size() ? (long long)in[inPos++] : 0; break;
        case U'「': if (cur->v == 0) pc = jump[pc]; break;
        case U'」': if (cur->v != 0) pc = jump[pc]; break;
        default: break;
        }
    }
    std::cout << out;
}

C# interpreter

using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Text;

class Node
{
    public BigInteger V = BigInteger.Zero;
    public Node? Parent;
    public List<Node> Kids = new();
    public Node(Node? parent) { Parent = parent; }
}

static class Kabaki
{
    static int Main(string[] args)
    {
        if (args.Length < 1) { Console.Error.WriteLine("usage: kabaki program.kb"); return 2; }

        Console.OutputEncoding = new UTF8Encoding(false);
        string src = File.ReadAllText(args[0], Encoding.UTF8);

        var input = new List<int>();
        foreach (var r in Console.In.ReadToEnd().EnumerateRunes()) input.Add(r.Value);
        int inPos = 0;

        var jump = new Dictionary<int, int>();
        var stack = new Stack<int>();
        for (int i = 0; i < src.Length; i++)
        {
            if (src[i] == '「') stack.Push(i);
            else if (src[i] == '」')
            {
                if (stack.Count == 0) { Console.Error.WriteLine("unmatched 」"); return 1; }
                int j = stack.Pop();
                jump[i] = j; jump[j] = i;
            }
        }
        if (stack.Count > 0) { Console.Error.WriteLine("unmatched 「"); return 1; }

        var cur = new Node(null);
        var output = new StringBuilder();

        for (int pc = 0; pc < src.Length; pc++)
        {
            switch (src[pc])
            {
                case '芽':
                    if (cur.Kids.Count == 0) cur.Kids.Add(new Node(cur));
                    cur = cur.Kids[0];
                    break;
                case '根':
                    if (cur.Parent != null) cur = cur.Parent;
                    break;
                case '右':
                    if (cur.Parent != null)
                    {
                        var s = cur.Parent.Kids;
                        int i = s.IndexOf(cur);
                        if (i + 1 == s.Count) s.Add(new Node(cur.Parent));
                        cur = s[i + 1];
                    }
                    break;
                case '左':
                    if (cur.Parent != null)
                    {
                        var s = cur.Parent.Kids;
                        int i = s.IndexOf(cur);
                        if (i > 0) cur = s[i - 1];
                    }
                    break;
                case '+': cur.V += 1; break;
                case '-': cur.V -= 1; break;
                case '剥':
                    if (cur.Parent != null)
                    {
                        var p = cur.Parent;
                        p.Kids.Remove(cur);
                        cur = p;
                    }
                    else
                    {
                        cur.V = 0;
                        cur.Kids.Clear();
                    }
                    break;
                case '出':
                    if (cur.V >= 0 && cur.V <= 0x10FFFF && Rune.IsValid((int)cur.V))
                        output.Append(char.ConvertFromUtf32((int)cur.V));
                    break;
                case '入':
                    cur.V = inPos < input.Count ? input[inPos++] : 0;
                    break;
                case '「':
                    if (cur.V.IsZero) pc = jump[pc];
                    break;
                case '」':
                    if (!cur.V.IsZero) pc = jump[pc];
                    break;
            }
        }

        Console.Out.Write(output.ToString());
        return 0;
    }
}

See also