Exemplo n.º 1
0
    @java.lang.Override
    public ASTNode visit(KApp kapp, Void _)  {
        if (kapp.getLabel() instanceof Token) {
            Token node = (Token) kapp.getLabel();

            /* this code needs to be refactored; this transformer is backend dependent
            so whenever somebody refactors the backends should split this transformer
            for each backend.
             */
            if (kompileOptions.experimental.legacyKast) {
                if (context.variableTokenSorts.contains(node.tokenSort())) {
                    String sort = KSorts.K;
                    String name = node.value();
                    int index = node.value().lastIndexOf(":");
                    if (index > 0) {
                        sort = node.value().substring(index + 1);
                        name = node.value().substring(0, index);
                    }

                    if (Sort.of("#" + sort).isDataSort()) {
                        return KApp.of(KLabelConstant.of(AddSymbolicK.symbolicConstructor(sort)), Token.kAppOf(Sort.BUILTIN_ID, name));
                    } else {
                        return KApp.of(KLabelConstant.of(AddSymbolicK.symbolicConstructor(KSorts.K)), Token.kAppOf(Sort.BUILTIN_ID, node.value()));
                    }
                }
            }

            if (context.variableTokenSorts.contains(node.tokenSort()))
                return new org.kframework.kil.Variable(node.value(), node.tokenSort());
        }
        return super.visit(kapp, _);    //To change body of overridden methods use File | Settings | File Templates.
    }
Exemplo n.º 2
0
 public static Term getTerm(Production prod, org.kframework.kil.loader.Context context) {
   if (prod.isSubsort()) {
     final Variable freshVar = Variable.getFreshVar(prod.getItems().get(0).toString());
     if (prod.containsAttribute("klabel")) {
       return KApp.of(KLabelConstant.of(prod.getKLabel(), context), freshVar);
     }
     return freshVar;
   }
   if (prod.isConstant()) {
     String terminal = ((Terminal) prod.getItems().get(0)).getTerminal();
     if (prod.getSort().equals(KSorts.KLABEL)) {
       return KLabelConstant.of(terminal, context);
     } else if (prod.getSort().equals(BoolBuiltin.SORT_NAME)) {
       return BoolBuiltin.kAppOf(terminal);
     } else if (prod.getSort().equals(IntBuiltin.SORT_NAME)) {
       return IntBuiltin.kAppOf(terminal);
     } else if (prod.getSort().equals(StringBuiltin.SORT_NAME)) {
       return StringBuiltin.kAppOf(terminal);
     } else {
       return GenericToken.kAppOf(prod.getSort(), terminal);
     }
   }
   if (prod.isLexical()) {
     return KApp.of(
         KLabelConstant.of("#token", context),
         StringBuiltin.kAppOf(prod.getSort()),
         Variable.getFreshVar("String"));
   }
   TermCons t = new TermCons(prod.getSort(), prod.getCons(), context);
   if (prod.isListDecl()) {
     t.getContents().add(Variable.getFreshVar(((UserList) prod.getItems().get(0)).getSort()));
     t.getContents().add(Variable.getFreshVar(prod.getSort()));
     return t;
   }
   for (ProductionItem item : prod.getItems()) {
     if (item instanceof Sort) {
       t.getContents().add(Variable.getFreshVar(((Sort) item).getName()));
     }
   }
   return t;
 }
Exemplo n.º 3
0
  public final Term makeSymbolicTerm(String sort, Term term) {
    assert allowSymbolic(sort);

    String ctor = symbolicConstructor(sort);
    Term symTerm;
    if (!allowKSymbolic(sort)) {
      symTerm = new TermCons(sort, ctor, context);
      ((TermCons) symTerm).getContents().add(term);
    } else {
      symTerm = KApp.of(KLabelConstant.of(ctor, context), term);
    }

    return symTerm;
  }
Exemplo n.º 4
0
 public Term freshSymSortN(String sort, int n) {
   return KApp.of(
       KLabelConstant.of("'#freshSymSortN", context),
       StringBuiltin.kAppOf(sort),
       IntBuiltin.kAppOf(n));
 }
Exemplo n.º 5
0
 public static Term incrementCondition(Term condition, Term kresultCnd) {
   if (condition == null) {
     return kresultCnd;
   }
   return KApp.of(KLabelConstant.ANDBOOL_KLABEL, condition, kresultCnd);
 }
Exemplo n.º 6
0
Arquivo: KApp.java Projeto: kszr/k
 /**
  * Constructs the application of the given KLabel to a KList with the given elements.
  *
  * @param label the string of a KLabelConstant which is applied to a KList with the given
  *     elements.
  * @param elements the elements of the KList.
  * @return a {@link KApp} which represents the application of the given KLabel to a KList with the
  *     given elements.
  */
 public static KApp of(String label, Term... elements) {
   return KApp.of(KLabelConstant.of(label), elements);
 }
Exemplo n.º 7
0
/** Class representing a builtin boolean token. */
public class BoolBuiltin extends Token {

  public static final String TRUE_STRING = "true";
  public static final String FALSE_STRING = "false";

  /** #token("#Bool", "true") */
  public static final BoolBuiltin TRUE_TOKEN = new BoolBuiltin(Boolean.TRUE);
  /** #token("#Bool", "false") */
  public static final BoolBuiltin FALSE_TOKEN = new BoolBuiltin(Boolean.FALSE);

  /** #token("#Bool", "true")(.KList) */
  public static final KApp TRUE = KApp.of(BoolBuiltin.TRUE_TOKEN);
  /** #token("#Bool", "false")(.KList) */
  public static final KApp FALSE = KApp.of(BoolBuiltin.FALSE_TOKEN);

  /**
   * Returns a {@link BoolBuiltin} representing a {@link boolean} value with the given {@link
   * String} representation.
   *
   * @param value
   * @return
   */
  public static BoolBuiltin of(String value) {
    checkValue(value);

    if (value.equals(BoolBuiltin.TRUE_STRING)) {
      return BoolBuiltin.TRUE_TOKEN;
    } else {
      return BoolBuiltin.FALSE_TOKEN;
    }
  }

  /**
   * Returns a {@link KApp} representing a {@link BoolBuiltin} with the given value applied to an
   * empty {@link KList}.
   *
   * @param value
   * @return
   */
  public static KApp kAppOf(String value) {
    checkValue(value);

    if (value.equals(BoolBuiltin.TRUE_STRING)) {
      return BoolBuiltin.TRUE;
    } else {
      return BoolBuiltin.FALSE;
    }
  }

  private static void checkValue(String value) {
    assert value.equals(BoolBuiltin.TRUE_STRING) || value.equals(BoolBuiltin.FALSE_STRING)
        : "unexpected value "
            + value
            + " for a builtin bool token; expected one of "
            + BoolBuiltin.TRUE_STRING
            + " or "
            + BoolBuiltin.FALSE_STRING;
  }

  private final Boolean value;

  private BoolBuiltin(Boolean value) {
    this.value = value;
  }

  protected BoolBuiltin(Element element) {
    super(element);
    String s = element.getAttribute(Constants.VALUE_value_ATTR);

    checkValue(s);

    value = Boolean.valueOf(s);
  }

  /** Returns a {@link Boolean} representing the (interpreted) value of the boolean token. */
  public Boolean booleanValue() {
    return value;
  }

  /**
   * Returns a {@link String} representing the sort name of a boolean token.
   *
   * @return
   */
  @Override
  public Sort tokenSort() {
    return Sort.BUILTIN_BOOL;
  }

  /**
   * Returns a {@link String} representing the (uninterpreted) value of the boolean token.
   *
   * @return
   */
  @Override
  public String value() {
    return value.toString();
  }

  @Override
  protected <P, R, E extends Throwable> R accept(Visitor<P, R, E> visitor, P p) throws E {
    return visitor.complete(this, visitor.visit(this, p));
  }
}
Exemplo n.º 8
0
  public static Term makeConfiguration(
      Term kast, String stdin, RunProcess rp, boolean hasTerm, Context context)
      throws TransformerException, IOException {

    if (hasTerm) {
      if (kast == null) {
        return rp.runParserOrDie(K.getProgramParser(), K.term, false, null, context);
      } else {
        Error.report("You cannot specify both the term and the configuration\nvariables.");
      }
    }

    HashMap<String, Term> output = new HashMap<String, Term>();
    boolean hasPGM = false;
    Enumeration<Object> en = K.configuration_variables.keys();
    while (en.hasMoreElements()) {
      String name = (String) en.nextElement();
      String value = K.configuration_variables.getProperty(name);
      String parser = K.cfg_parsers.getProperty(name);
      // TODO: get sort from configuration term in definition and pass it
      // here
      Term parsed = null;
      if (parser == null) {
        parser = "kast -groundParser -e";
      }
      parsed = rp.runParserOrDie(parser, value, false, null, context);
      output.put("$" + name, parsed);
      hasPGM = hasPGM || name.equals("$PGM");
    }
    if (!hasPGM && kast != null) {
      output.put("$PGM", kast);
    }
    if (!K.io && stdin == null) {
      stdin = "";
    }
    if (stdin != null) {
      KApp noIO = KApp.of(KLabelConstant.of("'#noIO", context));
      if (K.backend.equals("java")) {
        DataStructureSort myList =
            context.dataStructureListSortOf(DataStructureSort.DEFAULT_LIST_SORT);
        if (myList != null) {
          output.put("$noIO", DataStructureBuiltin.element(myList, noIO));
        }
      } else {
        output.put("$noIO", new ListItem(noIO));
      }
      output.put("$stdin", StringBuiltin.kAppOf(stdin + "\n"));
    } else {
      if (K.backend.equals("java")) {
        DataStructureSort myList =
            context.dataStructureListSortOf(DataStructureSort.DEFAULT_LIST_SORT);
        if (myList != null) {
          output.put("$noIO", DataStructureBuiltin.empty(myList));
        }
      } else {
        output.put("$noIO", org.kframework.kil.List.EMPTY);
      }
      output.put("$stdin", StringBuiltin.EMPTY);
    }

    if (GlobalSettings.verbose) sw.printIntermediate("Make configuration");

    return plug(output, context);
  }