diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2021-10-01 20:07:40 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2021-10-01 20:07:40 +0100 |
commit | 6a05583e37cd8db8a35179e6cfac0c97c435a377 (patch) | |
tree | 9618007476aba1604ddc2a724a3cf76808427ec8 | |
parent | dfcc320d3085f188a3aa02eb9edc69e3c4617fbd (diff) | |
download | cudl-6a05583e37cd8db8a35179e6cfac0c97c435a377.tar |
Changes approach to be more understandable
This is more in line with a definitional parser. A later implementation can be faster
-rw-r--r-- | cudl.c | 81 |
1 files changed, 34 insertions, 47 deletions
@@ -1,70 +1,57 @@ #include <stdio.h> -union cudl_val_uni { - double num; - char *str; - size_t offset; +struct cudl_array_value { + struct cudl_value *values; + size_t length; }; -struct cudl_val { - uint64_t tag; - union cudl_val_uni data; +struct cudl_map_value { + struct cudl_map_field { + char *key; + struct cudl_value value; + } *fields; + size_t length; }; -struct cudl_doc { - struct cudl_val *root; - size_t length; - size_t capacity; - char *strings; +struct cudl_value { + union { + char *string; + double number; + int boolean; + struct array_value array; + struct map_value map; + } data; + int tag; }; #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_value *value) { +} -void cudl_debug(struct cudl_doc doc) { +/* Free all children of the value, not the value itself */ +void cudl_deinit_value(struct cudl_value value) { 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; - } + switch (value.tag) { + case CUDL_TAG_ARRAY: + for (i = 0; i < value.data.array.length; i++) { + cudl_deinit_value(value.data.array.values[i]); + } + free(value.data.array.values); + break; + case CUDL_TAG_NULL: + case default: + 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_value *value) { } -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; +int cudl_parse(char *input, struct cudl_value *value) { } |