@Override
  public ASTNode transform(org.kframework.kil.Definition node) {
    Definition definition = new Definition(context);
    this.definition = definition;

    Module singletonModule = node.getSingletonModule();

    for (org.kframework.kil.Rule rule : singletonModule.getRules()) {
      if (!rule.containsAttribute(SymbolicBackend.SYMBOLIC)
          || rule.containsAttribute(Attribute.PREDICATE.getKey())
          || rule.containsAttribute(Attribute.ANYWHERE.getKey())) {
        continue;
      }

      try {
        definition.addRule((Rule) rule.accept(this));
      } catch (TransformerException e) {
        System.err.println(rule);
        System.err.flush();
        e.printStackTrace();
      }
    }

    for (String kLabelName : singletonModule.getModuleKLabels()) {
      definition.addKLabel(KLabelConstant.of(kLabelName, context));
    }

    /* collect the productions which have the attributes strict and seqstrict */
    Set<Production> productions = singletonModule.getSyntaxByTag("strict", context);
    productions.addAll(singletonModule.getSyntaxByTag("seqstrict", context));
    for (Production production : productions) {
      definition.addFrozenKLabel(KLabelConstant.of(production.getKLabel(), context));
    }

    this.definition = null;
    return definition;
  }
Пример #2
0
  @Override
  public void buildIndex() {

    /* populate the table of rules rewriting the top configuration */
    List<Index> indices = new ArrayList<Index>();
    indices.add(TopIndex.TOP);
    indices.add(BottomIndex.BOTTOM);
    for (KLabelConstant kLabel : definition.kLabels()) {
      indices.add(new KLabelIndex(kLabel));
      indices.add(new FreezerIndex(kLabel, -1));
      if (!kLabel.productions().isEmpty()) {
        int maxArity = getMaxArityForProductions(kLabel.productions());
        for (int i = 0; i < maxArity; ++i) {
          indices.add(new FreezerIndex(kLabel, i));
        }
      }
    }
    // for (KLabelConstant frozenKLabel : definition.frozenKLabels()) {
    //    for (int i = 0; i < frozenKLabel.productions().get(0).getArity(); ++i) {
    //        indices.add(new FreezerIndex(frozenKLabel, i));
    //    }
    // }
    for (String sort : definition.builtinSorts()) {
      indices.add(new TokenIndex(sort));
    }

    /* Map each index to a list of rules unifiable with that index */
    /* Heating rules and regular rules have their first index checked */
    /* Cooling rules have their second index checked */
    ImmutableMap.Builder<Index, List<Rule>> mapBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Index, List<Rule>> heatingMapBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Index, List<Rule>> coolingMapBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Index, List<Rule>> simulationMapBuilder = ImmutableMap.builder();

    for (Index index : indices) {
      ImmutableList.Builder<Rule> listBuilder = ImmutableList.builder();
      ImmutableList.Builder<Rule> heatingListBuilder = ImmutableList.builder();
      ImmutableList.Builder<Rule> coolingListBuilder = ImmutableList.builder();
      ImmutableList.Builder<Rule> simulationListBuilder = ImmutableList.builder();

      for (Rule rule : definition.rules()) {
        if (rule.containsAttribute("heat")) {
          if (index.isUnifiable(rule.indexingPair().first)) {
            heatingListBuilder.add(rule);
          }
        } else if (rule.containsAttribute("cool")) {
          if (index.isUnifiable(rule.indexingPair().second)) {
            coolingListBuilder.add(rule);
          }
        } else if (rule.containsAttribute("alphaRule")) {
          if (index.isUnifiable(rule.indexingPair().first)) {
            simulationListBuilder.add(rule);
          }

        } else {
          if (index.isUnifiable(rule.indexingPair().first)) {
            listBuilder.add(rule);
          }
        }
      }
      ImmutableList<Rule> rules = listBuilder.build();
      if (!rules.isEmpty()) {
        mapBuilder.put(index, rules);
      }
      rules = heatingListBuilder.build();
      if (!rules.isEmpty()) {
        heatingMapBuilder.put(index, rules);
      }
      rules = coolingListBuilder.build();
      if (!rules.isEmpty()) {
        coolingMapBuilder.put(index, rules);
      }
      rules = simulationListBuilder.build();
      if (!rules.isEmpty()) {
        simulationMapBuilder.put(index, rules);
      }
    }
    heatingRuleTable = heatingMapBuilder.build();
    coolingRuleTable = coolingMapBuilder.build();
    ruleTable = mapBuilder.build();
    simulationRuleTable = simulationMapBuilder.build();

    ImmutableList.Builder<Rule> listBuilder = ImmutableList.builder();
    for (Rule rule : definition.rules()) {
      if (!rule.containsKCell()) {
        listBuilder.add(rule);
      }
    }
    unindexedRules = listBuilder.build();
  }
 @Override
 public ASTNode transform(org.kframework.kil.KLabelConstant node) throws TransformerException {
   return KLabelConstant.of(node.getLabel(), this.context);
 }
Пример #4
0
  @Override
  public ASTNode transform(KItem kItem) {
    if (!(kItem.kLabel() instanceof KLabelConstant)) {
      throw new UnsupportedOperationException();
    }
    KLabelConstant kLabel = (KLabelConstant) kItem.kLabel();

    if (!(kItem.kList() instanceof KList)) {
      throw new UnsupportedOperationException();
    }
    KList kList = (KList) kItem.kList();

    if (kList.hasFrame()) {
      throw new UnsupportedOperationException();
    }

    String label = kLabel.smtlib();
    if (label == null) {
      throw new UnsupportedOperationException("missing SMTLib translation for " + kLabel);
    }

    if (label.startsWith("(")) {
      // smtlib expression instead of operator
      String expression = label;
      for (int i = 0; i < kList.getContents().size(); i++) {
        expression =
            expression.replaceAll(
                "\\#" + (i + 1) + "(?![0-9])",
                ((SMTLibTerm) kList.get(i).accept(this)).expression());
      }
      return new SMTLibTerm(expression);
    }

    List<Term> arguments;
    switch (label) {
      case "exists":
        Variable variable = (Variable) kList.get(0);
        label = "exists ((" + variable.name() + " " + variable.sort() + ")) ";
        arguments = ImmutableList.of(kList.get(1));
        break;
      case "extract":
        int beginIndex = ((IntToken) kList.get(1)).intValue();
        int endIndex = ((IntToken) kList.get(2)).intValue() - 1;
        label = "(_ extract " + endIndex + " " + beginIndex + ")";
        arguments = ImmutableList.of(kList.get(0));
        break;
      default:
        arguments = kList.getContents();
    }

    if (!arguments.isEmpty()) {
      StringBuilder sb = new StringBuilder();
      sb.append("(");
      sb.append(label);
      for (Term argument : arguments) {
        sb.append(" ");
        sb.append(((SMTLibTerm) argument.accept(this)).expression());
      }
      sb.append(")");
      return new SMTLibTerm(sb.toString());
    } else {
      return new SMTLibTerm(label);
    }
  }