Risky is a new language of mine, which features an interesting form of tacit programming. In this challenge, you'll take a Risky program as input, and visualize the parsing.
No knowledge of Risky is needed for this challenge, but it's a pretty interesting language so I'd recommend trying it :p.
Risky's parsing is based purely on the length of the program. It follows three rules:
- If the program consists of one operator, it is parsed as a nilad
- If the program consists of an even number of operators, the first operator is parsed as a monad and the rest is parsed as its argument
- Otherwise, the middle operator is parsed as a dyad, and both sides are parsed as arguments
This is recursive; the argument(s) to a monad or dyad are parsed as if they are their own programs.
This forms a tree of operators. For example, the program
-1+!2 would look roughly like this:
+ / \ - ! | | 1 2
Your task will be to represent this by offsetting the operators lower in the tree, showing the structure of the program. For example:
+ - ! 1 2
All of the operators stay in order, but are vertically offset in order to show the structure.
Input will consist of a string of operators. If you want, you can assume the input doesn't contain any invalid Risky operators, though it would not affect the parsing.
You can output a visualization of the program in one of four ways:
+ - [ + ? ! ! 2 0 1 } ] [
1 } ] [ ! ! 2 0 + ? - [ +
(Using a shorter program for the next two to save space)
2 ? + 1 [ !
! [ 1 + ? 2
Both LTR are RTL can be flipped upside down if you prefer.
Note that the above examples can be used as more complicated test cases. For the top/bottom ones,
-!1+!}+?0[ is the program shown. For the rest, it's
1 1 +
1 + 2 -
! ! ! ! !
This is code-golf, so shortest answer (in bytes) per language wins!