From 93be8bc9607b14bd4d3a13c24d9abc0cc7c80d40 Mon Sep 17 00:00:00 2001 From: Mark Delk Date: Sat, 22 Jan 2022 02:00:15 -0600 Subject: [PATCH] add missing headers, fix gitignore, rm c-only test framework --- .gitignore | 3 +- include/holyc/_parse.h | 16 +++++++ include/holyc/ast.h | 36 ++++++++++++++++ include/holyc/buffer.h | 10 +++++ lib/testing/README.md | 7 ---- lib/testing/include/testing.h | 40 ------------------ lib/testing/makefile | 52 ----------------------- lib/testing/src/testing.c | 52 ----------------------- makefile | 17 +------- test/asm.c | 78 ----------------------------------- test/main.c | 13 ------ 11 files changed, 65 insertions(+), 259 deletions(-) create mode 100644 include/holyc/_parse.h create mode 100644 include/holyc/ast.h create mode 100644 include/holyc/buffer.h delete mode 100644 lib/testing/README.md delete mode 100644 lib/testing/include/testing.h delete mode 100644 lib/testing/makefile delete mode 100644 lib/testing/src/testing.c delete mode 100644 test/asm.c delete mode 100644 test/main.c diff --git a/.gitignore b/.gitignore index bbcfdab..daccd6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -holyc -**/*.o +*.o *.a test/bin/ diff --git a/include/holyc/_parse.h b/include/holyc/_parse.h new file mode 100644 index 0000000..76b4c4a --- /dev/null +++ b/include/holyc/_parse.h @@ -0,0 +1,16 @@ +#ifndef HOLYC__PARSE +#define HOLYC__PARSE + +#include +#include + +typedef struct Parser { + NodeNameTable node_name_table; + Lexer lexer; +} Parser; + +Parser *parse_new(Parser *parser, char *input, int size); +AstNode *parse_parse(Parser *parser); +void parse_print_node(Parser *parser, AstNode *node); + +#endif // HOLYC__PARSE diff --git a/include/holyc/ast.h b/include/holyc/ast.h new file mode 100644 index 0000000..d521409 --- /dev/null +++ b/include/holyc/ast.h @@ -0,0 +1,36 @@ +#ifndef HOLYC_AST +#define HOLYC_AST + +typedef enum { + NODE_UNINITIALIZED, + NODE_INT, + NODE_BINOP_PLUS, + NODE_BINOP_MIN, + NODE_BINOP_MUL, + NODE_BINOP_DIV, + NODE_EXPR, +} AstType; + +#define NUM_NODES 7 + +typedef char *NodeNameTable[NUM_NODES]; + +typedef struct AstNode { + AstType type; + int value; + + struct AstNode *left; + struct AstNode *right; + + struct AstNode *expr_value; +} AstNode; + +// typedef struct BinOpNode { +// AstNode left; +// AstNode right; +// BinOpType type; +// } BinOpNode; + +AstNode *ast_new(AstNode *node, AstType type, int value); + +#endif // HOLYC_AST diff --git a/include/holyc/buffer.h b/include/holyc/buffer.h new file mode 100644 index 0000000..ed8f0b4 --- /dev/null +++ b/include/holyc/buffer.h @@ -0,0 +1,10 @@ +#ifndef HOLYC_BUFFER +#define HOLYC_BUFFER + +typedef struct Buffer { + char *start; + char *curr; + int size; +} Buffer; + +#endif // HOLYC_BUFFER diff --git a/lib/testing/README.md b/lib/testing/README.md deleted file mode 100644 index 89563e1..0000000 --- a/lib/testing/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# lib/testing - -minimal test framework. - -## license - -MIT diff --git a/lib/testing/include/testing.h b/lib/testing/include/testing.h deleted file mode 100644 index 436dda5..0000000 --- a/lib/testing/include/testing.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef TESTING_H -#define TESTING_H - -// Small test framework based on Go lang's `testing` pkg: -// https://pkg.go.dev/testing - -//-- - -#include -#include -#include -#include - -#define TEST_NAME_MAX_LEN 64 - -typedef void (*TestFn)(); - -// A single test. -// -typedef struct Test { - TestFn fn; - char name[TEST_NAME_MAX_LEN]; - bool passed; -} Test; - -// The test suite's context. -// -typedef struct TestRunner { - int argc; // argc from main() - char **argv; // argv from main() - int num_tests; // number of tests - Test tests[]; // tests to run -} T; - -void assert(Test *t, bool expr, char *msg); -T *test_init(Test *tests, int num_tests, int argc, char **argv); -bool run_test(T *t, int i); -int run_tests(T *t); - -#endif // TESTING_H diff --git a/lib/testing/makefile b/lib/testing/makefile deleted file mode 100644 index 1649718..0000000 --- a/lib/testing/makefile +++ /dev/null @@ -1,52 +0,0 @@ -UNAME := $(shell uname) - -#-- - -CFLAGS += -static -CFLAGS += -fPIC -CFLAGS += -nostdlib -ffreestanding -CFLAGS += -I include - -CFLAGS += -g -O0 - -CFLAGS += -Wall -CFLAGS += -Werror - -ifeq ($(UNAME), Linux) - CFLAGS += -fleading-underscore -endif -ifeq ($(UNAME), Darwin) - CFLAGS += -fno-stack-protector -endif - -#-- - -SHARED_LIB := libtesting.a -HEADERS := $(wildcard include/*.h) -SRCS := $(wildcard src/*.c) -SRC_OBJS := $(SRCS:.c=.o) -TESTS := $(wildcard test/*.c) -TEST_BINS := $(patsubst test/%.c,test/bin/%,$(TESTS)) - -default: $(SHARED_LIB) - -test/bin: - mkdir -p test/bin - -test/bin/%: test/%.c $(SHARED_LIB) test/bin - $(CC) $(CFLAGS) -L. -ltesting -e _start -o $@ $< - -test: $(TEST_BINS) - # ./test/bin/crt0 - # ./test/bin/stdio; code=$$?; [ $$code -eq 42 ] || echo "error -- should exit 42, got $$code"; - # ./test/bin/unistd - -$(SHARED_LIB): $(SRC_OBJS) - ar rcs $@ $^ - file $@ - -clean: - rm -rf $(SRC_OBJS) $(TEST_BINS) $(SHARED_LIB) test/bin - -lint: $(SRCS) $(HEADERS) $(TESTS) - clang-format -i $^ diff --git a/lib/testing/src/testing.c b/lib/testing/src/testing.c deleted file mode 100644 index e9d6c68..0000000 --- a/lib/testing/src/testing.c +++ /dev/null @@ -1,52 +0,0 @@ -#include - -void assert(Test *t, bool expr, char *msg) { - printf(" %s %s\n", expr ? "." : "x", msg); - if (expr) - return; - t->passed = false; -} - -// Setup a new test context -// -T *test_init(Test *tests, int num_tests, int argc, char **argv) { - T *t = malloc(sizeof(T)); - t->argc = argc; - t->argv = argv; - t->num_tests = num_tests; - - for (int i = 0; i < t->num_tests; i++) { - t->tests[i] = tests[i]; - t->tests[i].passed = true; - } - - return t; -} - -// Run a single test. -// -bool run_test(T *t, int i) { - Test *test = &t->tests[i]; - printf("-- %s\n", test->name); - - test->fn(test); - - return test->passed; -} - -// Run all tests. -// -int run_tests(T *t) { - int failed = 0; - - for (int i = 0; i < t->num_tests; i++) - if (!run_test(t, i)) - failed++; - - if (failed) - printf("ERR\n"); - else - printf("OK\n"); - - return failed; -} diff --git a/makefile b/makefile index 11d6b16..c2d64dd 100644 --- a/makefile +++ b/makefile @@ -68,21 +68,15 @@ CFLAGS += -I . -I lib/c/include -I include #-- -default: clean $(PROG) test ctest +default: test SRCS := $(wildcard src/*.c) OBJS := $(SRCS:.c=.o) LIBC := lib/c/libc.a LIBC_FLAGS := -Llib/c -lc -LIBTESTING := lib/testing/libtesting.a -LIBTESTING_FLAGS := -I lib/testing/include -Llib/testing -ltesting -TEST_EXE := ctest.out - $(LIBC): $(MAKE) -C lib/c -$(LIBTESTING): - $(MAKE) -C lib/testing $(PROG): $(OBJS) | $(LIBC) $(CC) -e _start $(CFLAGS) $(LIBC_FLAGS) $^ $(LIBC) -o $(PROG) @@ -90,7 +84,6 @@ $(PROG): $(OBJS) | $(LIBC) clean: rm -f $(OBJS) $(PROG) *.out test/*.o $(TEST_MAIN) $(MAKE) -C lib/c clean - $(MAKE) -C lib/testing clean #-- test/bin: @@ -105,13 +98,7 @@ test/bin/asm: test/asm.o $(filter-out src/main.o, $(OBJS)) $(LIBC) | test/bin $(CC) -e _start $(CFLAGS) $(LIBC_FLAGS) $^ -o $@ ./$@ -$(TEST_EXE): test/main.c $(LIBC) $(LIBTESTING) $(filter-out src/main.o, $(OBJS)) - $(CC) -e _start $(CFLAGS) $(LIBC_FLAGS) $(LIBTESTING_FLAGS) $^ -o $@ - -ctest: $(TEST_EXE) - ./$< - -test: test/bin/lex test/bin/parse FORCE +test: test/bin/lex test/bin/parse $(PROG) FORCE sh test/main.sh FORCE: diff --git a/test/asm.c b/test/asm.c deleted file mode 100644 index 2e72a1f..0000000 --- a/test/asm.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include - -#include -#include -#include - -void _print_asm(AstNode *node, int indent) { - switch (node->type) { - case NODE_UNINITIALIZED: - break; - case NODE_INT: - printf(" PUSH %d\n", node->value); - break; - case NODE_BINOP_PLUS: - case NODE_BINOP_MIN: - case NODE_BINOP_MUL: - case NODE_BINOP_DIV: - printf(" POP RDI\n"); - printf(" POP RDX\n"); - - if (node->type == NODE_BINOP_MUL) - printf(" IMUL RAX,RDI\n"); - else - printf(" CQO IDIV RDI\n"); - - printf(" PUSH RAX\n"); - // _parse_print_node(parser, node->left, indent + 2); - // _parse_print_node(parser, node->right, indent + 2); - break; - default: { - } - } -} - -void print_asm(AstNode *node) { - printf("\nmain:\n"); - - _print_asm(node, 0); - - printf(" POP RAX\n"); - - printf("\n_start:\n"); - printf(" // CALL main\n"); - printf(" MOV RDI, RDX\n"); - printf(" MOV RAX, %d\n", SYSCALL_EXIT); - printf(" SYSCALL\n"); -} - -void test_asm(char *input) { - printf("'%s'\n", input); - - Parser *parser = malloc(sizeof(Parser)); - parse_new(parser, input, strlen(input)); - - AstNode *node = parse_parse(parser); - - print_asm(node); - - printf("\n"); -} - -int main(int argc, char **argv) { - printf("== asm tests ===\n\n"); - - test_asm("42;"); - test_asm("1 + 2;"); - test_asm("1 - 2;"); - test_asm("1 + 2 - 3;"); - test_asm("3 * 4;"); - test_asm("3 / 4;"); - test_asm("3 / 4 * 5;"); - test_asm("1 + 2 * 3;"); - test_asm("(1 + 2) * 3;"); - test_asm("(1+3-1)*(2*3/2)-7;"); - - return 0; -} diff --git a/test/main.c b/test/main.c deleted file mode 100644 index 66a937c..0000000 --- a/test/main.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include - -#include - -int main(int argc, char **argv) { - Test tests[] = { - // {test_cc_setup, "test_cc_setup"}, - }; - - T *t = test_init(tests, sizeof(tests) / sizeof(Test), argc, argv); - return run_tests(t); -}