From 645a8026b6f6a89cda5a24d96cf3de39c3ab32e1 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Thu, 10 Jul 2025 19:44:04 +0300 Subject: initial commit --- test/compile_main.sh | 8 ++++++++ test/main.c | 19 +++++++++++++++++++ test/vec.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100755 test/compile_main.sh create mode 100644 test/main.c create mode 100644 test/vec.h (limited to 'test') diff --git a/test/compile_main.sh b/test/compile_main.sh new file mode 100755 index 0000000..5467005 --- /dev/null +++ b/test/compile_main.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eu + +mkdir -p build/test +gcc -E test/main.c -o build/test/main.c +./ngc build/test/main.c > build/test/main_ngc.c +gcc build/test/main_ngc.c -o build/test/main diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..489618b --- /dev/null +++ b/test/main.c @@ -0,0 +1,19 @@ +#include +#include "vec.h" + +typedef ints = vec[int](); + +int main() +{ + struct ints a = intscreate(); + for (size_t i = 0; i < 10; ++i) + intsappend(&a, i); + + int sum = 0; + for (size_t i = 0; i < 10; ++i) { + sum += *intsat(&a, i); + } + + printf("%i\n", sum); + intsdestroy(&a); +} diff --git a/test/vec.h b/test/vec.h new file mode 100644 index 0000000..810e663 --- /dev/null +++ b/test/vec.h @@ -0,0 +1,49 @@ +#ifndef VEC_H +#define VEC_H + +#include +#include +#include + +typedef any {}; + +typedef vec[any type]() { + struct <> { + size_t n; + size_t s; + type *buf; + }; + + static inline struct <> <>create() + { + return (struct <>){ + .n = 0, + .s = 0, + .buf = NULL + }; + } + + static inline void <>append(struct <> *v, type n) + { + v->n++; + if (v->n > v->s) { + v->s = v->s == 0 ? 1 : 2 * v->s; + v->buf = realloc(v->buf, v->s * sizeof(type)); + } + + v->buf[v->n - 1] = n; + } + + static inline type *<>at(struct <> *v, size_t i) + { + assert(i < v->n); + return &v->buf[i]; + } + + static inline void <>destroy(struct <> *v) + { + free(v->buf); + } +} + +#endif /* VEC_H */ -- cgit v1.2.3