# TRM in APL

Larry Moss and some others have created an interesting little machine
for teaching certain courses. This little machine is an interesting one where each instructions is a series of 1's followed by a series of #'s. The 1's are an argument indicating either a register or some value, in either case the value is the number of 1's that is in the instruction. The number of #'s indicates the instruction:

 # Add 1 to register ## Add # to register ### Goto instruction ahead #### Goto instruction behind ##### Pop character from word in register and jump ahead 1 if empty, two if 1, and three if #.

I figured that I would implement a simple interpreter for this machine in APL really quickly, and it was a very quick program. Here's the result:

```     ∇ R←N EVAL P;PC;I;V
[1]    R←N ⋄ PC←1 ⋄ EM←'IMPROPER HALT' ⍝ Set Init State and Program Count
[2]    P←(⊃'1#'∨.=⊂P)/P                ⍝ Strip whitespace
[3]    P←↑(('1'=P)∧1,2≠/P)⊂P           ⍝ Partition into instructions
[4]    P←(+/'#'=P),[1.5]+/'1'=P        ⍝ Reformat instructions to integer
[5]   LP:→(PC=1+⊃⍴P)/0                 ⍝ Check for halt
[6]    EM⎕SIGNAL((PC<1)∨PC>1+⊃⍴P)/90   ⍝ Exception on bad halt
[7]    (I V)←PC⌷P                      ⍝ I→Instruction  V→Register/Count