forked from NeoFlock/noom
lexer: hex literal floats and exponents
This commit is contained in:
37
src/lexer.c
37
src/lexer.c
@@ -92,11 +92,46 @@ noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (version >= NOOM_VERSION_52) { // 5.2 added exponent and decimal to hex literals.
|
||||||
|
|
||||||
|
if (s[len] == '.') { // decimals in hex. smh.
|
||||||
|
len++;
|
||||||
|
|
||||||
|
while (noomL_ishex(s[len])) {
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len == 3) { // only 0x. is a malformed number, even if followed by an exponent
|
||||||
|
*error = NOOML_ERROR_MALFORMED_NUM;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (noomL_lower(s[len]) == 'p') {
|
||||||
|
len++;
|
||||||
|
|
||||||
|
// sign for exponent
|
||||||
|
if (s[len] == '-' || s[len] == '+') len++;
|
||||||
|
|
||||||
|
noom_uint_t slen = len;
|
||||||
|
|
||||||
|
while (noomL_ishex(s[len])) {
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (slen == len) { // nothing after `p`
|
||||||
|
*error = NOOML_ERROR_MALFORMED_NUM;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (len == 2) { // nothing after the x; malformed number.
|
if (len == 2) { // nothing after the x; malformed number.
|
||||||
*error = NOOML_ERROR_MALFORMED_NUM;
|
*error = NOOML_ERROR_MALFORMED_NUM;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
} else {
|
} else {
|
||||||
while (noomL_isnumber(s[len])) { // int part
|
while (noomL_isnumber(s[len])) { // int part
|
||||||
|
|||||||
@@ -33,14 +33,14 @@ void print_node(noomP_Node* node, noom_uint_t depth) {
|
|||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
// uhh uhhh uhhhhh
|
// uhh uhhh uhhhhh
|
||||||
const char* code = "local a = 0xff + 5e-2 + .1e2";
|
const char* code = "local a = 0x0.1E";
|
||||||
noom_uint_t pos = 0;
|
noom_uint_t pos = 0;
|
||||||
|
|
||||||
printf("LEX OUTPUT:\n");
|
printf("LEX OUTPUT:\n");
|
||||||
|
|
||||||
noomL_Token token;
|
noomL_Token token;
|
||||||
while (1) {
|
while (1) {
|
||||||
noomL_lex(code, pos, &token, NOOM_VERSION_51);
|
noomL_lex(code, pos, &token, NOOM_VERSION_54);
|
||||||
|
|
||||||
printf("%s ", noomL_formatTokenType(token.type));
|
printf("%s ", noomL_formatTokenType(token.type));
|
||||||
for (noom_uint_t i = 0; i < token.length; i++) putchar((code + token.offset)[i]);
|
for (noom_uint_t i = 0; i < token.length; i++) putchar((code + token.offset)[i]);
|
||||||
|
|||||||
Reference in New Issue
Block a user