diff options
author | kdx <kikoodx@paranoici.org> | 2023-01-14 12:17:46 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-01-14 12:17:46 +0100 |
commit | cb750f75bcf9d5e23271d1df128d6c09bcdb917f (patch) | |
tree | df0ede2deb3defcd9fc906ded893d338068a768b | |
parent | 33d1a289f1237434ff906dd3d42970a976601089 (diff) | |
download | sloth-cb750f75bcf9d5e23271d1df128d6c09bcdb917f.tar.gz |
sloth context
-rw-r--r-- | main.c | 8 | ||||
-rw-r--r-- | sloth.c | 10 | ||||
-rw-r--r-- | sloth.h | 16 |
3 files changed, 34 insertions, 0 deletions
@@ -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; } @@ -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; + } +} @@ -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); |