Hacker’s Corner: from APL 2 APL2C
Vector 19:4, page 96
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 96
Has Adrian Gone Completely Potty?¶
That heading reminds me of the comment "it is really easy to spell bananananana – it’s just hard to know when to stop …" which was attributed to a Camacho grand-daughter. We know what APL2C is – it is Tilman Otto’s excellent translator from APL2 to ANSI C for input to any compiler you can name. Why might I want to translate from some other APL to this dialect? To run RainPro (aka GraPL.exe) from the $prompt on Unix, that’s why.
Easy enough, you would think. 10 years ago, it would have been really easy as Rain was written in APL*PLUS/PC and would obviously run on APL2 with almost no modification. Today, there is one major stumbling block – Control Structures! Tilman has implemented Eke’s ‘Del’ form and RainPro has been gently eliminating the right-arrow to the point where it has almost vanished from the workspace. So all I needed to try out my workspace in Tilman’s translator was a little something to map from the keyword-style to the Del-style control code.
And here it is, all 210 lines of it. The comments flag up the limitations – if you can live with these (or don’t mind extending it) feel free to mail me and demand a copy. My objective was compiled RainPro, so I know what I don’t do (see the XP approach etc., etc.) so I just left it out.
So What Does It Do, Exactly?¶
Let’s look at a simple test function in the ‘before’ and ‘after’ state.
⎕vr 'ifelseif'
∇ r←ifelseif x;qwe
[1] ⍝ Simple IF/ELSEIF block example
[2] :if x>12
[3] 'x>12'
[4] :elseif x>4
[5] 'x>4'
[6] :elseif x>0
[7] 'At least it was +ve'
[8] :end
[9] 'The End'
∇
⎕vr ⎕fx a2c 'ifelseif'
∇ r←ifelseif x;qwe
[1] ∇1 0=(x>12) ⍝ If
[2] :'x>12'
[3] :∇1 0=(x>4) ⍝ ElseIf
[4] :'x>4'
[5] :∇(x>0) ⍝ ElseIf
[6] 'At least it was +ve'
[7] ∇ ⍝ EndIf
[8] ∇ ⍝ EndIf
[9] ∇ ⍝ EndIf
[10] 'The End'
∇
You will have to refer back to Eke’s article for the exact specification, but you can see the general idea. I am careful to preserve the original name of the structure as a comment, which makes debugging the results in APL2C a whole lot easier.
And How Does It Do It?¶
Here we go, with very little more comment. The comments in the code should be enough to go on:
![The a2c function, lines [0] to [28]](a2c-1.jpg)
![The a2c function, lines [29] to [88]](a2c-2.jpg)
![The a2c function, lines [89] to [148]](a2c-3.jpg)
![The a2c function, lines [149] to [187], then “…. and a few more of the same …….”, then lines [207] to [210]](a2c-4.jpg)
Remaining Gotchas¶
The only real nasty is ⎕ELX which cannot be emulated in an APL2-style of error trap. I use very few of these, and I think the answer is to replace all of them by a utility called ∆EA which I can then simply replace with ⎕EA at conversion time. This will give me a decent chance of running on APLX also, which has to make it worth the effort. Apart from that, it all works rather well. It is nice to see RainPro running as a single-file EXE – makes it a very easy product to ship!