well, nothing works, at least there is something to debug
This commit is contained in:
14
src/main.c
14
src/main.c
@@ -1,7 +1,17 @@
|
|||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(void) {
|
||||||
puts("Hello");
|
ASTNodeArray context;
|
||||||
|
|
||||||
|
tokenize("3 + 4 * 5", &context);
|
||||||
|
|
||||||
|
AST tree = parse(&context);
|
||||||
|
print_AST(tree);
|
||||||
|
|
||||||
|
printf("Hola\n");
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,7 +107,9 @@ void print_AST(AST tree) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void print_node(ASTNode *node, int depth) {
|
void print_node(ASTNode *node, int depth) {
|
||||||
if (!node) return;
|
if (node == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (node->type == NODE_BINARY_OP) {
|
if (node->type == NODE_BINARY_OP) {
|
||||||
print_node(node->data.binary.right, depth + 1);
|
print_node(node->data.binary.right, depth + 1);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ find_package(cmocka REQUIRED)
|
|||||||
|
|
||||||
add_executable(test_nodeArray test_ASTNodeArray.c)
|
add_executable(test_nodeArray test_ASTNodeArray.c)
|
||||||
add_executable(test_lexer test_lexer.c)
|
add_executable(test_lexer test_lexer.c)
|
||||||
|
add_executable(test_parser test_parser.c)
|
||||||
|
|
||||||
target_link_libraries(test_nodeArray
|
target_link_libraries(test_nodeArray
|
||||||
calculator_lib
|
calculator_lib
|
||||||
@@ -13,5 +14,11 @@ target_link_libraries(test_lexer
|
|||||||
cmocka::cmocka
|
cmocka::cmocka
|
||||||
)
|
)
|
||||||
|
|
||||||
|
target_link_libraries(test_parser
|
||||||
|
calculator_lib
|
||||||
|
cmocka::cmocka
|
||||||
|
)
|
||||||
|
|
||||||
add_test(NAME nodeArray_tests COMMAND test_nodeArray)
|
add_test(NAME nodeArray_tests COMMAND test_nodeArray)
|
||||||
add_test(NAME lexer_tests COMMAND test_lexer)
|
add_test(NAME lexer_tests COMMAND test_lexer)
|
||||||
|
add_test(NAME parser_tests COMMAND test_parser)
|
||||||
|
|||||||
@@ -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() {
|
static void test_parsing_basic_expression(void **state) {
|
||||||
return EXIT_SUCCESS;
|
(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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user