/** * A filter for the sequence </br> Combines the sequential type and method declarations into a * block </br> * * @return return the sequence after combination. */ private Sequence combine() { boolean recBlock = false; Sequence normalSeq = new Sequence(); Sequence recSequence = new DeclSequence(); for (TypedAST ast : this.getIterator()) { if (ast instanceof TypeVarDecl || ast instanceof DefDeclaration || ast instanceof TypeAbbrevDeclaration) { Declaration d = (Declaration) ast; if (recBlock == false) { recBlock = true; recSequence = new DeclSequence(); } recSequence = Sequence.append(recSequence, d); } else { if (recBlock == true) { normalSeq = Sequence.append(normalSeq, recSequence); } normalSeq = Sequence.append(normalSeq, ast); recBlock = false; } } if (recBlock == true) { normalSeq = Sequence.append(normalSeq, recSequence); } return normalSeq; }
/** * Generate IL expression for a top-level declaration sequence</br> * * @see GenUtil.doGenModuleIL * @param ctx the context * @param isModule whether is is actually a module expression: true for the body of a module, * false for the body of a script * @return the IL expression of a module */ public Expression generateModuleIL(GenContext ctx, boolean isModule) { Sequence seqWithBlocks = combine(); TopLevelContext tlc = new TopLevelContext(ctx); seqWithBlocks.genTopLevel(tlc); Expression result = isModule ? tlc.getModuleExpression() : tlc.getExpression(); /*Iterator<TypedAST> ai = seqWithBlocks.iterator(); if (!ai.hasNext()) throw new RuntimeException("expected an expression in the list"); Expression decl = GenUtil.doGenModuleIL(ctx, ctx, ctx, ai, isModule);*/ return result; }
public static Sequence append(Sequence s, TypedAST e) { Iterator<TypedAST> innerIter = s.iterator(); if (s instanceof DeclSequence && e instanceof Declaration) { return DeclSequence.simplify(new DeclSequence(s, e)); } return new Sequence( () -> new Iterator<TypedAST>() { private boolean fetched = false; @Override public boolean hasNext() { if (innerIter.hasNext()) return true; return !fetched; } @Override public TypedAST next() { if (innerIter.hasNext()) return innerIter.next(); if (!fetched) { fetched = true; return e; } throw new RuntimeException(); } }); }