aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-14 20:20:48 +0100
committerkdx <kikoodx@paranoici.org>2023-01-14 20:20:48 +0100
commitfcd4bafc4a678e4a573ef836bc737ebfb499050a (patch)
treedfd53345f91a530eb54be9a31d74eaaa823a9599
parent0e83662094b5b48d6170abbcab4cb56a1caec008 (diff)
downloadsloth-fcd4bafc4a678e4a573ef836bc737ebfb499050a.tar.gz
remaining operators
-rw-r--r--sloth.c60
-rw-r--r--sloth.h3
2 files changed, 62 insertions, 1 deletions
diff --git a/sloth.c b/sloth.c
index e64264c..dced371 100644
--- a/sloth.c
+++ b/sloth.c
@@ -18,6 +18,21 @@ SlothError sloth_init(Sloth *ctx)
sloth_deinit(ctx);
return err;
}
+ err = sloth_dict_append(ctx, SLOTH_DICT_C("@", sloth_store));
+ if (err != NULL) {
+ sloth_deinit(ctx);
+ return err;
+ }
+ err = sloth_dict_append(ctx, SLOTH_DICT_C("!", sloth_retrieve));
+ if (err != NULL) {
+ sloth_deinit(ctx);
+ return err;
+ }
+ err = sloth_dict_append(ctx, SLOTH_DICT_C("<", sloth_compare));
+ if (err != NULL) {
+ sloth_deinit(ctx);
+ return err;
+ }
return NULL;
}
@@ -65,7 +80,7 @@ SlothError sloth_exec(Sloth *ctx, const char *s)
}
for (size_t i = 0; s[i] != '\0'; i++)
if (!isdigit(s[i]))
- return "sloth_exec: unrecognized token";
+ return "sloth_exec: unrecognized token"
"a litteral";
err = sloth_push(ctx, atoi(s));
if (err != NULL)
@@ -140,6 +155,49 @@ SlothError sloth_write(Sloth *ctx)
return NULL;
}
+SlothError sloth_store(Sloth *ctx)
+{
+ SlothError err;
+ SlothByte a, b;
+ err = sloth_pop(ctx, &a);
+ if (err != NULL)
+ return err;
+ err = sloth_pop(ctx, &b);
+ if (err != NULL)
+ return err;
+ ctx->mem[a] = b;
+ return NULL;
+}
+
+SlothError sloth_retrieve(Sloth *ctx)
+{
+ SlothError err;
+ SlothByte a;
+ err = sloth_pop(ctx, &a);
+ if (err != NULL)
+ return err;
+ err = sloth_push(ctx, ctx->mem[a]);
+ if (err != NULL)
+ return err;
+ return NULL;
+}
+
+SlothError sloth_compare(Sloth *ctx)
+{
+ SlothError err;
+ SlothByte a, b;
+ err = sloth_pop(ctx, &a);
+ if (err != NULL)
+ return err;
+ err = sloth_pop(ctx, &b);
+ if (err != NULL)
+ return err;
+ err = sloth_push(ctx, b < a);
+ if (err != NULL)
+ return err;
+ return NULL;
+}
+
void sloth_inspect_stack(const Sloth *ctx)
{
printf("<%lu> ", ctx->stack_size);
diff --git a/sloth.h b/sloth.h
index 8032403..0fc4ceb 100644
--- a/sloth.h
+++ b/sloth.h
@@ -37,4 +37,7 @@ SlothError sloth_pop(Sloth *ctx, SlothByte *v);
SlothError sloth_push(Sloth *ctx, SlothByte v);
SlothError sloth_add(Sloth *ctx);
SlothError sloth_write(Sloth *ctx);
+SlothError sloth_store(Sloth *ctx);
+SlothError sloth_retrieve(Sloth *ctx);
+SlothError sloth_compare(Sloth *ctx);
void sloth_inspect_stack(const Sloth *ctx);