aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc/simplify-tree.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/mds-kbdc/simplify-tree.c40
1 files changed, 22 insertions, 18 deletions
diff --git a/src/mds-kbdc/simplify-tree.c b/src/mds-kbdc/simplify-tree.c
index c46649e..d24569d 100644
--- a/src/mds-kbdc/simplify-tree.c
+++ b/src/mds-kbdc/simplify-tree.c
@@ -161,6 +161,7 @@ static int eliminate_alternation(mds_kbdc_tree_t* tree, mds_kbdc_tree_t* argumen
mds_kbdc_tree_t* next_alternative;
mds_kbdc_tree_t* new_argument;
size_t i;
+ int saved_errno;
/* Detach next statement, we do not want to duplicate all following statements. */
next_statement = tree->next, tree->next = NULL;
@@ -171,13 +172,7 @@ static int eliminate_alternation(mds_kbdc_tree_t* tree, mds_kbdc_tree_t* argumen
for (first = last = NULL; alternative; alternative = next_alternative)
{
/* Duplicate statement. */
- if (new_tree = mds_kbdc_tree_dup(tree), new_tree == NULL)
- {
- int saved_errno = errno;
- argument->alternation.inner = alternative;
- tree->next = next_statement;
- return errno = saved_errno, -1;
- }
+ fail_if (new_tree = mds_kbdc_tree_dup(tree), new_tree == NULL);
/* Join trees. */
if (last)
last->next = new_tree;
@@ -204,6 +199,11 @@ static int eliminate_alternation(mds_kbdc_tree_t* tree, mds_kbdc_tree_t* argumen
/* Reattach the statement that followed to the last generated statement. */
last->next = next_statement;
return 0;
+ fail:
+ saved_errno = errno;
+ argument->alternation.inner = alternative;
+ tree->next = next_statement;
+ return errno = saved_errno, -1;
}
@@ -567,7 +567,8 @@ static int simplify_alternation(mds_kbdc_tree_alternation_t* restrict tree)
NEW_ERROR(tree, WARNING, "singleton alternation");
memcpy(tree, temp, sizeof(mds_kbdc_tree_t));
free(temp);
- return simplify((mds_kbdc_tree_t*)tree);
+ fail_if (simplify((mds_kbdc_tree_t*)tree));
+ return 0;
}
/* Simplify. */
@@ -625,11 +626,10 @@ static mds_kbdc_tree_t* create_permutations(mds_kbdc_tree_t* elements)
mds_kbdc_tree_t* subperms = NULL;
mds_kbdc_tree_t* perm;
mds_kbdc_tree_t ordered;
- int saved_errno, no_perms;
+ int saved_errno, no_perms, stage = 0;
/* Error case. */
- if (elements == NULL)
- return NULL;
+ fail_if (elements == NULL);
/* Base case. */
if (elements->next == NULL)
@@ -639,6 +639,7 @@ static mds_kbdc_tree_t* create_permutations(mds_kbdc_tree_t* elements)
return first;
}
+ stage++;
for (previous_next = &elements; (argument = *previous_next); previous_next = &((*previous_next)->next))
{
/* Created ordered alternative for a permutation prototype. */
@@ -682,7 +683,8 @@ static mds_kbdc_tree_t* create_permutations(mds_kbdc_tree_t* elements)
saved_errno = errno;
mds_kbdc_tree_free(first);
mds_kbdc_tree_free(subperms);
- mds_kbdc_tree_destroy(&ordered);
+ if (stage > 0)
+ mds_kbdc_tree_destroy(&ordered);
errno = saved_errno;
return NULL;
}
@@ -730,7 +732,8 @@ static int simplify_unordered(mds_kbdc_tree_unordered_t* restrict tree)
NEW_ERROR(tree, WARNING, "singleton unordered subsequence");
memcpy(tree, temp, sizeof(mds_kbdc_tree_t));
free(temp);
- return simplify((mds_kbdc_tree_t*)tree);
+ fail_if (simplify((mds_kbdc_tree_t*)tree));
+ return -1;
}
/* Remove ‘.’:s. */
@@ -783,9 +786,9 @@ static int simplify_unordered(mds_kbdc_tree_unordered_t* restrict tree)
* if it does not list any permutations. */
NEW_ERROR_(result, INTERNAL_ERROR, 0, 0, 0, 0, 1,
"Fail to create permutations of an unordered sequence");
- errno = 0;
+ return 0;
}
- return tree->inner = arguments, -1;
+ fail_if (tree->inner = arguments, 1);
}
mds_kbdc_tree_free(arguments);
@@ -803,9 +806,8 @@ static int simplify_unordered(mds_kbdc_tree_unordered_t* restrict tree)
*/
static int simplify(mds_kbdc_tree_t* restrict tree)
{
-#define s(expr) if ((r = simplify(tree->expr))) return r
-#define S(type) if ((r = simplify_##type(&(tree->type)))) return r
- int r;
+#define s(expr) fail_if (simplify(tree->expr))
+#define S(type) fail_if (simplify_##type(&(tree->type)))
again:
if (tree == NULL)
return 0;
@@ -828,6 +830,8 @@ static int simplify(mds_kbdc_tree_t* restrict tree)
tree = tree->next;
goto again;
+ fail:
+ return -1;
#undef s
#undef S
}