Skip to content

Commit

Permalink
Support binary literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
dansanderson committed Jan 31, 2018
1 parent fb88459 commit d0ffacd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pico8/lua/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ def value(self):
integer, frac = self._data.split(b'.')
return float(int(integer, 16)) + float(int(frac, 16))/(16**len(frac))
return float(int(self._data, 16))
if b'b' in self._data:
if b'.' in self._data:
integer, frac = self._data.split(b'.')
return float(int(integer, 2)) + float(int(frac, 2))/(2**len(frac))
return float(int(self._data, 2))
return float(self._data)


Expand Down Expand Up @@ -227,6 +232,8 @@ class TokSymbol(Token):
(re.compile(br'\r'), TokNewline),
(re.compile(br'0[xX][0-9a-fA-F]+(\.[0-9a-fA-F]+)?'), TokNumber),
(re.compile(br'0[xX]\.[0-9a-fA-F]+'), TokNumber),
(re.compile(br'0[bB][01]+(\.[01]+)?'), TokNumber),
(re.compile(br'0[bB]\.[01]+'), TokNumber),
(re.compile(br'[0-9]+(\.(?!\.)[0-9]*)?([eE]-?[0-9]+)?'), TokNumber),
(re.compile(br'\.[0-9]+([eE]-?[0-9]+)?'), TokNumber),
(re.compile(br'::[a-zA-Z_][a-zA-Z0-9_]*::'), TokLabel),
Expand Down
24 changes: 24 additions & 0 deletions tests/pico8/lua/lexer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ def testNumberHexWithFrac(self):
self.assertEqual(lexer.TokNumber(b'0x1234567890abcdef.1bbf'),
lxr._tokens[0])

def testNumberBinary(self):
lxr = lexer.Lexer(version=4)
lxr._process_line(b'0b01101101\n')
self.assertEqual(2, len(lxr._tokens))
self.assertEqual(lexer.TokNumber(b'0b01101101'),
lxr._tokens[0])

def testNumberBinaryWithFrac(self):
lxr = lexer.Lexer(version=4)
lxr._process_line(b'0b01101101.0011\n')
self.assertEqual(2, len(lxr._tokens))
self.assertEqual(lexer.TokNumber(b'0b01101101.0011'),
lxr._tokens[0])

def testNumberValueDecimal(self):
lxr = lexer.Lexer(version=4)
lxr._process_line(b'123.456\n')
Expand All @@ -364,6 +378,16 @@ def testNumberValueHexFraction(self):
lxr._process_line(b'0xae.bc\n')
self.assertAlmostEqual(174.734, lxr._tokens[0].value, 3)

def testNumberValueBinaryInteger(self):
lxr = lexer.Lexer(version=4)
lxr._process_line(b'0b01101101\n')
self.assertEqual(109, lxr._tokens[0].value)

def testNUmberValueBinaryFraction(self):
lxr = lexer.Lexer(version=4)
lxr._process_line(b'0b01101101.0011\n')
self.assertAlmostEqual(109.1875, lxr._tokens[0].value, 3)

def testMultilineString(self):
lxr = lexer.Lexer(version=4)
lxr._process_line(b'[[one\n')
Expand Down

0 comments on commit d0ffacd

Please sign in to comment.