Skip to content

Commit

Permalink
fix(lexer): add scientific notation numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik committed Feb 16, 2025
1 parent 79a240c commit cfc69dd
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/arc/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,23 +420,24 @@ auto lexer::lex() -> token

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

lex_number:
lex_number:
if (last == '.' || last != '0' || (last == '0' && (curr != 'o' && curr != 'b' && curr != 'x')))
{
push(last);

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 cfc69dd

Please sign in to comment.