private K left(K term) { if (term instanceof KRewrite) { return ((KRewrite)term).left(); } else { return term; } } private K right(K term) {
@Override public K apply(KRewrite k) { // drop rhs return apply(k.left()); } }.apply(rule.body());
@Override public void apply(KRewrite k) { sb.append("\\rewrites{"); convert(k.att().get(Sort.class), false); sb.append("}("); apply(k.left()); sb.append(","); apply(k.right()); sb.append(")"); }
@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 void apply(KRewrite k) { isRHS = false; apply(k.left()); isRHS = true; isLHS = false; apply(k.right()); isLHS = true; }
@Override public void apply(KRewrite k) { assert !inRewrite; inRewrite = true; apply(k.left()); inRhs = true; apply(k.right()); inRhs = false; inRewrite = false; }
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; } }
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 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; }
@Override public K apply(KRewrite k) { k = (KRewrite) super.apply(k); return KRewrite(k.left(), k.right(), k.att()); }
@Override public K apply(KRewrite k) { K left = super.apply(k.left()); return KRewrite(left, k.right(), k.att()); } }.apply(term);
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; }
private Rule resolveInitRule(Production streamProduction, Rule rule) { Sort streamSort = streamProduction.sort(); // InCell, OutCell String initLabel = GenerateSentencesFromConfigDecl.getInitLabel(streamSort); // initInCell, initOutCell KLabel cellLabel = streamProduction.klabel().get(); // <in>, <out> // rule initInCell(Init) => <in> ... </in> if (isInitRule(initLabel, cellLabel.name(), rule)) { KRewrite body = (KRewrite) rule.body(); KApply right = (KApply) body.right(); KList klist = getContentsOfInitRule(streamProduction); right = KApply(right.klabel(), klist, right.att()); body = KRewrite(body.left(), right, body.att()); return Rule(body, rule.requires(), rule.ensures(), rule.att()); } return rule; }