global right$[] left$[] pos blank$ .
proc show stat$ . .
   write stat$
   for i to 5 - len stat$
      write " "
   .
   write "| "
   h = -len left$[] + 1
   for i = h to len right$[]
      if i <= 0
         c$ = left$[-i + 1]
      else
         c$ = right$[i]
      .
      if i = pos
         write "*"
      else
         write " "
      .
      write c$ & " "
   .
   print ""
.
func$ get .
   if pos <= 0
      return left$[-pos + 1]
   .
   return right$[pos]
.
proc put s$ . .
   if pos <= 0
      left$[-pos + 1] = s$
   else
      right$[pos] = s$
   .
.
proc mleft . .
   pos -= 1
   if pos <= 0 and len left$[] < (-pos + 1)
      left$[] &= blank$
   .
.
proc mright . .
   pos += 1
   if pos > 0 and len right$[] < pos
      right$[] &= blank$
   .
.
proc utm stat$ endstat$ bl$ init$ rules$[] trace . .
   blank$ = bl$
   pos = 1
   right$[] = strsplit init$ " "
   left$[] = [ ]
   for r$ in rules$[]
      r$[][] &= strsplit r$ " "
   .
   repeat
      if trace = 1
         show stat$
      else
         if steps mod 1000000 = 0
            write "."
         .
      .
      for i to len r$[][]
         if r$[i][1] = stat$ and r$[i][2] = get
            put r$[i][3]
            if r$[i][4] = "left"
               mleft
            elif r$[i][4] = "right"
               mright
            .
            stat$ = r$[i][5]
            break 1
         .
      .
      steps += 1
      until stat$ = endstat$
   .
   if trace = 1
      show stat$
   else
      print ""
      print "Steps: " & steps
   .
.
# 
repeat
   s$ = input
   until s$ = ""
   trace = 1
   if substr s$ 1 7 = "5-state"
      trace = 0
   .
   print "--- " & s$ & "---"
   s$ = input
   in1$[] = strsplit s$ " "
   in2$ = input
   r$[] = [ ]
   repeat
      s$ = input
      until s$ = ""
      r$[] &= s$
   .
   utm in1$[1] in1$[2] in1$[3] in2$ r$[] trace
   print ""
.
# 
input_data
Simple incrementer
q0 qf B
1 1 1
q0 1 1 right q0
q0 B 1 stay qf

Three-state busy beaver
a halt 0
0
a 0 1 right b
a 1 1 left c
b 0 1 left a
b 1 1 right b
c 0 1 left b
c 1 1 stay halt

5-state, 2-symbol probable Busy Beaver
A H 0
0
A 0 1 right B
A 1 1 left C
B 0 1 right C
B 1 1 right B
C 0 1 right D
C 1 0 left E
D 0 1 left A
D 1 1 left D
E 0 1 stay H
E 1 0 left A
