dloscutoff / pip

Pip: an imperative code-golf language
https://dloscutoff.github.io/pipdoc/
MIT License
34 stars 5 forks source link

"Reverse operands" meta-operator #33

Open dloscutoff opened 3 years ago

dloscutoff commented 3 years ago

Sometimes, it would be useful to reverse the operands of a binary operator. For example, to yank an expression and then use it as the lower bound of a Range:

(Ya+b),c

There is currently no way to save the parentheses, unless it's possible to rearrange the program so the yank happens first and then the expression is y,c.

If ~ were a meta-operator that swapped an operator's left and right operands, we could do

c,~Ya+b

potentially saving a byte.

TBD: What does the meta-operator do when applied to a ternary operator?

chunkybanana commented 1 year ago

If implemented, can this also reverse argument evaluation order?

dloscutoff commented 1 year ago

Interesting thought! I can see how that would be useful sometimes. From a quick look at the code, it also appears that the easiest way to implement ~ would be to swap the first and second branches of the expression's parse tree before evaluating, which would indeed result in the rhs being evaluated first. I'll have to think some more about how that would work when combined with the : meta-operator, though.