Szewczyk notation for Minsky machine

From Esolang
Jump to navigation Jump to search

Szewczyk notation is a tool invented by Kamila Szewczyk of thought useful when describing counter machines. Simply put, this notation for Minsky machines is a minimalistic representation of its capabilities.

Syntax

Minsky machine code is represented by a list of pairs separated from each other. The text representation looks like this:

a0 a1
b0 b1
c0 c1
 ...

Each row is given an unique, consecutive ID. The notation supports two instructions:

  • inc rX => X -1: increment register rX
  • dec rX, Y => X Y: decrement register rX if rX > 0, else jump to row with ID Y.

Properties

Numbers can be assumed of infinite precision. The ID's are 0 indexed and registers are initialized to 0

Examples

Infinite loop

1 -1
1 1
1 1

Cat

0 -3
0 5
0 -1
0 -2
1 0

I/O extension required.

Implementations

A tiny, 321 byte Perl emulator for the Minsky machine code (assuming Szewczyk notation).

#!/usr/bin/perl
use bignum;my(@a,@c,$d);push(@a,0)for(1..$ARGV[0]);s/(-?[0-9]+)[ \t]+(-?[0-9]+)/push(@{$c[$d++]},($1,$2));/ge while(<STDIN>);
for($d=0;$d<@c;$d++){$a[$c[$d][0]]++if$c[$d][1]==-1;if($c[$d][1]>=0){$d=$c[$d][1]-1if($a[$c[$d][0]]==0);$a[$c[$d][0]]--if($a[
$c[$d][0]]>0);}}print((shift@a)."\n")for(1..$ARGV[0]);

The following Python script is also an interpreter.

prgm = [[]]
prgm.pop(0)
while True:
    line = input('%s: ' % len(prgm)) # Displays line numbers automatically
    if line != "eof":
        line = list(line.split(" "))
        prgm.append([int(line[0]), int(line[1])])
    else:
        break
tape = []
for i in range(0, 5): # Adjustable
    tape.append(0)
# Executes the program
def szewczyk(A, B):
    global tape
    global inst
    global Input
    global part
    if B == -1:
        tape[A] += 1
        inst += 1
    elif B == -2: # Extensions
        print(chr(tape[A]), end="")
        inst += 1
    elif B == -3: # Extensions
        if part < len(Input):
            tape[A] = ord(Input[part])
        else:
            tape[A] = 0
        part += 1
        inst += 1
    else:
        if tape[A] > 0:
            tape[A] -= 1
            inst += 1
        else:
            inst = B
Input = input('>i ')
part = 0
inst = 0
while inst < len(prgm):
    szewczyk(prgm[inst][0], prgm[inst][1])

Extensions

An I/O extension can be implemented as x1 equal to -2 (for output) and -3 (for input) to a given register.