aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-14 12:17:46 +0100
committerkdx <kikoodx@paranoici.org>2023-01-14 12:17:46 +0100
commitcb750f75bcf9d5e23271d1df128d6c09bcdb917f (patch)
treedf0ede2deb3defcd9fc906ded893d338068a768b
parent33d1a289f1237434ff906dd3d42970a976601089 (diff)
downloadsloth-cb750f75bcf9d5e23271d1df128d6c09bcdb917f.tar.gz
sloth context
-rw-r--r--main.c8
-rw-r--r--sloth.c10
-rw-r--r--sloth.h16
3 files changed, 34 insertions, 0 deletions
diff --git a/main.c b/main.c
index 001f62d..ca14497 100644
--- a/main.c
+++ b/main.c
@@ -1,16 +1,24 @@
#include "getln.h"
+#include "sloth.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
(void)argc, (void)argv;
+ /* Initialize sloth. */
+ Sloth *const sloth = calloc(1, sizeof(Sloth));
+ if (sloth == NULL)
+ return 1;
/* Read stdin line by line until exhaustion. */
char *line = NULL;
while ((line = getln(stdin)) != NULL) {
printf("%s", line);
free(line);
}
+ /* Free everything. */
getln_cleanup();
+ sloth_deinit(sloth);
+ free(sloth);
return 0;
}
diff --git a/sloth.c b/sloth.c
new file mode 100644
index 0000000..99222a9
--- /dev/null
+++ b/sloth.c
@@ -0,0 +1,10 @@
+#include "sloth.h"
+#include <stdlib.h>
+
+void sloth_deinit(Sloth *ctx)
+{
+ if (ctx->stack != NULL) {
+ free(ctx->stack);
+ ctx->stack = NULL;
+ }
+}
diff --git a/sloth.h b/sloth.h
new file mode 100644
index 0000000..9b30e93
--- /dev/null
+++ b/sloth.h
@@ -0,0 +1,16 @@
+#pragma once
+#include <stdint.h>
+#include <stddef.h>
+
+/* Replace '16' by desired byte size on both following lines. */
+#define SLOTH_BITS 16
+typedef uint16_t SlothByte;
+
+typedef struct Sloth {
+ SlothByte *stack;
+ size_t stack_size;
+ size_t stack_capacity;
+ SlothByte mem[1 << SLOTH_BITS];
+} Sloth;
+
+void sloth_deinit(Sloth *ctx);