(>2&.)(<2)@1(1>(>2&-1@)<$i" "@(<2))
Try it online!
Input is one-indexed space separated integers followed by a zero.
The output is a list of space separated integers to stdout.
Today, I learned something "interesting" about Phooey.
If you looked at my other post, I mentioned the !
modifier, which makes any instruction use the value from the stack.
I figured, okay, I could do something like this, using the stack modifier to do a variable offset. Easy.
...>@<(1>!@$i" "(<))
It turns out that it wasn't entirely true.
else if(op.payload_type == PayloadType::SPECIAL) {
switch(op.payload.special) {
// snip
case SpecialPayload::STACK:
op.payload_type = PayloadType::NORMAL;
op.payload.normal = pop();
return op.payload.normal;
// Snip
}
}
While the user input variants don't do this, for some dumb reason, using the !
operator will only work once: after that, it will be constant.
So therefore, I had to do this manually.
(>2&.)(<2)@1(1>(>2&-1@)<$i" "@(<2))
# do
(
# We write into every other cell, as we
# need a temp buffer for indexing.
# Additionally, we need a null terminator to
# mark the start of the list.
>2
# Read integer into tape
&.
# Loop until the user enters a zero.
#
# This could be made easier if the developer
# initialized their variables to zero:
# int64_t read_int(std::istream& stream) {
# int64_t res; // <= uninitialized
# stream >> res;
# return res;
# }
# but no, this is a terribly coded esolang.
# Instead, we either have to do ~i or require
# a sentinel.
# while (*cell)
)
# Scan back to the beginning with another do-while.
# Again, we are writing every two cells.
(<2)
# Push the first index to the stack.
@1
# do
(1
# move to a scratch cell
>
# move to the cell position
# do { ptr++; } while (--n);
# do
(
# go to the next scratch cell
>2
# pop into the scratch cell
&
# subtract 1
-1
# push back
@
# while index is not zero
)
# BUG: we have a memory leak here: we leave the last
# index on the stack cuz it's PPCG and we dgaf about that
# To fix it, add an ampersand here.
# &
# Move backwards to the numerical index
<
# print the integer and a space
$i" "
# push the new index
@
# scan back to the beginning
(<2)
# loop forever
# while (1)
)
Animated version with the memory leak fixed (which I totally didn't create manually using raw ANSI escape codes in a Phooey file):