数值字面量

2018-06-16 14:41 更新

语法:

NumericLiteral :: 
DecimalLiteral 
HexIntegerLiteral
DecimalLiteral :: 
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt 
. DecimalDigits ExponentPartopt DecimalIntegerLiteral 
ExponentPartopt
DecimalIntegerLiteral :: 
0 
NonZeroDigit DecimalDigitsopt
DecimalDigits :: 
DecimalDigit 
DecimalDigits DecimalDigit
DecimalDigit :: one of 0 
1 2 3 4 5 6 7 8 9
NonZeroDigit :: one of 
1 2 3 4 5 6 7 8 9
ExponentPart :: 
ExponentIndicator SignedInteger
ExponentIndicator :: one of 
e E
SignedInteger :: 
DecimalDigits 
+ DecimalDigits 
- DecimalDigits
HexIntegerLiteral :: 
0x HexDigit
0X HexDigit 
HexIntegerLiteral HexDigit
HexDigit :: one of 
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F


源字符中的 NumericLiteral 后面不允许紧跟着 IdentifierStart 或 DecimalDigit。

注:例如:

3in

是错误的,不存在两个输入元素 3 和 in。


语义:

一个数值字面量代表一个 Number 类型的值。此值取决于两个步骤:第一,由字面量得出的数学值 (mathematical value)(MV);第二,这个数学值按照后面描述的规则舍入。

  • NumericLiteral::DecimalLiteral 的 MV 是 DecimalLiteral 的 MV。
  • NumericLiteral :: HexIntegerLiteral 的 MV 是 HexIntegerLiteral 的 MV。
  • DecimalLiteral :: DecimalIntegerLiteral . 的 MV 是 DecimalIntegerLiteral 的 MV 是。
  • DecimalLiteral :: DecimalIntegerLiteral . DecimalDigits 的 MV 是 DecimalIntegerLiteral 的 MV 加上 (DecimalDigits 的 MV 乘 10-n), 这里的 n 是 DecimalDigits 的字符个数。
  • DecimalLiteral :: DecimalIntegerLiteral . ExponentPart 的 MV 是 DecimalIntegerLiteral 的 MV 乘 10e, 这里的 e 是 ExponentPart 的 MV。
  • DecimalLiteral :: DecimalIntegerLiteral . DecimalDigits ExponentPart 的 MV 是 (DecimalIntegerLiteral 的 MV 加 (DecimalDigits 的 MV 乘 10-n)) 乘 10e, 这里的 n 是 DecimalDigits 的字符个数,e 是 ExponentPart 的 MV。
  • DecimalLiteral ::. DecimalDigits 的 MV 是 DecimalDigits 的 MV 乘 10-n, 这里的 n 是 DecimalDigits 的字符个数。
  • DecimalLiteral ::. DecimalDigits ExponentPart 的 MV 是 DecimalDigits 的 MV 乘 10e-n, 这里的 n 是 DecimalDigits 的字符个数,e 是 ExponentPart 的 MV。
  • DecimalLiteral :: DecimalIntegerLiteral 的 MV 是 DecimalIntegerLiteral 的 MV。
  • DecimalLiteral :: DecimalIntegerLiteral ExponentPart 的 MV 是 DecimalIntegerLiteral 的 MV 乘 10e, 这里的 e 是 ExponentPart 的 MV。
  • DecimalIntegerLiteral :: 0 的 MV 是 0。
  • DecimalIntegerLiteral :: NonZeroDigit DecimalDigits 的 MV 是 (NonZeroDigit 的 MV 乘 10n) 加 DecimalDigits 的 MV, 这里的 n 是 DecimalDigits 的字符个数。
  • DecimalDigits :: DecimalDigit 的 MV 是 DecimalDigit 的 MV。
  • DecimalDigits :: DecimalDigits DecimalDigit 的 MV 是 (DecimalDigits 的 MV 乘 10) 加 DecimalDigit 的 MV。
  • ExponentPart :: ExponentIndicator SignedInteger 的 MV 是 SignedInteger 的 MV。
  • SignedInteger :: DecimalDigits 的 MV 是 DecimalDigits 的 MV。
  • SignedInteger :: + DecimalDigits 的 MV 是 DecimalDigits 的 MV。
  • SignedInteger :: - DecimalDigits 的 MV 是 DecimalDigits 的 MV 取负。
  • DecimalDigit :: 0 或 HexDigit :: 0 的 MV 是 0。
  • DecimalDigit :: 1 或 NonZeroDigit :: 1 或 HexDigit :: 1 的 MV 是 1。
  • DecimalDigit :: 2 或 NonZeroDigit :: 2 或 HexDigit :: 2 的 MV 是 2。
  • DecimalDigit :: 3 或 NonZeroDigit :: 3 或 HexDigit :: 3 的 MV 是 3。
  • DecimalDigit :: 4 或 NonZeroDigit :: 4 或 HexDigit :: 4 的 MV 是 4。
  • DecimalDigit :: 5 或 NonZeroDigit :: 5 或 HexDigit :: 5 的 MV 是 5。
  • DecimalDigit :: 6 或 NonZeroDigit :: 6 或 HexDigit :: 6 的 MV 是 6。
  • DecimalDigit :: 7 或 NonZeroDigit :: 7 或 HexDigit :: 7 的 MV 是 7。
  • DecimalDigit :: 8 或 NonZeroDigit :: 8 或 HexDigit :: 8 的 MV 是 8。
  • DecimalDigit :: 9 或 NonZeroDigit :: 9 或 HexDigit :: 9 的 MV 是 9。
  • HexDigit :: a 或 HexDigit :: A 的 MV 是 10。
  • HexDigit :: b 或 HexDigit :: B 的 MV 是 11。
  • HexDigit :: c 或 HexDigit :: C 的 MV 是 12。
  • HexDigit :: d 或 HexDigit :: D 的 MV 是 13。
  • HexDigit :: e 或 HexDigit :: E 的 MV 是 14。
  • HexDigit :: f 或 HexDigit :: F 的 MV 是 15。
  • HexIntegerLiteral :: 0x HexDigit 的 MV 是 HexDigit 的 MV。
  • HexIntegerLiteral :: 0X HexDigit 的 MV 是 HexDigit 的 MV。
  • HexIntegerLiteral :: HexIntegerLiteral HexDigit 的 MV 是 (HexIntegerLiteral 的 MV 乘 16) 加 HexDigit 的 MV。


数值字面量的确切 MV 值一旦被确定,它就会舍入成 Number 类型的值。如果 MV 是 0,那么舍入值是 +0;否则,舍入值必须是 MV 对应的准确数字值(8.5 中定义),除非此字面量是有效数字超过 20 位的 DecimalLiteral,这种情况下,数字值可以用下面两种方式产生的 MV 值确定:一,将 20 位后的每个有效数字用 0 替换后产生的 MV,二,将 20 位后的每个有效数字用 0 替换,并且递增第 20 位有效数字位置的字面量值,产生的 MV。如果一个数字是 ExponentPart 的一部分,并且:

  • 它不是 0;或
  • 它的左侧是非零数字,它的右侧是不在 ExponentPart 的非零数字。


符合标准的实现,在处理严格模式代码(见 10.1.1)时,按照 B.1.1 的描述,不得扩展 NumericLiteral 包含 OctalIntegerLiteral 的语法。


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号