From 66c5134eb58e8566f564d59776bd5431c6333828 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 19 Oct 2025 23:04:04 +0100 Subject: [PATCH] Remove impl folder --- build.sh | 2 +- impl/constructor.c => constructor.c | 2 +- impl/test.c | 238 ---------------------------- main.c | 48 +----- impl/stream.c => stream.c | 2 +- impl/sv.c => sv.c | 2 +- impl/symtable.c => symtable.c | 2 +- impl/sys.c => sys.c | 2 +- impl/tag.c => tag.c | 2 +- test.c | 2 +- impl/vec.c => vec.c | 2 +- 11 files changed, 13 insertions(+), 291 deletions(-) rename impl/constructor.c => constructor.c (98%) delete mode 100644 impl/test.c rename impl/stream.c => stream.c (99%) rename impl/sv.c => sv.c (97%) rename impl/symtable.c => symtable.c (99%) rename impl/sys.c => sys.c (98%) rename impl/tag.c => tag.c (98%) rename impl/vec.c => vec.c (99%) diff --git a/build.sh b/build.sh index a3b91c6..c8e8cec 100644 --- a/build.sh +++ b/build.sh @@ -4,7 +4,7 @@ set -xe CFLAGS="-Wall -Wextra -std=c11 -ggdb -fsanitize=address -fsanitize=undefined -Wswitch -Wswitch-enum" LINK="-I." -LIB=$(find "./impl" -name "*.c" -not -name "main.c" -not -name "test.c") +LIB=$(find "./" -name "*.c" -not -name "main.c" -not -name "test.c") OUT="alisp.out" build() { diff --git a/impl/constructor.c b/constructor.c similarity index 98% rename from impl/constructor.c rename to constructor.c index 4c8efa4..e0fab52 100644 --- a/impl/constructor.c +++ b/constructor.c @@ -14,7 +14,7 @@ #include -#include +#include "./alisp.h" lisp_t *make_int(i64 i) { diff --git a/impl/test.c b/impl/test.c deleted file mode 100644 index e517286..0000000 --- a/impl/test.c +++ /dev/null @@ -1,238 +0,0 @@ -/* Copyright (C) 2025 Aryadev Chavali - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Unlicense - * for details. - - * You may distribute and modify this code under the terms of the - * Unlicense, which you should have received a copy of along with this - * program. If not, please go to . - - * Created: 2025-08-21 - * Description: Tests - */ - -#include -#include - -#include - -#define TEST_PASSED() printf("[%s]: Pass\n", __func__) -#define TEST(NAME, COND) \ - do \ - { \ - if (!(COND)) \ - { \ - printf("[%s]: `%s` failed!\n", __func__, (NAME)); \ - assert(0); \ - } \ - } while (0) - -// Sample data -const char *unique_words[] = { - "bibendum", "etiam", "gravida", "dui", "cursus", - "purus", "diam", "phasellus", "nam", "fermentum", - "leo", "enim", "ac", "semper", "non", - "mauris", "proin", "tellus", "vivamus", "lobortis", - "lacus", "neque", "in", "nullam", "felis", - "orci", "pede", "tempus", "nec", "at", - "tortor", "massa", "sed", "magna", "eget", - "tempor", "velit", "imperdiet", "praesent", "volutpat", - "tristique", "id", "commodo", "aliquet", "quis", - "pellentesque", "eleifend", "porta", "nunc", "euismod", - "aliquam", "a", "erat", "dignissim", "ut", - "vitae", "vel", "donec", -}; - -char *words[] = { - "aliquam", "erat", "volutpat", "nunc", "eleifend", - "leo", "vitae", "magna", "in", "id", - "erat", "non", "orci", "commodo", "lobortis", - "proin", "neque", "massa", "cursus", "ut", - "gravida", "ut", "lobortis", "eget", "lacus", - "sed", "diam", "praesent", "fermentum", "tempor", - "tellus", "nullam", "tempus", "mauris", "ac", - "felis", "vel", "velit", "tristique", "imperdiet", - "donec", "at", "pede", "etiam", "vel", - "neque", "nec", "dui", "dignissim", "bibendum", - "vivamus", "id", "enim", "phasellus", "neque", - "orci", "porta", "a", "aliquet", "quis", - "semper", "a", "massa", "phasellus", "purus", - "pellentesque", "tristique", "imperdiet", "tortor", "nam", - "euismod", "tellus", "id", "erat", -}; - -char words_text[] = - "aliquam erat volutpat nunc eleifend leo vitae magna in id erat non orci " - "commodo lobortis proin neque massa cursus ut gravida ut lobortis eget " - "lacus sed diam praesent fermentum tempor tellus nullam tempus mauris ac " - "felis vel velit tristique imperdiet donec at pede etiam vel neque nec dui " - "dignissim bibendum vivamus id enim phasellus neque orci porta a aliquet " - "quis semper a massa phasellus purus pellentesque tristique imperdiet " - "tortor nam euismod tellus id erat"; - -char text[] = - "Pellentesque dapibus suscipit ligula. Donec posuere augue in quam. " - "Etiam vel tortor sodales tellus ultricies commodo. Suspendisse potenti. " - "Aenean in sem ac leo mollis blandit. Donec neque quam, dignissim in, " - "mollis nec, sagittis eu, wisi. Phasellus lacus. Etiam laoreet quam sed " - "arcu. Phasellus at dui in ligula mollis ultricies. Integer placerat " - "tristique nisl. Praesent augue. Fusce commodo. Vestibulum convallis, " - "lorem a tempus semper, dui dui euismod elit, vitae placerat urna tortor " - "vitae lacus. Nullam libero mauris, consequat quis, varius et, dictum id, " - "arcu. Mauris mollis tincidunt felis. Aliquam feugiat tellus ut neque. " - "Nulla facilisis, risus a rhoncus fermentum, tellus tellus lacinia purus, " - "et dictum nunc justo sit amet elit."; - -void symtable_test(void) -{ - sym_table_t table = {0}; - sym_table_init(&table); - for (u64 i = 0; i < ARRSIZE(words); ++i) - sym_table_find(&table, SV(words[i], strlen(words[i]))); - - TEST("|table|=|set(words)|", table.count == ARRSIZE(unique_words)); - TEST("|table| < |words|", table.count < ARRSIZE(words)); - - TEST_PASSED(); - - sym_table_cleanup(&table); -} - -void int_test(void) -{ - i64 ints[] = { - 1, -1, (1 << 10) - 1, (-1) * ((1 << 10) - 1), INT_MIN, INT_MAX, - }; - - for (u64 i = 0; i < ARRSIZE(ints); ++i) - { - i64 in = ints[i]; - lisp_t *lisp = make_int(in); - i64 out = as_int(lisp); - - TEST("in == out", in == out); - } - - TEST_PASSED(); -} - -void sym_test(void) -{ - sys_t system = {0}; - sys_init(&system); - for (u64 i = 0; i < ARRSIZE(words); ++i) - { - char *in = words[i]; - lisp_t *lisp = intern(&system, SV(in, strlen(in))); - char *out = as_sym(lisp); - TEST("unique pointers when interning a symbol", in != out); - TEST("same size", strlen(in) == strlen(out)); - TEST("same string", strncmp(in, out, strlen(in)) == 0); - } - TEST_PASSED(); - sys_cleanup(&system); -} - -void vec_test1(void) -{ - sys_t system = {0}; - sys_init(&system); - - // Generating a vector word by word - lisp_t *lvec = make_vec(&system, 0); - for (u64 i = 0; i < ARRSIZE(words); ++i) - { - char *word = words[i]; - vec_append(as_vec(lvec), word, strlen(word)); - if (i != ARRSIZE(words) - 1) - vec_append(as_vec(lvec), " ", 1); - } - vec_append(as_vec(lvec), "\0", 1); - - vec_t *vec = as_vec(lvec); - - TEST("same size vector", vec->size == ARRSIZE(words_text)); - TEST("same as actually concatenated string", - strncmp(vec_data(vec), words_text, vec->size) == 0); - - TEST_PASSED(); - sys_cleanup(&system); -} - -void vec_test2(void) -{ - sys_t system = {0}; - sys_init(&system); - // Generating substrings - struct Test - { - u64 start, size; - } tests[] = { - {0, 16}, - {0, 32}, - {32, 64}, - {0, ARRSIZE(text)}, - }; - - for (u64 i = 0; i < ARRSIZE(tests); ++i) - { - struct Test test = tests[i]; - const sv_t substr = SV(text + test.start, test.size); - const u64 size = test.size / 2; - - lisp_t *lvec = make_vec(&system, size); - vec_append(as_vec(lvec), text + test.start, test.size); - TEST("Vector grew", as_vec(lvec)->size > size); - TEST("Vector's substring is equivalent to the actual substring", - strncmp(vec_data(as_vec(lvec)), substr.data, substr.size) == 0); - } - - printf("[vec_test2]: Pass\n"); - sys_cleanup(&system); -} - -void cons_test(void) -{ - sys_t system = {0}; - sys_init(&system); - - // Let's make a list of words using `cons` - lisp_t *lisp = NIL; - for (u64 i = 0; i < ARRSIZE(words); ++i) - { - char *word = words[i]; - lisp_t *lword = intern(&system, SV(word, strlen(word))); - lisp = cons(&system, lword, lisp); - } - - // Make sure we've essentially reversed the `words` array - u64 i = ARRSIZE(words); - for (lisp_t *iter = lisp; iter; iter = cdr(iter)) - { - lisp_t *item = car(iter); - TEST("we've reversed the array", - strncmp(words[i - 1], as_sym(item), strlen(words[i - 1])) == 0); - i -= 1; - } - - TEST_PASSED(); - - sys_cleanup(&system); -} - -typedef void (*test_fn)(void); - -const test_fn TESTS[] = { - int_test, sym_test, vec_test1, vec_test2, cons_test, -}; - -int main(void) -{ - for (u64 i = 0; i < ARRSIZE(TESTS); ++i) - { - TESTS[i](); - } - return 0; -} diff --git a/main.c b/main.c index 4db6f68..788df7a 100644 --- a/main.c +++ b/main.c @@ -16,54 +16,14 @@ #include #include -#include +#include "./alisp.h" int main(void) { stream_t stream = {0}; - char _data[] = "Hello, world!"; - sv_t data = SV(_data, ARRSIZE(_data) - 1); - char filename[] = "lorem.txt"; - - // stream_init_string(&stream, NULL, data); - - FILE *fp = fopen(filename, "rb"); - stream_init_file(&stream, filename, fp); - - // stream_init_file(&stream, "stdin", stdin); - - /// test 1 - do - { - printf("%s[%lu]: `%c`\n", stream.name, stream.position, - stream_next(&stream)); - } while (!stream_eoc(&stream)); - printf("%lu/%lu\n", stream.position, stream_size(&stream)); - - /// test 2 - stream.position = 0; - sv_t a = stream_substr(&stream, 100); - sv_t a_ = sv_copy(a); - stream_seek(&stream, 100); - sv_t b = stream_substr_abs(&stream, 0, 100); - sv_t b_ = sv_copy(b); - printf("a=b ? %s\na_=b_ ? %s\n", - memcmp(&a, &b, sizeof(a)) == 0 ? "yes" : "no", - a_.size == b_.size && strncmp(a_.data, b_.data, a_.size) == 0 ? "yes" - : "no"); - sv_t c = stream_substr(&stream, 100); - sv_t c_ = sv_copy(c); - stream_seek(&stream, 100); - sv_t d = stream_substr_abs(&stream, stream.position - 100, 100); - sv_t d_ = sv_copy(d); - printf("c=d ? %s\nc_=d_ ? %s\n", - memcmp(&c, &d, sizeof(a)) == 0 ? "yes" : "no", - c_.size == d_.size && strncmp(c_.data, d_.data, c_.size) == 0 ? "yes" - : "no"); - - printf("eoc?=%s\n", stream_eoc(&stream) ? "yes" : "no"); - + stream_init_pipe(&stream, "", stdin); + sv_t sv = stream_substr(&stream, 10); + printf("=> `" PR_SV "`\n", SV_FMT(sv)); stream_stop(&stream); - // fclose(fp); return 0; } diff --git a/impl/stream.c b/stream.c similarity index 99% rename from impl/stream.c rename to stream.c index 7b5e4e7..e163066 100644 --- a/impl/stream.c +++ b/stream.c @@ -14,7 +14,7 @@ #include -#include +#include "./alisp.h" stream_err_t stream_init_string(stream_t *stream, char *name, sv_t contents) { diff --git a/impl/sv.c b/sv.c similarity index 97% rename from impl/sv.c rename to sv.c index 97774c6..7debd30 100644 --- a/impl/sv.c +++ b/sv.c @@ -16,7 +16,7 @@ #include #include -#include +#include "./alisp.h" sv_t sv_copy(sv_t old) { diff --git a/impl/symtable.c b/symtable.c similarity index 99% rename from impl/symtable.c rename to symtable.c index abef727..39139eb 100644 --- a/impl/symtable.c +++ b/symtable.c @@ -12,7 +12,7 @@ * Description: Symbol Table implementation */ -#include +#include "./alisp.h" #include #include diff --git a/impl/sys.c b/sys.c similarity index 98% rename from impl/sys.c rename to sys.c index 4dcd664..d01b493 100644 --- a/impl/sys.c +++ b/sys.c @@ -12,7 +12,7 @@ * Description: System management */ -#include +#include "./alisp.h" #include #include diff --git a/impl/tag.c b/tag.c similarity index 98% rename from impl/tag.c rename to tag.c index e6396cc..8ca688a 100644 --- a/impl/tag.c +++ b/tag.c @@ -15,7 +15,7 @@ #include #include -#include +#include "./alisp.h" lisp_t *tag_int(i64 i) { diff --git a/test.c b/test.c index e517286..c4c1838 100644 --- a/test.c +++ b/test.c @@ -16,7 +16,7 @@ #include #include -#include +#include "./alisp.h" #define TEST_PASSED() printf("[%s]: Pass\n", __func__) #define TEST(NAME, COND) \ diff --git a/impl/vec.c b/vec.c similarity index 99% rename from impl/vec.c rename to vec.c index 12f577c..66a9682 100644 --- a/impl/vec.c +++ b/vec.c @@ -16,7 +16,7 @@ #include #include -#include +#include "./alisp.h" void vec_init(vec_t *vec, u64 size) {