Skip to content

Commit

Permalink
Support exponential notation in lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Feb 28, 2024
1 parent 34a6e30 commit 191515b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ static void number(struct GlobalState * state, int exprType, RewindState *rewind
invalidTarget(state, exprType, "literal");

for (size_t j = 0; j < state->parser.previous.length; ++j) {
if (state->parser.previous.start[j] == '.') {
if (start[j] == 'x' || start[j] == 'X') break;
if (start[j] == '.' || start[j] == 'e' || start[j] == 'E') {
#ifndef KRK_NO_FLOAT
emitConstant(krk_parse_float(start, state->parser.previous.length));
#else
Expand Down
6 changes: 6 additions & 0 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ static KrkToken number(KrkScanner * scanner, char c) {
while (isDigit(peek(scanner))) advance(scanner);
}

if (peek(scanner) == 'e' || peek(scanner) == 'E') {
advance(scanner);
if (peek(scanner) == '+' || peek(scanner) == '-') advance(scanner);
while (isDigit(peek(scanner))) advance(scanner);
}

return makeToken(scanner, TOKEN_NUMBER);
}

Expand Down
5 changes: 5 additions & 0 deletions src/vendor/rline.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ static int paint_krk_numeral(struct syntax_state * state) {
paint(1, FLAG_NUMERAL);
while (isdigit(charat())) paint(1, FLAG_NUMERAL);
}
if (charat() == 'e' || charat() == 'E') {
paint(1, FLAG_NUMERAL);
if (charat() == '-' || charat() == '+') paint(1, FLAG_NUMERAL);
while (isdigit(charat())) paint(1, FLAG_NUMERAL);
}
}
return 0;
}
Expand Down

0 comments on commit 191515b

Please sign in to comment.