def buildTree(tokens, stack):
    """
    Processes a list of @tokens {String[]} to create a tree.
    Optional @stack {Array?} is used internally during recursion.
    """

    instructions = []

    while len(tokens) > 0:
        token = tokens.popleft()

        if isinstance(token, str):
            instructions.append(token)
        elif token["tag"] == "#" or token["tag"] == "^" or token["tag"] == "?":
            stack.append(token)
            token["nodes"] = buildTree(tokens, stack)
            instructions.append(token)
        elif token["tag"] == "/":
            opener = stack.pop()
            return instructions
        else:
            instructions.append(token)

    return instructions