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

Add formatting hints to numbers #344

Merged
merged 8 commits into from
Apr 26, 2022
Merged

Add formatting hints to numbers #344

merged 8 commits into from
Apr 26, 2022

Conversation

wezm
Copy link
Contributor

@wezm wezm commented Apr 22, 2022

Fixes #328

Opening this PR for feedback, before proceeding further. The PR adds int hints so that number literals and number patterns retain their original format. For example:

$ echo 'let x : U32 = 1234; let _x : U16 = match x { 0x1234 => 1, 0b1111 => 2, "head" => 4,  _ => 3 }; _x' | mold -run cargo r -- elab
let x : U32 = 1234;
let _x : U16 = match x { 0b1111 => 2, 0x1234 => 1, "head" => 4, _ => 3 };
_x : U16

Possible further work

  • I think the extra IntStyle field in Const messes up PartialEq. Looks like I'll need to manually implement it and have it ignore that field. — I've now done this
    • Oh you totally covered this in the issue I linked "One thing we'll need to consider is to ignore the style hint when comparing constants for equality. We could do this by a custom overload PartialEq, or, by implementing our own method (eg. Const::logical_eq), which will involve updates to surface::elaboration::unification and core::semantics"
  • A way to add hints to binary data that is read, for example indicating that table_id in table_record in opentype.fathom should use the ASCII format.
  • Pehaps find a better home for the traits etc. that were added to core.
  • Propagate the style from the inputs to ops — I had a go at this
  • Add some more tests
  • Allow hex/bin signed ints and extend this to them
  • The ascii format uses big endian... should that be adjustable some how?

@wezm wezm force-pushed the int-hints branch 2 times, most recently from 4f7d67f to 15293a0 Compare April 22, 2022 03:52
@wezm wezm requested a review from brendanzab April 22, 2022 06:12
Copy link
Member

@brendanzab brendanzab left a comment

Choose a reason for hiding this comment

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

Looking good so far! I really like the merging used in the primops - it's nice to try to preserve the styling where possible.

Good point about the endianness of the ASCII literals. I wasn't really sure about them when I first implemented them, but yeah, they might require some more thought. 🤔

Binary,
Decimal,
Hexadecimal,
Ascii,
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if calling this AsciiBe would remind us of the endianness of this? But maybe just making an issue about this would be enough.

Copy link
Contributor Author

@wezm wezm Apr 26, 2022

Choose a reason for hiding this comment

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

A little research suggests that the common case is for these to be big-endian (E.g. OSType, PNG, AIFF, RIFF, AVI, WAV, DirectX, OpenType). Seems reasonable to just call it Ascii for now.

@wezm
Copy link
Contributor Author

wezm commented Apr 26, 2022

Ok I addressed the comments. I originally opened this PR with some parts remaining but many have been addressed. So it's probably suitable for merging now, if you agree.

Copy link
Member

@brendanzab brendanzab left a comment

Choose a reason for hiding this comment

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

Looks good!

@wezm wezm merged commit d1f609c into main Apr 26, 2022
@wezm wezm deleted the int-hints branch April 26, 2022 06:12
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.

Add hints to Consts to improve the distillation of integer constants
2 participants