@Override public K apply(KSequence k) { ArrayList<K> newItems = new ArrayList<>(k.items()); boolean change = false; for (int i = 0; i < newItems.size(); ++i) { K in = newItems.get(i); K out = apply(newItems.get(i)); newItems.set(i, out); change = change || (in != out); } if (change) { return KSequence(newItems, k.att()); } else { return k; } }
return result; } else { return KSequence(result);
@Override public K apply(KSequence k) { k = (KSequence) super.apply(k); return KSequence(k.items(), k.att()); }
@Override public K apply(KApply k) { List<K> children = new ArrayList<>(); for (K child : k.klist().items()) { K res = apply(child); if (res instanceof KSequence) { children.add(res); } else { children.add(KSequence(res)); } } return KApply(k.klabel(), KList(children), k.att()); }
private int convert(Rule r, StringBuilder sb, RuleType type, int ruleNum, String functionName) { try { convertComment(r, sb); sb.append("| "); K left = RewriteToTop.toLeft(r.body()); K requires = r.requires(); VarInfo vars = new VarInfo(); convertLHS(sb, type, left, vars); String result = convert(vars); String suffix = ""; boolean when = true; if (type == RuleType.REGULAR && options.checkRaces) { sb.append(" when start_after < ").append(ruleNum); when = false; } if (!requires.equals(KSequence(BooleanUtils.TRUE)) || !result.equals("true")) { suffix = convertSideCondition(sb, requires, vars, Collections.emptyList(), when, type, ruleNum); } sb.append(" -> "); convertRHS(sb, type, r, vars, suffix, ruleNum, functionName, true); return ruleNum + 1; } catch (NoSuchElementException e) { System.err.println(r); throw e; } catch (KEMException e) { e.exception.addTraceFrame("while compiling rule at " + r.att().getOptional(Source.class).map(Object::toString).orElse("<none>") + ":" + r.att().getOptional(Location.class).map(Object::toString).orElse("<none>")); throw e; } }
K lhs = RewriteToTop.toLeft(body); K rhs = RewriteToTop.toRight(body); if (rhs.equals(KSequence(BooleanUtils.FALSE)) && r.att().contains("owise")) { continue; if (!rhs.equals(KSequence(BooleanUtils.TRUE))) { throw KEMException.compilerError("Unexpected form for klabel predicate rule, expected predicate(_) => false [owise] or predicate(#klabel(`klabel`)) => true.", r);
K concretize(K term) { if (term instanceof KApply) { KApply app = (KApply) term; KApply newTerm = KApply(app.klabel(), KList(app.klist().stream() .map(this::concretize).collect(Collectors.toList()))); if (cfg.isParentCell(newTerm.klabel())) { return concretizeCell(newTerm); } else { return newTerm; } } else if (term instanceof KRewrite) { KRewrite rew = (KRewrite) term; return KRewrite(concretize(rew.left()), concretize(rew.right())); } else if (term instanceof KSequence) { return KSequence(((KSequence) term).stream() .map(this::concretize).collect(Collectors.toList())); } else { return term; } }
K lhs = RewriteToTop.toLeft(r.body()); K rhs = RewriteToTop.toRight(r.body()); if (rhs.equals(KSequence(BooleanUtils.FALSE))) { continue;
@Override public K apply(KAs k) { K res = apply(k.pattern()); KVariable var = (KVariable) k.alias(); if (!(res instanceof KSequence) && var.att().getOptional(Sort.class).orElse(Sorts.K()).equals(Sorts.K())) { res = KSequence(res); } return KAs(res, k.alias(), k.att()); } }.apply(term);
public K gen(Rule r, K body) { if (!cover || !r.att().getOptional(Source.class).isPresent()) { return body; } K left = RewriteToTop.toLeft(body); K right = RewriteToTop.toRight(body); String file = r.att().get(Source.class).source(); if (file.startsWith(JarInfo.getKBase())) { return body; } int line = r.att().get(Location.class).startLine(); int col = r.att().get(Location.class).startColumn(); String loc = file + ":" + line + ":" + col; String id = r.att().get("UNIQUE_ID"); allRulesFile.print(id); allRulesFile.print(" "); allRulesFile.println(loc); if (r.att().contains("macro") || r.att().contains("alias")) { //handled by macro expander return body; } return KRewrite(left, KSequence(KApply(KLabel("#logToFile"), KToken(StringUtil.enquoteKString(files.resolveKompiled("coverage.txt").getAbsolutePath()), Sorts.String()), KToken(StringUtil.enquoteKString(id + '\n'), Sorts.String())), right)); }
} else if (expectedSort.equals(Sorts.K())) { if (actualSort.equals(Sorts.KItem())) { return KSequence(visitChildren(term, Sorts.KItem(), actualSort)); } else { return KSequence(KApply(KLabel("inj", actualSort, Sorts.KItem()), KList(visitChildren(term, Sorts.KItem(), actualSort)), Att.empty().add(Sort.class, Sorts.KItem())));
@Test public void testArgumentInferredItemVar() { // 1 warning from inference parseTerm("f(V)~>g(V)", "K", KSequence(KApply(F, KApply(CONS, KApply(CAST_A, KVariable("V")), NIL)), KApply(G, KApply(CAST_A, KVariable("V")))), 1); }
@Test public void testFragment1() { K term = cell("<top>",cell("<t>",KVariable("F")), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(KVariable("F"), KVariable("Rest")))), KVariable("F2"))); K expected = cell("<top>",app("_ThreadCellBag_", cell("<t>", KVariable("_0"), KVariable("_1"), KVariable("_2")), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(cell("<t>-fragment", KVariable("_0"), KVariable("_1"), KVariable("_2")), KVariable("Rest")))), KVariable("_3"), KVariable("_4")))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); } @Test
@Test public void testFragment4() { K term = cell("<top>",cell("<t>",cell("<env>",KVariable("E")),KVariable("F")), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(KVariable("F"), KVariable("Rest")))), KVariable("F2"))); K expected = cell("<top>",app("_ThreadCellBag_", cell("<t>", KVariable("_0"), cell("<env>", KVariable("E")), KVariable("_1")), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(cell("<t>-fragment", KVariable("_0"), app("noEnvCell"), KVariable("_1")), KVariable("Rest")))), KVariable("_2"), KVariable("_3")))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testFragment2() { K term = cell("<top>",cell("<t>",cell("<k>",KVariable("K")),KVariable("F")), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(KVariable("F"), KVariable("Rest")))), KVariable("F2"))); K expected = cell("<top>",app("_ThreadCellBag_", cell("<t>", cell("<k>", KVariable("K")), KVariable("_0"), KVariable("_1")), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(cell("<t>-fragment", app("noKCell"), KVariable("_0"), KVariable("_1")), KVariable("Rest")))), KVariable("_2"), KVariable("_3")))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); } @Test
@Test public void testLeafContent() { K term = cell("<T>", cell("<k>", KSequence(KApply(KLabel("_+_"), KVariable("I"), KVariable("J")), KVariable("Rest")))); K expected = cell("<T>", cell("<ts>", cell("<t>", cell("<k>", KSequence(KApply(KLabel("_+_"), KVariable("I"), KVariable("J")), KVariable("Rest")))))); Assert.assertEquals(expected, pass.concretize(term)); }
@Test public void testFragment3() { K term = cell("<top>",cell("<t>",cell("<opt>",KVariable("O")),KVariable("F")), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(KVariable("F"), KVariable("Rest")))), KVariable("F2"))); K expected = cell("<top>",app("_ThreadCellBag_", cell("<t>", KVariable("_0"), KVariable("_1"), cell("<opt>", KVariable("O"))), cell("<t>", cell("<k>", KRewrite(KSequence(KVariable("Rest")), KSequence(cell("<t>-fragment", KVariable("_0"), KVariable("_1"), app(".OptCell")), KVariable("Rest")))), KVariable("_2"), KVariable("_3")))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); } @Test
@Test public void testFragmentBag() { K term = cell("<top>", KVariable("F"),cell("<t>", KRewrite(KVariable("Rest"),KSequence(KVariable("F"),KVariable("Rest"))))); K expected = cell("<top>", app("_ThreadCellBag_", KVariable("_0"), cell("<t>", KRewrite(KVariable("Rest"), KSequence(app("<top>-fragment",KVariable("_0"),KVariable("_1")),KVariable("Rest"))))), KVariable("_1")); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(bagCfgInfo, bagLabelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testSimpleClosure() { K term = cell("<k>", false, true, KApply(KLabel("_+_"), KVariable("I"), KVariable("J"))); K expected = ccell("<k>", KSequence(KApply(KLabel("_+_"), KVariable("I"), KVariable("J")), KVariable("DotVar0"))); Assert.assertEquals(expected, new CloseCells(cfgInfo, sortInfo, labelInfo).close(term)); }