diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2021-10-03 21:31:59 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2021-10-03 21:31:59 +0100 |
commit | d973b4b8eaaa5e7f1d10f9f02069c6d7bde25dcc (patch) | |
tree | 814987a1977dc62251fc05a1a76526d2479722dd | |
parent | 69852d9ae210080e999eb5bbb81481f527580a97 (diff) | |
download | cudl-d973b4b8eaaa5e7f1d10f9f02069c6d7bde25dcc.tar |
Add testing code and fix a few bugs
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | cudl.c | 17 | ||||
-rw-r--r-- | cudl.h | 34 | ||||
-rw-r--r-- | test.c | 9 | ||||
-rw-r--r-- | test.cudl | 5 |
6 files changed, 51 insertions, 25 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4ab3d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +test +*.o @@ -1,4 +1,9 @@ CC=tcc -BIN=test +CFLAGS=-I. -test: test.o +test: test.o cudl.o + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +test.o: cudl.h @@ -1,6 +1,7 @@ #include <stdio.h> #include <ctype.h> #include <stdlib.h> +#include <string.h> #include "cudl.h" #define STRIP_WHITESPACE(text) while (isspace(*text)) text++ @@ -59,7 +60,7 @@ void cudl_deinit_value(struct cudl_value value) { free(value.data.array.values); break; case CUDL_TAG_NULL: - case default: + default: break; } } @@ -67,19 +68,19 @@ void cudl_deinit_value(struct cudl_value value) { /* Parse a value from input and store it in value. * Return the number of bytes consumed. * Input must end with a null byte */ -size_t parse_value(char *input, struct cudl_value *value); +static size_t parse_value(char *input, struct cudl_value *value); static size_t parse_bool_or_null(char *input, struct cudl_value *value) { - if (strncmp(input, "null", 4)) { + if (strncmp(input, "null", 4) == 0) { value->tag = CUDL_TAG_NULL; return 4; } - if (strncmp(input, "true", 4)) { + if (strncmp(input, "true", 4) == 0) { value->tag = CUDL_TAG_BOOL; value->data.boolean = 1; return 4; } - if (strncmp(input, "false", 5)) { + if (strncmp(input, "false", 5) == 0) { value->tag = CUDL_TAG_BOOL; value->data.boolean = 0; return 5; @@ -158,8 +159,8 @@ static size_t parse_value(char *input, struct cudl_value *value) { } void cudl_parse_from_file(FILE *file, struct cudl_value *value) { - char *input; - if ((input = fread_all(file)) == NULL) { + char *input, *original_input; + if ((original_input = input = fread_all(file)) == NULL) { if (ferror(file)) cudl_err = CUDL_ERR_READING; else @@ -169,7 +170,7 @@ void cudl_parse_from_file(FILE *file, struct cudl_value *value) { input += cudl_parse(input, value); if (*input != '\0') cudl_deinit_value(*value); - free(input); + free(original_input); } size_t cudl_parse(char *input, struct cudl_value *value) { @@ -7,10 +7,7 @@ struct cudl_array_value { }; struct cudl_map_value { - struct cudl_map_field { - char *key; - struct cudl_value value; - } *fields; + struct cudl_map_field *fields; size_t length; }; @@ -19,24 +16,31 @@ struct cudl_value { char *string; double number; int boolean; - struct array_value array; - struct map_value map; + struct cudl_array_value array; + struct cudl_map_value map; } data; int tag; }; +struct cudl_map_field { + char *key; + struct cudl_value value; +}; + enum { - CUDL_TAG_NULL; - CUDL_TAG_BOOL; - CUDL_TAG_ARRAY; -} + CUDL_TAG_NULL, + CUDL_TAG_BOOL, + CUDL_TAG_ARRAY, +}; enum { - CUDL_OK = 0; - CUDL_ERR_OUT_OF_MEMORY; - CUDL_ERR_EXPECTED_VALUE; - CUDL_ERR_READING; - CUDL_ERR_EXPECTED_BOOL_OR_NULL; + CUDL_OK = 0, + CUDL_ERR_OUT_OF_MEMORY, + CUDL_ERR_EXPECTED_VALUE, + CUDL_ERR_READING, + CUDL_ERR_EXPECTED_BOOL_OR_NULL, + CUDL_ERR_UNMATCHED_BRACK, + CUDL_ERR_UNRECOGNISED_VALUE, }; extern int cudl_err; @@ -1,2 +1,11 @@ +#include <stdio.h> +#include "cudl.h" + int main() { + struct cudl_value value; + cudl_parse_from_file(stdin, &value); + if (cudl_err) + return cudl_err; + cudl_debug(value); + return 0; } diff --git a/test.cudl b/test.cudl new file mode 100644 index 0000000..150217a --- /dev/null +++ b/test.cudl @@ -0,0 +1,5 @@ +[ + %true + %false + [%null %null %false] +] |