well, nothing works, at least there is something to debug

This commit is contained in:
2026-03-25 07:43:00 -06:00
parent f11b6f8c12
commit f24671bd19
4 changed files with 78 additions and 6 deletions

View File

@@ -1,7 +1,17 @@
#include "lexer.h"
#include "parser.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
puts("Hello");
int main(void) {
ASTNodeArray context;
tokenize("3 + 4 * 5", &context);
AST tree = parse(&context);
print_AST(tree);
printf("Hola\n");
return EXIT_SUCCESS;
}

View File

@@ -107,7 +107,9 @@ void print_AST(AST tree) {
}
void print_node(ASTNode *node, int depth) {
if (!node) return;
if (node == NULL) {
return;
}
if (node->type == NODE_BINARY_OP) {
print_node(node->data.binary.right, depth + 1);

View File

@@ -2,6 +2,7 @@ find_package(cmocka REQUIRED)
add_executable(test_nodeArray test_ASTNodeArray.c)
add_executable(test_lexer test_lexer.c)
add_executable(test_parser test_parser.c)
target_link_libraries(test_nodeArray
calculator_lib
@@ -13,5 +14,11 @@ target_link_libraries(test_lexer
cmocka::cmocka
)
target_link_libraries(test_parser
calculator_lib
cmocka::cmocka
)
add_test(NAME nodeArray_tests COMMAND test_nodeArray)
add_test(NAME lexer_tests COMMAND test_lexer)
add_test(NAME parser_tests COMMAND test_parser)

View File

@@ -1,5 +1,58 @@
#include <stdlib.h>
#include "lexer.h"
#include "parser.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
int main() {
return EXIT_SUCCESS;
static void test_parsing_basic_expression(void **state) {
(void) state;
char expr[256] = "2 + 3 / 66 * 789";
ASTNodeArray tokens;
ASTNode node;
assert_int_equal(tokenize(expr, &tokens), LEXER_OK);
assert_int_equal(tokens.len, 7);
ASTNodeArray_get(&tokens, 0, &node);
assert_int_equal(node.type, NODE_INTEGER);
assert_int_equal(node.data.integer, 2);
ASTNodeArray_get(&tokens, 1, &node);
assert_int_equal(node.type, NODE_BINARY_OP);
assert_int_equal(node.data.binary.op, OP_ADD);
ASTNodeArray_get(&tokens, 2, &node);
assert_int_equal(node.type, NODE_INTEGER);
assert_int_equal(node.data.integer, 3);
ASTNodeArray_get(&tokens, 3, &node);
assert_int_equal(node.type, NODE_BINARY_OP);
assert_int_equal(node.data.binary.op, OP_DIV);
ASTNodeArray_get(&tokens, 4, &node);
assert_int_equal(node.type, NODE_INTEGER);
assert_int_equal(node.data.integer, 66);
ASTNodeArray_get(&tokens, 5, &node);
assert_int_equal(node.type, NODE_BINARY_OP);
assert_int_equal(node.data.binary.op, OP_MUL);
ASTNodeArray_get(&tokens, 6, &node);
assert_int_equal(node.type, NODE_INTEGER);
assert_int_equal(node.data.integer, 789);
AST tree = parse(&tokens);
assert_int_equal(tree.head->type, NODE_BINARY_OP);
}
int main(void) {
const struct CMUnitTest tests [] = {
cmocka_unit_test(test_parsing_basic_expression),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}