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; }
private Stream<? extends Sentence> genKore(Sort sort) { if (sort.equals(Sorts.K())) { return Stream.of(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KVariable("K")), BooleanUtils.TRUE), BooleanUtils.TRUE, BooleanUtils.TRUE)); } else { List<Sentence> res = new ArrayList<>(); res.add(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KVariable(sort.name(), Att().add(Sort.class, sort))), BooleanUtils.TRUE), BooleanUtils.TRUE, BooleanUtils.TRUE)); res.add(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KVariable("K")), BooleanUtils.FALSE), BooleanUtils.TRUE, BooleanUtils.TRUE, Att().add("owise"))); return res.stream(); } }
/** * Creates the seed module that can be used to parse rules. * Imports module markers RULE-CELLS and K found in /include/kast.k. * @param mod The user defined module from which to start. * @return a new module which imports the original user module and a set of marker modules. */ public Module getRuleGrammar(Module mod) { // import RULE-CELLS in order to parse cells specific to rules Module newM = new Module( mod.name() + "-" + RULE_CELLS , Set(mod, baseK.getModule(K).get(), baseK.getModule(RULE_CELLS).get(), baseK.getModule(DEFAULT_LAYOUT).get()) , Set() , Att() ); return newM; }
/** * Creates the seed module that can be used to parse configurations. * Imports module markers CONFIG-CELLS and K found in /include/kast.k. * @param mod The user defined module from which to start. * @return a new module which imports the original user module and a set of marker modules. */ public Module getConfigGrammar(Module mod) { // import CONFIG-CELLS in order to parse cells specific to configurations Module newM = new Module( mod.name() + "-" + CONFIG_CELLS , Set(mod, baseK.getModule(K).get(), baseK.getModule(CONFIG_CELLS).get(), baseK.getModule(DEFAULT_LAYOUT).get()) , Set() , Att() ); return newM; }
private static Att getCellPropertiesAsAtt(K k, String cellName, K ensures) { Att att = Att(); if (cellName.equals("k")) { att = att.add("maincell"); } if (ensures != null) { att = att.add("topcell"); } att = att.add("cell").add("cellName", cellName); return att.addAll(getCellPropertiesAsAtt(k)); }
private Production funProd(KLabel fun, K k) { List<ProductionItem> pis = new ArrayList<>(); K left = RewriteToTop.toLeft(k); K right = RewriteToTop.toRight(k); pis.add(Terminal(fun.name())); pis.add(Terminal("(")); pis.add(NonTerminal(sort(left))); for (KVariable var : closure(k)) { pis.add(Terminal(",")); pis.add(NonTerminal(var.att().getOptional(Sort.class).orElse(Sorts.K()))); } pis.add(Terminal(")")); return Production(fun, sort(right), immutable(pis), Att().add("function")); }
private static Set<Sentence> makeCasts(Sort outerSort, Sort innerSort, Sort castSort) { Set<Sentence> prods = new HashSet<>(); Att attrs1 = Att().add(Sort.class, castSort); prods.add(Production(KLabel("#SyntacticCast"), castSort, Seq(NonTerminal(castSort), Terminal("::" + castSort.toString())), attrs1)); prods.add(Production(KLabel("#SemanticCastTo" + castSort.toString()), castSort, Seq(NonTerminal(castSort), Terminal(":" + castSort.toString())), attrs1)); prods.add(Production(KLabel("#InnerCast"), outerSort, Seq(Terminal("{"), NonTerminal(castSort), Terminal("}"), Terminal("<:" + castSort.toString())), attrs1)); prods.add(Production(KLabel("#OuterCast"), castSort, Seq(Terminal("{"), NonTerminal(innerSort), Terminal("}"), Terminal(":>" + castSort.toString())), attrs1)); return prods; } }
private static Att getCellPropertiesAsAtt(K k) { if (k instanceof KApply) { KApply kapp = (KApply) k; if (kapp.klabel().name().equals("#cellPropertyListTerminator")) { return Att(); } else if (kapp.klabel().name().equals("#cellPropertyList")) { if (kapp.klist().size() == 2) { Tuple2<String, String> attribute = getCellProperty(kapp.klist().items().get(0)); return Att().add(attribute._1(), attribute._2()).addAll(getCellPropertiesAsAtt(kapp.klist().items().get(1))); } } } throw KEMException.compilerError("Malformed cell properties", k); }
public static Definition addSemanticsModule(Definition d) { java.util.Set<Module> allModules = mutable(d.modules()); Module languageParsingModule = Constructors.Module("LANGUAGE-PARSING", Set(d.mainModule(), d.getModule(d.att().get(Att.syntaxModule())).get(), d.getModule("K-TERM").get(), d.getModule(RuleGrammarGenerator.ID_PROGRAM_PARSING).get()), Set(), Att()); allModules.add(languageParsingModule); return Constructors.Definition(d.mainModule(), immutable(allModules), d.att()); }
public Module gen(Module mod) { Set<Sentence> res = new HashSet<>(); for (Sort sort : iterable(mod.definedSorts())) { Production prod = Production(KLabel("is" + sort.toString()), Sorts.Bool(), Seq(Terminal("is" + sort.toString()), Terminal("("), NonTerminal(Sorts.K()), Terminal(")")), Att().add(Attribute.FUNCTION_KEY).add(Attribute.PREDICATE_KEY, Sort.class, sort)); if (!mod.productions().contains(prod)) res.add(prod); } if (!res.isEmpty()) { res.add(SyntaxSort(Sorts.K())); } return Module(mod.name(), mod.imports(), (scala.collection.Set<Sentence>) mod.localSentences().$bar(immutable(res)), mod.att()); } }
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 testFrom() throws Exception { org.kframework.definition.Definition actual = Definition.from("module X endmodule"); Module modSyntax = Module.apply("X$SYNTAX", Set()); Module mod = new Module("X", Set(modSyntax), Set(), Att()); assertEquals(org.kframework.definition.Definition.apply(mod, Set(mod, modSyntax), Att()), actual); } }
@Test public void testRewriteWithCellVariable() { K term = cell("<T>", KRewrite(KVariable("KCell", Att().add(Sort.class, Sort("KCell"))), cell("<k>", intToToken(1)))); K expected = cell("<T>", cell("<ts>", cell("<t>", KRewrite(KVariable("KCell", Att().add(Sort.class, Sort("KCell"))), cell("<k>", intToToken(1)))))); Assert.assertEquals(expected, pass.concretizeCell(term)); }
/** * Ensure that a variable does not become a cell fragment if it is annotated * with a single-cell sort. */ @Test public void testSortedVar() { KVariable Y = KVariable("Y", Att().add(Sort.class, Sort("OptCell"))); K term = KRewrite(cell("<t>", cell("<env>"), KVariable("X"), Y), Y); K expected = KRewrite(cell("<t>", KVariable("X"), cell("<env>"), Y), Y); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@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()); }