[Question]
  [
[Vyxal](https://github.com/vyxal/vyxal) is a stack-based language, meaning that everything operates by popping and pushing values onto a stack. It has a bunch of useful flags, one of which is `r`.
Running a Vyxal program with the `r` flag causes functions to take their elements in reverse order.
For example, the program `5 3 -` means: Push 5 to stack, push 3 onto the stack, and subtract them by popping 3, popping 5, and subtracting the 3 from the 5 to yield 2. But with the `r` flag, it first pops the 5, then the 3, and subtracts 5 from 3 to yield -2.
For this challenge, we will be operating within a much-simplified version of Vyxal.
The digits 0-9 each push themselves to the stack , and lowercase letters are *dyadic* functions, meaning they pop two values from the stack and do something with them, before pushing the result. Everything else does nothing and won't be included in the input.
Your challenge is to take a program with this format and output a program that would do the same thing if the input order of each function is reversed.
This sounds complicated, so let's demonstrate with this example:
```
87a
```
To do this, we simply swap the 8 and the 7 to yield `78a`.
A more complicated example:
```
54a78bc
```
How it works:
```
      c # c is a function taking...
   78b  # This, where:
     b  # b is a function taking
   78   # 7 and 8
        # And...
54a     # This, where:
  a     # a is a function taking... 
54      # 5 and 4 
```
So to flip it, we just flip the operands of every function:
```
  <-->
<->|| c 
|| 78b
54a |
 |  |
 <-->
```
Yielding:
```
87b45ac
```
Which is the result!
Another way to explain this is:
An operator is one of `a-z`
A token is either a single digit, or of the format [TOKEN][TOKEN][OPERATOR]
For each token of the format [TOKEN][TOKEN][OPERATOR], flip the two tokens.
# Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), shortest wins!
# Testcases
You can assume there will always be two values to pop whenever there's a function/operator.
```
123ab => 32a1b
59a34bz => 43b95az
123456abcde => 65a4b3c2d1e
1 => 1
12a53bx7c08d9ef => 980de735b21axcf
12a34bc56d78efg => 87e65df43b21acg
```
As usual, these are created by hand, so please tell me if they're wrong.
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~15~~ ~~12~~ ~~10~~ 9 bytes
-1 byte thanks to [Kevin Cruijssen](https://codegolf.stackexchange.com/users/52210/kevin-cruijssen)!
This could be 8 bytes without the `l`, but why would I remove that?
```
**vyxal**iŠìì
```
[Try it online!](https://tio.run/##yy9OTMpM/V9TVvm/rLIiMSfz6ILDaw6v@V9bq/Pf0Mg4MYnL1DLR2CSpigvIMzE1S0xKTknlMgTyEk2NkyrMkw0sUixT00B8oKpkU7MUc4vUtHQA "05AB1E – Try It Online")
```
v             # for each character y:
 y            #   push y
  x           #   duplicate TOS and double the copy
              #   double is a noop on alphabetic strings
   a          #   is the doubled copy alphabetic / the current char a function?
    l         #   convert the result to lowercase
     i        #   if the command was a function:
              #     stack before: [..., x1, x2, f]
      Š       #     triple-swap:  [..., f, x1, x2]
       ì      #     prepend:      [..., f, x2x1]
        ì     #     prepend:      [..., x2x1f]
```
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck), 83 bytes
```
,[[>+>>>++++[>]<-<<[>]<<-]>>>>[-]<[+<<<<[<]<[[->+<]<]>+[>]>-[<<[<]>+[>]>-]]<,]<[.<]
```
[Try it online!](https://tio.run/##LYpJCoBAEAMfNNOCu4eQjzR9cBsRwYPg@8dWrEuSItM17me65yPnqMpAMjhKgwBvQMwlVQwa4Ci8qTB4Gt8rRT/9DzNEvxSwnMtqrJtpbrulH9a0PQ "brainfuck – Try It Online")
Maintains a stack of tokens. When an operator is read, append it to the top two tokens on the stack. Each token should be read from right to left.
```
For each character in input:
,[
  Move forward one cell and compare to 85
  [>+>>>++++[>]<-<<[>]<<-]
  Clean up comparison cell
  >>>>[-]<
  If greater than 85 (and thus is operator):
  [
    Remove comparison result
    +
    Append last two tokens together
    <<<<[<]<[[->+<]<]>
    Append operator to end of this token
    +[>]>-[<<[<]>+[>]>-]
  ]
  <
,]
<[.<] output result
```
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal), ~~18~~ ~~12~~ 8 bytes
```
(n:Ǎ[∇pp
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=&code=%28n%3A%C7%8D%5B%E2%88%87pp&inputs=123ab&header=&footer=)
*-5 thanks to @AUsername*
*and -1 to an extra golf on that golf*
*but then another -4 from A Username*
The irony here is that using the `r` flag makes this way too long.
## Explained
```
(n:Ǎ[∇pp
(              # for each character in the input:
 n:Ǎ           #     remove all letters from that character - if this is a letter, it will leave an empty string. Otherwise, it will leave it as-is.
    [          #     if the result isn't empty:
     ∇         #          rotate the top three items
      pp       #          and double prepend
```
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), ~~34~~ 33 bytes
```
{$[y<58;x,y;(-2_x),,(,/2#|x),y]}/
```
[Try it online!](https://ngn.bitbucket.io/k#eJxLs6pWia60MbWwrtCptNbQNYqv0NTR0dDRN1KuAbIqY2v1ubjSFJQMjYwTk5RALFPLRGOTpColqKiJqVliUnJKKoQPFU00NU6qME82sEixTE2DiQF1JZuapZhbpKalKwEAN5UeZA==)
A single neat K scan.
Returns a singleton list with the output.
-1 byte from Traws.
## Explanation
```
{$[y<58;x,y;(-2_x),,(,/|-2#x),y]}/
{                               }/ scan from left
 $[                            ]   if
   y<58;                           current command <58 (is digit)
        x,y                        then join the two
           ;                       otherwise
                    (,/|-2#x)      join the last two commands, reversed
                             ,y    concat with current command
            (-2_x),,               nest, and  join with the rest of the output
```
[Answer]
# JavaScript (ES6), ~~52~~ 49 bytes
Expects an array of characters. Returns a string.
```
a=>a.map(c=>a.push(o=1/c?c:a.pop()+a.pop()+c))&&o
```
[Try it online!](https://tio.run/##dc9LCsIwEAbgfU9RupAEMTVN04dQPYi4mEySqmhTrIp4@RrFB0qdTSbwf8zMFs7Q4WHTHieN06a3VQ/VHNgeWoL3pj11a@IqHuMCZ/7rWkLHrxcpHY1cj67p3M6wnauJJUvGWMQTASpaURp@Ko5DkQBXwUBeliBSdf0SPp8KVUq4BsMTUpmBQm3eyotMQqoEJpqbQfWz01Px4QkghbrkOC10aexD@mxZTLXJhVQJhwvaP9JfgzLTeWFs/ZJFbjKprT/KU6z7Gw "JavaScript (Node.js) – Try It Online")
### Commented
```
a =>                // a[] = input array, re-used as the stack
  a.map(c =>        // for each character c in a[]:
    a.push(         //   push in the stack ...
      o =           //     ... the new value o:
        1 / c ?     //       if c is a digit:
          c         //         just use c
        :           //       else:
          a.pop() + //         use the concatenation of
          a.pop() + //         the last 2 values from the stack
          c         //         followed by c
    )               //   end of push()
  ) && o            // end of map(); return the last value
```
[Answer]
# [Haskell](https://www.haskell.org/), 55 bytes
```
head.foldl(%)[]
s%x|x<'a'=[x]:s
(a:b:s)%x=(a++b++[x]):s
```
[Try it online!](https://tio.run/##y0gszk7Nyfmfm5iZZ1tQWhJcUuSTp6CikKagZGiUaGqcVGGebGCRYpmapsSVZhvzPyM1MUUvLT8nJUdDVTM6lqtYtaKmwkY9Ud02uiLWqphLI9EqyapYU7XCViNRWztJWxsorGlV/P//v@S0nMT04v@6yQUFAA "Haskell – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 14 bytes
```
ＦＳ⊞υ⁺⭆⊗№βι⊟υιυ
```
[Try it online!](https://tio.run/##JYq7CoQwEAD7@4qUG/CK03sIltpYCAG/IIlPCNkQs/7@GrluZhi76WhRO@YFo4DeB0pjirtfQUqh6NiACqEcHfDPgw7QIRk3T9Ai@QSmELuUecIAdEO25qHynLI3zK9SV29jP9/pV8/Lys/TXQ "Charcoal – Try It Online") Link is to verbose version of code. Explanation: Based on @Arnauld's answer; for every input character, if it is a letter then pop the top two elements off the result stack and concatenate the three, otherwise just push the digit to the stack, however in Charcoal it turns out to be golfier to pop `N` elements off the result stack where `N` is `2` for a letter and `0` for a digit, and concatenate the character.
```
ＦＳ
```
Loop over the characters of the input string.
```
⊞υ⁺⭆⊗№βι⊟υι
```
Count the number of appearances of the character in the predefined lowercase alphabet, double that, pop that many terms from the predefined empty list, join everything together, and push that to the list.
```
υ
```
Output the final result.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 10 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
ªç8îá/╤╫╬∩
```
[Run and debug it](https://staxlang.xyz/#p=a687388ca02fd1d7ceef&i=123ab%0A59a34bz%0A123456abcde%0A1%0A12a53bx7c08d9ef%0A12a34bc56d78efg%0A&m=2)
same idea as my K answer.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jellylanguage), 17 bytes
```
;ṭ@Ṫ,Ṫ{;ʋ¥ØDi¥}?/
```
[Try It Online!](https://jht.hyper-neutrino.xyz/tio#WyIiLCI74bmtQOG5qizhuap7O8qLwqXDmERpwqV9Py8iLCIiLCIiLFsiXCIxMmEzNGJjNTZkNzhlZmdcIiJdXQ==)
Since the input is guaranteed to be non-empty (clarified by OP), we do not need to reduce from an initial value and can replace `ƒ“”` with `/` (thanks to Leaky Nun for -2 bytes).
There's gotta be a better way to get rid of that `ṭ@Ṫ,Ṫ{;ʋ¥` mess in the middle.
```
;ṭ@Ṫ,Ṫ{;ʋ¥ØDi¥}?/    Main Link
                /    Reduce
               ?     If
              }      - The right argument
            i          - Appears in
          ØD           - "0123456789"
;                    - Append it to the accumulating stack
                     Otherwise
 --======¥           - Previous two; 2,2-chain x (a b) y = x a (x b y)
 ṭ@                  - Append to the accumulator the result of
   -=--=ʋ            - Previous four; 1,2,2,2-chain x (A b c d) y = (x A) b (x c y) d y
   Ṫ                   - Pop the last element off the stack
    ,                  - And pair it with
     Ṫ{                - Pop the last element off the left side (the stack)
       ;               - Append the incoming character
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~81~~ 77 bytes
Python port of the [Javascript answer](https://codegolf.stackexchange.com/a/231151/)
A recursive lambda might solve this with fewer bytes though.
*-4 bytes thanx to ovs*
```
def f(i,s=[]):
 for c in i:s+=[c<'a'and c or s.pop()+s.pop()+c]
 return s[-1]
```
[Try it online!](https://tio.run/##bckxDoQgEIXh3lNMNxBdE6NGY5aTEApEiTQDAbbY0yPNZhur9@d74ZsvT2Mpx2nBMtclIRXfGrA@ggFH4LbUCmneqFHTUa0eqQ8@MN7@1qgG4pk/kSDJ16BKiI4yw3nSy7ob7HBd9mnWtexfuRDPzssN "Python 3 – Try It Online")
[Answer]
# [Factor](https://factorcode.org/), 71 bytes
```
EBNF: A e=e e[a-z]=>[[first3 1string surround]]|[0-9]=>[[1string]];EBNF
```
[Try it online!](https://tio.run/##VY7LTsMwEEX3@Yoh@1ZJHOfRqpVAAtRNN4hVlMXYHqcVKA22I7UFvj3ESZFgZc25x3NHo3QnM7y@7PbPK0Bj8GKho2ZJotVg6aOnVpKFNzItvUNnyLlLZ46tA@vGp7GwDnb7FVhp0MnD8Piwf1rBPdCGgCpcXOvNtqr00VjHIJ7/gO2NOfWtquuvKlqUk3LL6nrtVwyfEMYJQxFCyEtkqbiGE0l5hkIq8tNEkDNxzmVUqJL0TEZb8kzlBekmhG@oxnuWUAOhPATBHUyLYbMFlmAsRnBr8ChlouR4na3fMh9kHFPBZKJi8qFH8WT9O8DjsogU5YyLJMaz1LP09yYvFTllXOmxbrRkM/wA "Factor – Try It Online")
EBNF definition strikes again :) Defines a function named `A` which takes a string, parses it, and returns its r-flagged version as a string.
(TIO version is slightly older than 0.98 stable, and requires ending `;EBNF` instead of surrounding the body with `[=[ ]=]`. The TIO version happens to be shorter.)
A bit of jankiness comes from the fact that the character class `[0-9]` gives its charcode, not any kind of sequence (array, vector, or string). `1string` is used to keep all intermediate results as strings.
## Ungolfed
```
! If part of a postfix expression,
! parse the two operands and the operator, swap the operands, and concatenate
expr = expr expr [a-z] => [[ first3 1string surround ]]
! Otherwise, take a single digit operand and make it a string
     | [0-9] => [[ 1string ]]
```
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), 27 bytes
```
∊{⍺∊⎕D:⍺,⍵⋄(2↑⍵)⍺⊂⍛,2↓⍵}/⍤⌽
```
[Try it online!](https://tio.run/##jYy9SgNBFIX7PMV2k8UN7u7s7I@VoIW2xk4sZnZmQyBsLCJEQyxUgkYn@EOwsBJS2FkJItjEN7kvEu9NREyXZuZyzvk@edSq6RPZajdqptsxpTZ61gP7fgD2AW6/4GrYq8Lw3HdCJ3DXNsF@YsS/n3DSX8eB74H9wP8MA2xgNN4@7Ff@DCvhSyyGS/gcRQXYyar8jLjFlLINPD2a3FxWQxjc4@lSS85nD5PHhc1OiC1gcAd2BMOL6Run9Whc39vCd39ntz5rlg72LAi5VMxhIpM8Uqd4YRKJWKpcG@ZUPRYwl0IpuOomuZ/qzBTzGQG5iHWSmqLBKu3jDgl5KAMSRlxlQpIwFjJSPA918E@Ypb42CRcqDGQ3J2GamFjoAjmM8l/h9UsxfW2WPw "APL (Dyalog Extended) – Try It Online")
Straightforward reduction, as in many other answers. Since APL's reduction is from right to left, the reduction is done on the input string reversed. Also, the top of the stack is on the left side, and the stack items are kept nested until the very end (where `∊` flattens the entire structure).
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), 34 bytes
```
{⍵[⍒∊{0 2 1⊂⍤+@⍺∊3×⍵}/⌽0,⍸~⍵∊⎕D]}⌽
```
[Try it online!](https://tio.run/##jYy9SgNBFIX7PMV2k2CCOzs7@9MJWmhr7MRifkMgbCwirIZYKARdnKCCWAcs7KyEELCJb3JfZL0TRGKXZu5wzvk@cT7o6EsxGPY6phyZQhtdj8F9noJ7hocvuKvGTahuwiAKaGtnD9wSI/b9ipPJLg7CNrgF3msMsIHZy8HZpPFn2Ar/x2JYb@JrFBXg3rbmLUwfwc2gul19MJg@YdM93sf35PCoW/eLAHtCIyYkCQjPBYvlFf4wiXkipNKGBM02oaTlQ8GZLFMVZjo3dj3zgOKJTjNje6QxvBh5IYsE9cKYyZwLL0y4iCVTkaYbwjwLtUkZlxEVpfLCLDUJ1xY5jNSv8H5uV@/94gc "APL (Dyalog Extended) – Try It Online")
I had an idea of grade-and-indexing, where base 3 numbers define the ordering of elements. It works well, but it is unfortunately slightly longer than the easy solution.
This works by reconstructing the postfix structure based on the indices of operators.
```
Input: '59a34bz' (expected output: '43b95az')
{...}⌽  Reverse the string and pass onto the dfn.
        This has the structure of prefix notation, with args swapped.
        ⍵←'zb43a95'
⍸~⍵∊⎕D  Extract the (1-based) indices of operators.
        v←1 2 5
{...}/⌽0,  Reverse v and reduce on it with the starting value of 0.
           ⍺ gets the indices (1, 2, then 5), and ⍵ is the current vector.
∊3×⍵    Multiply all numbers in ⍵ by 3 and flatten it.
0 2 1⊂⍤+@⍺  Add 0 2 1 to the ⍺-th element in ⍵, creating a nested vector.
        ⍺←1, ⍵←0: ⊂(0 2 1)
        ⍺←2, ⍵←↑: 0 2 1 → 0 6 3 → 0(6 8 7)3
        ⍺←5, ⍵←↑: 0 6 8 7 3 → 0 18 24 21 9 → 0 18 24 21(9 11 10)
⍵[⍒∊...]  Flatten the final result, and sort ⍵ by descending order of that.
        Flatten:   0 18 24 21 9 11 10
        Input(⍵):  z  b  4  3 a  9  5
        Sort desc: 43b95az
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 29 bytes
```
S`\B
+1`(.+)¶(.+)¶(\D)
$2$1$3
```
[Try it online!](https://tio.run/##K0otycxL/P8/OCHGiUvbMEFDT1vz0DYoGeOiyaVipGKoYvz/v6GRcWISl6llorFJUhUXkGdiapaYlJySymUI5CWaGidVmCcbWKRYpqaB@EBVyaZmKeYWqWnpAA "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation:
```
S`\B
```
Split each character onto its own line. (Sadly `_S` doesn't seem to do what I want.)
```
+1`(.+)¶(.+)¶(\D)
$2$1$3
```
Processing each operator in turn, prefix it with its reversed operands.
[Answer]
# [J](http://jsoftware.com/), 41 bytes
```
[:|.@;((;@;1 0&{);2}.])`;@.(58>3 u:[)/@|.
```
[Try it online!](https://tio.run/##TY49C8IwFEV3f8XDwbSgsUn62jRBKQhOTq4imE/FxUkoVX97LWLQ4S73nAv3OkwpibBSQGAOBagxCwqb/W47HNSTtjrLdKsZFLNHrvmLHvOTbmmGci3grg75sn3SIZ8Ed7kBEdwwS2ChIAJhXBhLvqQUtkHTJ4aNEaXtE63QlFY47ln4W5dYGet8SBYjsPqgVDSy8KEWaDkznYu/qUFhu9oV0jchJlvWoUIfxyej7s5/9njFYeVrGeKZDG8 "J – Try It Online")
Well, this is quite long and inelegant because of the required boxing in J. Perhaps someone can suggest a better approach.
I believe the current approach is doing more or less the same thing as Razetime's K answer.
[Answer]
# [Japt](https://github.com/ETHproductions/japt) v2.0a0 [`-h`](https://codegolf.meta.stackexchange.com/a/14339/), 15 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
Input as a character array, output as a string.
```
£pXiUoX²è\a)¬)Ì
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=2.0a0&flags=LWg&header=cQ&code=o3BYaVVvWLLoXGEprCnM&input=IjU0YTc4YmMi)
```
£pXiUoX²è\a)¬)Ì     :Implicit input of array U
£                   :Map each X
 p                  :  Push to U
  Xi                :    Prepend to X
    Uo              :      Pop this many elements from U
      X²            :        Duplicate X
        è           :        Count occurrences of
         \a         :        RegEx /[a-z]/g
           )        :      End pop
            ¬       :      Join
             )      :  End push
              Ì     :  Last element
                    :Implicit output of last element of resulting array
```
[Answer]
# Vim, 51 bytes
```
:s/./\r&/g
qq{:/\v(.+)\n(.+)\n(\a)/s//\2\1\3
@qq@qJ
```
[Try it online!](https://tio.run/##K/v/36pYX08/pkhNP52rsLDaSj@mTENPWzMmD0rGJGrqF@vrxxjFGMYYczkUFjoUev3/b2iUaGySlGxqlmJukZqWDgA "V (vim) – Try It Online")
The `J` can be omitted if a leading newline is permissible.
[Answer]
# [Python 3](https://docs.python.org/3/), 80 bytes
```
f=lambda i,o=[]:i and f(i[1:],o+[(i[0]>'9'and o.pop()+o.pop()or'')+i[0]])or o[0]
```
[Try it online!](https://tio.run/##TU9LCoMwEN17iiEgJmiLrX9BLyJZJMa0gTYRzaLt5W2039nM@zGPGe/2bHSyLLK5sCsXDFRkmo7WCpgWILHqDjWNTNg5FNM2qIJVN/vRjJiE722mICDhGqAOg3FgscNsZ2gAe@AGHY4J4yh6kaxiScofH@q8NMsZ78XwlX4eyxJ@K/q4FNUg/2R3oc9yUZSDPCGPeJ501RaUhq263oLjpLTFyJ9h14I/I/AB28j9ZQkhyxM "Python 3 – Try It Online")
[Answer]
# [TypeScript](https://www.typescriptlang.org/)'s type system, 155 bytes
```
//@ts-ignore
type F<C,S=[]>=C extends[infer T,...infer C]?F<C,T extends`${number}`?[T,...S]:S extends[{},{},...infer N]?[`${S[0]}${S[1]}${T}`,...N]:S>:S[0]
```
[Try it at the TS playground](https://www.typescriptlang.org/play?#code/PTACBcGcFoEsHMB2B7ATgUwFDgJ4Ad0ACAMQB4BhAGgGUBeAbQF0A+W8w9AD3HUQBNI9WIgBm6VIQAqlAHRzhYieUYB+MlUkduvAQAMAJAG9EAVwC2AI3EBfXSvrS5M6owBc1LT36DD1yr9l5UXFCADlVegNDanoABkZrIxiARgSjSVtAmXD3Znc4xkxsfCJJdEhwZMJaElJ6ACIAZnrKeoAmFvqAQ07kzot6lkwQQlGAPRVigily8Dbq2oa+1o7W5tae1oGhkfHJoA)
This is a generic type `F<C>` where C is a tuple type of characters, which returns a string literal type.
If the input could instead be a tuple type of elements which are either character or number:
## [TypeScript](https://www.typescriptlang.org/)'s type system, 144 bytes
```
//@ts-ignore
type G<C,S=[]>=C extends[infer T,...infer C]?G<C,[T,S]extends[string,[{},{},...infer N]]?[`${S[0]}${S[1]}${T}`,...N]:[T,...S]>:S[0]
```
[Try it at the TS playground](https://www.typescriptlang.org/play?ssl=1&ssc=1&pln=2&pc=132#code/PTACBcGcFoEsHMB2B7ATgUwFDgJ4Ad0ACAcQB4BhAGgGUBeAbQF0A+W8w9AD3HUQBNI9WIgBm6VIQAqlAHRzhYieUYB+MlXrTqjLj36DI4VMPiV6AbwC+lK7PmjxhAHKNV9AAYASc9XoAGRktvXwBGQO9JS3c7GRcALk0Y7WY43wDMbHwiSXRDEMJaElJ6AGZKACZKACIAQyrKEOqAIyqWTBBCToA9FUyCKVzwcoKi+kbKstr6qpa2ju7eoA)
]