git @ Cat's Eye Technologies Decoy / master junk / thencemuffin.lua
master

Tree @master (Download .tar.gz)

thencemuffin.lua @masterraw · history · blame

-- SPDX-FileCopyrightText: Copyright (c) 2023-2024 Chris Pressey, Cat's Eye Technologies.
-- This work is distributed under a 2-clause BSD license. For more information, see:
-- SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Decoy

ruxRolli = require "rux-rolli"


load = function(programText)
    return makeComposite({
        makeText(programText, { makeRange(0, 1) }),
        put(0, 0, "X", setCursor("IP", newCursor(0, 0, 1, 0), newPlayfield()),
        newStack(),
    })
end

next = function(conf)
    local text = getTextChild(conf)
    local pf = getPlayfieldChild(conf, 0)
    local stack = getStackChild(conf)
    if not (text and pf and stack) then
        return errorWith("Malformed configuration", conf)
    end
    local range = getRanges(text)[0]
    local s = getString(text)
    local i = getRangeIndex(range)
    if i >= length(s) then
        return haltWith(conf)
    else
        local nextText = moveRange(text, 0, 1)
        local char = getString(text).charAt(i)
        if char == "I" then
            return inputWith(newComposite({
                nextText,
                pf,
                push("*", stack)
            }))
        else
            local nextStack
            if char == "S" then
                nextStack = pop(stack)
            else
                nextStack = push("A", stack)
            end
            local nextPf = moveCursor("IP", 1, 0, put(2, 1, char, pf))
            return nextWith(newComposite({
                nextText,
                nextPf,
                nextStack,
            }))
        end
    end
end

recv = function(conf, input)
    local text = getTextChild(conf)
    local pf = getPlayfieldChild(conf, 0)
    local stack = getStackChild(conf)
    if not (text and pf and stack) then
        return errorWith("Malformed configuration", conf)
    end
    return nextWith(newComposite({
        text,
        pf,
        push(input, stack),
    }))
end