summaryrefslogtreecommitdiff
path: root/src/lib/main.c
diff options
context:
space:
mode:
authorpeter <peter@pmikkelsen.com>2026-08-30 20:52:43 +0000
committerpeter <peter@pmikkelsen.com>2026-08-30 20:52:43 +0000
commit729964bea692f9683860978b89848784a7fc2f46 (patch)
treeba5b37a61206142fce18214c7af341ac837a7923 /src/lib/main.c
parent04827577ea502a9f0edc69e00e008751c8119cbc (diff)
Still a lot to be done, but I'm going to bed.
Two remaining big issues: * aplwc (the parameter passed "everywhere") - should it move or not? * values returned from the public API should not move. FossilOrigin-Name: 31ab3154b8f3d5ac167218496e9e204324bf0ac9d36b7bada8fe2a6c8f93d51c
Diffstat (limited to 'src/lib/main.c')
-rw-r--r--src/lib/main.c82
1 files changed, 50 insertions, 32 deletions
diff --git a/src/lib/main.c b/src/lib/main.c
index 81cd6bb..03c31a5 100644
--- a/src/lib/main.c
+++ b/src/lib/main.c
@@ -2,24 +2,32 @@
#include "boot.h"
enum aplwc_rc
-aplwc_new(struct aplwc **p, uint8_t *data, size_t size)
+aplwc_new(struct aplwc **p, struct aplwc_fns fns)
{
- struct aplwc tmp_aplwc;
-
- memory_init(&tmp_aplwc, data, size);
-
- *p = memory_alloc_aplwc(&tmp_aplwc);
- if(*p == NULL)
- return tmp_aplwc.last_error;
- memcpy(*p, &tmp_aplwc, sizeof(tmp_aplwc));
-
- return aplwc_load_module(*p, "Boot", (const char *)boot_src, boot_src_len);
+ struct aplwc *aplwc;
+ size_t size;
+
+ if(fns.alloc == NULL)
+ return APLWC_OUT_OF_MEMORY;
+ aplwc = fns.alloc(sizeof(*aplwc), sizeof(*aplwc), &size);
+ if(aplwc == NULL)
+ return APLWC_OUT_OF_MEMORY;
+ *p = aplwc;
+
+ aplwc->fns = fns;
+ memory_init(aplwc);
+ return aplwc_load_module(aplwc, "Boot", (const char *)boot_src, boot_src_len);
}
void
aplwc_destroy(struct aplwc *p)
{
- UNUSED_PARAMETER(p);
+ if(p == NULL)
+ return;
+
+ memory_destroy(p);
+ if(p->fns.dealloc)
+ p->fns.dealloc(p);
}
const char *
@@ -70,26 +78,30 @@ aplwc_read(struct aplwc *aplwc, const char *line)
struct token_list *tokens;
struct aplwc_line *ret;
- text = chars_to_string(aplwc, line, -1);
- if(text == NULL)
- return NULL;
+ MEMPTRS(aplwc, 2);
+
+ ret = NULL;
- tokens = scan_string(aplwc, text, 1);
- if(tokens == NULL)
- return NULL;
+ if((text = chars_to_string(aplwc, line, -1)) == NULL)
+ goto end;
+ MEMPTR(0, text);
- aplwc->last_error = check_tokens(tokens);
- if(aplwc->last_error != APLWC_OK)
- return NULL;
+ if((tokens = scan_string(aplwc, text, 1)) == NULL)
+ goto end;
+ MEMPTR(1, tokens);
- ret = memory_alloc_aplwc_line(aplwc);
- if(ret == NULL)
- return NULL;
+ if((aplwc->last_error = check_tokens(tokens)) != APLWC_OK)
+ goto end;
+
+ if((ret = memory_alloc_aplwc_line(aplwc)) == NULL)
+ goto end;
ret->aplwc = aplwc;
ret->text = text;
ret->tokens = tokens;
+end:
+ MEMPTRS_EXIT(aplwc);
return ret;
}
@@ -131,16 +143,22 @@ aplwc_load_module(struct aplwc *aplwc, const char *name, const char *src, int sr
struct string *text, *namestr;
struct token_list *tokens;
+ MEMPTRS(aplwc, 3);
+
if((namestr = chars_to_string(aplwc, name, -1)) == NULL)
- goto error;
+ goto end;
+ MEMPTR(0, namestr);
+
if((text = chars_to_string(aplwc, src, srclen)) == NULL)
- goto error;
+ goto end;
+ MEMPTR(1, text);
+
if((tokens = scan_string(aplwc, text, 0)) == NULL)
- goto error;
- if(load_module(aplwc, namestr, tokens) != APLWC_OK){
-error:
- return aplwc_last_error(aplwc);
- }
+ goto end;
+ MEMPTR(2, tokens);
- return APLWC_OK;
+ aplwc->last_error = load_module(aplwc, namestr, tokens);
+end:
+ MEMPTRS_EXIT(aplwc);
+ return aplwc_last_error(aplwc);
}