private static void flattenKList(
     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.KList) {
       org.kframework.kil.KList kList = (org.kframework.kil.KList) term;
       KILtoBackendJavaKILTransformer.flattenKList(flatList, kList.getContents());
     } else {
       flatList.add(term);
     }
   }
 }
  @Override
  public ASTNode transform(org.kframework.kil.KList node) throws TransformerException {
    List<org.kframework.kil.Term> list = new ArrayList<org.kframework.kil.Term>();
    KILtoBackendJavaKILTransformer.flattenKList(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.KLIST)) {
      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 KList(builder.build(), variable);
  }
  @Override
  public ASTNode visit(org.kframework.kil.KList node, Void _void) {
    List<org.kframework.kil.Term> list = new ArrayList<>();
    KILtoBackendJavaKILTransformer.flattenKList(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.KLIST)) {
      variable = (Variable) this.visitNode(list.remove(list.size() - 1));
    }

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

    return builder.build();
  }