private static void flattenKSequence(
     List<org.kframework.kil.Term> flatList, List<org.kframework.kil.Term> nestedList) {
   for (org.kframework.kil.Term term : nestedList) {
     if (term instanceof org.kframework.kil.KSequence) {
       org.kframework.kil.KSequence kSequence = (org.kframework.kil.KSequence) term;
       KILtoBackendJavaKILTransformer.flattenKSequence(flatList, kSequence.getContents());
     } else {
       flatList.add(term);
     }
   }
 }
Exemplo n.º 2
0
 @Override
 public Void visit(KSequence k , Void _) {
     if (k.isEmpty()) {
         builder.append(".K");
     } else {
         boolean first = true;
         for (Term t : k.getContents()) {
             if (!first) {
                 builder.append(" ~> ");
             }
             first = false;
             this.visitNode(t);
         }
     }
     return null;
 }
  @Override
  public ASTNode transform(org.kframework.kil.KSequence node) throws TransformerException {
    List<org.kframework.kil.Term> list = new ArrayList<org.kframework.kil.Term>();
    KILtoBackendJavaKILTransformer.flattenKSequence(list, node.getContents());

    Variable variable = null;
    if (!list.isEmpty()
        && list.get(list.size() - 1) instanceof org.kframework.kil.Variable
        && list.get(list.size() - 1).getSort().equals(org.kframework.kil.KSorts.K)) {
      variable = (Variable) list.remove(list.size() - 1).accept(this);
    }

    ImmutableList.Builder<Term> builder = new ImmutableList.Builder<Term>();
    for (org.kframework.kil.Term term : list) {
      builder.add((Term) term.accept(this));
    }

    return new KSequence(builder.build(), variable);
  }
  @Override
  public ASTNode visit(org.kframework.kil.KSequence node, Void _void) {
    List<org.kframework.kil.Term> list = new ArrayList<>();
    KILtoBackendJavaKILTransformer.flattenKSequence(list, node.getContents());

    Variable variable = null;
    if (!list.isEmpty()
        && list.get(list.size() - 1) instanceof org.kframework.kil.Variable
        && list.get(list.size() - 1).getSort().equals(org.kframework.kil.Sort.K)) {
      variable = (Variable) this.visitNode(list.remove(list.size() - 1));
    }

    KSequence.Builder builder = KSequence.builder();
    for (org.kframework.kil.Term term : list) {
      builder.concatenate((Term) this.visitNode(term));
    }
    if (variable != null) {
      builder.concatenate(variable);
    }

    return builder.build();
  }
Exemplo n.º 5
0
  @Override
  public ASTNode transform(Rule node) throws TransformerException {

    final boolean heating = node.containsAttribute(MetaK.Constants.heatingTag);
    final boolean cooling = node.containsAttribute(MetaK.Constants.coolingTag);
    if (!(heating || cooling)) return node;
    if (!(node.getBody() instanceof Rewrite)) {
      GlobalSettings.kem.register(
          new KException(
              KException.ExceptionType.ERROR,
              KException.KExceptionGroup.CRITICAL,
              "Heating/Cooling rules should have rewrite at the top.",
              getName(),
              node.getFilename(),
              node.getLocation()));
    }
    KSequence kSequence;
    Rewrite rewrite = (Rewrite) node.getBody();
    if (heating) {
      if (!(rewrite.getRight() instanceof KSequence)) {
        GlobalSettings.kem.register(
            new KException(
                KException.ExceptionType.ERROR,
                KException.KExceptionGroup.CRITICAL,
                "Heating rules should have a K sequence in the rhs.",
                getName(),
                node.getFilename(),
                node.getLocation()));
      }
      kSequence = (KSequence) rewrite.getRight();
    } else {
      if (!(rewrite.getLeft() instanceof KSequence)) {
        GlobalSettings.kem.register(
            new KException(
                KException.ExceptionType.ERROR,
                KException.KExceptionGroup.CRITICAL,
                "Cooling rules should have a K sequence in the lhs.",
                getName(),
                node.getFilename(),
                node.getLocation()));
      }
      kSequence = (KSequence) rewrite.getLeft();
    }
    List<Term> kSequenceContents = kSequence.getContents();
    if (kSequenceContents.size() != 2) {
      GlobalSettings.kem.register(
          new KException(
              KException.ExceptionType.ERROR,
              KException.KExceptionGroup.CRITICAL,
              "Heating/Cooling rules should have exactly 2 items in their K Sequence.",
              getName(),
              node.getFilename(),
              node.getLocation()));
    }
    final Term freezer = kSequenceContents.get(1);
    if (!(freezer instanceof Freezer)) {
      kSequenceContents = new ArrayList<Term>(kSequenceContents);
      kSequenceContents.set(1, new ContextsToHeating(context).freeze(freezer));
      kSequence = kSequence.shallowCopy();
      kSequence.setContents(kSequenceContents);
      rewrite = rewrite.shallowCopy();
      if (heating) {
        rewrite.replaceChildren(rewrite.getLeft(), kSequence, context);
      } else {
        rewrite.replaceChildren(kSequence, rewrite.getRight(), context);
      }
      node = node.shallowCopy();
      node.setBody(rewrite);
    }
    return node;
  }