Author: mesyeti <mesyeti@mesyeti.uk>
add hexadecimal, binary, and octal
basic-std/std/stringView.bas | 17 +++++++++++++++++ basic/source/lexer.c | 20 ++++++++++++++++---- basic/source/parser.c | 13 ++++++++++++- basic/source/parser.h | 2 +- basic/test.bas | 2 +-
diff --git a/basic/source/lexer.c b/basic/source/lexer.c index 87e73856767fdf3f60f27920472111412a3a0ed2..9dacb830df350ef14cb009a9bf64f5e3169af130 100644 --- a/basic/source/lexer.c +++ b/basic/source/lexer.c @@ -44,14 +44,26 @@ default: return false; } } +// TODO: make this better static bool IsNumeric(const char* str) { - size_t len = strlen(str); + if ((str[0] == '0') && (str[1] == 'x')) { + str += 2; - for (size_t i = 0; i < len; ++ i) { - if ((str[i] < '0') || (str[i] > '9')) return false; + return strspn(str, "0123456789ABCDEFabcdef") == strlen(str); } + else if ((str[0] == '0') && (str[1] == 'b')) { + str += 2; - return true; + return strspn(str, "01") == strlen(str); + } + else if ((str[0] == '0') && (str[1] == 'o')) { + str += 2; + + return strspn(str, "01234567") == strlen(str); + } + else { + return strspn(str, "0123456789") == strlen(str); + } } #define GET_CHAR(VAR) \ diff --git a/basic/source/parser.c b/basic/source/parser.c index d7f6b966306f43c378242dd7c0b73b5914721345..ee4549af9e4ec1e7962645e7a354c1ac55ab8f2c 100644 --- a/basic/source/parser.c +++ b/basic/source/parser.c @@ -100,7 +100,18 @@ ret.string = (StringNode) {INFO(NODE_STRING), NewString(token->contents)}; return ret; } case TOKEN_INT: { - ret.integer = (IntNode) {INFO(NODE_INT), atoi(token->contents)}; + if ((token->contents[0] == '0') && (token->contents[1] == 'x')) { + ret.integer = (IntNode) {INFO(NODE_INT), (int) strtoll(token->contents, NULL, 16)}; + } + else if ((token->contents[0] == '0') && (token->contents[1] == 'b')) { + ret.integer = (IntNode) {INFO(NODE_INT), (int) strtoll(token->contents, NULL, 2)}; + } + else if ((token->contents[0] == '0') && (token->contents[1] == 'o')) { + ret.integer = (IntNode) {INFO(NODE_INT), (int) strtoll(token->contents, NULL, 8)}; + } + else { + ret.integer = (IntNode) {INFO(NODE_INT), atoi(token->contents)}; + } return ret; } case TOKEN_LPAREN: { diff --git a/basic/source/parser.h b/basic/source/parser.h index 08b18bed1c2cdb6506389ece5c29aa3a1256f5c8..352a585f1b08a241b236d6e4cb5934da33b1d7a6 100644 --- a/basic/source/parser.h +++ b/basic/source/parser.h @@ -38,7 +38,7 @@ typedef union Node Node; typedef struct { NodeInfo i; - int value; + int value; // TODO: make this a long long } IntNode; typedef struct { diff --git a/basic/test.bas b/basic/test.bas index 524df023217eb3f46800e17ad650e9482c298654..7b0ab0f85a488e96d24f097f339fa495d34bc127 100644 --- a/basic/test.bas +++ b/basic/test.bas @@ -14,4 +14,4 @@ dim adder as AddFunc adder = &Add -printf("2 + 2 = %d\n", adder(2, 2)) +printf("0x0F + 0x0F = %.2X\n", adder(0xF, 0xF)) diff --git a/basic-std/std/stringView.bas b/basic-std/std/stringView.bas new file mode 100644 index 0000000000000000000000000000000000000000..a426ab61411e71c12d77d3e6e51552ebf61c0092 --- /dev/null +++ b/basic-std/std/stringView.bas @@ -0,0 +1,17 @@ +'NITRON + +type Char = u32 + +type StringView + string as ptr(char) + length as uint +end + +const U8_1_BYTE_MASK 0x80 +const U8_2_BYTE_MASK 0xE0 +const U8_3_BYTE_MASK 0xF0 +const U8_4_BYTE_MASK 0xF8 + +private func CPLength(cp as ptr(char)) uint + +end