visitZeroOrMore method

  1. @override
ExpressionGenerator visitZeroOrMore(
  1. ZeroOrMoreExpression node
)
inherited

Implementation

@override
ExpressionGenerator visitZeroOrMore(ZeroOrMoreExpression node) {
  final g = ExpressionGenerator(node);
  g.generate = (block) {
    g.declareVariable(block);
    final child = node.expression;
    g.variables = {};
    if (g.variable == null) {
      block.doWhile$(ref(Members.ok), (block) {
        final g1 = acceptChild(child, g);
        g1.generate(block);
      });

      block.assign(Members.ok, true$);
    } else {
      void fail(CodeBlock block) {
        block.break$();
      }

      final init = literalList([], ref(child.resultType));
      final list = g.allocate();
      block.assignFinal(list, init);
      block.while$(true$, (block) {
        final g1 = acceptChild(child, g);
        g1.generate(block);
        generatePostCode(g1, block, null, fail);
        final element = nullCheck(ref(g1.variable!), child.resultType);
        final args = [element];
        final call = methodCallExpression(ref(list), 'add', args);
        block.addStatement(call);
      });

      block.if$(ref(Members.ok).assign(true$), (block) {
        block.assign(g.variable!, ref(list));
      });
    }
  };

  return g;
}