Binary notation integers (0b100 as 4).

This commit is contained in:
Revence Kalibwani 2011-10-21 21:44:56 +03:00
parent d359764fba
commit 264f881a81
3 changed files with 23 additions and 4 deletions

View File

@ -107,11 +107,14 @@
};
Lexer.prototype.numberToken = function() {
var match, number;
var is_binary, match, number, numlen;
if (!(match = NUMBER.exec(this.chunk))) return 0;
number = match[0];
numlen = number.length;
is_binary = /0b([01]+)/.exec(number);
if (is_binary) number = (parseInt(is_binary[1], 2)).toString();
this.token('NUMBER', number);
return number.length;
return numlen;
};
Lexer.prototype.stringToken = function() {
@ -620,7 +623,7 @@
IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/;
NUMBER = /^0x[\da-f]+|^\d*\.?\d+(?:e[+-]?\d+)?/i;
NUMBER = /^0x[\da-f]+|^0b[01]+|^\d*\.?\d+(?:e[+-]?\d+)?/i;
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[^\n\S]*)?\1/;

View File

@ -133,8 +133,14 @@ exports.Lexer = class Lexer
numberToken: ->
return 0 unless match = NUMBER.exec @chunk
number = match[0]
numlen = number.length
# Now, since it is not JavaScript-descended, if it is binary, we will have
# to massage it into something similar and then hand it over. May the user
# forgive us.
is_binary = /0b([01]+)/.exec number
number = (parseInt is_binary[1], 2).toString() if is_binary
@token 'NUMBER', number
number.length
numlen
# Matches strings, including multi-line strings. Ensures that quotation marks
# are balanced within the string's contents, and within nested interpolations.
@ -578,6 +584,7 @@ IDENTIFIER = /// ^
NUMBER = ///
^ 0x[\da-f]+ | # hex
^ 0b[01]+ | # binary
^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal
///i

View File

@ -7,8 +7,17 @@
# * Scientific Notation Integer Literals
# * Scientific Notation Non-Integer Literals
# * Non-Integer Literals
# * Binary Integer Literals
# Binary Integer Literals
# Binary notation is understood as would be decimal notation.
test "Parser recognises binary numbers", ->
eq 4, 0b100.valueOf()
eq '11', 0b100.toString 3
eq '100', 0b100['toString'] 2
# Decimal Integer Literals
test "call methods directly on numbers", ->