Cubix, 10 bytes
(W0^I?>O2@
Test it online!Test it online!
This code is wrapped to the following cube net:
( W
0 ^
I ? > O 2 @ . .
. . . . . . . .
. .
. .
The code is then run with the IP (instruction pointer) starting on the I
, facing east. I
inputs a signed integer from STDIN, pushing it onto the stack.
The next command is ?
, which changes the direction of the IP depending on the sign of the top item. If the input is 0, it keeps moving in same direction, running through the following code:
>
- Point the IP to the east. (No-op since we're already going east.)O
- Output the top item as an integer.2
- Push 2 to the stack. This is practically a no-op, because...@
- Terminates the program.
If the input is negative, the IP turns left at the ?
; because this is a cube, the IP moves onto the 0
in the second row, heading east. 0
pushes a literal 0, then this code is run:
^
- Point the IP north.W
- "Sidestep" the IP one spot to the left.(
- Decrement the top item.
The TOS is now -1
, and the IP wraps around the cube through a bunch of no-ops .
until it hits the >
. This runs the same output code mentioned above, outputting -1
.
If the input is positive, the same thing happens as with negative inputs, with one exception: the IP turns right instead of left at the ?
, and wraps around the cube to the 2
, which pushes a literal 2. This is then decremented to 1 and sent to output.