mux function
Creates State that generates sourcecode that will multiplex
the results
of state
computation from different branches into one computation point,
transforming multiple inputs computations into single output computation.
This function is used internally by Sequence
to prevent duplication of the
generated code. All elements of Sequence
, except the last element, if they
generate a multiple outputs (branches), are transformed into a single output
(branch).
Implementation
State mux(State state) {
final type = state.type.trim();
if (type == 'void') {
const template = '''
var {{tmp}} = false;
{{@state}}
if ({{tmp}}) {
{{@accept}}
}
{{@reject}}
''';
const automaton = Automaton(
accept: '{{tmp}} = true;',
reject: '',
result: 'null',
template: template,
);
const generator = AutomatonGenerator(automaton);
final start = generator.generate(type, state);
return start;
} else {
const template = '''
({{type}},)? {{tmp}};
{{@state}}
if ({{tmp}} != null) {
{{@accept}}
}
{{@reject}}
''';
const automaton = Automaton(
accept: '{{tmp}} = ({{0}},);',
reject: '',
result: '{{tmp}}.\$1',
template: template,
);
const generator = AutomatonGenerator(automaton);
final start = generator.generate(type, state, values: {'type': type});
return start;
}
}