Skip to content

Implement Numeric Separator#6143

Merged
chakrabot merged 6 commits into
chakra-core:masterfrom
boingoing:numericseparator
Jun 4, 2019
Merged

Implement Numeric Separator#6143
chakrabot merged 6 commits into
chakra-core:masterfrom
boingoing:numericseparator

Conversation

@boingoing
Copy link
Copy Markdown
Contributor

@boingoing boingoing commented Jun 4, 2019

Implement Numeric Separator

Add support for numeric separator characters in numeric literals. These are just syntactic sugar which improve readability of numeric constants in source. They desugar completely out of the numbers when parsed.

Numeric digits in the constant may have a single '_' character between them. The constant may not begin or end with a '_' character and there may not be multiple numeric separators in a row. All numeric constants are supported. Includes decimal, hex, octal and binary numeric constants.

Legacy octal integer constants do not support numeric separators. Numeric values parsed from string functions (like ToNumber or parseInt) also do not support numeric separators.

    1234 === 1_2_3_4; // true
    12.34e56 === 1_2.3_4e5_6; // true
    0xff === 0xf_f; // true
    0o17 === 0o1_7; // true
    0b11 === 0b1_1; // true

Numeric Separator proposal is in stage 3 and implemented by JSC and V8. See proposal:
https://github.com/tc39/proposal-numeric-separator

Fixes: #6060

Comment thread test/Number/NumericSeparator.js Outdated
{
name: "Strings parsed as number do not support numeric separators for decimal literals",
body: function () {
assert.areEqual(NaN, Number('12_34'), "NaN === Number('12_34')");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: indentation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for noticing. There were tab characters on this line.

if (*psz == '_' && isNumericSeparatorEnabled && pszSave < psz && psz[-1] == '0' && FHexDigit(psz[1], &unused))
{
psz++;
goto LSkipZeroes;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, this file and these gotos 😆

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

#define DEFAULT_CONFIG_ES6RegExSticky (true)
#define DEFAULT_CONFIG_ES2018RegExDotAll (true)
#define DEFAULT_CONFIG_ESBigInt (false)
#define DEFAULT_CONFIG_ESNumericSeparator (true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for defaulting this to true. : )

At some point I think we'll need to comb through some of the old always-on feature flags and clean them up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah. We've made passes before and took out some of the ancient flags for things which are never getting disabled. Worth looking at again.

@fatcerberus
Copy link
Copy Markdown
Contributor

fatcerberus commented Jun 4, 2019

Why did they pick underscores for this :-/

I like the C++ style better honestly

int m = 36'000'000;

Maybe there was a compatibility reason that couldn't be done but would have been much more pleasant to look at IMO. Then again I'm biased because with the exception of C I kind of hate the _ character with a passion. 😛

Also there's no good reason for 1_________________2 to be a legal numeric literal.

@zenparsing
Copy link
Copy Markdown
Contributor

@fatcerberus I've grown to like the C++ separators as well. Oh well!

Multiple consecutive underscores are actually disallowed, so that helps a little : )

@boingoing What kind of error message does this generate if there are multiple consecutive underscores? Some engines actually say that multiples are allowed; we might want to do that as well (but it can be a follow-up, of course).

@boingoing
Copy link
Copy Markdown
Contributor Author

@zenparsing Like most of our error messages, the one emitted when we find multiple consecutive underscores is a generic one. It says Unexpected identifier after numeric literal which is not the most useful in explaining why the syntax is an error. It's nontrivial to emit a more specific message. At least it gives you the right character position, though, so should be obvious enough.

boingoing and others added 6 commits June 4, 2019 11:39
Add support for numeric separator characters in numeric literals. These are just syntactic sugar which improve readability of numeric constants in source. They desugar completely out of the numbers when parsed.

Numeric digits in the constant may have a single '_' character between them. The constant may not begin or end with a '_' character and there may not be multiple numeric separators in a row. All numeric constants are supported. Includes decimal, hex, octal and binary numeric constants.

```javascript
1234 === 1_2_3_4; // true
12.34e56 === 1_2.3_4e5_6; // true
0xff === 0xf_f; // true
0o17 === 0o1_7; // true
0b11 === 0b1_1; // true
```

Numeric Separator proposal is in stage 3 and implemented by JSC and V8. See proposal:
https://github.com/tc39/proposal-numeric-separator

Fixes: chakra-core#6060
@chakrabot chakrabot merged commit 71adb7d into chakra-core:master Jun 4, 2019
chakrabot pushed a commit that referenced this pull request Jun 4, 2019
Merge pull request #6143 from boingoing:numericseparator

Implement Numeric Separator

Add support for numeric separator characters in numeric literals. These are just syntactic sugar which improve readability of numeric constants in source. They desugar completely out of the numbers when parsed.

Numeric digits in the constant may have a single `'_'` character between them. The constant may not begin or end with a `'_'` character and there may not be multiple numeric separators in a row. All numeric constants are supported. Includes decimal, hex, octal and binary numeric constants.

Legacy octal integer constants do not support numeric separators. Numeric values parsed from string functions (like ToNumber or parseInt) also do not support numeric separators.

```javascript
    1234 === 1_2_3_4; // true
    12.34e56 === 1_2.3_4e5_6; // true
    0xff === 0xf_f; // true
    0o17 === 0o1_7; // true
    0b11 === 0b1_1; // true
```

Numeric Separator proposal is in stage 3 and implemented by JSC and V8. See proposal:
https://github.com/tc39/proposal-numeric-separator

Fixes: #6060
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.

Implement Numeric Sparators

4 participants