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 additional number bases #3

Open
Stupremee opened this issue Jan 21, 2020 · 13 comments
Open

Support additional number bases #3

Stupremee opened this issue Jan 21, 2020 · 13 comments

Comments

@Stupremee
Copy link

Stupremee commented Jan 21, 2020

It would be very helpful if you can specify numbers in other bases, e.g. hexadecimal (0xFF) or binary (0b1001), and convert them via functions like in Python. (hex(255) -> 0xFF, bin(2) -> 10)

@Stupremee Stupremee changed the title Support additional bases Support additional number bases Jan 21, 2020
@likebike
Copy link
Owner

Great idea. I'll try to add this soon.

@Stupremee
Copy link
Author

I can add this if it's okay.

@likebike
Copy link
Owner

I can add this if it's okay.

Sure, you can if you want to. You mostly just need to modify the read_const() function: https://github.com/likebike/fasteval/blob/master/src/parser.rs#L345

@Stupremee
Copy link
Author

Yep already saw it

@likebike
Copy link
Owner

As far as the idea about hex()/bin()/... functions goes, that's going to be a bit complicated. Those functions would need to produce strings, not numbers, and they could only be used as arguments within a print() function (because that is the only place where strings are allowed).

...So i suggest that we first just implement the parsing of numeric literals ( 0xFF, 0b010101, 0o746 ), and ignore the string formatting for now.

@likebike
Copy link
Owner

Instead of using functions like hex()/bin()/oct()/..., another way to do the string formatting of alternate bases is by using printf-style formatting codes, like this: print("hex=%x dec=%d oct=%o bin=%b", 100, 100, 100, 100)

I like Go's choice of formatting codes: https://golang.org/pkg/fmt/#hdr-Printing

I am already planning to add printf codes to print(). In fact, my Go implementation of this library already has that feature, but dynamic string formatting is not a built-in feature in Rust, so I will need to implement it myself.

@Stupremee
Copy link
Author

Stupremee commented Jan 23, 2020

That’s a good idea 👍.
Rust formatting has support for hex, oct and binary. Probably you can use that.

@likebike
Copy link
Owner

The Rust formatting is really nice, but it is compile-time only. fasteval needs runtime formatting. Maybe we can find a way to re-use the Rust formatting infrastructure at runtime.

@Stupremee
Copy link
Author

Can't you just use the format macro?

@likebike
Copy link
Owner

All macros are evaluated at compile-time. We need a solution that can perform string formatting at run-time.

@Stupremee
Copy link
Author

Ohh, yes you are right.

@Stupremee
Copy link
Author

I don't have much time at the moment. So the PR will take around a week.

@likebike
Copy link
Owner

No problem. Take your time -- I appreciate your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants