Skip to content

Commit

Permalink
Allow _ separator inside numbers
Browse files Browse the repository at this point in the history
Underscores can be used to improve readability and are actually
ignored for parsing numbers

Example

let x = 123_456
let y = 555.666_777
let z = 0xAB_CD_01_23
let w = 12.345_67e+2
  • Loading branch information
VasiliyRyabtsev committed Mar 7, 2022
1 parent 407be26 commit b215043
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 5 additions & 2 deletions doc/source/reference/language/datatypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ An Integer represents a 32 bit (or better) signed number.::
local b = 0x0012 //hexadecimal
local c = 075 //octal
local d = 'w' //char code
local e = 123_456 // underscores may be used to visually sepearate digit groups
local f = 0xAB_CD_01_23 // also underscores

--------
Float
--------

A float represents a 32 bit (or better) floating point number.::

local a=1.0
local b=0.234
local a = 1.0
local b = 0.234
local c = 6.5e-11 //exponential notation
local d = 12.345_67e+2 // digit group separators

--------
String
Expand Down
11 changes: 6 additions & 5 deletions squirrel/sqlexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ SQInteger isexponent(SQInteger c) { return c == 'e' || c=='E'; }


#define MAX_HEX_DIGITS (sizeof(SQInteger)*2)
#define NUM_NEXT() { do NEXT() while (CUR_CHAR==_SC('_')); }
SQInteger SQLexer::ReadNumber()
{
#define TINT 1
Expand All @@ -713,22 +714,22 @@ SQInteger SQLexer::ReadNumber()
SQInteger type = TINT, firstchar = CUR_CHAR;
SQChar *sTemp;
INIT_TEMP_STRING();
NEXT();
NUM_NEXT();
if(firstchar == _SC('0') && (toupper(CUR_CHAR) == _SC('X') || scisodigit(CUR_CHAR)) ) {
if(scisodigit(CUR_CHAR)) {
type = TOCTAL;
while(scisodigit(CUR_CHAR)) {
APPEND_CHAR(CUR_CHAR);
NEXT();
NUM_NEXT();
}
if(scisdigit(CUR_CHAR)) Error(_SC("invalid octal number"));
}
else {
NEXT();
NUM_NEXT();
type = THEX;
while(isxdigit(CUR_CHAR)) {
APPEND_CHAR(CUR_CHAR);
NEXT();
NUM_NEXT();
}
if(_longstr.size() > MAX_HEX_DIGITS) Error(_SC("too many digits for an Hex number"));
}
Expand All @@ -750,7 +751,7 @@ SQInteger SQLexer::ReadNumber()
}

APPEND_CHAR(CUR_CHAR);
NEXT();
NUM_NEXT();
}
}
TERMINATE_BUFFER();
Expand Down

0 comments on commit b215043

Please sign in to comment.