KLabel productionLabel = null; List<K> leftChildren = null; K left = RewriteToTop.toLeft(rule.body()); boolean constructorBased = constructorChecks.isConstructorBased(left); if (left instanceof KApply) { .map(NonTerminal::sort).collect(Collectors.toList()); productionLabel = production.klabel().get(); if (isFunction(prod) || rule.att().contains(Attribute.ANYWHERE_KEY)) { leftChildren = ((KApply) left).items(); equation = true; } else if ((rule.att().contains("heat") || rule.att().contains("cool")) && heatCoolEq) { equation = true; productionSort = topCellSort; owise = rule.att().contains("owise"); sb.append(rule.toString()); sb.append("\n"); if (equation) { if (!rule.att().contains(Attribute.SIMPLIFICATION_KEY)) { rule = rule.withAtt(rule.att().add(Attribute.SIMPLIFICATION_KEY)); sb.append("\\implies{R} (\n \\and{R} (\n \\not{R} (\n "); for (Rule notMatching : functionRules.get(productionLabel)) { if (notMatching.att().contains("owise")) { continue;
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 applied = super.apply(k); for (Rule r : rules) { if (!r.requires().equals(BooleanUtils.TRUE)) { throw KEMException.compilerError("Cannot compute macros with side conditions.", r); K left = RewriteToTop.toLeft(r.body()); K right = RewriteToTop.toRight(r.body()); if (reverse) { K tmp = left; if (match(subst, left, applied, r)) { if (cover) { if (!r.att().contains("UNIQUE_ID")) System.out.println(r.toString()); coverage.println(r.att().get("UNIQUE_ID"));
public void check(Sentence sentence) { if (sentence instanceof Rule) { check(((Rule) sentence).body()); } }
private void appendStepFunction(StringBuilder sb, RuleType ruleType, Rule r, int ruleNum, String functionName) { if (ruleType == RuleType.REGULAR) { if (options.checkRaces) { String location = enquoteString(r.att().getOptional(Source.class).map(Object::toString).orElse("<none>") + ":" + r.att().getOptional(Location.class).map(Object::toString).orElse("<none>")); sb.append("), ((").append(ruleNum).append("), ").append(getRaceRuleType(r)).append(", ").append(location); } sb.append("), (StepFunc "); K right = RewriteToTop.toRight(r.body()); Optional<KLabel> next = getNextOperation(right, true); if (next.isPresent()) { sb.append(realStepFunctions.get(next.get())); } else { Optional<KApply> kCell = hasKCellContents(right); if(kCell.isPresent()) { sb.append("(get_next_op_from_exp kCell)"); } else if (!hasKCellContents(RewriteToTop.toLeft(r.body())).isPresent()) { sb.append(functionName); } else { sb.append("step"); } } sb.append("))"); } }
/** * Converts the org.kframework.Rules to backend Rules, also plugging in the automaton rule */ public void addKoreRules(Module module, GlobalContext global) { KOREtoBackendKIL transformer = new KOREtoBackendKIL(module, this, global, true); List<org.kframework.definition.Rule> koreRules = JavaConversions.setAsJavaSet(module.rules()).stream() .filter(r -> !r.att().contains(AUTOMATON)) .collect(Collectors.toList()); koreRules.forEach(r -> { if (r.att().contains(Att.topRule())) { reverseRuleTable.put(r.hashCode(), reverseRuleTable.size()); } }); koreRules.forEach(r -> { Rule convertedRule = transformer.convert(Optional.of(module), r); addRule(convertedRule); if (r.att().contains(Att.topRule())) { ruleTable.put(reverseRuleTable.get(r.hashCode()), convertedRule); } }); Optional<org.kframework.definition.Rule> koreAutomaton = JavaConversions.setAsJavaSet(module.localRules()).stream() .filter(r -> r.att().contains(AUTOMATON)) .collect(Collectors.collectingAndThen( Collectors.toList(), list -> list.size() == 1 ? Optional.of(list.get(0)) : Optional.empty() )); if (koreAutomaton.isPresent()) { automaton = transformer.convert(Optional.of(module), koreAutomaton.get()); } }
private int numLookups(Rule r) { class Holder { int i; } Holder h = new Holder(); new VisitK() { @Override public void apply(KApply k) { if (ConvertDataStructureToLookup.isLookupKLabel(k)) { h.i++; } super.apply(k); } }.apply(r.requires()); return h.i; }
pattern = new Rule(KORE.KVariable("Result"), BooleanUtils.TRUE, BooleanUtils.TRUE, KORE.Att()); parsedPattern = pattern;
stream(module.rules()).filter(r -> !r.att().contains(Attribute.MACRO_KEY) && !r.att().contains(Attribute.ALIAS_KEY)).forEach(r -> { K left = RewriteToTop.toLeft(r.body()); if (left instanceof KApply) { KApply kapp = (KApply) left; if (r.att().contains(Attribute.ANYWHERE_KEY)) { if (kapp.klabel().name().equals(KLabels.INJ)) { K k = kapp.items().get(0); visitor.apply(entry.getValue().body()); visitor.apply(entry.getValue().requires());
private List<KLabel> computeKLabelsForPredicate(Set<Rule> rules) { List<KLabel> labels = new ArrayList<>(); for (Rule r : rules) { K body = r.body(); K lhs = RewriteToTop.toLeft(body); K rhs = RewriteToTop.toRight(body); if (rhs.equals(KSequence(BooleanUtils.FALSE)) && r.att().contains("owise")) { continue;
private int sortRules(Rule r1, Rule r2) { return ComparisonChain.start() .compareTrueFirst(r1.att().contains("structural"), r2.att().contains("structural")) .compareFalseFirst(r1.att().contains("owise"), r2.att().contains("owise")) .compareFalseFirst(indexesPoorly(r1), indexesPoorly(r2)) .result(); }
private KApply getLookup(Rule r, int idx) { class Holder { int i = 0; KApply lookup; } Holder h = new Holder(); new VisitK() { @Override public void apply(KApply k) { if (h.i > idx) return; if (k.klabel().name().equals("#match") || k.klabel().name().equals("#setChoice") || k.klabel().name().equals("#mapChoice") || k.klabel().name().equals("#filterMapChoice")) { h.lookup = k; h.i++; } super.apply(k); } }.apply(r.requires()); return h.lookup; }
/** 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()); }