private K left(K term) { if (term instanceof KRewrite) { return ((KRewrite)term).left(); } else { return term; } } private K right(K term) {
private K right(K term) { if (term instanceof KRewrite) { return ((KRewrite)term).right(); } else { return term; } }
@Override public K apply(KRewrite k) { // drop rhs return apply(k.left()); } }.apply(rule.body());
@Override public void apply(KRewrite k) { heated = k.right(); super.apply(k); }
private boolean isInitRule(String initLabel, String cellLabel, Sentence s) { try { // rule initXCell(Init) => <x> ... </x> KRewrite body = (KRewrite) ((Rule) s).body(); KApply left = (KApply) body.left(); KApply right = (KApply) body.right(); return left.klabel().name().equals(initLabel) // initXCell && right.klabel().name().equals(cellLabel); // <x> } catch (ClassCastException ignored) { return false; } }
private KList getContentsOfInitRule(Production streamProduction) { String streamName = streamProduction.att().get("stream"); // stdin, stdout String initLabel = GenerateSentencesFromConfigDecl.getInitLabel( Sort(GenerateSentencesFromConfigDecl.getSortOfCell(streamName))); // initStdinCell, initStdoutCell String cellLabel = "<" + streamName + ">"; // <stdin>, <stdout> java.util.List<Sentence> initRules = stream(getStreamModule(streamName).localSentences()) .filter(s -> isInitRule(initLabel, cellLabel, s)) .collect(Collectors.toList()); assert initRules.size() == 1; Sentence initRule = initRules.get(0); // rule initXCell(Init) => <x> ... </x> KRewrite body = (KRewrite) ((Rule) initRule).body(); KApply right = (KApply) body.right(); return right.klist(); }
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); } }
@Override public void apply(KRewrite k) { if (!isHOLE(k.left())) { throw KEMException.compilerError("Only the HOLE can be rewritten in a context definition", context); } super.apply(k); }
@Override public DefaultMutableTreeNode apply(KRewrite k) { DefaultMutableTreeNode node = new DefaultMutableTreeNode("=>"); DefaultMutableTreeNode l = apply(k.left()); DefaultMutableTreeNode r = apply(k.right()); if (l.isLeaf() && r.isLeaf()) { String text = ToKast.apply(k); if (text.length() < 80) { node.setUserObject(text); return node; } } node.add(l); node.add(r); return node; }
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); }
private Optional<Sort> getPredicateSort(Rule r) { KLabel topKLabel; Optional<Sort> sort; if (r.body() instanceof KApply) { topKLabel = ((KApply) r.body()).klabel(); sort = mod.attributesFor().apply(topKLabel).getOptional(Attribute.PREDICATE_KEY, Sort.class); } else if (r.body() instanceof KRewrite) { KRewrite rw = (KRewrite) r.body(); if (rw.left() instanceof KApply) { topKLabel = ((KApply) rw.left()).klabel(); sort = mod.attributesFor().apply(topKLabel).getOptional(Attribute.PREDICATE_KEY, Sort.class); } else { sort = Optional.empty(); } } else sort = Optional.empty(); return sort; }