<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2021-10-01 17:29:10 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2021-10-01 17:29:10 +0100
commitdfcc320d3085f188a3aa02eb9edc69e3c4617fbd (patch)
treeca07151b2445ccb3ded4b9e50954bfa8343ff944
downloadcudl-dfcc320d3085f188a3aa02eb9edc69e3c4617fbd.tar
Initial commit
-rw-r--r--README3
-rw-r--r--cudl.c70
2 files changed, 73 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..aa941d1
--- /dev/null
+++ b/README
@@ -0,0 +1,3 @@
+CUDL - Clear and Unmistakable Data Language
+
+In C
diff --git a/cudl.c b/cudl.c
new file mode 100644
index 0000000..04b3d9b
--- /dev/null
+++ b/cudl.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+
+union cudl_val_uni {
+ double num;
+ char *str;
+ size_t offset;
+};
+
+struct cudl_val {
+ uint64_t tag;
+ union cudl_val_uni data;
+};
+
+struct cudl_doc {
+ struct cudl_val *root;
+ size_t length;
+ size_t capacity;
+ char *strings;
+};
+
+#define CUDL_TAG_NULL 0
+#define CUDL_TAG_ARRAY 1
+
+#define CUDL_GET_TAG(tag) ((tag) >> 56)
+
+#define CUDL_OK 0
+#define CUDL_ERR_OUT_OF_MEMORY 1
+#define CUDL_ERR_MISSING_VALUE 2
+
+#define BUFFER_LEN 1024
+
+static FILE file;
+static char buffer[BUFFER_LEN];
+
+void cudl_debug(struct cudl_doc doc) {
+ int i;
+ for (i = 0; i < doc.length; i++) {
+ switch (CUDL_GET_TAG(doc.root[i].tag)) {
+ case CUDL_TAG_NULL:
+ fprintf(stderr, "null\n");
+ break;
+ case CUDL_TAG_ARRAY:
+ fprintf(stderr, "array with %d elements, ending after %d blocks\n", CUDL_GET_LENGTH(doc.root[i].tag), doc.root[i].data.offset);
+ break;
+ case default:
+ fprintf(stderr, "Unknown value\n");
+ break;
+ }
+ }
+}
+
+void cudl_deinit_doc(struct cudl_doc doc) {
+ free(doc.cudl_val);
+ free(doc.strings);
+}
+
+int cudl_parse_from_file(FILE input_file, struct cudl_doc *doc) {
+ int c;
+
+ /* Init doc */
+ if ((doc->root = malloc(256 * sizeof(struct cudl_val))) == NULL)
+ return CUDL_ERR_OUT_OF_MEMORY;
+ if ((doc->strings = malloc(256)) == NULL) {
+ free(doc->root);
+ return CUDL_ERR_OUT_OF_MEMORY;
+ }
+
+ if ((c = next_char(file)) == EOF)
+ return CUDL_ERR_MISSING_VALUE;
+}