Introduction
Ah, INTERCAL...
As much as I'd like encourage everyone to Try it Online, text output is just painful.
According to the docs it uses the "Turing Text Model".
While an... interesting concept, using it is about as much fun as shooting yourself in the foot.
And what do we do with a task like this? Automate it.
The Turing Text Model
The characters INTERCAL knows are printed on a circular tape that can only be moved in the positive direction. Printing is done by passing tape head movement commands in an array to the READ OUT
statement.
Every ASCII character is written on the inside of that tape (The outside has the characters for the input on it, duh). This results in the characters' bytes being on the tape in reverse. Also the tape head moves backwards along the character list, because its positioning is based on the outside of the tape.
The head starts at position 0.
Now the magic starts. I'll be using Truttle1's explanation on how to achieve output.
- Set up an array with the output length.
- Fill the entries in the array with tape head movement values.
- Find the current character's ASCII value in binary
- Reverse it and convert back a number
n
. - Subtract
n
from the current head postion and modulo by 256 if needed, resulting in a valuer
r
is the value you need to store in the array- The head is now at position
n
.
DO READ OUT
the array.
PLEASE NOTE
- Array is pre-initialized with
0
, first index is1
- INTERCAL uses extended 8-bit ASCII characters, hence the tape length of 256.
- Between 1/3rd and 1/5th of all lines have to start with
PLEASE
. Note that in that case you drop theDO
forGIVE UP
andREAD OUT
, but not for anything else, as seen in the examples.
Challenge
Given an input string, output a valid INTERCAL program that prints that string and terminates.
Examples
Prints "BUZZ"
DO ,1 <- #4
DO ,1 SUB #1 <- #190
DO ,1 SUB #2 <- #152
PLEASE DO ,1 SUB #3 <- #336
DO READ OUT ,1
PLEASE GIVE UP
Whitespace is optional. The following prints "FIZZ"
DO,1<-#4DO,1SUB#1<-#158DO,1SUB#2<-#208DO,1SUB#3<-#312PLEASEREADOUT,1PLEASEGIVEUP
(Examples shamelessly stolen from Truttle1's FizzBuzz program from the video.)
You can find an ungolfed reference implementation in python 3 here
Rules
- No standard loopholes
- This is code-golf, shortest code wins.
- You can expect input >= 1, ASCII characters only.
- The program may not throw any errors apart from
ICL774I RANDOM COMPILER BUG
andICL666I COMPILER HAS INDIGESTION
. First one happens at random, second one is C-Intercal running out of memory and circumventing that isn't part of the challenge
ICL666I COMPILER HAS INDIGESTION
, which is to say the compiler runs out of memory. I assume this is permitted? \$\endgroup\$