Made the arrangements for the mentioned changes in the last commit, for now just integers but IT WILL be capable of handling doubles as fractions
This commit is contained in:
@@ -3,10 +3,11 @@
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// 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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user