Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bitwise XOR (^), AND (&), OR (|) #18

Closed
wmiller848 opened this issue Jul 18, 2016 · 3 comments
Closed

Support bitwise XOR (^), AND (&), OR (|) #18

wmiller848 opened this issue Jul 18, 2016 · 3 comments
Assignees
Milestone

Comments

@wmiller848
Copy link
Contributor

It would be great if this could support bitwise operations.

https://en.wikipedia.org/wiki/Bitwise_operation
https://golang.org/ref/spec#Arithmetic_operators

I'm working on a project that generates expressions and I've looked at a few options, built in AST support from go/token, go/constant, go/types is good but getting it do this auto conversion is a pain. I started writing my own AST then I found your project and thought it would be a great fit.

Here you state

All numeric constants and variables are converted to float64 for evaluation

This would require something like float64(int64(variable) & int64(otherVariable)) when evaluating these bitwise expressions.

For 10.5 | 20.2 to just work in your expression evaluator would be super amazing

I'm not sure what to say about ^ (power) vs ^ (xor) but I'm sure there is a good solution!

Cheers!

@Knetic Knetic self-assigned this Jul 18, 2016
@Knetic
Copy link
Owner

Knetic commented Jul 18, 2016

These operators sound reasonable, but a note (and tests) will need to be made somewhere about the rounding strategy. If a user does 10.5 | 20.2, some might expect 10.5 to be rounded to 11 rather than truncated. Truncation makes the most sense, and if they want to round up they can do so before evaluation. But still needs tests/documentation somewhere (readme probably).

As for XOR, I backed myself a bit into a corner with that. I'm tempted to do an API break by turning ^ into XOR (to match C-style) and use ** for exponents (borrowing from Ruby).

I think for now I'd accept a PR that added |, &, and ~ (bitwise NOT) as new OperatorToken types, and a new TokenKind ("BITWISE") which evaluates before arithmetic operators. I'm not entirely sold on an API break for XOR (as mentioned above), and I'll have to look into other options. But at least with &, |, and ~ one can accomplish the same thing (even if it's a bit roundabout).

I probably won't be able to work on it this week, so if you (or anyone) would like to put together a PR with the above stuff, then I'd be down to accept it.

@wmiller848
Copy link
Contributor Author

First round for a PR.

This addresses my needs, lets talk about how it affects others.

#20

Cheers

@Knetic Knetic modified the milestone: 2.0 Jul 25, 2016
@Knetic
Copy link
Owner

Knetic commented Jul 26, 2016

Done as of c6a6abe

@Knetic Knetic closed this as completed Jul 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants