I think i'm done, lexer works fine and errors work fine too, amazing actually, should be moving on to the parser so that i can construct the expression tree

This commit is contained in:
2026-03-10 07:27:35 -06:00
parent 73451fcca9
commit 903fdbd6ff
2 changed files with 39 additions and 2 deletions

View File

@@ -120,7 +120,11 @@ LexerErr tokenize(const char *input, ASTNodeArray *out) {
while (input[offset] != '\n' && input[offset] != '\0') {
int current = input[offset];
if (isdigit(current) && state == WAIT_FOR_NUMBER) {
if (isdigit(current)) {
if (state != WAIT_FOR_NUMBER) {
ASTNodeArray_free(&arr);
return LEXER_WRONG_SYNTAX;
}
ASTNode new_node;
LexerErr result = tokenize_number(input, &offset, &new_node);
@@ -131,7 +135,10 @@ LexerErr tokenize(const char *input, ASTNodeArray *out) {
ASTNodeArray_push(&arr, new_node);
state = WAIT_FOR_OPERATOR;
} else if (isoperator(current) && state == WAIT_FOR_OPERATOR) {
} else if (isoperator(current)) {
if (state != WAIT_FOR_OPERATOR) {
return LEXER_WRONG_SYNTAX;
}
ASTNode new_node = {
.type = NODE_BINARY_OP,
.data.binary.op = current,

View File

@@ -45,6 +45,34 @@ static void test_tokenize_normal_expresion(void **state) {
assert_int_equal(node.data.integer, 789);
}
static void test_tokenize_unrecognized_symbol(void **state) {
(void) state;
char expr[256] = " 2 j 3 / 66 } 789";
ASTNodeArray tokens = {
.len = 0,
.cap = 0,
};
assert_int_equal(tokenize(expr, &tokens), LEXER_NOT_RECOGNIZED_SYMBOL);
assert_int_equal(tokens.len, 0);
assert_int_equal(tokens.cap, 0);
}
static void test_tokenize_wrong_sintax(void **state) {
(void) state;
char expr[256] = "2 3 / 66 789";
ASTNodeArray tokens = {
.len = 0,
.cap = 0,
};
assert_int_equal(tokenize(expr, &tokens), LEXER_WRONG_SYNTAX);
assert_int_equal(tokens.len, 0);
assert_int_equal(tokens.cap, 0);
}
static void test_string_to_number_normal(void **state) {
(void) state;
@@ -76,6 +104,8 @@ int main(void) {
cmocka_unit_test(test_string_to_number_normal),
cmocka_unit_test(test_string_to_number_overflow),
cmocka_unit_test(test_tokenize_normal_expresion),
cmocka_unit_test(test_tokenize_unrecognized_symbol),
cmocka_unit_test(test_tokenize_wrong_sintax),
};
return cmocka_run_group_tests(tests, NULL, NULL);