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

Extended number formatting #312

Merged
merged 1 commit into from
Mar 20, 2024
Merged

Extended number formatting #312

merged 1 commit into from
Mar 20, 2024

Conversation

triallax
Copy link
Collaborator

@triallax triallax commented Feb 5, 2024

Fixes #264.

TODO:

  • Add format specifiers for strings (and possibly other types?)
  • Fix todos
  • Check that format specifiers are valid (currently anything invalid will crash Numbat)
  • Fix units not being printed in quantity interpolations with format specifiers
  • Research possible choices other than strfmt
  • Add tests
  • Document
  • Improve error messages

numbat/src/ast.rs Outdated Show resolved Hide resolved
numbat/src/parser.rs Outdated Show resolved Hide resolved
numbat/src/tokenizer.rs Outdated Show resolved Hide resolved
numbat/src/typechecker.rs Outdated Show resolved Hide resolved
numbat/src/value.rs Outdated Show resolved Hide resolved
numbat/src/ast.rs Outdated Show resolved Hide resolved
numbat/src/parser.rs Outdated Show resolved Hide resolved
@triallax triallax marked this pull request as ready for review March 12, 2024 17:34
@@ -26,6 +26,15 @@ let speed = 25 km/h
print("Speed of the bicycle: {speed} ({speed -> mph})")
```

Rust-style format specifiers are also supported in interpolations. For instance:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not put too much emphasis on rust maybe?

Suggested change
Rust-style format specifiers are also supported in interpolations. For instance:
Format specifiers are also supported in interpolations. For instance:

book/src/procedures.md Outdated Show resolved Hide resolved
examples/numbat_syntax.nbt Outdated Show resolved Hide resolved
numbat/src/vm.rs Outdated Show resolved Hide resolved
numbat/src/value.rs Outdated Show resolved Hide resolved
numbat/src/vm.rs Outdated
Comment on lines 884 to 893
strfmt::strfmt(&format!("{{value{}}}", specifiers), &vars)
.map_err(|err| match err {
strfmt::FmtError::Invalid(s) => {
RuntimeError::InvalidFormatSpecifiers(s)
}
strfmt::FmtError::KeyError(_) => unreachable!(),
strfmt::FmtError::TypeError(s) => {
RuntimeError::InvalidTypeForFormatSpecifiers(s)
}
})?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please extract this into a function to avoid the duplication?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best I could do was extracting the function passed to map_err. I couldn't get the strfmt::strfmt(...) portion to be shared between the two branches because vars has a different type in each branch (in one its values are of type f64, in the other of type String. I could maybe do some generic shenanigans or, god forbid, use a macro, but doesn't seem worth it to me.

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Just a couple of minor suggestions.

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sharkdp sharkdp merged commit b070b88 into master Mar 20, 2024
15 checks passed
@sharkdp sharkdp deleted the extended-number-formatting branch March 20, 2024 18:51
@triallax
Copy link
Collaborator Author

Thanks!

@sharkdp
Copy link
Owner

sharkdp commented Mar 20, 2024

Changes are live on https://numbat.dev/

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

Successfully merging this pull request may close these issues.

Extended number formatting
2 participants