Skip to content

Commit

Permalink
Binary notation integers (0b100 as 4).
Browse files Browse the repository at this point in the history
  • Loading branch information
revence27 committed Oct 21, 2011
1 parent d359764 commit 264f881
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/coffee-script/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -578,6 +584,7 @@ IDENTIFIER = /// ^

NUMBER = ///
^ 0x[\da-f]+ | # hex
^ 0b[01]+ | # binary
^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal
///i

Expand Down
9 changes: 9 additions & 0 deletions test/numbers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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", ->
Expand Down

0 comments on commit 264f881

Please sign in to comment.