Skip to content

Added hex numbers support#28

Open
michalf1 wants to merge 3 commits into
nreco:masterfrom
michalf1:hex_number_support
Open

Added hex numbers support#28
michalf1 wants to merge 3 commits into
nreco:masterfrom
michalf1:hex_number_support

Conversation

@michalf1

Copy link
Copy Markdown

No description provided.

@VitaliyMF

VitaliyMF commented Jun 26, 2020

Copy link
Copy Markdown
Contributor

This PR cannot be accepted:

  • existing tests are not passed, no tests for expressions with hex-numbers
  • this code can lead to 'index-out-of-range': if (s[lexem.End] == '0' && s[lexem.End + 1] == 'x') {
  • motivation to have hex-numbers in expressions is not explained. Hex constants are not typical, and when they are needed it is easier to provide custom function in the evaluation context to parse them, something like this:
varContext["ParseInt32"] = (Func<string,int>)((s) => {
  int numConst;
  if (!Int32.TryParse(lexem.GetValue(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out numConst)) {
    throw new Exception(String.Format("Invalid number: {0}", lexem.GetValue()));
  }
});
Console.WriteLine(lambdaParser.Eval("ParseInt(\"0xFF\")", varContext));

Michal added 2 commits June 26, 2020 22:09
Added tests for hex numbers
@michalf1

Copy link
Copy Markdown
Author

Hello Vitaliy,
I don't agree that hex constants are not typical (I use them quite often). In my opinion the parser is incomplete without that functionality. Anyway I made some fixes and tests - now it's passing all tests.
Br,
Michał

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.

2 participants