GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Add C#-style type accelerators and suffixes for ushort, uint, ulong, and short literals. by vexx32 · Pull Request #7575 · PowerShell/PowerShell Conversation
Collaborator
PR SummaryThis has been split off of #7509 at request of Ilya (@iSazonov) as that PR has become overly complex, and this portion of the PR was already approved by Powershell Committee. Adds the following numeric literal suffixes:
Adds the following type accelerators:
And of course, adds tests for all the above. PR Checklist
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsCatch up to PowerShell/PowerShell/master Catch up to master
Rain Sallow (/u/ta11ow) (vexx32)
requested review from
Bruce Payette (BrucePay) and
Dongbo Wang (daxian-dbw)
as code owners
Closed
6 of 8 tasks
How did that even happen, seriously...
|
There was an error while loading. Please reload this page.
|
I don't intend to fix the code factor issues here. I'll address them during the parser refactor that will come after this. (Much of the code causing the majority of the issues will be replaced anyhow; see #7509). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| else | ||
| { | ||
| result = d * multiplier; | ||
| result = d; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why we remove the multiplier?
What scenario address the code? It would nice to add a comment.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah, I thought I got all those. Correcting...
(I was trying to see if I could do the multiplier before the conversions to avoid throwing too much here, but it doesn't work because Int64.MaxValue is too high to retain precision with Double values in the upper range there.)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| result = ((long)Convert.ChangeType(d, typeof(long), CultureInfo.InvariantCulture)) * multiplier; | ||
| result = ((long)Convert.ChangeType(d, typeof(long), CultureInfo.InvariantCulture) * multiplier); | ||
| } | ||
| else if (suffix == NumberSuffixFlags.Short) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I believe we could replace one level of if-else-if with switch to get more readable code.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. Agreed!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| { | ||
| if (short.TryParse(strNum, style, NumberFormatInfo.InvariantInfo, out short s)) | ||
| { | ||
| result = s * (short)multiplier; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What result do we expect for multiplier = 1Mb?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
At present, that will throw an error (too big for short value type) and is expected.
With some of the upcoming changes (that you can see if you build the version in the original PR) that instead simply registers as an invalid numeric literal, as I am able to verify first that the multiplier does not overflow the intended value type bounds without introducing a morass of if/else.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Closed under next PR.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
| else if (suffix.HasFlag(NumberSuffixFlags.Unsigned) && ulong.TryParse(strNum, style, NumberFormatInfo.InvariantInfo, out ulong u)) | ||
| { | ||
| u *= (ulong)multiplier; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I wonder that we define multiplier as long - I expect ulong.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I am also pondering that myself, but I've not seen a completely necessary need to change it. It does seem to simplify calculations against other signed types, however; unsigned types often need a cast one way or the other when doing multiplication with signed types.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| @{ Script = "0x100000000"; Expected = [int64]0x100000000 } | ||
| @{ Script = "0x100000000"; Expected = [int64]0x100000000 } | ||
| #Tests for short notation | ||
| @{ Script = "10s"; Expected = "10" } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think now we should test not only value but target type too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
That is probably wise. I'll take some time over lunch to add tests for value types. :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| $testInvalidNumerals = @( | ||
| @{ Script = "16p" } | ||
| @{ Script = "20ux" } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Should we add 20x?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sure, if we want to. x is neither a valid type suffix nor a multiplier in PS, though, so testing for it is more or less the same as testing 16p at the moment. We could go through the alphabet if you want. :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
No :-) We should cover all code paths. Do we cover?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
x should result in an error. I'll pop it in. :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| @{ Script = "21ss" } | ||
| @{ Script = "100ll" } | ||
| @{ Script = "150su" } | ||
| @{ Script = "10ds" } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Should we add 10ud?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah, good catch. Absolutely.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ilya (@iSazonov) Also noting down now before I forget:
Presently the literal 0x10d is legal in PowerShell, although Decimal.TryParse() does not allow the hexadecimal syntax and that parse fails. Its actual return type is Int32.
I plan on fixing this as I go through the parser rewrite after this PR.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
0x10d - have we tests for it?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oh, lol. I am clearly not quite up to speed this morning. Of course 0x10d is legal. D is a hexadecimal character... and that's exactly why decimals will never result from a hex literal ending in d. I'm gonna make a comment in the code that reflects this, because it's not immediately obvious why Decimal doesn't even bother to check for hex literals.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
(and yes I believe there are tests for it, but I'll pop one in the type checks just to be sure.)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Added all these. :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| result = null; | ||
| return false; | ||
| } | ||
| else if (suffix == NumberSuffixFlags.Short) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I believe we could replace one level of if-else-if with switch to get more readable code.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Again, agreed. Sorted out. :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
PowerShell Committee approved this in #7509. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Partially replace if stack with switches Add missing multiplier.
Apparently int16 * int16 always results in int32. Wat.
|
OK, pretty sure I got that short parsing sorted. I can understand doing operations in the console types can be freely converted, but if you're gonna specify |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Attempting... this always seems far messier than it should be. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
How did that even happen, seriously...
Partially replace if stack with switches Add missing multiplier.
Apparently int16 * int16 always results in int32. Wat.
Teach me to try to restructure the test file too much! ;)
Fix test cases for reals with certain suffixes
No, really, I have no idea how the heck this happened. It's gone now.
Because I cannot find a distinct commit that actually modified this file
…werShell into Tokenizer/TypeSuffixes
|
Okay... Ilya (@iSazonov) apologies about the mess. Not sure what it is with me and rebasing, but I've yet to manage to figure it out and get it done cleanly. The Linux build seems to be failing on some kind of permissions issue with that build environment, not sure what that's about. Everything else looks green, though. :) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
You should reset to your last commit and then rebase. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ilya (@iSazonov) yeah, point taken. Rebasing is something I've yet to get to grips with. CLosing this for now, let's continue from a cleaner slate in #7813, where everything's all ready. :D |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.