How to reduce a 0..n cardinality child to a nested list in a generator

I'm struggling to make a generator for a list that generates a nested binary expression.
Thus we have
concept ComplexCondition with child
subcondition: SubCondition[0..n]

and want to generate a structure that consists of Conjunctions

concept Conjunction with children
left: Expression
right: Expression

so that ComplexCondition { a, b, c, d } is translated to
Conjunction (Conjunction (Conjunction a b) c) d

A complicating fact is that we cannot make intermediate structures because the subconditions need to be able to access their ancestors in order to get to some contextual information
1 comment
Comment actions Permalink
Ahh, I figured it out.
I made a template with an integer parameter (the index of the current subconditie)

template conjunction
input    ComplexCondition

index : int

content node:
<TF $IF$[Conjunction $COPY_SRC%[false] $CALL$conjunction[false]] /  TF>
    $ELSE$[<T $COPY_SRC$[false] T>]

The $IF$-macro checks whether genContext.index < node.subcondition.size - 1
Both $COPY_SRC$ macros return node.subcondition[genContext.index]
and the parameter to the $CALL$-macro is index + 1

At the toplevel I can call this template with parameter 0.

BTW, I realize that this actually returns
Conjunction a (Conjunction b (Conjunction c d))
but that does not really matter, for our purpose.
Starting the index with subcondition.size - 1, reversing the arguments in the template and recursively calling with index - 1, will get the right-reduction.

Please sign in to leave a comment.