diff --git a/include/lexer.h b/include/lexer.h index eccf1dc..4f3c211 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -3,10 +3,11 @@ #include #include +#include // For identifing typedef enum { - NODE_NUMBER, + NODE_INTEGER, NODE_BINARY_OP, } ASTNodeType; @@ -41,7 +42,7 @@ typedef enum { typedef struct ASTNode { ASTNodeType type; union { - double number; + int64_t integer; struct { struct ASTNode *left; struct ASTNode *right; @@ -69,7 +70,7 @@ size_t ASTNodeArray_len(ASTNodeArray *arr); // Lexer funtions as well as few functionality LexerErr tokenize(const char* input, ASTNodeArray *out); LexerErr tokenize_number(const char* input, size_t *offset, ASTNode *out); -LexerErr string_to_number(const char* input, size_t *offset, double *number); +LexerErr string_to_number(const char* input, size_t *offset, int64_t *number); void reverser_string(char* input); #endif // !LEXER_H diff --git a/src/lexer.c b/src/lexer.c index b0abbb8..4663f3f 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -114,7 +114,7 @@ size_t ASTNodeArray_len(ASTNodeArray *arr) { // CURRENTLY, it only supports ints, not clear how floating // point is implemented but i'll figure it out -LexerErr string_to_number(const char *input, size_t *offset, double *number) { +LexerErr string_to_number(const char *input, size_t *offset, int64_t *number) { char buf[128] = { '\0' }; size_t buf_pos = 0; @@ -130,7 +130,7 @@ LexerErr string_to_number(const char *input, size_t *offset, double *number) { } int c = 0; - long count = 0; + int64_t count = 0; while (buf[c] != '\0') { int digit = buf[c] - '0'; @@ -144,7 +144,7 @@ LexerErr string_to_number(const char *input, size_t *offset, double *number) { c++; } - *number = (double) count; + *number = count; *offset = current; return LEXER_OK; } diff --git a/test/test_ASTNodeArray.c b/test/test_ASTNodeArray.c index cc28297..59c98cb 100644 --- a/test/test_ASTNodeArray.c +++ b/test/test_ASTNodeArray.c @@ -12,18 +12,18 @@ static void test_array_push(void **state) { // We use 2 to force resize and checking anything wrong with malloc ASTNodeArray arr = ASTNodeArray_init(2); ASTNode node1 = { - .type = NODE_NUMBER, - .data = { .number = 90 } + .type = NODE_INTEGER, + .data = { .integer = 90 } }; ASTNode node2 = { - .type = NODE_NUMBER, - .data = { .number = 80 } + .type = NODE_INTEGER, + .data = { .integer = 80 } }; ASTNode node3 = { - .type = NODE_NUMBER, - .data = { .number = 70 } + .type = NODE_INTEGER, + .data = { .integer = 70 } }; assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK); @@ -44,18 +44,18 @@ static void test_array_pop(void **state) { // Set to force desize ASTNodeArray arr = ASTNodeArray_init(16); ASTNode node1 = { - .type = NODE_NUMBER, - .data = { .number = 90 } + .type = NODE_INTEGER, + .data = { .integer = 90 } }; ASTNode node2 = { - .type = NODE_NUMBER, - .data = { .number = 80 } + .type = NODE_INTEGER, + .data = { .integer = 80 } }; ASTNode node3 = { - .type = NODE_NUMBER, - .data = { .number = 70 } + .type = NODE_INTEGER, + .data = { .integer = 70 } }; assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK); @@ -69,8 +69,8 @@ static void test_array_pop(void **state) { ASTNode node4; assert_int_equal(ASTNodeArray_pop(&arr, 1, &node4), ARRAY_OK); - assert_int_equal(node4.type, NODE_NUMBER); - assert_int_equal(node4.data.number, 80); + assert_int_equal(node4.type, NODE_INTEGER); + assert_int_equal(node4.data.integer, 80); ASTNodeArray_free(&arr); } diff --git a/test/test_lexer.c b/test/test_lexer.c index 8d59e72..e9d1c81 100644 --- a/test/test_lexer.c +++ b/test/test_lexer.c @@ -11,7 +11,7 @@ static void test_string_to_number(void **state) { char num[16] = "2333t55"; size_t offset = 0; - double result = 0; + int64_t result = 0; assert_int_equal(string_to_number(num, &offset, &result), 0); assert_int_equal(offset, 4);