From 2ffe0bcaa096bf06eb986d51d1ccf6ad6c22775d Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Sun, 10 May 2026 22:04:38 -0500 Subject: [PATCH] . --- Makefile | 6 +++--- lib/stack.c | 57 +++++++++++++++++++++++++++++++++++++++++----------- lib/tyche.h | 5 +++++ lib/value.c | 18 ++++++++--------- test/tests.c | 14 +++++++++++++ 5 files changed, 75 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 8588edc..43de1f8 100644 --- a/Makefile +++ b/Makefile @@ -30,12 +30,12 @@ endif DEBUG_CFLAGS=-Og -ggdb3 ${WARNINGS} -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined -fsanitize=leak \ -fno-sanitize-recover=all -fstack-protector-strong -fstack-clash-protection -fno-common ${ADD_DBG_FLAGS} \ -DCHECK_TYCHE_BUGS=1 -DEBUG_LDFLAGS=-fsanitize=address +DEBUG_LDFLAGS=-fsanitize=address -fsanitize=undefined -fsanitize=leak RELEASE_CFLAGS=-O3 -flto=auto -march=native -mtune=native -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -fstack-protector-strong RELEASE_LDFLAGS=-flto=auto -CFLAGS+=-std=c11 -fPIC -fvisibility=hidden +CFLAGS+=-std=c99 -fPIC -fvisibility=hidden LDFLAGS+= # @@ -50,7 +50,7 @@ check: ./tyche-test clean: - rm -f tyche libtyche.a libtyche.so* tyche-test src/tyche.o src/tests.o lib/vm.o src/*.d lib/*.d + rm -f tyche libtyche.a libtyche.so* tyche-test **/*.o src/*.d lib/*.d install: tyche libtyche.a libtyche.so.${VERSION} lib/tyche.h install -m 644 libtyche.a libtyche.so.${VERSION} ${PREFIX}/lib diff --git a/lib/stack.c b/lib/stack.c index 1dc9ef9..6996e7d 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -11,6 +11,8 @@ typedef struct { size_t fp_cap; } Stack; +static TYC_RESULT stack_push_fp(Stack* s); + static void stack_init(Stack* s) { s->stack_n = s->fp_n = 0; @@ -21,15 +23,17 @@ static void stack_init(Stack* s) assert(s->stack); assert(s->fp); + + stack_push_fp(s); } -static void stack_destroy(Stack* s) +static void stack_finalize(Stack* s) { free(s->stack); free(s->fp); } -static void stack_push(Stack* s, VALUE v) +static TYC_RESULT stack_push(Stack* s, VALUE v) { if (s->stack_n == s->stack_cap) { s->stack_cap *= 2; @@ -39,40 +43,69 @@ static void stack_push(Stack* s, VALUE v) s->stack[s->stack_n] = v; ++s->stack_n; + return T_OK; } -static VALUE stack_pop(Stack* s) +static size_t stack_top_fp(Stack* s) { + return s->fp[s->fp_n - 1]; } -static VALUE stack_peek(Stack* s) +static TYC_RESULT stack_peek(Stack* s, VALUE* v_out) { + if (s->stack_n < stack_top_fp(s)) + return T_ERR_STACK_UNDERFLOW; + *v_out = s->stack[s->stack_n - 1]; + return T_OK; } -static uint32_t stack_len(Stack* s) +static TYC_RESULT stack_pop(Stack* s, VALUE* v_out) { + TYC_RESULT err = stack_peek(s, v_out); + if (err) + return err; + --s->stack_n; + return T_OK; +} + +static size_t stack_len(Stack* s) +{ + return s->stack_n - stack_top_fp(s); } static VALUE stack_get(Stack* s, int32_t key) { + abort(); // TODO } static void stack_set(Stack* s, int32_t key, VALUE v) { + abort(); // TODO } -static void stack_push_fp(Stack* s) +static TYC_RESULT stack_push_fp(Stack* s) { + if (s->fp_n == s->fp_cap) { + s->fp_cap *= 2; + s->fp = realloc(s->fp, s->fp_cap * sizeof s->fp[0]); + assert(s->fp); + } + + s->fp[s->fp_n] = (uint32_t) s->stack_n; + ++s->fp_n; + return T_OK; } -static void stack_pop_fp(Stack* s) +static TYC_RESULT stack_pop_fp(Stack* s) { + if (s->fp_n == 1) + return T_ERR_STACK_FP_UNDERFLOW; + s->stack_n = stack_top_fp(s); + --s->fp_n; + return T_OK; } -static uint32_t stack_top_fp(Stack* s) -{ -} - -static uint32_t stack_fp_level(Stack* s) +static size_t stack_fp_level(Stack* s) { + return s->fp_n; } \ No newline at end of file diff --git a/lib/tyche.h b/lib/tyche.h index f730485..ec3a9a7 100644 --- a/lib/tyche.h +++ b/lib/tyche.h @@ -5,4 +5,9 @@ typedef enum { TT_NIL, TT_INTEGER, TT_REAL, TT_STRING, TT_STRING_CONST, TT_ARRAY, TT_TABLE, TT_FUNCTION, TT_NATIVE_PTR, } TYC_TYPE; +typedef enum { + T_OK = 0, + T_ERR_STACK_UNDERFLOW = -1, T_ERR_STACK_FP_UNDERFLOW = -2, +} TYC_RESULT; + #endif //TYCHE_TYCHE_H diff --git a/lib/value.c b/lib/value.c index 539c7d2..ed14ca8 100644 --- a/lib/value.c +++ b/lib/value.c @@ -11,11 +11,9 @@ typedef struct { int32_t i; float f; uint32_t idx; - }; + } v; } VALUE; -static_assert(sizeof(VALUE) <= 8, "VALUE must be < 8 bytes"); - static TYC_TYPE value_type(VALUE v) { return v.type; @@ -27,7 +25,7 @@ static int32_t value_integer(VALUE v) if (v.type != TT_INTEGER) abort(); #endif - return v.i; + return v.v.i; } static float value_real(VALUE v) @@ -36,7 +34,7 @@ static float value_real(VALUE v) if (v.type != TT_REAL) abort(); #endif - return v.f; + return v.v.f; } static uint32_t value_idx(VALUE v) @@ -45,7 +43,7 @@ static uint32_t value_idx(VALUE v) if (v.type != TT_FUNCTION && v.type != TT_NATIVE_PTR && v.type != TT_ARRAY && v.type != TT_TABLE && v.type != TT_STRING && v.type != TT_STRING_CONST) abort(); #endif - return v.idx; + return v.v.idx; } static VALUE create_value_nil() @@ -55,12 +53,12 @@ static VALUE create_value_nil() static VALUE create_value_integer(int32_t v) { - return (VALUE) { .type = TT_INTEGER, .i = v }; + return (VALUE) { .type = TT_INTEGER, .v = { .i = v } }; } static VALUE create_value_real(float f) { - return (VALUE) { .type = TT_INTEGER, .f = f }; + return (VALUE) { .type = TT_REAL, .v = { .f = f } }; } static VALUE create_value_idx(TYC_TYPE type, uint32_t idx) @@ -69,10 +67,10 @@ static VALUE create_value_idx(TYC_TYPE type, uint32_t idx) if (type != TT_FUNCTION && type != TT_NATIVE_PTR && type != TT_ARRAY && type != TT_TABLE && type != TT_STRING && type != TT_STRING_CONST) abort(); #endif - return (VALUE) { .type = type, .idx = idx }; + return (VALUE) { .type = type, .v = { .idx = idx } }; } static bool value_is_zero(VALUE v) { - return v.type == TT_NIL || (v.type == TT_INTEGER && v.i == 0); + return v.type == TT_NIL || (v.type == TT_INTEGER && v.v.i == 0); } \ No newline at end of file diff --git a/test/tests.c b/test/tests.c index 55ecc68..913eeda 100644 --- a/test/tests.c +++ b/test/tests.c @@ -2,6 +2,7 @@ #include #include +#include #include #define EQ(a, b) (memcmp(a, b) == 0) @@ -9,8 +10,21 @@ int main() { // values + printf("### Values\n"); assert(value_type(create_value_integer(42)) == TT_INTEGER); assert(value_integer(create_value_integer(-42)) == -42); assert(fabsf(value_real(create_value_real(42.4f)) - 42.4f) < 0.00001f); assert(value_idx(create_value_idx(TT_FUNCTION, 42)) == 42); + + // values + printf("### Stack\n"); + Stack s; + stack_init(&s); + stack_push(&s, create_value_integer(10)); + stack_push(&s, create_value_integer(20)); + stack_push(&s, create_value_integer(30)); + + assert(stack_len(&s) == 3); + + stack_finalize(&s); } \ No newline at end of file