[Question]
  [
Given an expression matching the regex `/^[-~]*x$/`, evaluate it in terms of `x` and output a string matching the regex `/^-?x[+-]\d+$/`.
For example, the string `-~x` evaluates to `x+1`, while the string `-~-x` evaluates to `-x+1`, and the string `-~-~--x` evaluates to `x+2`.
We start from `x` and evaluate the string right to left. `-` negates the term, while `~` converts `y` to `-y-1`.
Testcases:
```
    x  x+0
   ~x -x-1
  -~x  x+1
 ~-~x -x-2
-~-~x  x+2
--~~x  x+0
  ~-x  x-1
 -~-x -x+1
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"). Shortest answer in bytes wins.
The input/output format is strict. The `"x"` is mandatory.
  
[Answer]
# [Python 2](https://docs.python.org/2/), ~~42~~ ~~41~~ 39 bytes
```
lambda s,x=0:'-x%+d'[len(s)%2:]%eval(s)
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUaFYp8LWwEpdt0JVO0U9Oic1T6NYU9XIKlY1tSwxB8j@n5ZfpFCskJmnEK1eoa6jXgcidMFknS6UA6V166DCFeqxVlwKBUWZeSVA89V17dR10kBGAQA "Python 2 – Try It Online")
[Answer]
# [Retina](https://github.com/m-ender/retina), ~~47~~ 44 bytes
```
+`--|~~
((~)|-)*x
$&+$#2
T`+`-`^~.*
~
-
--
```
[Try it online!](https://tio.run/##K0otycxL/K@q4Z7wXztBV7emro6LS0OjTrNGV1OrgktFTVtF2YgrJAEolxBXp6fFVcely6Wry/X/fwVXXQWXLhDX6YIZYFK3DixQAQA "Retina – Try It Online") Edit: Saved 3 bytes thanks to @MartinEnder. Expanation:
```
+`--|~~
```
Delete pairs of adjacent duplicates.
```
((~)|-)*x
$&+$#2
```
Count the number of `~`s which gives us the magnitude of the term.
```
T`+`-`^~.*
```
If the first character is a `~` then the term should be negative.
```
~
-
--
```
If the number of `~`s and `-`s is odd then the `x` should be negative.
[Answer]
# JavaScript, 59 bytes
```
x=>['-'[x.length&1]]+'x'+['+'[(x=eval(x.join` `))<0^0]]+~~x
```
[Try it online!](https://tio.run/##TY2xjsIwEER7vuIq1pZjK/SY/r5hzwgr2YBRZHOO4VYp/OshCs0VMxrNPGnu/uWnLodH0TH1tAx2YXtC0IBsRorXctsfnFPAoBAUoGBLLz8KNvcU4uXrIuWxPbcrUisvVcz20GT6fYZMUkAm348hEkjTrbnQdyyUB9@ReOTU0TSZqfQhSpOigI1svD3929KzmL8cCgmEnwiIczPb1uH66NQg0BjjFYCTUi68q7zTq6rewua6fvzT8xs)
[Answer]
# [PowerShell Core](https://github.com/PowerShell/PowerShell), 102 bytes
```
$s=-1
switch -r($args[$args.Count..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a"|% *ce '+-','-'|% *ce '1x','x'
```
[Try it online!](https://tio.run/##bY5NT4QwEIbv/RUNGQWEIYtXQ0KyWa8a19tmo1i7okFZacmSsPDXseWjAbNzaDLPvPN0jvmJFyLlWYYsL3gHh6juQEQYEnH6lCylWDiQFB9i17/BOi9/ZBCs9m4d1JBECMmdzoNoWtUjNo0F4rXyILHOV/SGcWp7aPs22lMbVqqt7K4hJHYIVeXHjlVZPrUqb2W5BmHPcAnbEWI4T7bj@hIawRy3aBS3y/QkWWBs2wunjer/V1z8UJsRZ26Xnul9XmwSluLD2xdnktZ9GiQX0qfAq6OC/J1GFF6GScFFmUkFruFAY53r@e5xuy6FzL8Hzz4eRLq2JWNcCO0wPuS/k8rknpVLh4xT12Z2wbRthk/TLXNVQxrS/QE "PowerShell Core – Try It Online")
Passes an expression as a string using splatting  
Then processes it from right to left  
For each char, negate `$s` and `$a`, and if the char is `~` , decrement `$a`  
Then concatenate them all: `$s + "x+" + $a`  
Replace `+-` with `-`  
Remove the useless 1 before x
[Answer]
# [Perl 5](https://www.perl.org/), 75 + 1 (-n)= 76 bytes
```
map{$y=-$y;$y--if/~/}reverse/-|~/g;printf"%sx%+d",((-1)**y/-~//)=~s/1//r,$y
```
[Try it online!](https://tio.run/##BcFBCoMwEAXQu4QIah0@LlyJhyk0loDGMBHJYDtH7/S9HHibzPZnvr0s5GX2QhRXKL4crsAlgD6K95w5pnN1TanN4@WGtqWx63sBKdAtWjACPHgxU6q/I5/xSMUo/QE "Perl 5 – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 69 bytes
```
a;s;f(char*p){for(a=s=0;*p;a-=*p++%5*~-s)s^=2;printf("-x%+d"+!!s,a);}
```
[Try it online!](https://tio.run/##PY7LCoMwEEX3/QpNEfIwRQpdTdMfsRZCrDXQxmBSEER/PU1EujkzdxguR/GXUiFIcNBh1cuRWjJ3w4ilcKICakFyQS1jxYWu3BH3EGewoza@w4hPBWsRy3NXSgJLOGqj3t/2mV2db/Vw6m@H@Jh9pDaYzFu7rxsxowmVaE3gG1e@h33ydT9HVgskndSjo5CvdQOasSiJ007@MneDosMSfg "C (gcc) – Try It Online")
-1 byte thanks to @ThePirateBay
[Answer]
# [Ruby](https://www.ruby-lang.org/), 36 bytes
Port of [user48543‘s Python answer](https://codegolf.stackexchange.com/a/142941/11261).
```
->s{x=0
"-x%+d"[s.size%2..]%eval(s)}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsEiN9ulpSVpuhY3VXTtiqsrbA24lHQrVLVTlKKL9Yozq1JVjfT0YlVTyxJzNIo1a6FqI6PVK9R11OtAhC6YrNOFcqC0bh1UuEI9Vi81MTlDISWfS0GhoLSkWEFJtVhB105BtVhJQVUhOt5QR8ENSMbGcqXmpUAsWLAAQgMA)
[Answer]
# [><> (Fish)](https://esolangs.org/wiki/Fish), ~~91~~ 79 bytes
Was producing incorrect output, but for a change fixing this actually saved me bytes.
```
10>i:'-'=?v:'~'=?vv
 .\r*-10r~<1-@:$~/~
d3/"+"on; .3dv?(0r/
;n^?)-10:o"x"\"-"o/
```
[Try this](https://mousetail.github.io/Fish/#eyJ0ZXh0IjoiMTA+aTonLSc9P3Y6J34nPT92dlxuIC5cXHIqLTEwcn48MS1AOiR+L35cbmQzL1wiK1wib247IC4zZHY/KDByL1xuO25ePyktMTA6b1wieFwiXFxcIi1cIm8vIiwiaW5wdXQiOiItLX4tfngiLCJzdGFjayI6IiIsIm1vZGUiOiJudW1iZXJzIn0==)
[![enter image description here](https://i.stack.imgur.com/mlLSw.png)](https://i.stack.imgur.com/mlLSw.png)
[![enter image description here](https://i.stack.imgur.com/8gDrZ.png)](https://i.stack.imgur.com/8gDrZ.png)
Significantly harder to parse in reverse order. Basically produce a expression of the form `ax + b`
* For a `-`, set `a` to `-a`.
* For a `~`, set `b` to `b-a`, then re-use the same code to set `a` to `-a`.
Rest of the code is printing the solution. Special cases for printing the `-` in front of `x` and needing a `+` if the extra number is not negative.
[Answer]
# [05AB1E (legacy)](https://github.com/Adriandmen/05AB1E/tree/fb4a2ce2bce6660e1a680a74dd61b72c945e6c3b), 20 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
á0:.E…-x+ì„+-¤:IgÉi¦
```
Port of [*(deleted) user48543*'s Python answer](https://codegolf.stackexchange.com/a/142941/52210).
[Try it online](https://tio.run/##ASsA1P8wNWFiMWX//8OhMDouReKApi14K8Os4oCeKy3CpDpJZ8OJacKm//8tfi14) or [verify all test cases](https://tio.run/##MzBNTDJM/V9Waa@k8KhtkoKSfaXL/8MLDaz0XB81LNOt0D685lHDPG3dQ0usKtMPd2YeWva/Vud/tFKFko5SHYjQBZN1ulAOlNatgwpDRSuUYgE).
**Explanation:**
```
á      # Only keep the letters of the (implicit) input (the "x")
 0:    # Replace the "x" in the (implicit) input with "0"
   .E  # Evaluate and execute it as Python code
…-x+ì  # Prepend string "-x+"
„+-¤:  # Replace a potential "+-" with "-"
IgÉi   # If the length of the original input is odd:
    ¦  #  Remove the leading "-"
       # (after which the result is output implicitly)
```
[Answer]
# Java 8, ~~186~~ ~~168~~ 167 bytes
```
s->{s=s.replaceAll("--|~~","");int l,i=s.length()-(l=(s=s.replace("-~","")).length())>>1;return(l-(s=s.replace("~","")).length()>0?"-"+s+~i:s+"+"+i).replace("--","");}
```
Definitely room for improvements..
-1 byte thanks to *@ceilingcat* replacing `(a-b)/2` with `a-b>>1`.
[Try it online.](https://tio.run/##jZDBTsMwDIbveworp0SdK7gStYgHYBeOiEPowsjw0ipJJ9BoXr0YtaDBAao4Upx8@f3be3M02HbW77cvY0MmRrg1zp9WAM4nG55MY2HzmQLcpeD8Dho5H6LSfD/w5ojJJNfABjxUY8T6FKtYBtsRC9wQSYH4nrNYC6E0KwOtHQNk/S49S4WSKnn2g/GJVd@IqutLHWzqg5eEP@HfbH1xLVAUscjuKhaCl1Nn0jjZGEY9ee/6R2LvcwvH1m3hwEOY@7x/AKPmCbzFZA9l26ey46dEXvqykeKVi@s/ifw/gguYjIuEFlGYFxX8YobVMH4A)
**Explanation:**
```
s->{                            // Method with String as both parameter and return-type
  s=s.replaceAll("--|~~","");   //  Remove all "--" and "~~"
  int l,                        //  Temp integer to reduce bytes
      i=s.length()-(l=(s=s.replaceAll("-~","")).length())>>1;
                                //  Remove all "-~" and save count in `i`
  return(l-(s=s.replaceAll("~","")).length()
                                //  Remove all remaining "~"
         >0?                    //  and if its non-empty:
           "-"                  //   Start with a minus sign
           +s                   //   followed by the remaining `s`
           +~i                  //   followed by `-i-1`
          :                     //  Else:
           s                    //   Start with the remaining `s`
           +"+"                 //   followed by a plus sign
           +i                   //   followed by `i`
         ).replaceAll("--","");}//  And then remove all "--"
```
]