aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-14 21:24:47 +0100
committerkdx <kikoodx@paranoici.org>2023-01-14 21:24:47 +0100
commit49aeb9687849941d73e63669ff1313295e22af76 (patch)
tree956b7d8316c2377dcb29d9db7bfdd540a3ed43ad
parent37b650f1ea6086de77c8a46294cd04c35bcf0e02 (diff)
downloadsloth-49aeb9687849941d73e63669ff1313295e22af76.tar.gz
stdlib bs
-rw-r--r--README1
-rw-r--r--sloth.c26
-rw-r--r--sloth.h3
-rw-r--r--stdlib6
4 files changed, 34 insertions, 2 deletions
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 ;