isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
262 stars 57 forks source link

Added Operator Chaining #106

Closed Maltysen closed 9 years ago

Maltysen commented 9 years ago

One of the best features of Python is operator chaining. This was obviously missing in Pyth since we evaluate each operator individually. This is my attempt to fix this. Instead of the operator comparisons returning a Boolean, I instead made them return a custom OpChain object which contains the chain to expand as a string. When it needs to be used as a boolean, the overloaded __bool__ method evaluates the chain. The __str__ method also returns "True" or "False", making it virtually indistinguishable from a boolean.

There is probably a bug with this somewhere, so tell me if you catch one. I have written this for ==, !=, <, and > so far.

orlp commented 9 years ago

I actually propose getting rid of bools altogether, and only using 1 and 0.

This is orthogonal to chaining comparison though.

isaacg1 commented 9 years ago

About operator chaining: Can these objects be put in sets? That would be a useful feature. Does repr() work?

Nice this is a rather dramatic change that has a the potential to cause a lot of issues, I'd like to see / create some test cases.

Maltysen commented 9 years ago

Oh yeah I forgot about __repr__. Just had to replace __str__ with repr, now it works.

And yes it can be added to sets. {[qTT returns {True}

I also added >= which I forgot last time.

isaacg1 commented 9 years ago

Alright, my tests indicate that repr works, while set doesn't.

More importantly, <0<2 3 throws an error. I would expect it to return True, like 0<(2<3) in python.

isaacg1 commented 9 years ago

More generally, h<0 1 doesn't seem to work, nor anything else that treats a bool as 0 or 1.

Maltysen commented 9 years ago

Ah good point. I need to implement __int__ on it.

isaacg1 commented 9 years ago

Likewise, implementing __hash__ would be good because of:

py pyth.py -c '{[<01<01' {True, True}

isaacg1 commented 9 years ago

I think we'll also need to add the new type to is_num, and to replace all calls of isinstance(a, int) with some is_int method.

Maltysen commented 9 years ago

This really isn't going to happen anytime soon, especially with Pyth5 going on. Closing so I can do other pulls.