codeplea / tinyexpr

tiny recursive descent expression parser, compiler, and evaluation engine for math expressions
https://codeplea.com/tinyexpr
zlib License
1.61k stars 245 forks source link

Comparisons and logical operators #29

Open cschreib opened 7 years ago

cschreib commented 7 years ago

I've been using TinyExpr in my project and recently needed standard comparison operators (<, <=, >, >=) and boolean logic (!, &&, ||). I've just implemented this in my fork with this commit https://github.com/cschreib/tinyexpr/commit/c1b01d879ea5bb7d14a6529988327bb407641a9b and this one https://github.com/cschreib/tinyexpr/commit/4cdeb9150e3579349d4513795dfadd41a7dcb45a (adding == and !=).

Booleans are simply 0.0 for false and 1.0 (or any non-zero value) for true.

I've written a test suite to make sure that the precedence with respect to other binary operators is correct, and it all seems to work fine. There is one little thing that I do not understand though, is that I was expecting !!1 to produce 1, since --1 produces 1 in TinyExpr. I do not understand what the difference is, but !!1 currently produces 0 regardless of how many negation operators are used. It's no big deal for me, but it would be good if this worked to be consistent with the other unary operators.

Let me know if you're interested in merging this feature, then I'll prepare a pull request.

cschreib commented 7 years ago

Commit https://github.com/cschreib/tinyexpr/commit/9de3ed238a2a2d4e669b50b1f6dc159b15e0e951 fixes the issue with !!1 != 1. Thanks to @codeplea for spotting the mistake.

This commit also fixes the right-to-left version of TinyExpr (which I had broken badly by mistake), and will correctly optimize stuff like -!--!-a into -!!a (note: !! is not a no-op for numbers which are neither 0 nor 1: !!2 == 1).

godlikepanos commented 6 years ago

This seems like a useful feature. Have you considered making a pull request?

cschreib commented 6 years ago

I have considered it, but as per the guidelines set in https://github.com/codeplea/tinyexpr/blob/master/CONTRIBUTING I have opened an issue first to gauge interest. I interpret the subsequent lack of reply as a hint that this is not a desired feature, but I'm happy to be wrong :)

Also, my fork was based on another fork which included CMake build scripts. Adding these CMake scripts has been proposed here and rejected, so perhaps this is also a source of restraint.

codeplea commented 6 years ago

Sorry for the very late reply.

I will likely merge this into a branch, spend some time tweaking and adding documentation, and then merge into master. I've already created a pull request here.

Most of my hesitation was exactly as you expected. However, I've had people ask for both CMake support and logical operators recently, so I'm giving in. It only took a year. lol

Nice work!

cschreib commented 6 years ago

Excellent news! Glad you found it to be an interesting addition.

tanis2000 commented 5 years ago

It would be nice to see this branch get merged into master now. Are there any plans?

codeplea commented 5 years ago

It's merged into the "logic" branch. It needs documentation before going to master. It also needs test cases added.

I won't have time to work on it for a while. Feel free to help.

gfmartins commented 5 years ago

If I'm not wrong, the Operator Precedence, in the logical branch is wrong. In the branch the precedence is this: [1] > | < | >= | <= | == | != [2] AND | OR

it should be: [1] > | < | >= | <= [2] == | != [3] AND [4] OR

https://en.cppreference.com/w/c/language/operator_precedence

Blake-Madden commented 3 years ago

I forked a C++ version of this library that includes logic and comparison operators in case that is useful to anyone:

https://github.com/Blake-Madden/tinyexpr-plusplus

kdunn926 commented 2 years ago

It's merged into the "logic" branch. It needs documentation before going to master. It also needs test cases added.

@codeplea - There seem to be a reasonable number of test cases, were there others you had in mind for rounding out what is already there?

I'll work on adding some documentation.