[Question]
  [
In this challenge, you implement an interpreter for a simple stack-based programming language. Your language must provide the following instructions:
* push a positive number
* pop two numbers and push their sum
* pop two numbers and push their difference (second number - first number)
* pop a number and push it twice (dup)
* pop two numbers and push them so that they are in opposite order (swap)
* pop a number and discard it (drop)
You may assume instructions will never be called with less arguments on the stack than are needed.
The actual instructions can be chosen for each implementation, please specify how each instruction is called in the solution. Your program/function must output/return the stack after all instructions are performed sequentially. Output the stack in whatever format you prefer. The stack must be empty at the start of your program.
## Examples
For these examples, the stack is represented bottom-to-top.
```
1 2 + -> [3]
1 drop -> []
10 2 - 3 + -> [11]
9 dup - -> [0]
1 2 3 -> [1 2 3]
1 dup 2 + -> [1 3]
3 2 5 swap -> [3 5 2]
```
  
[Answer]
## Regex (PCRE 2), 104 bytes (60 + 46)
* +2 bytes to properly remove excess spaces after `~` commands
Search:
```
((1+) (1+) a)|((1+) \5(1+) -)|((1+) :)|((1+) (1+) s)|(1+ ~ ?)
```
Replacement:
```
${1:+$2$3:${4:+$6:${7:+$8 $8:${9:+$11 $10:}}}}
```
Input and output in unary.
Each regex substitution resolves one operation, so you need to repeatedly use the regex to replace until the result doesn't change to get a final output.
[Try it online!](https://regex101.com/r/7CHzB8/3)
No language seems to support the PCRE 2 replacement syntax to allow writing an easy wrapper
* `11111` push a number in unary
* `a` add 2 numbers
* `-` subtract the first number from the second
* `:` duplicate
* `s` swap
* `~` pop
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 103 bytes
-2bytes by l4m2
```
p=>p.split` `.reduce(([a=[],b=[],...S],i)=>[{A:a+b,S:b-a,X:[b,a],P:b}[i]??[1/i?+i:a,a,b],S].flat(2),[])
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70kLz8ldcGiNNulpSVpuhY30wts7Qr0igtyMksSFBL0ilJTSpNTNTSiE22jY3WSQISenl5wrE6mpq1ddLWjVaJ2kk6wVZJuok6EVXSSTmKsToBVUm10Zqy9fbShfqa9dqZVok6iTlKsTnCsXlpOYomGkaZOdKwm1Lp1yfl5xfk5qXo5-ekaaRrqhgpGCo7qmppcGOIB2EQNgMqDFYyxarFUcFEIxmqUkYIxVnEXHJYbA8VNFSKAMhBXL1gAoQE)
Operators:
* `A`: Add
* `S`: Sub
* `D`: Dupe
* `X`: eXchange
* `P`: Pop
* `\d+`: push Number
One space between each tokens.
Output is the array where first value is stack top, and last value is stack bottom.
[Answer]
# [TypeScript](https://www.typescriptlang.org/)'s type system, 206 bytes
```
//@ts-ignore
type F<C,S=[0,0]>=[S,C]extends[[infer A,infer B,...infer V],[infer I,...infer R]]?F<R,I extends`${any}`?[I,...S]:[...[[B],[A,A,B],[B,A],[`${A}${B}`],B extends`${A}${infer Z}`?[Z]:0][I],...V]>:S
```
[Try it at the TS playground](https://www.typescriptlang.org/play?#code/PTACBcGcFoEsHMB2B7ATgUwFDgJ4Ad0ACAMQB4BhAGgGUBeAbQAZLGBdAPgesvNfQA9w6RABNI9erEQAzdKkIBBSlNnyAQpQB02lXMIA1VpUky9ASS07T8gEqtWAfjI3KZwgKGjIAAwAkAbwBDRBwAX28HegttTWpWAC56GIk1I3olJVTjDQU0v38FUIC1cKM1d0FhMXzCgN15AC1wyIaEtiijGMN2eOpMTGAAKkJCRkJaQjxkPEwRkYBGccIRAFcZucIAJiXIAHdA9bmAZiXAkRFZuYAWHZWAIw3CQeB+3AJCfiWyegAieZ-KIQ-v9AUdAcD5iCgZCYbDIQCtoCrhwBsA5gA9ByYIA)
This is a generic type `F<C>` which takes input as a tuple of items which are either unary numbers as strings of 1s, or numbers 0 to 4 for the 5 commands:
| num | function |
| --- | --- |
| 0 | Pop |
| 1 | Duplicate |
| 2 | Swap |
| 3 | Add |
| 4 | Subtract |
This solution works identically to the one below, only it skips the step of splitting on spaces, and indexes into an array instead of a dictionary, so it ends up quite a bit shorter.
# [TypeScript](https://www.typescriptlang.org/)‚Äôs type system, ~~324~~ ~~309~~ ~~251~~ 220 bytes
```
//@ts-ignore
type F<C,S=[0,0]>=[S,C]extends[[infer A,infer B,...infer V],`${infer I} ${infer R}`]?F<R,I extends`1${any}`?[I,...S]:[...{p:[B],d:[A,A,B],x:[B,A],a:[`${A}${B}`],s:B extends`${A}${infer Z}`?[Z]:0}[I],...V]>:S
```
[Try it at the TS playground](https://www.typescriptlang.org/play?#code/PTACBcGcFoEsHMB2B7ATgUwFDgJ4Ad0ACAMQB4BhAGgGUBeAbQAZLGBdAPgesvNfQA9w6RABNI9erEQAzdKkIBBSlNnyAQpQB02lXMIA1VpQAGAEgDeu+QEkAvoQtXCAJVvHWAfjLPK1wgKFRSGMARgsAQ0QcNw96ay1talYALnptTXM8VLUjEVSlJRzKfmzKBSNw1LNzBVsLNTcjSGS1f0FhMWraxxk9AC0Y+j6Uxls4o3TDdmTqTEwQQkhwcIBjAGtFgAtkAHdEQnBkPGhD6AAjZHBDgFtNQkZIQnDwA82iC6vka8IRZEIUcCbKTwTRzXAEQgAL0ItBIpAARCFCCEkeFkSikRisVjCPxFoR4ex5sBCIQAHoeOZAA)
This is a generic type `F<C>` which takes input as a string type of space separated commands or unary numbers, space terminated. Output in unary, first element is top of stack.
Commands:
| name | function |
| --- | --- |
| `a` | Add |
| `s` | Subtract |
| `x` | Swap |
| `p` | Pop |
| `d` | Duplicate |
I was surprised by how short this was when it was 100 bytes longer than it is now, so now I'm *really* surprised.
Explanation:
```
type F<
  // C is the input string type
  C,
  // S is the stack, starts with two useless elements
  S=[0,0],
>=[S,C]extends[
    // get the top 2, A and B, and the rest V, of the stack
    [infer A,infer B,...infer V],
    // get the first word I and the rest R of C
    `${infer I} ${infer R}`
  ]
    // recurse, setting C to R, and setting S to...
    ?F<R,
      // does I start with a 1?
      I extends`1${any}`
        // if so, prepend it to S
        ?[I,...S]
        // otherwise, it's a command; index into this object
        // to replace the top 2 of the stack:
        :[...{
          // pop- just the second
          p:[B],
          // dup- first twice, then second
          d:[A,A,B],
          // swap: second, then first
          x:[B,A],
          // add: concatenate the first and second
          a:[`${A}${B}`],
          // subtract: the second with the first taken away
          s:B extends`${A}${infer Z}`?[Z]:0
        // then append V
        }[I],...V]
    >
  // if C is empty (no more commands are left), return S
  :S
```
[Answer]
# Trivial Eval/Exec Answers in Stack Languages
For languages where the command set required is already a thing, and evaluating a string is the shortest solution.
Don't add answers in stack languages that aren't purely "exec".
## [Vyxal](https://github.com/Vyxal/Vyxal/tree/version-2) `W`, 1 byte
```
ƒñ
```
[Try it Online!](https://vyxal.pythonanywhere.com/?v=2&c=1#WyJXIiwiIiwixJYiLCIiLCIiXQ==)
Uses the command set `+-:$_` and `[0-9]+` for pushing numbers.
## [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 3 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
.V)
```
[Try it online.](https://tio.run/##yy9OTMpM/f9fL0zz/39DBSMFbQVDhRgFQwMgU1fBGMi1VHABskBSxkDSBazEGEiaKhQDAA)
Uses the command set `+-Ds\` for `add`; `subtract`; `duplicate`; `swap`; `drop` respectively. `.V` executes the (implicit) input as 05AB1E code; and `)` wraps the entire stack into a list (which is output implicitly at the end).
## [sclin](https://github.com/molarmanful/sclin), 1 bytes
```
#
```
[Try it on scline!](https://scline.fly.dev/#H4sIAENId2UCA1MyVjBSMFUoLk8sUNIDAFWXUb8NAAAA#H4sIAENId2UCA1MGAP.eZXABAAAA#) Uses `dup`, `pop`, `swap`, `+`, `-`.
## [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 2 characters
```
?f
```
Where `?` reads a line from stdin and executed it as `dc` code and `f` dumps the stack.
In the simple stack language use `d` for dup, `r` for swap and `ss` for drop.
[Try it online!](https://tio.run/##S0n@/98@7f9/QwUjBW0FBYUUIDZWMFHQBdKmCkVA0kzBXMFCobgYhCwB "dc ‚Äì Try It Online")
# [RProgN](https://github.com/TehFlaminTaco/Reverse-Programmer-Notation), 1 byte
```
C
```
[Try it online!](https://tio.run/##Kyooyk/P@//f@f///8YKRgqmCjEA "RProgN ‚Äì Try It Online")
Requires space between commands, `[]\` are Drop, Dup, and Swap respectively.
[Answer]
# [flex](https://www.gnu.org/software/flex/), 179 bytes
```
%{
s[9];i;t;
%}
%%
a s[--i-1]+=s[i];
s s[--i-1]-=s[i];
d s[i++]=s[i-1];
x s[i-2]^=s[i-1]^(s[i-1]=s[i-2]);
p --i;
[0-9]+ s[i++]=atoi(yytext);
\n for(t=i;t--;)printf("%d ",s[t]);
%%
```
[Try it online!](https://tio.run/##PZDBboMwDIbP81NYVdOCWFhhJxTYbbvuAVgqZRAgEiKIRCpo2rMzM7qd/PuT/eu3P5Xr1qbXM@b56/vbyr7AlZkURngB7BsYA4Wu5NzwREaFK40U4P4Jv5OaiIkiubWEBcwb4Km83sk12Gux41DAiGQhoLzwTEZ/68pbEyyL17OnkY8BGzsFvqA0nItwnMzgm@DAajw8utJvNoytlBugrSrkN6RL4mWJSVt0vRpa5H3TA9w602uctKpxnGwrsLbwoKvOIh/wuCE88xc84@mEv/gYxE@7QZ7n@0QItR30mmCKChIcIbmQdPhMbYb0AkhJ0@XwAw "Bash ‚Äì Try It Online")
Uses:
* `[0-9]+` push a number
* `a` sum
* `s` difference
* `d` dup
* `x` swap
* `p` drop
[Answer]
# Brain-flak, 294 bytes
```
{(()[]<(({}()))({[()](<{}>)}(){}){{}<>({}{})((<>))}{}(({}()))({[()](<{}>)}(){}){
{}<>([{}]{})((<>))}{}(({}()))({[()](<{}>)}(){}){{}<>(({}))((<>))}{}(({}()))({[()
](<{}>)}(){}){{}<>(({}({}))[({}[{}])])((<>))}{}(({}()))({[()](<{}>)}(){}){{}<>{}
((<>))}{}>[[]]){{}({}[()()()()()]<>)(((<>)))}{}{}{}}<>
```
[Try it Online!](https://brain-flak.github.io/?c=lY3bCcAACAPXSXYQFxEnCe7e2Md3WwIiyUUFsDoADUhCBTZCk7ShoTSRTr0CkaS3N7o0/Yd3zF/0WSjP/cT@3F3gJrOqe929Aj5qx7igpVbuHQ&o=8&i=M1YwUjBV0DUBAA)
What better way to "Implement a simple stack language" than by using "a simple stack language"! And also a nice excuse to show off a new online interpreter for brain-flak that I have been working on.
Conveniently, a lot of the behavior described in the challenge is built-in to how brain-flak works. For example, *Your program/function must output/return the stack after all instructions are performed sequentially* is just describing the fundamental operation of brain-flak. And also, we could take each of the listed operations and transliterate it to brain-flak:
```
# Sum
({}{})
# Subtract
([{}]{})
# Duplicate
(({}))
# Swap
(({}({}))[({}[{}])])
# Pop
{}
```
Unfortunately, transliteration does not a valid answer make. So we need to do a bit of parsing to figure out which operation to call.
Without clarification from OP (and seeing as many answers have assumed positive input integers), this answer assumes that "push an arbitrary integer" means "an arbitrary positive integer". Here is the input format:
* -1: sum
* -2: subtract
* -3: duplicate
* -4: swap
* -5: pop
* All other positive integers: Push that integer
Technically, negative numbers other than 5 listed also work to push a number. 0 does not work.
("TOS" means "Top of Stack")
### Commented/readable version:
```
# While primary stack is not empty...
{
    # Keep track of the stack height +1 at this point...
    (()[]<
        # Increment TOS
        (({}()))
        # TOS != 0
        ({[()](<{}>)}(){})
        # If TOS != 0...
        {
            # OPCode -1: Sum
            # Pop two numbers and push their sum to the alternate stack
            {}
            <>
            ({}{})
            # Go back to main stack and replace TOS with 2 zeros
            ((<>))
        }{}
        # Increment TOS
        (({}()))
        # TOS != 0
        ({[()](<{}>)}(){})
        # If TOS != 0...
        {
            # OPCode -2: subtract
            # Pop two numbers and push their difference to the alternate stack. Second number - first number.
            {}
            <>
            ([{}]{})
            # Go back to main stack and replace TOS with 2 zeros
            ((<>))
        }{}
        # Increment TOS
        (({}()))
        # TOS != 0
        ({[()](<{}>)}(){})
        # If TOS != 0...
        {
            # OPCode -3: duplicate
            # to the alternate stack
            {}
            <>
            (({}))
            # Go back to main stack and replace TOS with 2 zeros
            ((<>))
        }{}
        # Increment TOS
        (({}()))
        # TOS != 0
        ({[()](<{}>)}(){})
        # If TOS != 0...
        {
            # OPCode -4: swap
            # on the alternate stack
            {}
            <>
            (({}({}))[({}[{}])])
            # Go back to main stack and replace TOS with 2 zeros
            ((<>))
        }{}
        # Increment TOS
        (({}()))
        # TOS != 0
        ({[()](<{}>)}(){})
        # If TOS != 0...
        {
            # OPCode -5: pop
            # from the alternate stack
            {}
            <>
            {}
            # Go back to main stack and replace TOS with 2 zeros
            ((<>))
        }{}
    # And see whether any extra values were added to the stack during that last bit
    # Essentially this means: "Push a 1 if none of the opcodes in that snippet matched, otherwise 0"
    # Additionally, if one of the opcodes matched, there will be an extra value on this stack. Remember that, it's important.
    >[[]])
    # If none of the opcodes matched...
    {
        # Pop a useless '1'
        {}
        # Push TOS - 5 onto alternate stack
        # This is the original positive integer that needs to be pushed
        ({}[()()()()()]<>)
        # Push THREE zeroes onto main stack
        (((<>)))
    }
    # Pop three values off the stack
    {}{}{}
}
# Switch to main stack
<>
```
I'm certain this could be golfed further.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 114 bytes
```
f=->s,*r{s[/.+? ?/]?f[$',*r+(s>?/?[s.to_i]:(q=r.pop;s>?.?[q,q]:s>?-?[]:(w=r.pop;s>?,?[q-w]:s>?+?[q,w]:[q+w])))]:r}
```
[Try it online!](https://tio.run/##VYq7CgIxEEV7vyKFkI15oXaR3fmQYRAsAlabhxJE/PY42mi6c@655X559B5nu1SzK8@K3mkQ4AkibiVPeqoLeMDqbuv5SmHKc3FpTSeeHWA2mQKjBeTWfs1ws@3b9OfGiFk3UkpRKK@eRES5FwdxFF7S5k/dqHZUM6qW1N8 "Ruby ‚Äì Try It Online")
### Usage
* `/` dup
* `.` drop
* `-` subtract
* `,` swap
* `+` add
[Answer]
# [Go](https://go.dev), ~~321~~ ~~320~~ 312 bytes
```
import(."strings";."fmt")
func f(c string)(o[]int){for _,e:=range Fields(c){n,a,b,A:=0,0,0,len(o)-1
B:=A-1
if A>-1{a=o[A]}
if A>0{b=o[B]}
switch e{case"D":o=o[:A]
case"d":o=append(o,a)
case"s":o=append(o[:B],a,b)
case"+":o=append(o[:B],b+a)
case"-":o=append(o[:B],b-a)
default:Sscan(e,&n);o=append(o,n)}}
return}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=ZVLBitswEKVXfcVUkCIROcQJC1sHLzhkeyylyS0NRbHl1NSRvZa8bRH-h957CYV-Rn-kX1PJcrJZFh2keW9mNPNmfv0-VKe_NU-_8oOAIy8kKo511WhYY1UWqVD4T6vz4Pbfq1tPkAlWuinkQeHFBOdHjSnKW5lCTlLwDCXVdldITU1eNfCZiShuuLT53xWizBRJqZGMsz1LonjK3CmFJBUNQrSM4sReRQ7JXRAaHlfbZNd5e2r21lxaU30rdPoFhEm5EniFo8oSUbJDvZ05m9e1kBmpGKceVdfoNlruXAUDN37B7cfnuOAlF1guEzlvSx2tVcolEeyNpIurXyXtOtQI3TayOwv4U_-oBWycSG2qwRSyHgRbgPheQ69Zh7yabhSEgkFaKK0gii29McjgEGYwxsx7m3nXsR5cXaABmVq_AOZXvmHoqbeQQXBBp-cMM5g_ubIZu6TOnv0Ynom5hW9APZXCbtjMUbaFD7YrXUqC4ziG9Sb5uAH7cpvSL4R2_fiV8O0ZxNMezImeWF2om_jr9eT-oeWlhaw8DKyLFaRPnRM8eoD4DkaPQEaP9JO0ZfSRvZt72xDa2VqelQL371cwlIKGwZxO_v4P)
Very straight-forward split-on-spaces then switch-on-token.
-1 by aliasing indexes and elements.
-8 by using the following instruction names:
* `D`rop
* `d`uplicate
* `s`wap
* `+`, `-`, and integers
[Answer]
# [Perl 5](https://www.perl.org/) `-a`, ~~101~~ 92 bytes
```
map{/\W$/?eval"\$a[-2]$&=pop\@a":/x/?pop@a:push@a,/d/?$a[-1]:/s/?(pop@a,pop@a):$_}@F;say"@a"
```
[Try it online!](https://tio.run/##HYvBCsIwEAV/ZQlBFBu3rQQkIsnJm2cPpshCCwrVLEalIv66MfYyPJg33N16ndKF@I1@L9F2T@qFl3RQdSMnGw7sHQmDA9q8HRl@xJOjAlu0/1fVGIxop6MsRs6MPH7cdh3pJXKbklpCDRoUtJkRBljB/Bv4fg7XmNROL8qqTIp@ "Perl 5 ‚Äì Try It Online")
### Operations:
* `+` = add
* `-` = subtract
* `s` = swap
* `d` = duplicate
* `x` = drop
* Anything with a digit in it is assumed to be a number. This implies that this will work with integers and floats as well as allowing a sign indicator at the front of the number.
Operators and numbers must be separated from each other by at least one space. No error checking was specified, so this code does not do any.
[Answer]
# [Python](https://www.python.org), 111 bytes
*-11 bytes, thanks to emanresu A, uses bytes `0x00`-`0x03` as commands*
```
def g(s):
 S=[]
 exec(s.translate('S+=[0]; S[-1]+=1; *_,a,b=S;S[-2:]=[b,a-b,a]; S[-1:]=[];'.split()))
 return S
```
[Attempt This Online!](https://ato.pxeger.com/run?1=dVJLboMwEFW3nGIUkGwTsDCoUhtEL8GSWhXhkxBFBhmipmfpJpv2Tu0VeokaDCmJUqyxzMz72GO_fzZv3bYWp9PHoSvdh686L0rY4JasDIijhBtQHIsMt7STqWj3aVdgFC-jxOMhxInL-DJiIdgvTuqsozhUKX_Fo2TtpK6KEdRneIho2-yrDhNCDJBFd5ACYm37fffT-5bad6xtMEJ0V1cCZ1uJkWlbFNFK5MURZ4RAWUvIoBLQKj2jkZXocIkXpm3atmVRNSxK9bRQcBMY-LAE9wmSgM_x53Iu62aoX5TPX6-r9frkbQtPebgQjD6M3VQyB-LEnvMfIT-oLQxkj18dqhf4O0mgHfrVJXAu_n8rep-pHexKwp7cBvZICRT8HtrXVLcoUD8-n_qOno-ep4Kp8NHsrSQoXWfIQep21bytdogTou98enK_)
---
# [Python](https://www.python.org), 122 bytes
*-8 bytes, thanks to `psmears`*
```
def f(s):
 S=[]
 for c in s:exec('S+=[0] S[-1]+=1 *_,a,b=S;S[-2:]=[b,a-b,a] S[-1:]=[]'.split()['#*$.'.index(c)])
 return S
```
[Attempt This Online!](https://ato.pxeger.com/run?1=dVJdToQwEI6vnGKyJaEt0FCIiWLwEjw2jWH5iSQGCD-6ehVf9kXvpKextMsKZm3Tpp35fqaTvn92r-Nj2xyPH9NY-Tdfb0VZQYUHEluQJkJaULU95FA3MMTlocyxk7qJCCSkwufSTTjQBy_z9kl6pyJhLBOx9zJfLQOZA9JhQ_dUj5gIB1GbOaxuivKAcyKJBX05Tn0Dqang--q56-tmxBXeIYootW2mps2Y2XaEAAIOIbjg34OIpLXCn9NF33Y6v0mfx6xr9ObgZYtAefgQnXw4v6iENHFhr_m3UEyqBE0ONlyk_envSyLjMJ-2wLX4_62YfZZ28D8SdHHT7BMlUvBrGF4y06JIXUJp2r98hB8)
---
# [Python](https://www.python.org), 127 bytes (no eval)
*taking `emanresu A`'s suggestion to its extreme*
```
def f(s):
 S=[]
 for c in s:
  if"#"==c:S+=[0]
  if"*"==c:S[-1]+=1
  if"$"==c:*S,a,b=S;S+=[b,a-b,a]
  if"."==c:*S,_=S
 return S
```
[Attempt This Online!](https://ato.pxeger.com/run?1=fVLLaoQwFKVbv-KgLnxjRgrtlPQnXIoUH7EjiEpUBlf9kG5m0_5O1-3XNBqdVx8JCcnJPefc3OT1vR37XVMfDm9DX7h3ny85K1AYnblVENIoVlA0HBnKGp2AUBaqplKabUObRn4sEUsikUtimxKJ6TNmhU7ipDR8mMJTJ3HFWEjeGvBEQwWc9QOvEco0vm4-NHRtkrEOTV2NcxKcJXmSllXZjw76HRuxL6sKKUP5XDec5UjHCRe59oy3QpFxpeViZxSGqlnQLAu67omue56cVNOEBoINbLiPiIL4gnE8z3nTzgEX58cmpaXktP7LxxdGLoLFjJBf1aDNzFVOP1e4Rz6IRGa6H_-43Wx9ulMgbabVVeyFwz91mdzW2pArFevkKQUWViAYt-j2iaxYIDabWL7r-s2-AQ)
* push number: `#` followed by n times `*`
* drop: `.`
* swap: `$$..`
* add: `$$.$.$$...$$...`
* subt: `$.$$...`
* dup: `#$.$$.$.$$...`
*It is possible to go down to 3 operations, by creating a new operation `'` that pushes `1` and replacing `#` with `''$.$$..` and `*` with `'$$.$.$$...$$...`. But then the pushed integer is no longer clearly determinable from the push integer instruction*
---
# [Python](https://www.python.org), 193 bytes
```
def f(s):
 S=[]
 for c in s:
  if"/"<c<":":S[-1]=10*S[-1]+int(c)
  if"#"==c:S+=[0]
  if"$"==c:*S,a,b=S;S+=[a+b,a-b]
  if":"==c:S+=[S[-1]]
  if","==c:S[-2:]=S[:-3:-1]
  if"."==c:*S,_=S
 return S
```
[Attempt This Online!](https://ato.pxeger.com/run?1=VZFNToQwGIbjlsQ7fGlnAUOLlGYSp069RJcNMfwMkQ0QfmI8i5vZ6D08hp7GQgFh177v0ydf24-v5r1_ravb7XPoC_r4851fCyjczhMOKKljB4q6hQzKCjoTQVmgB3TJLkggoTRlsWThcVr4ZdW7mWcZjKTMhPKlDmObHKbkqEhCUqmexirxU5LQdAbEemTSzSmxqaaRiKXSgnJhStsFi_JFKgfaaz-0FSh7kd-7-6YdJypchBmODgHyPMDAIAIf6DNoHjsbYq3ztm6mfleHxkACzFdNaDwU-OxibEufhWEtd4Z8MLoJCndGHGH-PxK3mnG1o8R28lG1TM_2IDe6E7EgN9AJurfE3oObTRTbV1m--Q8)
## Operations:
According to the comments on the [sandbox version](https://codegolf.meta.stackexchange.com/questions/2140/sandbox-for-proposed-challenges/26150#26150) it is allowed to split operations into smaller suboperations
Atomics:
* `#` push zero
* `0`-`9` pop number multiply by 10 add digit
* `$` pop two numbers push their sum and difference
* `:` duplicate top number
* `,` swap top two numbers
* `.` drop top number
simulating operations from requirements:
* `#` followed by sequence of digits pushes a number
* `$.` pops two numbers and pushes their sum
* `$,.` pops two numbers and pushes their difference
[Answer]
# [Gema](http://gema.sourceforge.net/), ~~206~~ 203 characters
```
<N>=@push{s;$1}
+=@set{s;@add{$s;@pop{s}$s}}
-=@set{t;$s}@set{s;@sub{@pop{s}$s;$t}}
d=@push{s;$s}
r=@set{t;$s}@set{n;@pop{s}$s}@set{s;$t}@push{s;$n}
s=@pop{s}
 =
\Z=@f{}
f:=@cmps{${s;};;;;$s @pop{s}@f{}}
```
As I like `dc`, borrowed its commands: `d` dup, `r` swap, `s` drop.  
The stack is dumped top to bottom.
Sample run:
```
bash-5.2$ echo '1 2 + d 3 4 - 5 r 6 7 8 s s 9' | gema '<N>=@push{s;$1};+=@set{s;@add{$s;@pop{s}$s}};-=@set{t;$s}@set{s;@sub{@pop{s}$s;$t}};d=@push{s;$s};r=@set{t;$s}@set{n;@pop{s}$s}@set{s;$t}@push{s;$n};s=@pop{s}; =;\Z=@f{};f:=@cmps{${s;};;;;$s @pop{s}@f{}}'
9 6 -1 5 3
```
[Try it online!](https://tio.run/##XYw9DsIwDIV3nyKDN5SBn4kQlBNwAMRSSAtLS1Snk@WzBwOhILw8Pfl737Xtm1J2h70PaaIbk8OlwMIHarOW0MTIqJnuiUmQRMC@n9lp@2A0nXlmHGbF4tdIAuP/aPhxVovO5skgQL4SYDycjj50LNBtfbj0iRiVEqeHZCr3BKSUzcqsjTWvGI19AA "Gema ‚Äì Try It Online") / [Try all test cases online!](https://tio.run/##lVLBjtMwED3XXzFEZt2yirJp2QOkRuEA0l5AghtJtMrG7rbabhJlXISU9beXsZOmgNjD5mDPzHtvxn7OXYnbo9LVvuw0hB/BaDS3VYlaztksEzEs4VIUUqzEkKJLhviKsBBWAx7HvvgOFIQuvxIn@crDPhhK6tQzHksrKlxD58dQsBRswVjb7WqzAfEEr8O31wj/2Ze05rUAcVO3B0P7N42HvQs@/Wp1ZbSi8OuDYGzTdLBzJFoh4P2r6ZpZWtggAdWwWefVMuBziJrWRPf6sYywq3wA4rj@8kGm7QG3PSY8tuxSpqgNJWmpVM9pb5u2R8vRWpaHA2oSSk88PNz1Eynhhnjq3BIt6/4V1X80HbuQbJLUlqEcGQwky3/IdNNbtnkv0@qxxZ4Tyyb0cYSR5wj2KGC9XpMV3pUAFgFzBpT0eiXsd2hGePAkYDNXk3Mgb24@f5dckO8JmLIaeb3DszfkJSxg4Vr91B1qdTuZembQpBc8Lin9GaPID43yvKYh08kcfn7M4TqFJ8whyzzvr5MEIOVzmqKAiwvQ1bbxv40zRTW1Pv4G "Bash ‚Äì Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 131 129 bytes
```
k[99],*s=k,t;main(p){
for(;p=getchar()-64;*s=p<0?*s*8+p+16:p<4?p=*s--,t=*s,p:p<6?*s+t:p<8?-*s:!++s);
for(;s>k;printf("%d ",*s--));}
```
[Try it online!](https://tio.run/##JYzNCoJAFEZfpYJgfqFRE3U0E5U2QqtW0YBMaSLZ4MwuenYbbXXv@e65n6StlNPUX8PwRpBOemL4q@4GoOCneY@AA5W0DyOf9Qgg9T3IraTiXYo0CrDCzI9U7KUqQZpSYuwgyia@vWNjlyClSEdrjDXkS58@9FyN3WAasNneVxsyP0LIv9MkmHCyQrD8XGSCORZOpXDnhFVZUZUzWkW44s@L7dpgn1/K4w8 "C (gcc) ‚Äì Try It Online")
Inspired by [Python answer](https://codegolf.stackexchange.com/a/267443/152), saved 23 bytes by implementing keywords as primitives:
***Primitives***
* octal-digit: multiply stack top by 8, add digit value.
* swap-pop: swap top two items, pop new top into temp register.
* add: add temp register to stack top
* negate: negate top of stack
* push-zero: push a 0 onto top of tack
These primitives are mapped to to ASCII ranges, which allow us to build the following operations with nonsense names.
**OPERATION KEYWORDS:**
* `^` = Number entry, should be followed by octal-digits
* `AD` = Add (swap-pop;add)
* `AGE` = Sub (swap-pop;negate;add)
* `CUE` = Swap (swap-pop;push-zero;add)
* `LADLE` = Dup (push-zero;swap-pop;add;push-zero;add)
* `CODA` = Drop (Swap;swap-pop)
* `@` = terminate and print
USAGE:
```
$> echo "^1^2AD^1CODA^12^2AGE^3AD^9LADLEAGE^1^2^3^1LADLE^2AD^3^2^5CUE@" | ./a.out
2 5 3 3 1 3 2 1 0 11 3 
```
---
previous:
# [C (gcc)](https://gcc.gnu.org/), 244, 229, 223, 204, 161, 154 bytes
*thanks to @ceilingcat for -7*
```
k[99],*s=k,t;main(p){for(;p=getchar()-42;*s=t=p<0?!++s:p>5?8*t+p-6:p>4?*--s:p>3?
*s++:p>2?*--s-t:p>1?p=*--s,*s++=t,p:*--s+t);for(;s>k;printf("%d ",*s--));}
```
[Try it online!](https://tio.run/##jZDNToNAHMTvPMXYaIRdthRojQUpaaIHL62x9WRM3AKlpC1sYElj1Gevu9AHMHuZ38z89ytheZKcHYLly9PrfP28XKwCA2D4VCvCoj1ushpZKeuvANvqcKhOOBVyhyqR/IC0yAvZ9ANUD8zTtCemadVuerI7OnHR41DjY3shp6O6uiDRuM7qY1FymYGXKURdlNIgznn/Pp1@2KSJ9rYMj7woTWF9b6vaDEWUZzLZ8dq02NgLVUVG4mEUX1HaBGI2ie@JpILdKT2OCWPa9GPSUKqE1zlMKunGItJg6yiStgg0UWmF3THNbB92t9mag5sUA1VjzLLCX/2Hbw3Ps8C4niFLdhVu4cKjcB24HjwGn2I6ZNqED3eoM1/piU1u8QM4HFUrYShH@arRpS5GcLXUz//3huc/ "C (gcc) ‚Äì Try It Online")
**OPERATIONS:**
* = Number entry: follow with octal digits
* `+` = Add
* `-` = Sub
* `,` = Swap
* `.` = Dup
* `/` = Drop
* `*` = terminate and print
**Usage:**
```
$> echo ' 1 2+ 1/ 12 2- 3+ 9.- 1 2 3 1. 2+ 3 2 5,*' |  /a out 
2 5 3 3 1 3 2 1 0 11 3 
```
How it works: `k` is the stack storage and `s` points to the top. `p` is the next program character from standard input.  
`p-42` maps the range "`*+,-./01234567`" into `0..14`.
A '`*`' (0) exits the loop. Otherwise, it's a big if-else statement using ternary operators.  
Any ASCII character below '`*`' starts a number entry by pushing a 0 onto the stack.  
Characters from '`0`' upward are converted to octal digits and added to 8\* the stack top `t`.  
The remaining 5 characters manipulate the stack pointer and produce the value that should be on top.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 48 bytes
```
Ôº∑Ôº≥‚â°¬ßŒπ¬π+‚äûœÖ‚Å∫‚äüœÖ‚äüœÖ-‚äûœÖ‚Åª‚äüœÖ‚äüœÖr‚âî‚äüœÖŒπu‚äûœÖ‚Ü®œÖ‚Å∞wÔº¶Ôº•¬≤‚äüœÖ‚äûœÖŒ∫‚äûœÖÔº©ŒπÔº©œÖ
```
[Try it online!](https://tio.run/##fY@5CsMwEER7fYVwtcI25OjiykmVImDIFwifIkISOuJAyLcraxNsV2l22Nk3A1sP3NaayxjHQciWwlWZ4O/eCtUDY9SNwtcDhdJfVdO@QGR0j/ab1Ny1NEmTE62CGyBktJLBQaUNBIbLrIwVPzDfgDeh/pAWydI50asFEcsxbGrO6Ey6W7MjnjttKdy4gcPavWQeiDZtx4P0a9GFOw9iavmQCh/3MDsYLGI8kj1xIzeksRpHMORAUhLzp/wC "Charcoal ‚Äì Try It Online") Link is to verbose version of code. Takes a list of newline-terminated words as input (could save two bytes by requiring symbols). Explanation:
```
Ôº∑Ôº≥‚â°¬ßŒπ¬π
```
For each input string, switch on its second character.
```
+‚äûœÖ‚Å∫‚äüœÖ‚äüœÖ
-‚äûœÖ‚Åª‚äüœÖ‚äüœÖ
r‚âî‚äüœÖŒπ
u‚äûœÖ‚Ü®œÖ‚Å∞
wÔº¶Ôº•¬≤‚äüœÖ‚äûœÖŒ∫
```
Handle `+`, `-`, `drop`, `dup` and `swap` respectively.
```
‚äûœÖÔº©Œπ
```
Anything else is assumed to be a number to be pushed to the stack.
```
Ôº©œÖ
```
Output the final stack.
[Answer]
# [Pip](https://github.com/dloscutoff/pip) `-p`, 50 bytes
```
V"lPU:".{@a?a(a."POl+:POl 0l@>:2 @l lPK1"^sa)}MJgl
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m724ILNgWbSSboFS7IKlpSVpuhY3jcKUcgJCrZT0qh0S7RM1EvWUAvxztK2AhIJBjoOdlZGCQ45CToC3oVJccaJmra9Xeg5Up3W1NZeJkUJBaXGGgokRV6YChGnAZaCtoM1loKugy2VgqJBSlF_AZWCkkFIKpIwVissTC7isayFGbIpWMlbSUTICYlMgNjCGOwsA)
Takes commands as separate command-line arguments. Outputs the final stack as a list, top first. The commands are as follows:
```
42  push nonzero number
i   push 0
0+  +
0-  -
01  drop
02  dup
03  swap
```
### Explanation
As usual for language interpreters in Pip, this is a translate-and-eval solution. Since `l` is preinitialized to the empty list, we'll use that to store the stack. The overall logic of the program is:
```
V"lPU:".{...}MJgl
               g   ; List of command-line args
             MJ    ; Map this function to each and join into a single string:
        {...}      ;   Translation code (see below)
 "lPU:".           ;   Prepend lPU: to each translated command
V                  ; Evaluate
                l  ; Print the final value of l (formatted nicely by -p flag)
```
Inside the translation function:
```
@a?a(a."POl+:POl 0l@>:2 @l lPK1"^sa)
@a                                    ; First character of command
  ?                                   ; Is it truthy (nonzero)?
   a                                  ; If so, return unchanged (this is a push command)
    (                             a)  ; Otherwise, treat the command as an index into...
     a."                       "      ;   Prepend the command to this string
                                ^s    ;   and split on spaces
```
which boils down to the following translation table:
```
42  lPU:42          ; Push 42 onto l
i   lPU:i           ; Push i (preinitialized to 0) onto l
0+  lPU:0+POl+:POl  ; Pop number, add 0, pop another number, add, push result
0-  lPU:0-POl+:POl  ; Pop number, subtract from 0, pop another number, add, push result
01  lPU:0l@>:2      ; Push 0, then remove first two elements of l
02  lPU:@l          ; Push first element of l
03  lPU:lPK1        ; Remove element at index 1, then push it
```
[Answer]
# [Haskell](https://www.haskell.org/), 143 bytes
```
([]#).words
(a:b:s)#(c:r)|c=="s"=(b:a:s)#r|c=="+"=(a+b:s)#r|c=="-"=(b-a:s)#r
(a:s)#(c:r)|c=="d"=(a:a:s)#r|c=="x"=s#r
s#(n:r)=(read n:s)#r
s#_=s
```
[Try it online!](https://tio.run/##VczLCsIwEIXhvU8xJC5SSsQL3QTmSVRkmrRaTGPJCHbhsxsbu1C3P985F@Jr431q8ZDU/iiL1eMWHS8UmdpwIZU1sXhaRMECVW0ox/gJ5RSorL9BZ6FnkQ/@5i7r3/kokCfHUoXJoIoNOQjzmOUJOfXUBRxiF@7LVmxgC3q3rmCEEhxUwKBFetnW05mTtsPwBg "Haskell ‚Äì Try It Online")
## Operations
* `+` Add
* `-` Subtract
* `s` Swap
* `d` Dupe
* `x` Drop
* Everything else is interpreted as a number
All commands are separated by spaces, and this code does no error checking.
[Answer]
# [Vyxal 3](https://github.com/Vyxal/Vyxal/tree/version-3), 29 bytes
```
‚∏†+‚∏†-‚∏†:‚∏†$‚∏†_W‚Çà√ó¬£?·πÇ(nnK[‚åä|O¬•iƒñ]W
```
[Try it Online! (link is to literate version)](https://vyxal.github.io/latest.html#WyJsIiwiIiwiKC4gYWRkKSAoLiBzdWIpICguIGR1cCkgKC4gc3dhcCkgKC4gcG9wKSB3cmFwIG5lZy0xICogc2V0LXJlZ1xuJ2ZvciAoaW5wdXQgc3BsaXQtc3BhY2VzKVxuICBuIG5cbiAgKGlzLW51bT8pID8gZmxvb3IgOiBvcmQgZ2V0LXJlZyBpbmRleCBjYWxsXG5jbG9zZS1hbGwgd3JhcCIsIiIsIjMgNSA2IGkgaSIsIjMuMy4wIl0=)
Wow, the SBCS really doesn't do the beauty of literate mode justice:
```
(. add) (. sub) (. dup) (. swap) (. pop) wrap neg-1 * set-reg
'for (input split-spaces)
  n n
  (is-num?) ? floor : ord get-reg index call
close-all wrap
```
Command set is:
```
i -> add
j -> subtract
k -> duplicate
l -> swap
m -> pop
[0-9]+ -> number
```
## Explained
```
‚∏†+‚∏†-‚∏†:‚∏†$‚∏†_W‚Çà√ó¬£?·πÇ(nnK[‚åä|O¬•iƒñ]W¬≠‚Å°‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å£‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å£‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å§‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å°‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å¢‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å¢‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å£‚Äã‚Äé‚Äè‚Äã‚Å¢‚Å†‚Å°‚Äå‚Å¢‚Å§‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å£‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å°‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å£‚Å°‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å¢‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å£‚Å¢‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å£‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å§‚Å§‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å£‚Å£‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å°‚Å™‚Äè‚Å†‚Å™‚Å™‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å£‚Å§‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å¢‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å£‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å§‚Å°‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å°‚Å§‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å§‚Å¢‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å¢‚Å™‚Äè‚Å†‚Å™‚Å™‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å§‚Å£‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å£‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å§‚Å§‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å¢‚Å§‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å°‚Å°‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å°‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å¢‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å°‚Å¢‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å£‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå‚Å¢‚Å°‚Å£‚Äã‚Äé‚Äè‚Äã‚Å¢‚Å†‚Å°‚Äå‚Å¢‚Å°‚Å§‚Äã‚Äé‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å£‚Å§‚Å™‚Äè‚Å†‚Äé‚Å™‚Å°‚Å™‚Å†‚Å™‚Å¢‚Å§‚Å°‚Å™‚Äè‚Äè‚Äã‚Å°‚Å†‚Å°‚Äå¬≠
‚∏†+‚∏†-‚∏†:‚∏†$‚∏†_W                    # ‚Äé‚Å°Push a list of:
‚∏†+                             # ‚Äé‚Å¢  lambda x, y: x + y
  ‚∏†-                           # ‚Äé‚Å£  lambda x, y: x - y
    ‚∏†:                         # ‚Äé‚Å§  lambda (stack): stack.push(stack[-1])
      ‚∏†$                       # ‚Äé‚Å¢‚Å°  lambda (stack): stack[-2], stack[-1] = stack[-1], stack[-2]
        ‚∏†_                     # ‚Äé‚Å¢‚Å¢  lambda (stack): stack.pop()
# ‚Äé‚Å¢‚Å£Technically, all these lambdas are monadic (take one argument) so far.
           ‚Çà√ó                  # ‚Äé‚Å¢‚Å§Multiply each lambda by -1 to make them operate on the stack
             ¬£                 # ‚Äé‚Å£‚Å°And put the stack lambda list in the register
              ?·πÇ               # ‚Äé‚Å£‚Å¢Split the input on spaces,
                (              # ‚Äé‚Å£‚Å£and to each command `n` in that,
                 nn            # ‚Äé‚Å£‚Å§  Push two copies of `n`
                   K           # ‚Äé‚Å§‚Å°  and test whether the second copy is numeric
                    [‚åä         # ‚Äé‚Å§‚Å¢  if so, simply evaluate and leave on the stack
                      |        # ‚Äé‚Å§‚Å£  otherwise:
                       O       # ‚Äé‚Å§‚Å§    get the unicode code-point of `n`
                        ¬•i     # ‚Äé‚Å¢‚Å°‚Å°    index into the register
                          ƒñ    # ‚Äé‚Å¢‚Å°‚Å¢    and call the pushed lambda.
# ‚Äé‚Å¢‚Å°‚Å£That's the reason the command set was chosen - the ord value mod 5 corresponds to the lambda. Any set of 5 characters could have been chosen, but `i` was the closest to `d`, which I knew to be `100`.
                           ]W  # ‚Äé‚Å¢‚Å°‚Å§Close all structures, and wrap the stack when finished.
üíé
```
Created with the help of [Luminespire](https://vyxal.github.io/Luminespire).
[Answer]
# [Haskell](https://www.haskell.org/), 99 bytes
```
foldl(!)[]
s!c|c<0= -c:s|1<2=[j(+),j(-),\(n:x)->n:n:x,\(a:b:x)->b:a:x,tail]!!c$s
j o(a:b:x)=b`o`a:x
```
[Try it online!](https://tio.run/##LcmxDoMgFEDR3a@QxAFSXgJ2I9IfQRPxtVotBVIcOvjtpaTpdHNy7zY9bs7lJeg@z8FdHSXMDFUieGAndA2o0iG7VpuNnhjfKDDeU6/eDC5elRZZNf08KVu829UNhGCTqq0O/6mnMYzl5qddvY6v1e/NEgxIwaHlksOZiyF/cHZ2SRkwxi8 "Haskell ‚Äì Try It Online")
Takes a list of integers as input, where:
* `0`: sum
* `1`: subtract
* `2`: dup
* `3`: swap
* `4`: pop
* `-n`: push n
Prints the stack as a list with the top element first.
---
This works by building a list of functions and using the command integer to index into this list. Negative commands are managed separately.
The definitions of `dup`, `swap`, and `pop` are fairly straightforward, but rather than defining two separate functions for `sum` and `subtract` I've defined a single function `j` that takes an operator as an extra argument and applies this operator to the first two elements of the stack. Passing `(+)` and `(-)` to `j` then does the trick.
[Answer]
# [PowerShell Core](https://github.com/PowerShell/PowerShell), 118 bytes
```
switch -r($args){\D{$a,$b,$s=$s}s{$s=$b,$a+$s}[-+]{$s=,("$b$_$a"|iex)+$s}u{$s=$a,$a,$b+$s}r{$s=,$b+$s}\d{$s=,$_+$s}}$s
```
[Try it online!](https://tio.run/##bU5Nb4JAEL3zKyZkUyGyjWh66MGExLbXNtWbGrPCWjFY6A5EE@S30x02oBj3sm/ex8zL0pNUuJdJwsNUyZrtpmWNpzgP98CVw4T6QbdcvZVMeGzrMZwyrLCkX09iqKclH66J8BybbdmGCfsSy7NLUtEYdZLCRKjGaPAqMsOGhophXVlW4FignweBY/swhqHtgT2x3Vs2UmlGdI8daTOHiQn4/q32ClGRASdh1N80hkmzXwP/7oZOtNf7GplfAE@i6UCY6rlwgY9UvYtwzz@3BxnmUDYZlkvMZwKlB0yeMy3ICKbANn2VqA5zzJI4hwEMLONSEosk154ntoOg9d2LLeKHNP69ppdf81mBeXo0vdaBKUZvXoShRAQKd@W4/GtXPS9UfHTczr942LY719q@TQ@4dmqkyqqs@h8 "PowerShell Core ‚Äì Try It Online")
Takes an array in parameter containing the instructions, returns an array top to bottom.
Explanation for the previous version, but the idea is still more or less the same:
```
switch -r($args){                       # for each of the instruction switch as regular expressions:
s{$a,$b,$s=$s;$s=$b,$a+$s}              # s for Swap
u{$b,$s=$s;$s=$b,$b+$s}                 # u for dUp
r{$b,$s=$s}                             # r for dRop
[+-]{$a,$b,$s=$s;$s=,("$b$_$a"|iex)+$s} # + or - for subtract and sum
\d{$s=,$_+$s}                           # to push a number
}                                       # end of the foreach
$s                                      # return the stack
```
[Answer]
# [Cabsi](https://github.com/ConorOBrien-Foxx/Cabsi), 173 bytes
```
1PUSH NULL
2GETW
3JNL 8
4DUP
5YEET
6ORD
7GOTO $1
8JNL 64
9PRINT
10GOTO 8
33POP
34GOTO 2
36SWAP
37GOTO 2
43ADD
44GOTO 2
45SUB
46GOTO 2
57YOINK
58MAKEI
59GOTO 2
62DUP
63GOTO 2
```
[Try it here!](https://conorobrien-foxx.github.io/Cabsi/?code=1PUSHwwNULLwa2GETWwa3JNLww8wa4DUPwa5YEETwa6ORDwa7GOTOwwx101wa8JNLww64wa9PRINTwa10GOTOww8wa33POPwa34GOTOww2wa36SWAPwa37GOTOww2wa43ADDwa44GOTOww2wa45SUBwa46GOTOww2wa57YOINKwa58MAKEIwa59GOTOww2wa62DUPwa63GOTOww2&input=10ww2wwx19ww3wwx17wwx1qdupww2wwx17wwx10sx3bapwwx1qdupww0wwx10sx3bapwwx19wwx10sx3bapwwwxdrop) Implements dup (`>`) add (`+`) sub (`-`) drop (`!`) and swap (`$`). Prints stack top-to-bottom. Expects instructions to be separated by spaces/newlines.
Technically, only the first character of non-numeric instructions are parsed, so the "literate" input `10 2 - 3 + >dup 2 + $swap >dup 0 $swap - $swap !drop` works just as well as the minified `10 2 - 3 + > 2 + $ > 0 $ - $ !`.
## Explanation
As Cabsi is a stack language, the code can implement a stack-based language using its own stack. Thankfully, since the target language is not very complex, there is not a lot of overhead we need.
```
1 PUSH NULL     REM Stack work delimiter
REM ReadLoop:
2 GETW          REM Read word from STDIN
3 JNL 8         REM If end of input, jump to PrintLoop
4 DUP           REM Duplicate token read
5 YEET          REM And place it on the secondary stack
6 ORD           REM Get Ord(Token[0])
7 GOTO $1       REM Goto the line specified by that ordinal
REM PrintLoop:
8 JNL 64        REM If work delimiter reached, end program
9 PRINT         REM Print top of stack 
10 GOTO 8       REM Jump to PrintLoop
REM If "!" (ord 33):
33 POP          REM Pop top of stack
34 GOTO 2       REM Jump to ReadLoop
REM If "$" (ord 36):
36 SWAP         REM Swap top two on stack
37 GOTO 2       REM Jump to ReadLoop
REM If "+" (ord 43):
43 ADD          REM Add top two on stack
44 GOTO 2       REM Jump to ReadLoop
REM If "-" (ord 45):
45 SUB          REM Subtract top two on stack
46 GOTO 2       REM Jump to ReadLoop
REM If "0" through "9" (ords 48 through 57)
57 YOINK        REM Pull the full representation from the secondary stack
58 MAKEI        REM Cast to integer
59 GOTO 2       REM Jump to ReadLoop
REM If ">" (ord 62)
62 DUP          REM Duplicate top on stack
63 GOTO 2       REM Jump to ReadLoop
```
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck), ~~684~~ 649 bytes
```
,[<<--[>+<++++++]>-[->-<]>[[<]+[>]<-]<<<<<<<<<<<<<<<<[[->]<<<<<<<<<<<<<<<<]>[->+<]>[->+<]>[->+<]>[->+<]>[->+<]>[->+<]>[->+<]>[->+<]>>[-]<[->+<]>[<+[,<<++++[>++++++++<-]>[->-<]>[---------------->[-<++++++++++>]<[->+<]<+>]<[->+<]>]>>>>>>>>>[>]+[<]<<<<<<<-[->>>>>>>>[>]<+[<]<<<<<<<]>->->->->-<<<<<]>[->>>>>>>>[>]<<[->>+<<]>[-<+>]>[-<+>]<[<]<-<-<-<-<-<]>[->>>>>>>[>]<[-]<[<]<-<-<-<-<]>[->>>>>>[>]<[-<->]<[<]<-<-<-<]>[->>>>>[>]<[->+>+<<]>>[-<<+>>]<<<[<]<-<-<]>[->>>>[>]<[-<+>]<[<]<-<]>,]>>[[<++++++++++>[-<-[<+<<]<[+[->+<]<+<<]>>>>>]<[-]-[>+<-----]>---[-<<+>>]<<-<<<<<[<]+[>]>>>>[-<<<<<[<]>+[>]>>>>]<[->>>+<<<]>>>]<<<<<<<[<]>[.[-]>]++++++++++.[-]>>>>>>>]
```
[Try it online!](https://tio.run/##nVLLDgIhDPyVvbM1Pq5Nf6ThoCYmxsSDid@P00KB1ZvlANuWmeksl9f5/ry9r49SVmUmUkmcPLKQkhBnUeWcVDJT5q9QtPwkcYMA88eGPXOkOOnKLgaiWkCChCr6CmQ49ZAA4nEUMLTAOAljNcU26SjwVIELscZsU6shA9rzxtQ2NgTqa7qlrmbbMMq1yrRB6GVtk1RCowKX@x/N0dpwhpIsq93Q2SEjQgJYrCnMcmSRqtKfg5sLH/A4OmE1o70LJ@wZiZRrdXMcMhy1Ft0BHH@gh3/XyKUc9stxoeW0pA8 "brainfuck ‚Äì Try It Online")
All numbers have to be followed by a space. Uses `+-,./` for `add`, `sub`, `dup`, `drop`, and `swap` respectively.
Prints stack from bottom to top separated by newlines.
I confess this is a touch over-engineered, and could be shorter if I used less human-readable I/O.
## With Comments
```
READ AN INSTRUCTION
IF ITS A NUMBER THEN GRAB ALL OF IT UNTIL THE SPACE
OTHERWISE ITS A REGULAR INSTRUCTION
ITS ACTUALLY EASIER TO CHECK IF ITS A REGULAR INSTRUCTION THAN A NUMBER BECAUSE THERE ARE MORE NUMBERS THAN INSTRUCTIONS
SO WE CHECK THAT FIRST
>>>>>>>>>>>>>>>>>>>>>>>>>>>> THIS LINE MAKES DEBUGGING EASIER BUT ISNT NEEDED
,[
    <<--[>+<++++++]>-
    SUBTRACT 42 FROM INP
    [->-<]
    COLLAPSE IT LEFT
    >[
     CLEAR THE ONE AT NEGATIVE SIXTEEN
     THIS IS A GUARD VALUE
     <<<<<<<<<<<<<<<<[-]>>>>>>>>>>>>>>>>
    [<]+[>]<-]
    SUM UP THE FIRST NINE NUMBERS AS THESE ARE THE DIGITS
    WE IGNORE 0 AS ITS NOT USEFUL
    <<<<<<<<<<<<<<<<
    IF THE GUARD IS TRIPPED THEN WE DISCARD THE WHOLE STACK
    [[->]<<<<<<<<<<<<<<<<]>
    [->+<]>[->+<]>[->+<]>[->+<]>[->+<]>[->+<]>[->+<]>[->+<]>
    >[-]<[->+<]> I PUT IT IN THE ZERO ANYWAY FOR SANITY
    IF THIS IS TRUTHY THEN ITS A NUMBER THAT WE NEED TO PUSH
    IN SUCH CASE WE NEED TO KEEP GRABBING DIGITS UNTIL WE SEE A 32 SPACE
    [
        <+[
            ,
            32
            <<++++[>++++++++<-]>
            SUBTRACT FROM INP
            [->-<]
            >
            [
                IF ITS STILL TRUTHY THEN ITS A NUMBER (PROBABLY)
                SUBTRACT 16 THAN MULTIPLY BY TEN ONTO THIS
                ----------------
                >[-<++++++++++>]
                AND COPY BACK
                <[->+<]
                <+> SET THE NOT FLAG
            ]
            COPY THE NOTFLAG UNTO HERE TO CHECK IF WE NEED TO LOOP
            <[->+<]>
        ]
        GO SIX RIGHT AND PUSH A NEW VALUE TO THE STACK
        >>>>>>>>>[>]+[<]<<<<<<<-
        [->>>>>>>>[>]<+[<]<<<<<<<]
        >->->->->-
        <<<<<
    ]
    > IF THIS IS POSITIVE THAN ITS A SWAP INSTEAD
    [
        WE USE SCRATCH SPACE NEXT TO THE NUMBERS TO MAKE SWAPPING THEM POSSIBLE
        -
        >>>>>>>>[>]<<
        COPY THIS NUMBER TWO ACROSS
        [->>+<<]
        >[-<+>] COPY THE NEXT NUMBER INTO THIS PLACE
        >[-<+>] AND THIS ONE TOO
        <[<]<-<-<-<-<-< AND GO HOME
    ]
    > IF THIS IS POSITIVE THAN ITS A DROP
    [
        -
        >>>>>>>[>]< GO TO THE TOP OF THE STACK
        [-] CLEAR IT
        <[<]<-<-<-<-< AND GO HOME
    ]
    > IF THIS IS POSITIVE THAN ITS A SUB
    [
        -
        >>>>>>[>]< GO TO THE TOP OF THE STACK
        [-<->] SUB IT FROM THE LAST
        <[<]<-<-<-< AND GO HOME
    ]
    > IF THIS IS POSITIVE THAN ITS A DUP
    [
        -
        >>>>>[>]< GO TO THE TOP OF THE STACK
        [->+>+<<] COPY IT ACROSS THE NEXT TWO
        >>[-<<+>>] COPY THE ONE BACK TWO
        <<<[<]<-<-< AND GO HOME
    ]
    > FINALLY IF THIS IS POSITIVE THAN ITS ADD
    [
        -
        >>>>[>]< GO TO THE TOP OF THE STACK
        [-<+>] ADD IT UNTO THE LAST
        <[<]<-< AND GO HOME
    ]
    >,
]
ONCE WERE DONE WE NEED TO PRINT ALL VALUES ON THE STACK
>>
[
    GET ALL THE DIGITS
    [
        <++++++++++>
        [-<-[<+<<]<[+[->+<]<+<<]>>>>>] DIVMOD
        <[-] UNNEEDED VALUE
        SET TO 48 AND ADD TO THE MOD VALUE
        -[>+<-----]>---
        [-<<+>>]
        <<
        PUSH THIS VALUE TO THE NUMBERSTACK
        -<<<<<[<]+[>]>>>>
        [-<<<<<[<]>+[>]>>>>]
        <[->>>+<<<] MOVE THE NEW VALUE BACK
        >>>
    ]
    PRINT THEM ALL OUT
    <<<<<<<[<]>[.[-]>]
    PRINT A NEWLINE AS WELL ++++++++++.[-]
    >>>>>>> NEXT NUMBER
]
```
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 50 bytes
```
T‚Üê{(‚ç∫‚ç∫2‚Üë‚çµ),2‚Üì‚çµ}
p‚Üê+/T
m‚Üê¬Ø1‚àò‚ä•T
x‚Üê‚äÉ,‚ä¢
s‚Üê‚åΩT
d‚Üê1‚àò‚Üì
‚éï‚Üê‚éï
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v8/5FHbhGqNR727gMjoUdvER71bNXWAjMlARi1XAVBWWz@EKxdIH1pv@KhjxqOupSFcFUDuo65mnUddi7iKQeyevSFcKUAGWEXb5P//CxQMdYx0HvWu4UoBskB0gYKxTq6CkY6hAZibq1ChYAlmGQMVwpQY6VRAlRcrmALFjUFsAA "APL (Dyalog Classic) ‚Äì Try It Online")
* Programs start on the right with `‚ç¨` (empty array, called *zilde*) and run as their own lines, not as strings.
* `n,` pushes `n` onto the stack.
* `p`  adds the top two.
* `m`  subtracts the top from the second.
* `x`  duplicates the top.
* `s`  swaps the top two.
* `d`  drops the top.
Note that the spaces are necessary.
Polish notation is used, i.e. read right-to-left (opposite of the examples in the question), as APL itself is right-to-left.
The stack is stored and displayed top-to-bottom (opposite of the examples in the question).
E.g. using Polish notation `1 dup 2 +` becomes `p 2,x 1,‚ç¨` and outputs `3 1`
My first time using *dops* (user-defined operators) in APL. Doesn't seem too useful for golfing, unless you are defining multiple similar functions. `T` is a dop defined as `{(‚ç∫‚ç∫2‚Üë‚çµ),2‚Üì‚çµ}`. This is a monadic operator that takes a monadic function on the left, and applies it to the first two elements of the array on the right, then catenates with the rest of the array (you can even use this to define new stack operations).
Would love to know how this can be improved upon.
EDIT: There was a bug in my minus function that would negate the whole stack. None of the test cases noticed it though. Fixed now but had to add one byte.
[Answer]
# [Julia 1.0](http://julialang.org/), 90 bytes
```
f()=[]
f(+,b...;c=f(b...))=try c[[+;2:end]]catch;try[c[2]+c[1];c[3:end]]catch;[+;c]end;end
```
[Try it online!](https://tio.run/##TZDBasMwEETv@oqtTxJxTGS3hcY4PbXQQ79A6LCWZapiHCMrbgr993QlJxCDYGf0dmTm@zQ4lOfLpeeiUZr1fJO3RVHUpul5HIRogv8Fo9SmLvd27LQ2GMxXTa4yqtQbo6SujaruLwk2mnRN59L54wQNUHp3SoPMpWbzD0ax0AJj/dGDAzeCt9gNbrQzFwzow7wlaJ4GF7jLIdsesusF2XbBoeCfNmAxoZ9twVcQhRBQ/B0At3TAzQgfY4BXeB@OGJ4fCYA9YMqJ8Ys1PGbdRfGWMhLgCei5t4uNNorYCVD21UnM5N0YhpFjc/Drlutp8aGBNql7Jns7T9YE2@0hy6FdcaqJxaoklFAB/bVKk2YSYmerIZOMZUatWUXIE6Qeo1GRKCNSwmY1SOxIbSlydSRFvKyJSe9uD9xWJL35Dw "Julia 1.0 ‚Äì Try It Online")
input and output stacks are lists in reverse order.
It works by chosing clever ways to represent the commands, grouped in 3 types, detected with try/catch (shorter than multiple dispatch here)
* Arrays:
	+ drop: `[]`, will return `c[2:end]`
	+ dup: `[1,1]` will return `c[[1;1;2:end]]`
* Functions:
	+ +: `+`, returns `[c[2]+c[1];c[3:end]]`
	+ -: `-`, returns `[c[2]-c[1];c[3:end]]`
	+ swap: `vcat`, returns `[c[2];c[1];c[3:end]]`
* Floats:
	+ pushing numbers must be floats, otherwise they might not throw an error in the "array" part
]