private K apply(K term) { if (labelInfo.isFunction(term)) return term; List<K> items = IncompleteCellUtils.flattenCells(term); if (items.size() != 1) { return term; } K item = items.get(0); if (isCell(item)) { return term; } else if (item instanceof KRewrite) { final KRewrite rew = (KRewrite) item; if (Stream.concat( IncompleteCellUtils.flattenCells(rew.left()).stream(), IncompleteCellUtils.flattenCells(rew.right()).stream()) .anyMatch(this::isCell)) { return term; } } KLabel computation = cfg.getCellLabel(cfg.getComputationCell()); return IncompleteCellUtils.make(computation, false, item, true); }
public static boolean isOpenLeft(KApply cell) { return isOpen(cell.klist().items().get(0)); } public static boolean isOpenRight(KApply cell) {
private K addFreshCell(K body) { if (freshVars.size() == 0) { return body; } KApply cellTerm = IncompleteCellUtils.make(KLabels.GENERATED_COUNTER_CELL, false, KRewrite(FRESH, KApply(KLabel("_+Int_"), FRESH, KToken(Integer.toString(freshVars.size()), Sorts.Int()))), false); return KApply(KLabels.CELLS, body, cellTerm); }
List<K> otherChildren = Lists.newArrayList(); int ix = 0; boolean ellipses = IncompleteCellUtils.isOpenLeft(app) || IncompleteCellUtils.isOpenRight(app); for (K item : IncompleteCellUtils.getChildren(app)) { if (isCompletionItem(item)) { children.add(item); return IncompleteCellUtils.make(target, ellipses, otherChildren, ellipses);
boolean openLeft = IncompleteCellUtils.isOpenLeft(cell); boolean openRight = IncompleteCellUtils.isOpenRight(cell); List<K> contents = IncompleteCellUtils.getChildren(cell); if (item instanceof KRewrite) { KRewrite rw = (KRewrite) item; for (K leftItem : IncompleteCellUtils.flattenCells(rw.left())) { filterRequired(requiredLeft, leftItem); for (K rightItem : IncompleteCellUtils.flattenCells(rw.right())) { filterRequired(requiredRight, rightItem);
public static List<K> flattenCells(K cells) { List<K> children = new ArrayList<K>(); flattenCells(children, cells); return children; }
private Rule getExitCodeRule(Definition parsedDefinition) { Module mainMod = parsedDefinition.mainModule(); Set<Production> exitProds = stream(mainMod.productions()).filter(p -> p.att().contains("exit")).collect(Collectors.toSet()); if (exitProds.size() == 0) { return null; } else if (exitProds.size() > 1) { throw KEMException.compilerError("Found more than one or zero productions with 'exit' attribute. Exactly one production, a cell, must have this attribute, designating the exit code of krun. Found:\n" + exitProds); } Production exitProd = exitProds.iterator().next(); return Rule(IncompleteCellUtils.make(exitProd.klabel().get(), false, KApply(KLabel("#SemanticCastToInt"), KVariable("_")), false), BooleanUtils.TRUE, BooleanUtils.TRUE); }
Stream<K> streamSideCells(K side) { List<K> cells = IncompleteCellUtils.flattenCells(side); // TODO error handling return cells.stream(); }
KApply cell(String name, boolean openLeft, boolean openRight, K... ks) { return IncompleteCellUtils.make(KLabel(name), openLeft, Arrays.asList(ks), openRight); }
private Stream<K> streamCells(K term) { return IncompleteCellUtils.flattenCells(term).stream(); }
public static boolean isOpenRight(KApply cell) { return isOpen(cell.klist().items().get(2)); }
KApply cell(String name, boolean openLeft, boolean openRight, K... ks) { return IncompleteCellUtils.make(KLabel(name), openLeft, Arrays.asList(ks), openRight); }
public static List<K> getChildren(KApply cell) { return flattenCells(cell.klist().items().get(1)); }
private static void flattenCells(List<K> children, K item) { if (item instanceof KApply && KLabels.CELLS.equals(((KApply) item).klabel())) { for (K deeper : ((KApply) item).klist().items()) { flattenCells(children, deeper); } } else { children.add(item); } } public static List<K> flattenCells(K cells) {
protected K addRootCell(K term) { KLabel root = cfg.getCellLabel(cfg.getRootCell()); if (term instanceof KApply && ((KApply) term).klabel().equals(root)) { return term; } else if (term instanceof KRewrite) { KRewrite rew = (KRewrite) term; K left = addRootCell(rew.left()); if (left == rew.left()) { return KRewrite(rew.left(), rew.right()); } else { return IncompleteCellUtils.make(root, true, term, true); } } else { return IncompleteCellUtils.make(root, true, term, true); } }
List<K> children = IncompleteCellUtils.flattenCells(item); if(children.size() == 1 && children.get(0) == item) { final KLabel label = ((KApply) item).klabel();
if (hasConfigurationOrRegularVariable || isStream) { initializer = Production(KLabel(initLabel), sort, Seq(Terminal(initLabel), Terminal("("), NonTerminal(Sorts.Map()), Terminal(")")), Att().add("initializer").add("function").add("noThread")); initializerRule = Rule(KRewrite(KApply(KLabel(initLabel), INIT), IncompleteCellUtils.make(KLabel("<" + cellName + ">"), false, childInitializer, false)), BooleanUtils.TRUE, ensures == null ? BooleanUtils.TRUE : ensures, Att().add("initializer")); } else { initializer = Production(KLabel(initLabel), sort, Seq(Terminal(initLabel)), Att().add("initializer").add("function").add("noThread")); initializerRule = Rule(KRewrite(KApply(KLabel(initLabel)), IncompleteCellUtils.make(KLabel("<" + cellName + ">"), false, childInitializer, false)), BooleanUtils.TRUE, ensures == null ? BooleanUtils.TRUE : ensures, Att().add("initializer")); sentences.add(getExitCode); KVariable var = KVariable("Exit", Att.empty().add(Sort.class, Sorts.Int())); Rule getExitCodeRule = Rule(KRewrite(KApply(getExitCodeLabel, IncompleteCellUtils.make(KLabels.GENERATED_TOP_CELL, true, IncompleteCellUtils.make(KLabel(klabel), false, var, false), true)), var), BooleanUtils.TRUE, BooleanUtils.TRUE); sentences.add(getExitCodeRule);