Quantcast
Viewing latest article 16
Browse Latest Browse All 17

Answer by lyxal for Write a Stack Exchange compliant brainfuck explainer

Vyxaloj, 69 bytes

:4$꘍,`([%^])`\\`><+-,.[]`f:→+∑%ṡ';:vL¦0pṪZƛ÷$꘍nh←=Tuw∨□ḣ∇‟İ„L›„↲p4$꘍∑

Try it Online!

Explained

`([%^])`

Push the string "([%^])" to the stack. This will serve as the basis for aregular expression that groups on non-BF commands

\\

Push a backslash to the stack. This will serve to escape all the characters inthe BF character set ([ and ] need to be escaped, because they interfere with the range set)

`><+-,.[]`f

Push the string "><+-,.[]" to the stack, and turn it into a list of characters.It's important it's turned into a list of characters because the backslash willbe prepended to each character using vectorised addition.

:→

But first, put the character set into the ghost variable for later storage -there's no BF character set element in vyxal, so to save bytes, it needs to bestored.

+∑

Prepend the backslash to each character just like I said I would, and convert itback to a single string.

%

And place that into the regex pattern string from earlier using string formatting

ṡ';

Split the input program on that regex, and keep only non-empty strings. This splitsthe program into single bf chars and groups of NOPs.

:vL¦0pṪZ

Get the length of each group, get the cumulative sum of that, prepend a 0 and zipwith each group. This has the effect of calculating how many spaces to prepend toeach group.

ƛ

To each item X = [spaces, character_group] in that, do the following

÷ð*p

Prepend spaces spaces to character_group

nh←=Tuw∨

Get the index of character_group in the BF character set stored in theghost variable. This is basically a hacky version of because doesn'twork on the version the site uses (it's fixed in the 2.6 preleases, but I'msticking with 2.4.1)

□ḣ

Wrap all inputs (program and explanation strings) into a single list and pushthe program separately from the explanation strings.

∇‟İ

Do some weird stack manipulation to retrieve the corresponding explanationstring using the index generated by nh←=Tuw∨.

„L›„↲$+

Left justify the character group with the prepended spaces with extra spacesto match the length of the input program + 1. And add that to the explanationusing some extra weird stack manipulation.

4ð*p∑

Indent that 4 spaces, and convert into a single string.

;⁋

Join the entire list on newlines. The j flag would usually take care of this,but 69 is a funny number.


Viewing latest article 16
Browse Latest Browse All 17

Trending Articles