KVariable newDotVariable() { KVariable newLabel; do { newLabel = KVariable("_" + (counter++)); } while (vars.contains(newLabel)); vars.add(newLabel); return newLabel; }
KVariable newDotVariable(Sort sort) { KVariable newLabel; do { newLabel = KVariable("_" + (counter++), Att().add(Sort.class, sort)); } while (vars.contains(newLabel)); vars.add(newLabel); return newLabel; }
KVariable newDotVariable(Sort sort) { KVariable newLabel; do { newLabel = KVariable("_" + (counter++), Att().add(Sort.class, sort)); } while (vars.contains(newLabel)); vars.add(newLabel); return newLabel; }
KVariable newDotVariable() { KVariable newLabel; do { newLabel = KVariable("_" + (counter++), Att().add("anonymous")); } while (vars.contains(newLabel)); vars.add(newLabel); return newLabel; }
KVariable newDotVariable(Sort s) { KVariable newLabel; do { if (s == null) { newLabel = KVariable("DotVar" + (counter++), Att().add("anonymous")); } else { newLabel = KVariable("DotVar" + (counter++), Att().add("anonymous").add(Sort.class, s)); } } while (vars.contains(newLabel)); vars.add(newLabel); return newLabel; }
@Override public K apply(KVariable k) { if (normalization.containsKey(k)) { return KVariable(normalization.get(k), k.att().add("denormal", k.name())); } return super.apply(k); } }.apply(denormal);
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); }
@Override public void apply(KVariable k) { if (k.att().contains("denormal")) { normalization.put(KVariable(k.att().get("denormal")), k.name()); } } }.apply(normal);
@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)); }
@Test public void testConcatStarCell() { K term = cell("<top>", KRewrite(KVariable("Y"), cells(KVariable("Y"), cell("<t>", KVariable("X"))))); K expected = cell("<top>", KRewrite(KVariable("Y"), KApply(KLabel("_ThreadCellBag_"), KVariable("Y"), cell("<t>", KVariable("_0"), KVariable("_1"), KVariable("_2"))))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@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 testSimpleSplitting() { KVariable Y = KVariable("Y", Att().add(Sort.class, Sort("OptCell"))); K term = KRewrite(cell("<t>", cell("<env>"), KVariable("X"), Y), KVariable("X")); K expected = KRewrite(cell("<t>", KVariable("X"), cell("<env>"), Y), cell("<t>-fragment", KVariable("X"), app("noEnvCell"), app(".OptCell"))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testArgumentAnnItemVar() { parseTerm("f(V:A)", "K", KApply(F, KApply(CONS, KApply(CAST_A, KVariable("V")), NIL))); }
@Test public void testArgumentAnnSubItemVar() { parseTerm("f(V:B)", "K", KApply(F, KApply(CONS, KApply(CAST_B, KVariable("V")), NIL))); } }
@Test public void testAddOptCell() { K term = cell("<t>", KVariable("X"), KRewrite(cells(), cell("<opt>"))); K expected = cell("<t>", KVariable("_0"), KVariable("_1"), KRewrite(KApply(KLabel(".OptCell")), cell("<opt>"))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testCloseList() { K term = KApply(KLabels.CELLS, cell("<list>", true, false, intToToken(1)), cell("<list>", false, true, intToToken(2)), cell("<list>", true, true, intToToken(3))); K expected = KApply(KLabels.CELLS, ccell("<list>", KApply(KLabel("_List_"), KVariable("DotVar0"), intToToken(1))), ccell("<list>", KApply(KLabel("_List_"), intToToken(2), KVariable("DotVar1"))), ccell("<list>", KApply(KLabel("_List_"), KVariable("DotVar2"), KApply(KLabel("_List_"), intToToken(3), KVariable("DotVar3"))))); Assert.assertEquals(expected, new CloseCells(cfgInfo, sortInfo, labelInfo).close(term)); }
@Test public void testConcatStarCellEmptyl() { K term = cell("<top>", KRewrite(KVariable("Y"), cells())); K expected = cell("<top>", KRewrite(KVariable("Y"), KApply(KLabel(".ThreadCellBag")))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
private static Set<Sentence> genProjection(Sort sort, Module m) { KLabel lbl = getProjectLbl(sort, m); KVariable var = KVariable("K", Att.empty().add(Sort.class, sort)); Rule r = Rule(KRewrite(KApply(lbl, var), var), BooleanUtils.TRUE, BooleanUtils.TRUE, Att().add("projection")); if (m.definedKLabels().contains(lbl)) { return Set(r); } return Set(Production(lbl, sort, Seq(Terminal(lbl.name()), Terminal("("), NonTerminal(Sorts.K()), Terminal(")")), Att().add("function").add("projection")), r); }
@Test public void testUselessVariable() { K term = cell("<t>", cell("<env>"), cell("<k>"), cell("<opt>"), KVariable("X")); K expected = cell("<t>", cell("<k>"), cell("<env>"), cell("<opt>")); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }