Friday, April 15, 2022

OISC:2 It works! Mostly.

Here is a very basic implementation of OISC:2, written in Python.
Please feel free to play with it, but leave the attribution header.

Improved code with examples and a ReadMe can be found on GitHub.

# OISC2 without file reading or coprocessor.
# This is based on an implementation of Subleq found here:
#           https://rosettacode.org/wiki/Subleq#Python
# It has very limited error testing.  Buyer beware.
# McChuck, April 15, 2022

import sys

def oisc2(mem, negmem):

    negmem = [0,2,0,0,0,0,0]+negmem

#   IP, Next, Return, Reg a, Reg b, Reg c, Mode

    IP = -1

    NEXT = -2

    RET = -3

    negmem.reverse()

    mem=mem+negmem

    try:

        while mem[IP] >= 0:

            a=mem[mem[IP]]

            b=mem[mem[IP]+1]

            mem[NEXT]=mem[IP]+2

            if a>0:

                if b>0:

                    mem[b]-=mem[a]

                elif b==0:

                    print(chr(mem[a]), end="")

                else:

                    if mem[a]<=0:

                        mem[RET]=mem[NEXT]

                        mem[NEXT]=abs(b)

            elif a==0:

                if b>0:

                    mem[b]=ord(sys.stdin.read(1))

                elif b==0:

                    mem[NEXT]=-1

                else:

                    mem[mem[abs(b)]]=ord(sys.stdin.read(1))

            else:

                if b>0:

                    if mem[mem[abs(a)]]<=0:

                        mem[RET]=mem[NEXT]

                        mem[NEXT]=b

                elif b==0:

                    print(chr(mem[mem[abs(a)]]), end="")

                else:

                    mem[mem[abs(b)]]-=mem[mem[abs(a)]]

            mem[IP] = mem[NEXT]


        print("\nOISC2 completed successfully.")


    except (ValueError, IndexError, KeyboardInterrupt):

        print("OISC2 aborted at: ", ip)

        print("A: ", a, "B: ", b)

        print(mem)


    finally:

        print("\nFinished.")


oisc2([12, 12, 14, -30, 14, 0, 13, 2, 13, 4, 12, -2,
        0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 10, 0, 14,
        -29, 38, -29, 0, -40, -41, -40, 30, 0, 0, 13, 29], [0,0,1,2,3,4,5,6,0,0])


# 0 Z, Z     # You can't jump back to 0, so have to pad the beginning.
# 2 Start: L, -Cont
# 4 L, 0              # Print "Hello, world!" directly
# 6 M1, Start
# 8 M1, Start+2
# 10 Z, -Start
# 12 Z: 0 .
# 13 M1: -1 .
# 14 L: "Hello, world!\n"
# 29 LP: L .
# 30 Cont: -LP Halt
# 32 -LP 0            # Now print it again, but indirectly
# 34 -M1P -LPP
# 36 -M1P Cont
# 38 Halt: 0 0
# 40 M1P: M1 .
# 41 LPP: LP .


No comments:

Post a Comment

I reserve the right to remove egregiously profane or abusive comments, spam, and anything else that really annoys me. Feel free to agree or disagree, but let's keep this reasonably civil.