summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter <peter@pmikkelsen.com>2026-08-08 15:38:35 +0000
committerpeter <peter@pmikkelsen.com>2026-08-08 15:38:35 +0000
commitb1d9069f57439bed333d83c7041871c54f0b8d97 (patch)
tree10048b0fc474a6e9ff91affc44455191a651cd5e
parent059ecb51f46d5c86f8d9abbe3914d0a028725957 (diff)
Initial import of code
FossilOrigin-Name: 1bd90d4b94de6d63bd8639b8eae399c3dabf321e704ff08c03e99bbe6dfb7764
-rw-r--r--CMakeLists.txt41
-rw-r--r--src/aplwc.h16
-rw-r--r--src/aplwc_internal.h32
-rw-r--r--src/errors.c18
-rw-r--r--src/libmain.c45
-rw-r--r--src/main.c23
-rw-r--r--src/malloc.c11
7 files changed, 186 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..fd1839b
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,41 @@
+cmake_minimum_required(VERSION 3.25)
+
+project(aplwc
+ VERSION 0.0.1
+ LANGUAGES C
+)
+
+set(CMAKE_C_STANDARD 99)
+set(CMAKE_C_STANDARD_REQUIRED ON)
+set(CMAKE_C_EXTENSIONS OFF)
+
+add_library(common INTERFACE)
+target_include_directories(common INTERFACE
+ "src"
+)
+target_compile_options(common INTERFACE
+ $<$<CONFIG:Debug>:
+ -Wall
+ -Wextra
+ -Wpedantic
+ -Werror
+ -fsanitize=address,undefined
+ >
+)
+target_link_options(common INTERFACE
+ $<$<CONFIG:Debug>:
+ -fsanitize=address,undefined
+ >
+)
+
+add_library(aplwc
+ src/errors.c
+ src/libmain.c
+ src/malloc.c
+)
+target_link_libraries(aplwc PRIVATE common)
+
+add_executable(aplwc-repl
+ src/main.c
+)
+target_link_libraries(aplwc-repl PRIVATE common aplwc)
diff --git a/src/aplwc.h b/src/aplwc.h
new file mode 100644
index 0000000..7a90147
--- /dev/null
+++ b/src/aplwc.h
@@ -0,0 +1,16 @@
+#ifndef APLWC_H
+#define APLWC_H
+
+typedef enum {
+ APLWC_OK,
+ APLWC_NOT_IMPLEMENTED,
+ APLWC_MALLOC_FAILED,
+} AplwcRc;
+
+typedef struct Aplwc Aplwc;
+
+AplwcRc aplwc_new(Aplwc **);
+AplwcRc aplwc_destroy(Aplwc *);
+const char *aplwc_errstr(Aplwc *);
+
+#endif /* APLWC_H */
diff --git a/src/aplwc_internal.h b/src/aplwc_internal.h
new file mode 100644
index 0000000..f80c8ab
--- /dev/null
+++ b/src/aplwc_internal.h
@@ -0,0 +1,32 @@
+#ifndef APLWC_INTERNAL_H
+#define APLWC_INTERNAL_H
+
+#define UNUSED_PARAMETER(arg) ((void)arg)
+
+#define RETURN_ERROR(aplwc, rc, msg) do{ \
+ Aplwc *_p = (aplwc); \
+ AplwcRc _r = APLWC_##rc; \
+ if(_p) _p->last_error = _r; \
+ return _r; \
+ }while(0)
+
+#define NELEM(x) (sizeof(x)/sizeof(x[0]))
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+struct Aplwc {
+ AplwcRc last_error;
+ char last_error_msg[64];
+};
+
+/* errors.c */
+const char *rc_to_string(AplwcRc);
+
+/* malloc.c */
+void *malloc_zero(size_t);
+
+#endif /* APLWC_INTERNAL_H */
diff --git a/src/errors.c b/src/errors.c
new file mode 100644
index 0000000..ab76082
--- /dev/null
+++ b/src/errors.c
@@ -0,0 +1,18 @@
+#include <aplwc.h>
+#include <aplwc_internal.h>
+
+static const char *errors[] = {
+ [APLWC_OK] =
+ "",
+ [APLWC_NOT_IMPLEMENTED] =
+ "Not yet implemented",
+};
+
+const char *
+rc_to_string(AplwcRc rc)
+{
+ const char *str = NULL;
+ if((rc > 0) && (rc < NELEM(errors)))
+ str = errors[rc];
+ return str;
+}
diff --git a/src/libmain.c b/src/libmain.c
new file mode 100644
index 0000000..1458bf8
--- /dev/null
+++ b/src/libmain.c
@@ -0,0 +1,45 @@
+#include <aplwc.h>
+#include <aplwc_internal.h>
+
+AplwcRc
+aplwc_new(Aplwc **p)
+{
+ *p = malloc_zero(sizeof(Aplwc));
+ if(*p == NULL)
+ RETURN_ERROR(*p, MALLOC_FAILED, "");
+
+ return APLWC_OK;
+}
+
+AplwcRc
+aplwc_destroy(Aplwc *p)
+{
+ if(p)
+ free(p);
+
+ return APLWC_OK;
+}
+
+const char *
+aplwc_errstr(Aplwc *aplwc)
+{
+ static char buf[256];
+ buf[0] = 0;
+
+ const char *err = NULL;
+ const char *msg = NULL;
+ if(aplwc){
+ err = rc_to_string(aplwc->last_error);
+ msg = aplwc->last_error_msg;
+ }
+
+ if(!err)
+ err = "(unable to produce error string)";
+
+ if(msg && (strlen(msg) > 0))
+ snprintf(buf, sizeof(buf), "%s: %s", err, msg);
+ else
+ snprintf(buf, sizeof(buf), "%s", err);
+
+ return buf;
+}
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..209c88a
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <aplwc.h>
+
+int
+main(void)
+{
+ Aplwc *aplwc;
+
+ int ret = aplwc_new(&aplwc);
+ if(ret){
+ fprintf(stderr, "Couldn't create new aplwc instance: %s\n", aplwc_errstr(aplwc));
+ return 1;
+ }
+
+
+ ret = aplwc_destroy(aplwc);
+ if(ret){
+ fprintf(stderr, "Couldn't destroy the aplwc instance: %s\n", aplwc_errstr(aplwc));
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/src/malloc.c b/src/malloc.c
new file mode 100644
index 0000000..abf0884
--- /dev/null
+++ b/src/malloc.c
@@ -0,0 +1,11 @@
+#include <aplwc.h>
+#include <aplwc_internal.h>
+
+void *
+malloc_zero(size_t size)
+{
+ void *p = malloc(size);
+ if(p)
+ memset(p, 0, size);
+ return p;
+}