diff options
Diffstat (limited to '')
-rw-r--r-- | src/mds-kbdc/simplify-tree.c | 102 | ||||
-rw-r--r-- | src/mds-kbdc/simplify-tree.h | 2 | ||||
-rw-r--r-- | src/mds-kbdc/tree.h | 24 |
3 files changed, 106 insertions, 22 deletions
diff --git a/src/mds-kbdc/simplify-tree.c b/src/mds-kbdc/simplify-tree.c index fd3ece2..83eb013 100644 --- a/src/mds-kbdc/simplify-tree.c +++ b/src/mds-kbdc/simplify-tree.c @@ -55,6 +55,7 @@ { \ errors_size = errors_size ? (errors_size << 1) : 2; \ fail_if (xxrealloc(old_errors, *errors, errors_size, mds_kbdc_parse_error_t*)); \ + old_errors = NULL; \ } \ fail_if (xcalloc(error, 1, mds_kbdc_parse_error_t)); \ (*errors)[errors_ptr + 0] = error; \ @@ -75,22 +76,105 @@ /** + * Temporary storage variable for the new error, + * it is made available so that it is easy to + * make adjustments to the error after calling + * `NEW_ERROR` + */ +static mds_kbdc_parse_error_t* error = NULL; + +/** + * The number of elements allocated for `*errors` + */ +static size_t errors_size = 0; + +/** + * The number of elements stored in `*errors` + */ +static size_t errors_ptr = 0; + +/** + * Pointer to the list of errors + */ +static mds_kbdc_parse_error_t*** errors; + +/** + * The old `*errors` used temporary when reallocating `*errors` + */ +static mds_kbdc_parse_error_t** old_errors = NULL; + + + +/** + * Simplify a subtree + * + * @param tree The tree + * @return Zero on success, -1 on error + */ +static int simplify(mds_kbdc_tree_t* restrict tree) +{ +#define s(expr) if ((r = simplify(tree->expr))) return r; + int r; + again: + if (tree == NULL) + return 0; + + switch (tree->type) + { + case MDS_KBDC_TREE_TYPE_INFORMATION: s (information.inner); break; + case MDS_KBDC_TREE_TYPE_FUNCTION: s (function.inner); break; + case MDS_KBDC_TREE_TYPE_MACRO: s (macro.inner); break; + case MDS_KBDC_TREE_TYPE_ASSUMPTION: s (assumption.inner); break; + case MDS_KBDC_TREE_TYPE_FOR: s (for_.inner); break; + case MDS_KBDC_TREE_TYPE_IF: + s (if_.inner); + s (if_.otherwise); + break; + + case MDS_KBDC_TREE_TYPE_MAP: + break; + + case MDS_KBDC_TREE_TYPE_ALTERNATION: + break; + + case MDS_KBDC_TREE_TYPE_UNORDERED: + break; + + case MDS_KBDC_TREE_TYPE_MACRO_CALL: + break; + + default: + break; + } + + tree = tree->next; + goto again; +#undef s +} + + +/** * Simplify a tree and generate related warnings in the process * - * @param tree The tree, it may be modified - * @param errors `NULL`-terminated list of found error, `NULL` if no errors were found or if -1 is returned - * @return -1 if an error occursed that cannot be stored in `*errors`, zero otherwise + * @param tree The tree, it may be modified + * @param errors_ `NULL`-terminated list of found error, `NULL` if no errors were found or if -1 is returned + * @return -1 if an error occursed that cannot be stored in `*errors`, zero otherwise */ -int simplify_tree(mds_kbdc_tree_t** restrict tree, mds_kbdc_parse_error_t*** restrict errors) +int simplify_tree(mds_kbdc_tree_t* restrict tree, mds_kbdc_parse_error_t*** restrict errors_) { - mds_kbdc_parse_error_t* error; - mds_kbdc_parse_error_t** old_errors = NULL; - size_t errors_size = 0; - size_t errors_ptr = 0; + int r, saved_errno; + errors = errors_; *errors = NULL; - return 0; + if (r = simplify(tree), !r) + return 0; + + saved_errno = errno; + mds_kbdc_parse_error_free_all(old_errors); + mds_kbdc_parse_error_free_all(*errors), *errors = NULL; + errno = saved_errno; + return r; } diff --git a/src/mds-kbdc/simplify-tree.h b/src/mds-kbdc/simplify-tree.h index 5009fb6..c45f1de 100644 --- a/src/mds-kbdc/simplify-tree.h +++ b/src/mds-kbdc/simplify-tree.h @@ -30,7 +30,7 @@ * @param errors `NULL`-terminated list of found error, `NULL` if no errors were found or if -1 is returned * @return -1 if an error occursed that cannot be stored in `*errors`, zero otherwise */ -int simplify_tree(mds_kbdc_tree_t** restrict tree, mds_kbdc_parse_error_t*** restrict errors); +int simplify_tree(mds_kbdc_tree_t* restrict tree, mds_kbdc_parse_error_t*** restrict errors); #endif diff --git a/src/mds-kbdc/tree.h b/src/mds-kbdc/tree.h index c36cec4..d12adea 100644 --- a/src/mds-kbdc/tree.h +++ b/src/mds-kbdc/tree.h @@ -98,57 +98,57 @@ /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_map_t` */ -#define MDS_KBDC_TREE_TYPE_MAP 15 +#define MDS_KBDC_TREE_TYPE_MAP 14 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_array_t` */ -#define MDS_KBDC_TREE_TYPE_ARRAY 16 +#define MDS_KBDC_TREE_TYPE_ARRAY 15 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_keys_t` */ -#define MDS_KBDC_TREE_TYPE_KEYS 17 +#define MDS_KBDC_TREE_TYPE_KEYS 16 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_string_t` */ -#define MDS_KBDC_TREE_TYPE_STRING 18 +#define MDS_KBDC_TREE_TYPE_STRING 17 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_nothing_t` */ -#define MDS_KBDC_TREE_TYPE_NOTHING 19 +#define MDS_KBDC_TREE_TYPE_NOTHING 18 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_alternation_t` */ -#define MDS_KBDC_TREE_TYPE_ALTERNATION 20 +#define MDS_KBDC_TREE_TYPE_ALTERNATION 19 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_unordered_t` */ -#define MDS_KBDC_TREE_TYPE_UNORDERED 21 +#define MDS_KBDC_TREE_TYPE_UNORDERED 20 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_macro_call_t` */ -#define MDS_KBDC_TREE_TYPE_MACRO_CALL 22 +#define MDS_KBDC_TREE_TYPE_MACRO_CALL 21 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_return_t` */ -#define MDS_KBDC_TREE_TYPE_RETURN 23 +#define MDS_KBDC_TREE_TYPE_RETURN 22 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_break_t` */ -#define MDS_KBDC_TREE_TYPE_BREAK 24 +#define MDS_KBDC_TREE_TYPE_BREAK 23 /** * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_continue_t` */ -#define MDS_KBDC_TREE_TYPE_CONTINUE 25 +#define MDS_KBDC_TREE_TYPE_CONTINUE 24 @@ -326,7 +326,7 @@ typedef struct mds_kbdc_tree_nesting mds_kbdc_tree_assumption_t; /** - * Treeu structure for making the assumption + * Tree structure for making the assumption * that there is a mapping to a key or string */ typedef struct mds_kbdc_tree_assumption_have |