public void addDefault(String cell, KApply term) { defaultCells.put(Sort(cell), term); }
public void addUnit(String cell, KLabel label) { units.put(Sort(cell), label); }
public void addConcat(String cell, KLabel label) { concats.put(Sort(cell), label); }
private void applyVarLhs(KVariable k, StringBuilder sb, VarInfo vars) { String varName = encodeStringToVariable(k.name()); vars.vars.put(k, varName); Sort s = k.att().getOptional(Sort.class).orElse(Sort("")); if (mainModule.sortAttributesFor().contains(s)) { String hook = mainModule.sortAttributesFor().apply(s).<String>getOptional("hook").orElse(""); if (sortVarHooks.containsKey(hook)) { sb.append("("); sb.append(sortVarHooks.get(hook).apply(s)); sb.append(" as ").append(varName).append(")"); return; } } sb.append(varName); }
private Sort renameSort(Sort sort) { sort = definition.smtSortFlattening().getOrDefault(sort, sort); if (sort == Sort.LIST) { return Sort.of(KORE.Sort("IntSeq")); } else if (sort == Sort.of(Sorts.Id())) { return Sort.INT; } else { return sort; } }
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(); }
private KApply cell(String s, Map<String, String> att, K body) { K cellAtt = att.entrySet().stream() .map(e -> KApply(KLabel("#cellProperty"), KToken(e.getKey(), Sort("#CellName")), KToken(StringUtil.enquoteKString(e.getValue()), Sort("KString")))) .reduce(KApply(KLabel("#cellPropertyListTerminator")), (k1, k2) -> KApply(KLabel("#cellPropertyList"), k2, k1)); return KApply(KLabel("#configCell"), KToken(s, Sort("#CellName")), cellAtt, body, KToken(s, Sort("#CellName"))); } }
public Token KToken(String s, org.kframework.kore.Sort sort, Att att) { return !sort.equals(KORE.Sort("KBoolean")) ? Token.of(Sort(sort), s) : Token.of(Sort(Sorts.Bool()), s); }
@Override public scala.collection.Set<Sort> getCellBagSortsOfCell(Sort k) { return Set(Sort(k.name() + "Bag", k.params())); } }
@Override public K apply(KToken k) { return KToken(k.s(), Sort(k.sort().name(), k.sort().params()), k.att()); }
@Test public void test24() { String def = "" + "module TEST\n" + "syntax A\n" + "syntax A ::= f(A) [klabel(l)]\n" + "endmodule"; parseRule("l(_) => .K", def, 0, KApply(KLabel("#ruleNoConditions"),KApply(KLabel("#KRewrite"), KApply(KLabel("#KApply"), KToken("l",Sort("KLabel")), KToken("_",Sort("#KVariable"))), KApply(KLabel("#EmptyK")) ))); }
private void parseProgram(String input, String def, String startSymbol, int warnings, boolean expectedError) { Module test = ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by RuleGrammarTest"), "TEST"); ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(gen.getProgramsGrammar(test), true); Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule = parser.parseString(input, Sort(startSymbol), Source.apply("generated by RuleGrammarTest")); printout(rule, warnings, expectedError); }
private K unparseThenParse(K origTerm, OutputModes outputMode) { byte[] unparsed = new KPrint().serialize(origTerm, outputMode); switch (outputMode) { case JSON: return JsonParser.parse(unparsed); case BINARY: return BinaryParser.parse(unparsed); case KAST: return KoreParser.parse(bytes2String(unparsed), new Source("KPrintTest")); default: return new KToken("###", Sort("UnsupportedOutputMode")); } }
@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)); }
private void parseProgram(String input, String def, String startSymbol, int warnings, K expectedResult) { Module test = ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by RuleGrammarTest"), "TEST"); ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(gen.getProgramsGrammar(test), true); Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule = parser.parseString(input, Sort(startSymbol), Source.apply("generated by RuleGrammarTest")); printout(rule, warnings, false); Assert.assertEquals(expectedResult, rule._1().right().get()); }
private void parseTerm(String term, String sort, K expected, int expectWarnings) { String source = "AddEmpytListsTest." + testName.getMethodName(); final Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> parseResult = parser.parseString(term, Sort(sort), new Source(source)); if (parseResult._1().isLeft()) { Assert.assertTrue("Unexpected parse errors" + parseResult._1().left().get(), false); } K actual = new TreeNodesToKORE(Outer::parseSort, false).down(parseResult._1().right().get()); Assert.assertEquals(expected, actual); if (parseResult._2().size() != expectWarnings) { Assert.assertTrue("Unexpected parse warnings" + parseResult._2(), false); } }
/** * 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()); }
/** Ensure that the splitting in {@linkplain #testPredicateExpansion()} does not happen * in a term which applies the sort predicate for a <em>different</em> cell fragment sort * to a cell fragment variable. */ @Test public void testUnrelatedPredicate() { Rule term = new Rule(KRewrite(cell("<t>", cell("<env>"), KVariable("X"), KVariable("Y", Att().add(Sort.class, Sort("OptCell")))), KVariable("X")) , app("isTopCellFragment",KVariable("X")) , BooleanUtils.TRUE , Att()); K replacement = app("<t>-fragment", KVariable("X"), app("noEnvCell"), app(".OptCell")); K expectedBody = KRewrite(cell("<t>", KVariable("X"), cell("<env>"), KVariable("Y", Att().add(Sort.class, Sort("OptCell")))), replacement); Rule expected = new Rule(expectedBody , app("isTopCellFragment", replacement) , BooleanUtils.TRUE, Att()); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
/** When a cell fragment variable occurs as an argument to the appropriate cell fragment sort predict, we * specialize the expansion by splitting it into a conjunction of individual sort predicate tests on * the variables the cell fragment variable splits into, rather than just using the generic replacement term. * This is a very special case of statically simplifying predicate applications. */ @Test public void testPredicateExpansion() { Rule term = new Rule(KRewrite(cell("<t>", cell("<env>"), KVariable("X"), KVariable("Y", Att().add(Sort.class, Sort("OptCell")))), KVariable("X")) , app("isThreadCellFragment",KVariable("X")) , BooleanUtils.TRUE , Att()); K expectedBody = KRewrite(cell("<t>", KVariable("X"), cell("<env>"), KVariable("Y", Att().add(Sort.class, Sort("OptCell")))), cell("<t>-fragment", KVariable("X"), app("noEnvCell"), app(".OptCell"))); Rule expected = new Rule(expectedBody , BooleanUtils.and(BooleanUtils.TRUE, app("isKCell", KVariable("X"))) , BooleanUtils.TRUE, Att()); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }