Skip to content

Commit

Permalink
fix(lexer): add scientific notation numbers (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik authored Feb 16, 2025
1 parent 79a240c commit 1df75e8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
24 changes: 21 additions & 3 deletions src/arc/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,17 @@ auto lexer::lex() -> token

auto dot = last == '.' ? 1 : 0;
auto flt = 0;
auto exp = 0;

while (true)
{
if (reader_.ended())
break;

if (curr == '\'' && (last == '\'' || last == 'f' || last == '.'))
if (curr == '\'' && (last == '\'' || last == 'f' || last == '.' || last == 'e' || last == 'E'))
throw comp_error(loc_, "invalid number literal");

if ((curr == '.' || curr == 'f') && last == '\'')
if ((curr == '.' || curr == 'f' || curr == 'e' || curr == 'E') && last == '\'')
throw comp_error(loc_, "invalid number literal");

if (curr == '\'')
Expand All @@ -449,6 +450,22 @@ auto lexer::lex() -> token
flt++;
else if (curr == '.')
dot++;
else if (curr == 'e' || curr == 'E')
{
exp++;
if (exp > 1)
throw comp_error(loc_, "invalid number literal");
push(curr);
advance();

// TODO: check stream end
if (curr == '+' || curr == '-')
{
push(curr);
advance();
}
continue;
}
else if (!(curr > 47 && curr < 58))
break;

Expand All @@ -462,7 +479,8 @@ auto lexer::lex() -> token
if (dot > 1 || flt > 1 || (flt && buffer_[buflen_ - 1] != 'f'))
throw comp_error(loc_, "invalid number literal");

if (dot || flt)
// TODO: exp can be int or float
if (dot || flt || exp)
return token{ token::FLT, spacing_, loc_, std::string{ &buffer_[0], buflen_ } };

return token{ token::INT, spacing_, loc_, std::string{ &buffer_[0], buflen_ } };
Expand Down
24 changes: 21 additions & 3 deletions src/gsc/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,17 @@ auto lexer::lex() -> token

auto dot = last == '.' ? 1 : 0;
auto flt = 0;
auto exp = 0;

while (true)
{
if (reader_.ended())
break;

if (curr == '\'' && (last == '\'' || last == 'f' || last == '.'))
if (curr == '\'' && (last == '\'' || last == 'f' || last == '.' || last == 'e' || last == 'E'))
throw comp_error(loc_, "invalid number literal");

if ((curr == '.' || curr == 'f') && last == '\'')
if ((curr == '.' || curr == 'f' || curr == 'e' || curr == 'E') && last == '\'')
throw comp_error(loc_, "invalid number literal");

if (curr == '\'')
Expand All @@ -449,6 +450,22 @@ auto lexer::lex() -> token
flt++;
else if (curr == '.')
dot++;
else if (curr == 'e' || curr == 'E')
{
exp++;
if (exp > 1)
throw comp_error(loc_, "invalid number literal");
push(curr);
advance();

// TODO: check stream end
if (curr == '+' || curr == '-')
{
push(curr);
advance();
}
continue;
}
else if (!(curr > 47 && curr < 58))
break;

Expand All @@ -462,7 +479,8 @@ auto lexer::lex() -> token
if (dot > 1 || flt > 1 || (flt && buffer_[buflen_ - 1] != 'f'))
throw comp_error(loc_, "invalid number literal");

if (dot || flt)
// TODO: exp can be int or float
if (dot || flt || exp)
return token{ token::FLT, spacing_, loc_, std::string{ &buffer_[0], buflen_ } };

return token{ token::INT, spacing_, loc_, std::string{ &buffer_[0], buflen_ } };
Expand Down

0 comments on commit 1df75e8

Please sign in to comment.