/** * Takes a configuration declaration and returns the sentences that it desugars into. * * Cells of multiplicity 1 desugar into an initializer production, an initializer rule, and a cell production. * Cells of multiplicity * desugar into an initializer production, an initializer rule, a cell production, and a bag * sort to represent a bag of those cells. * Cells of multiplicity ? desugar into an initializer production, an initializer rule, a cell production, and an * empty production indicating the absence of that cell. * Cells with children additionally generate a *CellFragment sort with the same arity as the cell production, * but the arguments made optional by generating additional sorts. * Cells which have parents and are not multiplicity * generate a CellOpt sort which is a supersort of the cell sort * and has an additional production name like {@code <cell>-absent}. (For a cell with multiplicitly ? this is * necessary to distinguish a fragment that did capture the state of the cell when it wasn't present, from * a cell fragment that didn't even try to capture the cell). * * Currently the implementation does not handle initializer rules; we will address this eventually. * @param body The body of the configuration declaration. * @param ensures The ensures clause of the configuration declaration. * @param att The attributes of the configuration declaration. * @param m The module the configuration declaration exists in. * @return A set of sentences representing the configuration declaration. */ public static Set<Sentence> gen(K body, K ensures, Att att, Module m, boolean kore) { return genInternal(body, ensures, att, m, kore)._1(); }
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); }
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(); }
if (label.sort().equals(Sort("#CellName"))) { String cellName = label.s(); Att cellProperties = getCellPropertiesAsAtt(kapp.klist().items().get(1), cellName, ensures); Multiplicity multiplicity = convertStringMultiplicity( cellProperties.getOption("multiplicity"), term); boolean isStream = cellProperties.getOption("stream").isDefined(); Tuple4<Set<Sentence>, List<Sort>, K, Boolean> childResult = genInternal( cellContents, null, cfgAtt, m, kore); Tuple4<Set<Sentence>, Sort, K, Boolean> myResult = computeSentencesOfWellFormedCell(isLeafCell, isStream, kore, multiplicity, cfgAtt, m, cellName, cellProperties, childResult._2(), childResult._3(), ensures, hasConfigOrRegularVariable(cellContents)); return Tuple4.apply((Set<Sentence>)childResult._1().$bar(myResult._1()), Lists.newArrayList(myResult._2()), myResult._3(), false); if (label.sort().equals(Sort("#CellName"))) { String cellName = label.s(); Sort sort = Sort(getSortOfCell(cellName)); Option<Set<Production>> initializerProduction = m.productionsFor().get(KLabel(getInitLabel(sort))); if (initializerProduction.isDefined()) { Set<Production> realProds = stream(initializerProduction.get()).filter(p -> !p.att().contains("recordPrd", Production.class)).collect(Collections.toSet()); return Tuple4.apply(Set(), Lists.newArrayList(sort), KApply(KLabel(getInitLabel(sort))), true); } else if (realProds.head().items().size() == 4) { return Tuple4.apply(Set(), Lists.newArrayList(sort), KApply(KLabel(getInitLabel(sort)), INIT), true); return genInternal(generatedTop, ensures, cfgAtt, m, kore); Tuple4<Set<Sentence>, List<Sort>, K, Boolean> childResult = genInternal(cell, null, cfgAtt, m, kore);
configDecl -> stream(GenerateSentencesFromConfigDecl.gen(configDecl.body(), configDecl.ensures(), configDecl.att(), parser.getExtensionModule(), kore))) .collect(Collections.toSet());
K ensures, boolean hasConfigurationOrRegularVariable) { String sortName = getSortOfCell(cellName); Sort sort = Sort(sortName); String initLabel = getInitLabel(sort); Sentence initializer; Rule initializerRule; rhs = optionalCellInitializer(hasConfigurationOrRegularVariable, cellProperties, initLabel); } else if (multiplicity == Multiplicity.OPTIONAL) { rhs = optionalCellInitializer(hasConfigurationOrRegularVariable, cellProperties, initLabel); } else {
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 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; }
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); }
Set<Sentence> newSentences = GenerateSentencesFromConfigDecl.gen(generatedTop, BooleanUtils.TRUE, Att.empty(), mod.getExtensionModule(), true); sentences = (Set<Sentence>) sentences.$bar(newSentences); RuleGrammarGenerator gen = new RuleGrammarGenerator(def); ParseInModule mod = RuleGrammarGenerator.getCombinedGrammar(gen.getConfigGrammar(m), true); Set<Sentence> newSentences = GenerateSentencesFromConfigDecl.gen(freshCell, BooleanUtils.TRUE, Att.empty(), mod.getExtensionModule(), true); sentences = (Set<Sentence>) sentences.$bar(newSentences);
RuleGrammarGenerator parserGen = new RuleGrammarGenerator(def); Module m = RuleGrammarGenerator.getCombinedGrammar(parserGen.getConfigGrammar(m1), true).getExtensionModule(); Set<Sentence> gen = GenerateSentencesFromConfigDecl.gen(configuration, BooleanUtils.FALSE, Att(), m, false); Att initializerAtts = Att().add("initializer"); Att productionAtts = initializerAtts.add("function").add("noThread");