aboutsummaryrefslogtreecommitdiff
path: root/sloth.c
diff options
context:
space:
mode:
Diffstat (limited to 'sloth.c')
-rw-r--r--sloth.c60
1 files changed, 59 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);