From 49aeb9687849941d73e63669ff1313295e22af76 Mon Sep 17 00:00:00 2001 From: kdx Date: Sat, 14 Jan 2023 21:24:47 +0100 Subject: stdlib bs --- README | 1 + sloth.c | 26 +++++++++++++++++++++++++- sloth.h | 3 ++- stdlib | 6 ++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 stdlib diff --git a/README b/README index 81948dc..aff068d 100644 --- a/README +++ b/README @@ -15,6 +15,7 @@ supports string literal (array of bytes) + ( a b -- c ) < ( a b -- c ) . ( a -- ) +neg ( a -- b ) implementation of usually built-in features diff --git a/sloth.c b/sloth.c index 7c861c8..500c327 100644 --- a/sloth.c +++ b/sloth.c @@ -33,6 +33,11 @@ SlothError sloth_init(Sloth *ctx) sloth_deinit(ctx); return err; } + err = sloth_dict_append(ctx, SLOTH_DICT_C("neg", sloth_neg)); + if (err != NULL) { + sloth_deinit(ctx); + return err; + } err = sloth_dict_append(ctx, SLOTH_DICT_C(":", sloth_compile_begin)); if (err != NULL) { sloth_deinit(ctx); @@ -43,6 +48,11 @@ SlothError sloth_init(Sloth *ctx) sloth_deinit(ctx); return err; } + err = sloth_dict_append(ctx, SLOTH_DICT_C(".t", sloth_inspect_stack)); + if (err != NULL) { + sloth_deinit(ctx); + return err; + } return NULL; } @@ -283,6 +293,19 @@ SlothError sloth_compare(Sloth *ctx) return NULL; } +SlothError sloth_neg(Sloth *ctx) +{ + SlothError err; + SlothByte a; + err = sloth_pop(ctx, &a); + if (err != NULL) + return err; + err = sloth_push(ctx, -a); + if (err != NULL) + return err; + return NULL; +} + SlothError sloth_compile_begin(Sloth *ctx) { if (ctx->compile) @@ -311,9 +334,10 @@ SlothError sloth_compile_end(Sloth *ctx) return NULL; } -void sloth_inspect_stack(const Sloth *ctx) +SlothError sloth_inspect_stack(Sloth *ctx) { printf("<%lu> ", ctx->stack_size); for (size_t i = 0; i < ctx->stack_size; i++) printf("%d ", ctx->stack[i]); + return NULL; } diff --git a/sloth.h b/sloth.h index cc56b76..53d7c24 100644 --- a/sloth.h +++ b/sloth.h @@ -55,6 +55,7 @@ SlothError sloth_write(Sloth *ctx); SlothError sloth_store(Sloth *ctx); SlothError sloth_retrieve(Sloth *ctx); SlothError sloth_compare(Sloth *ctx); +SlothError sloth_neg(Sloth *ctx); SlothError sloth_compile_begin(Sloth *ctx); SlothError sloth_compile_end(Sloth *ctx); -void sloth_inspect_stack(const Sloth *ctx); +SlothError sloth_inspect_stack(Sloth *ctx); diff --git a/stdlib b/stdlib new file mode 100644 index 0000000..d89547b --- /dev/null +++ b/stdlib @@ -0,0 +1,6 @@ +: dup 0 @ 0 ! 0 ! ; +: drop 0 @ ; +: swap 0 @ 1 @ 0 ! 1 ! ; +: - neg + ; +: not 1 swap - ; +: = - not ; -- cgit v1.2.3