Exemple #1
0
  public static Production makeFunction(
      String funSort, String funName, String argSort, org.kframework.kil.loader.Context context) {
    List<ProductionItem> prodItems = new ArrayList<ProductionItem>();
    prodItems.add(new Terminal(funName));
    prodItems.add(new Terminal("("));
    prodItems.add(new Sort(argSort));
    prodItems.add(new Terminal(")"));

    Production funProd = new Production(new Sort(funSort), prodItems);
    funProd.addAttribute(new Attribute("prefixlabel", funName));
    if (MetaK.isComputationSort(funSort)) {
      funProd.addAttribute(new Attribute("klabel", funName));
      String consAttr = funSort + "1" + funName + "Syn";
      funProd.addAttribute(new Attribute("cons", consAttr));
      context.conses.put(consAttr, funProd);
      context.putLabel(funProd, consAttr);
    }

    return funProd;
  }
Exemple #2
0
  public static StringBuilder getSdfForDefinition(Definition def, Context context) {

    StringBuilder sdf = new StringBuilder("module Integration\n\n");
    sdf.append("imports Common\n");
    sdf.append("imports KTechnique\n");
    sdf.append("imports KBuiltinsBasic\n\n");
    sdf.append("exports\n\n");
    sdf.append("context-free syntax\n");

    DefinitionSDFVisitor psdfv = new DefinitionSDFVisitor(true, context);
    CollectTerminalsVisitor terminals = new CollectTerminalsVisitor();
    psdfv.visitNode(def);
    terminals.visitNode(def);

    for (Production p1 : psdfv.listProds)
      for (Production p2 : psdfv.listProds)
        if (p1 != p2) {
          Sort srt1 = ((UserList) p1.getItems().get(0)).getSort();
          Sort srt2 = ((UserList) p2.getItems().get(0)).getSort();
          if (psdfv.subsorts.contains(new Subsort(srt1, srt2)))
            psdfv.subsorts.add(new Subsort(p1.getSort(), p2.getSort()));
        }

    sdf.append(psdfv.sdf);

    sdf.append("%% subsorts 1\n");
    sdf.append("context-free priorities\n{\n");
    // 1
    // print Sort -> K > A -> B > K -> Sort
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append("    " + StringUtil.escapeSort(s) + " -> K");
        // sdf.append(" {cons(\"K12" + StringUtil.escapeSort(s) + "\")}");
        sdf.append("\n");
      }
    }
    sdf.append("} .> {\n");
    for (Subsort subs : psdfv.subsorts) {
      Sort s1 = subs.getSmallSort();
      Sort s2 = subs.getBigSort();
      if (!s1.isBaseSort() && !s2.isBaseSort()) {
        sdf.append("    " + StringUtil.escapeSort(s1) + " -> " + StringUtil.escapeSort(s2));
        // sdf.append(" {cons(\"" + StringUtil.escapeSort(s2) + "12" + StringUtil.escapeSort(s1) +
        // "\")}");
        sdf.append("\n");
      }
    }
    sdf.append("} .> {\n");
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append("    K -> " + StringUtil.escapeSort(s));
        // sdf.append(" {cons(\"" + StringUtil.escapeSort(s) + "12K\")}");
        sdf.append("\n");
      }
    }
    sdf.append("}\n\n");

    sdf.append("%% subsorts 1a\n");
    sdf.append("context-free priorities\n{\n");
    // 1
    // print Sort -> K > A -> B > K -> Sort
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append("    " + StringUtil.escapeSort(s) + " -> K");
        // sdf.append(" {cons(\"K12" + StringUtil.escapeSort(s) + "\")}");
        sdf.append("\n");
      }
    }
    sdf.append("} .> {\n");
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append("    K -> " + StringUtil.escapeSort(s));
        // sdf.append(" {cons(\"" + StringUtil.escapeSort(s) + "12K\")}");
        sdf.append("\n");
      }
    }
    sdf.append("}\n\n");

    sdf.append("%% subsorts 2\n");
    // print K -> Sort > Sort -> K
    sdf.append("context-free priorities\n{\n");
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append("    K -> " + StringUtil.escapeSort(s));
        // sdf.append(" {cons(\"" + StringUtil.escapeSort(s) + "12K\")}");
        sdf.append("\n");
      }
    }
    sdf.append("} .> {\n");
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append("    " + StringUtil.escapeSort(s) + " -> K");
        // sdf.append(" {cons(\"K12" + StringUtil.escapeSort(s) + "\")}");
        sdf.append("\n");
      }
    }
    sdf.append("}\n");

    sdf.append("context-free syntax\n");

    for (Production p : psdfv.outsides) {
      if (p.isListDecl()) {
        UserList si = (UserList) p.getItems().get(0);
        sdf.append(
            "    "
                + StringUtil.escapeSort(si.getSort())
                + " "
                + StringUtil.enquoteCString(si.getSeparator())
                + " "
                + StringUtil.escapeSort(p.getSort())
                + " -> "
                + StringUtil.escapeSort(p.getSort()));
        sdf.append(" {cons(\"" + context.getConses().inverse().get(p) + "\")}\n");
        sdf.append("    \"." + p.getSort() + "\" -> " + StringUtil.escapeSort(p.getSort()));
        sdf.append(" {cons(\"" + StringUtil.escapeSort(p.getSort()) + "1Empty\")}\n");
      } else if (p.containsAttribute("bracket")) {
        // don't add bracket attributes added by the user
      } else {
        sdf.append("    ");
        List<ProductionItem> items = p.getItems();
        for (int i = 0; i < items.size(); i++) {
          ProductionItem itm = items.get(i);
          if (itm instanceof Terminal) {
            Terminal t = (Terminal) itm;
            sdf.append(t.toString() + " ");
          } else if (itm instanceof NonTerminal) {
            NonTerminal srt = (NonTerminal) itm;
            // if we are on the first or last place and this sort is not a list, just print the sort
            if (i == 0 || i == items.size() - 1) {
              sdf.append(StringUtil.escapeSort(srt) + " ");
            } else {
              // if this sort should be inserted to avoid the priority filter, then add it to the
              // list
              psdfv.insertSorts.add(srt);
              String tempstr = srt.toString();
              if (tempstr.endsWith("CellSort") || tempstr.endsWith("CellFragment")) tempstr = "Bag";
              sdf.append("InsertDz" + StringUtil.escapeSort(tempstr) + " ");
            }
          }
        }
        sdf.append("-> " + StringUtil.escapeSort(p.getSort()));
        sdf.append(SDFHelper.getSDFAttributes(p, context.getConses()) + "\n");
      }
    }
    for (NonTerminal ss : psdfv.insertSorts)
      sdf.append(
          "    " + StringUtil.escapeSort(ss) + " -> InsertDz" + StringUtil.escapeSort(ss) + "\n");

    sdf.append("\n");
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append(
            "     K CastTypeDz \""
                + s.toString()
                + "\"    -> VariableDz    {cons(\""
                + StringUtil.escapeSort(s)
                + "1Cast\")}\n");
        sdf.append(
            "     K CastTypeDz \""
                + s.toString()
                + "{\" TagListDz \"}\"    -> VariableDz    {cons(\""
                + StringUtil.escapeSort(s)
                + "1CastAttr\")}\n");
      }
    }
    sdf.append("     K CastTypeDz \"K\"        -> VariableDz    {cons(\"K1Cast\")}\n");
    sdf.append("     K CastTypeDz \"KItem\"    -> VariableDz    {cons(\"KItem1Cast\")}\n");
    sdf.append(
        "     K CastTypeDz \"K{\" TagListDz \"}\"        -> VariableDz    {cons(\"K1CastAttr\")}\n");
    sdf.append(
        "     K CastTypeDz \"KItem{\" TagListDz \"}\"    -> VariableDz    {cons(\"KItem1CastAttr\")}\n");
    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append(
            "     " + StringUtil.escapeSort(s) + "DzVar   -> " + StringUtil.escapeSort(s) + "\n");
      }
    }

    sdf.append("\n");
    sdf.append("    VariableDz -> K\n");

    sdf.append("\n\n");
    for (Sort sort : psdfv.constantSorts) {
      String s = StringUtil.escapeSort(sort);
      sdf.append("    Dz" + s + "        -> " + s + "    {cons(\"" + s + "1Const\")}\n");
    }

    sdf.append("\n");
    // sdf.append("    DzDzINT        -> DzDzInt\n");
    // sdf.append("    DzDzID        -> DzDzId\n");
    // sdf.append("    DzDzSTRING    -> DzDzString\n");
    // sdf.append("    DzDzFLOAT    -> DzDzFloat\n");

    sdf.append("\n");

    sdf.append("lexical syntax\n");
    for (Production p : psdfv.constants) {
      sdf.append(
          "    " + p.getItems().get(0) + " -> Dz" + StringUtil.escapeSort(p.getSort()) + "\n");
    }

    sdf.append("\n\n%% sort predicates\n");
    // print is<Sort> predicates (actually KLabel)
    for (NonTerminal sort : psdfv.userSorts) {
      if (!sort.getSort().isKSort()) {
        sdf.append(
            "    \"" + AddPredicates.syntaxPredicate(sort.getSort()) + "\"      -> DzKLabel\n");
      }
      if (AddSymbolicK.allowKSymbolic(sort.getSort())) {
        sdf.append(
            "    \"" + AddPredicates.symbolicPredicate(sort.getSort()) + "\"      -> DzKLabel\n");
        sdf.append(
            "    \"" + AddSymbolicK.symbolicConstructor(sort.getSort()) + "\"      -> DzKLabel\n");
      }
    }

    sdf.append("\n\n");

    sdf.append("\n%% terminals reject\n");
    sdf.append(SDFHelper.getFollowRestrictionsForTerminals(terminals.terminals));

    sdf.append("context-free restrictions\n");

    for (NonTerminal s : psdfv.userSorts) {
      if (!s.getSort().isBaseSort()) {
        sdf.append("    " + StringUtil.escapeSort(s) + "DzVar -/- [a-zA-Z0-9\\{]\n");
      }
    }
    sdf.append("    VariableDz -/- [a-zA-Z0-9\\{]\n");

    sdf.append("lexical restrictions\n");
    sdf.append("%% some restrictions to ensure greedy matching for user defined constants\n");
    // sdf.append("    DzDzInt -/- [0-9]\n");
    sdf.append("    \"is\" -/- [\\#A-Z]\n");
    sdf.append("\n");

    // lexical rules
    sdf.append("lexical syntax\n");
    java.util.Set<Sort> lexerSorts = new HashSet<>();
    for (Production p : psdfv.lexical) {
      Lexical l = (Lexical) p.getItems().get(0);
      lexerSorts.add(p.getSort());
      sdf.append(
          "    " + l.getLexicalRule() + " -> " + StringUtil.escapeSort(p.getSort()) + "Dz\n");
      if (l.getFollow() != null && !l.getFollow().equals("")) {
        psdfv.restrictions.add(new Restrictions(new NonTerminal(p.getSort()), null, l.getFollow()));
      }

      if (!p.containsAttribute("noAutoReject")) {
        // reject all terminals that match the regular expression of the lexical production
        if (p.containsAttribute("regex")) {
          Pattern pat = Pattern.compile(p.getAttribute("regex"));
          for (Terminal t : terminals.terminals) {
            Matcher m = pat.matcher(t.getTerminal());
            if (m.matches())
              sdf.append(
                  "    "
                      + t.toString()
                      + " -> "
                      + StringUtil.escapeSort(p.getSort())
                      + "Dz {reject}\n");
          }
        } else {
          // if there is no regex attribute, then do it the old fashioned way, but way more
          // inefficient
          // add rejects for all possible combinations
          for (Terminal t : terminals.terminals) {
            sdf.append(
                "    "
                    + t.toString()
                    + " -> "
                    + StringUtil.escapeSort(p.getSort())
                    + "Dz {reject}\n");
          }
        }
      }
    }

    // adding cons over lexical rules
    sdf.append("context-free syntax\n");
    for (Sort s : lexerSorts) {
      sdf.append(
          "    "
              + StringUtil.escapeSort(s)
              + "Dz -> "
              + StringUtil.escapeSort(s)
              + " {cons(\""
              + StringUtil.escapeSort(s)
              + "1Const\")}\n");
    }
    sdf.append("\n\n");

    // follow restrictions
    sdf.append("context-free restrictions\n");
    for (Restrictions r : psdfv.restrictions) {
      if (r.getTerminal() != null && !r.getTerminal().getTerminal().equals(""))
        sdf.append(
            "    "
                + StringUtil.enquoteCString(r.getTerminal().getTerminal())
                + " -/- "
                + r.getPattern()
                + "\n");
      else
        sdf.append(
            "    " + StringUtil.escapeSort(r.getNonTerminal()) + " -/- " + r.getPattern() + "\n");
    }

    return sdf;
  }
Exemple #3
0
  /** @param cmds represents the arguments/options given to krun command.. */
  public static void execute_Krun(String cmds[]) {
    Context context = new Context();
    K.init(context);

    CommandlineOptions cmd_options = new CommandlineOptions();
    CommandLine cmd = cmd_options.parse(cmds);
    if (cmd == null) {
      printKRunUsageS(cmd_options);
      /* printKRunUsageE(cmd_options); */
      /* TODO: Switch to this when the user has tried to use an experimental option. */
      System.exit(1);
    }

    if (!cmd.hasOption("debug-info")) {
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread() {
                public void run() {
                  try {
                    deleteDirectory(new File(K.krunTempDir));
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                }
              });
    }

    // set verbose
    if (cmd.hasOption("verbose")) {
      GlobalSettings.verbose = true;
    }

    // set fast-kast
    if (cmd.hasOption("fast-kast")) {
      GlobalSettings.fastKast = !GlobalSettings.fastKast;
    }

    if (GlobalSettings.verbose) sw.printIntermediate("Deleting temporary krun directory");

    try {

      // Parse the program arguments

      if (cmd.hasOption("search")
          || cmd.hasOption("search-final")
          || cmd.hasOption("search-all")
          || cmd.hasOption("search-one-step")
          || cmd.hasOption("search-one-or-more-steps")) {
        K.maude_cmd = "search";
        K.io = false;
        K.do_search = true;
        if (cmd.hasOption("search") && cmd.hasOption("depth")) {
          K.searchType = SearchType.STAR;
        }
      }
      if (cmd.hasOption("search-final")) {
        K.searchType = SearchType.FINAL;
      }
      if (cmd.hasOption("search-all")) {
        K.searchType = SearchType.STAR;
      }
      if (cmd.hasOption("search-one-step")) {
        K.searchType = SearchType.ONE;
      }
      if (cmd.hasOption("search-one-or-more-steps")) {
        K.searchType = SearchType.PLUS;
      }
      if (cmd.hasOption("help")) {
        K.help = true;
      }
      if (cmd.hasOption("help-experimental")) {
        K.helpExperimental = true;
      }
      if (cmd.hasOption("version")) {
        K.version = true;
      }
      // CLEANUP_OPTIONS: The option --directory was added, replacing --k-definition and
      // --compiled-def.
      if (cmd.hasOption("directory")) {
        K.directory = new File(cmd.getOptionValue("directory")).getCanonicalPath();
        org.kframework.utils.Error.checkIfInputDirectory(K.directory);
      }
      if (cmd.hasOption("main-module")) {
        K.main_module = cmd.getOptionValue("main-module");
      }
      if (cmd.hasOption("syntax-module")) {
        K.syntax_module = cmd.getOptionValue("syntax-module");
      }
      if (cmd.hasOption("parser")) {
        K.customParser = cmd.getOptionValue("parser");
      }
      if (cmd.hasOption("term")) {
        K.term = cmd.getOptionValue("term");
      }
      if (cmd.hasOption("io")) {
        String v = cmd.getOptionValue("io");
        if (v.equals("on")) K.io = true;
        else if (v.equals("off")) K.io = false;
        else Error.report("Unrecognized option: --io " + v + "\nUsage: krun --io [on|off]");
      }
      if (cmd.hasOption("statistics")) {
        String v = cmd.getOptionValue("statistics");
        if (v.equals("on")) K.statistics = true;
        else if (v.equals("off")) K.statistics = false;
        else
          Error.report(
              "Unrecognized option: --statistics " + v + "\nUsage: krun --statistics [on|off]");
      }
      if (cmd.hasOption("color")) {
        String v = cmd.getOptionValue("color");
        if (v.equals("on")) K.color = ColorSetting.ON;
        else if (v.equals("off")) K.color = ColorSetting.OFF;
        else if (v.equals("extended")) K.color = ColorSetting.EXTENDED;
        else
          Error.report(
              "Unrecognized option: --color " + v + "\nUsage: krun --color [on|off|extended]");
      }
      if (cmd.hasOption("terminal-color")) {
        String v = cmd.getOptionValue("terminal-color");
        Color terminalColor = ColorUtil.getColorByName(v);
        if (terminalColor != null) {
          K.terminalColor = terminalColor;
        } else {
          Error.report("Invalid terminal color: " + v);
        }
      }

      if (cmd.hasOption("parens")) {
        String v = cmd.getOptionValue("parens");
        if (v.equals("greedy")) {
          K.parens = true;
        } else if (v.equals("smart")) {
          K.parens = false;
        } else {
          Error.report(
              "Unrecognized option: --parens " + v + "\nUsage: krun --parens [greedy|smart]");
        }
      }

      // testcase generation
      if (cmd.hasOption("generate-tests")) {
        K.do_testgen = true;
        K.io = false;
        K.do_search = true;
      }
      if (cmd.hasOption("maude-cmd")) {
        K.maude_cmd = cmd.getOptionValue("maude-cmd");
      }
      /*
       * if (cmd.hasOption("xsearch-pattern")) { K.maude_cmd = "search";
       * K.do_search = true; K.xsearch_pattern =
       * cmd.getOptionValue("xsearch-pattern"); //
       * System.out.println("xsearch-pattern:" + K.xsearch_pattern); }
       */
      if (cmd.hasOption("pattern")) {
        K.pattern = cmd.getOptionValue("pattern");
      }
      if (cmd.hasOption("bound")) {
        K.bound = cmd.getOptionValue("bound");
      }
      if (cmd.hasOption("depth")) {
        K.depth = cmd.getOptionValue("depth");
      }
      if (cmd.hasOption("graph")) {
        K.showSearchGraph = true;
      }
      if (cmd.hasOption("output-mode")) {
        K.output_mode = cmd.getOptionValue("output-mode");
      }
      if (cmd.hasOption("log-io")) {
        String v = cmd.getOptionValue("log-io");
        if (v.equals("on")) K.log_io = true;
        else if (v.equals("off")) K.log_io = false;
        else Error.report("Unrecognized option: --log-io " + v + "\nUsage: krun --log-io [on|off]");
      }
      if (cmd.hasOption("debug")) {
        K.debug = true;
      }
      if (cmd.hasOption("debug-gui")) {
        K.guidebug = true;
      }
      if (cmd.hasOption("trace")) {
        K.trace = true;
      }
      if (cmd.hasOption("profile")) {
        K.profile = true;
      }
      if (cmd.hasOption("ltlmc")) {
        K.model_checking = cmd.getOptionValue("ltlmc");
      }
      if (cmd.hasOption("prove")) {
        K.prove = cmd.getOptionValue("prove");
      }
      if (cmd.hasOption("smt")) {
        K.smt = cmd.getOptionValue("smt");
      }
      if (cmd.hasOption("output")) {
        if (!cmd.hasOption("color")) {
          K.color = ColorSetting.OFF;
        }
        K.output = new File(cmd.getOptionValue("output")).getCanonicalPath();
      }
      if (cmd.hasOption("c")) {

        if (K.term != null) {
          Error.report("You cannot specify both the term and the configuration\nvariables.");
        }

        K.configuration_variables = cmd.getOptionProperties("c");
        String parser = null;
        for (Option opt : cmd.getOptions()) {
          if (opt.equals(cmd_options.getOptions().getOption("c")) && parser != null) {
            K.cfg_parsers.setProperty(opt.getValue(0), parser);
          }
          if (opt.equals(cmd_options.getOptions().getOption("cfg-parser"))) {
            parser = opt.getValue();
          }
        }
      }
      if (cmd.hasOption("backend")) {
        K.backend = cmd.getOptionValue("backend");
      }
      // printing the output according to the given options
      if (K.help) {
        printKRunUsageS(cmd_options);
        System.exit(0);
      }
      if (K.helpExperimental) {
        printKRunUsageE(cmd_options);
        System.exit(0);
      }
      if (K.version) {
        String msg = FileUtil.getFileContent(KPaths.getKBase(false) + KPaths.VERSION_FILE);
        System.out.println(msg);
        System.exit(0);
      }

      String[] remainingArguments = null;
      if (cmd_options.getCommandLine().getOptions().length > 0) {
        remainingArguments = cmd.getArgs();
      } else {
        remainingArguments = cmds;
      }
      String programArg = new String();
      if (remainingArguments.length > 0) {
        programArg = remainingArguments[0];
        K.pgm = programArg;
      }
      String lang = null;
      if (K.pgm != null) {
        File pgmFile = new File(K.pgm);
        if (pgmFile.exists()) {
          lang = FileUtil.getExtension(K.pgm, ".", K.fileSeparator);
        }
      }

      if (GlobalSettings.verbose) sw.printIntermediate("Parsing command line options");

      /* CLEANUP_OPTIONS */
      if (!cmd.hasOption("directory")) {
        K.directory = new File(K.userdir).getCanonicalPath();
      }
      {
        // search for the definition
        File[] dirs =
            new File(K.directory)
                .listFiles(
                    new FilenameFilter() {
                      @Override
                      public boolean accept(File current, String name) {
                        return new File(current, name).isDirectory();
                      }
                    });

        K.compiled_def = null;
        for (int i = 0; i < dirs.length; i++) {
          if (dirs[i].getAbsolutePath().endsWith("-kompiled")) {
            if (K.compiled_def != null) {
              String msg = "Multiple compiled definitions found.";
              GlobalSettings.kem.register(
                  new KException(
                      ExceptionType.ERROR,
                      KExceptionGroup.CRITICAL,
                      msg,
                      "command line",
                      new File(".").getAbsolutePath()));
            } else {
              K.compiled_def = dirs[i].getAbsolutePath();
            }
          }
        }

        if (K.compiled_def == null) {
          String msg = "Could not find a compiled definition.";
          GlobalSettings.kem.register(
              new KException(
                  ExceptionType.ERROR,
                  KExceptionGroup.CRITICAL,
                  msg,
                  "command line",
                  new File(".").getAbsolutePath()));
        }
      }

      if (K.compiled_def == null) {
        Error.report(
            "Could not find a compiled K definition. Please ensure that\neither a compiled K definition exists in the current directory with its default\nname, or that --directory has been specified.");
      }
      File compiledFile = new File(K.compiled_def);
      if (!compiledFile.exists()) {
        Error.report(
            "\nCould not find compiled definition: "
                + K.compiled_def
                + "\nPlease compile the definition by using `kompile'.");
      }

      context.dotk = new File(new File(K.compiled_def).getParent() + File.separator + ".k");
      if (!context.dotk.exists()) {
        context.dotk.mkdirs();
      }
      context.kompiled = new File(K.compiled_def);
      K.kdir = context.dotk.getCanonicalPath();
      K.setKDir();
      /*
       * System.out.println("K.k_definition=" + K.k_definition);
       * System.out.println("K.syntax_module=" + K.syntax_module);
       * System.out.println("K.main_module=" + K.main_module);
       * System.out.println("K.compiled_def=" + K.compiled_def);
       */

      if (GlobalSettings.verbose) sw.printIntermediate("Checking compiled definition");

      // in KAST variable we obtain the output from running kast process
      // on a program defined in K
      Term KAST = null;
      RunProcess rp = new RunProcess();

      if (!context.initialized) {
        String path = K.compiled_def + "/defx-" + K.backend + ".bin";
        Definition javaDef;
        if (new File(path).exists()) {
          javaDef = (Definition) BinaryLoader.load(K.compiled_def + "/defx-" + K.backend + ".bin");
        } else {
          GlobalSettings.kem.register(
              new KException(
                  ExceptionType.ERROR,
                  KExceptionGroup.CRITICAL,
                  "Could not find compiled definition for backend '"
                      + K.backend
                      + "'.\n"
                      + "Please ensure this backend has been kompiled."));
          throw new AssertionError("unreachable");
        }

        if (GlobalSettings.verbose) sw.printIntermediate("Reading definition from binary");

        // This is essential for generating maude
        javaDef = new FlattenModules(context).compile(javaDef, null);

        if (GlobalSettings.verbose) sw.printIntermediate("Flattening modules");

        try {
          javaDef = (Definition) javaDef.accept(new AddTopCellConfig(context));
        } catch (TransformerException e) {
          e.report();
        }

        if (GlobalSettings.verbose) sw.printIntermediate("Adding top cell to configuration");

        javaDef.preprocess(context);

        if (GlobalSettings.verbose) sw.printIntermediate("Preprocessing definition");

        K.definition = javaDef;

        if (GlobalSettings.verbose) sw.printIntermediate("Importing tables");

        K.kompiled_cfg =
            (org.kframework.kil.Configuration)
                BinaryLoader.load(K.compiled_def + "/configuration.bin");

        CommandLine compileOptions =
            (CommandLine) BinaryLoader.load(K.compiled_def + "/compile-options.bin");
        if (compileOptions.hasOption("sortCells")) GlobalSettings.sortedCells = true;

        if (GlobalSettings.verbose) sw.printIntermediate("Reading configuration from binary");
      }

      if (!cmd.hasOption("main-module")) {
        K.main_module = K.definition.getMainModule();
      }
      if (!cmd.hasOption("syntax-module")) {
        K.syntax_module = K.definition.getMainSyntaxModule();
      }

      if (GlobalSettings.verbose) sw.printIntermediate("Resolving main and syntax modules");

      if (K.pgm != null) {
        KAST = rp.runParserOrDie(K.getProgramParser(), K.pgm, false, null, context);
      } else {
        KAST = null;
      }

      if (GlobalSettings.verbose) sw.printIntermediate("Kast process");

      if (K.term != null) {
        if (K.parser.equals("kast") && !cmd.hasOption("parser")) {
          if (K.backend.equals("java")) {
            K.parser = "kast -ruleParser";
          } else {
            K.parser = "kast -groundParser";
          }
        }
      }

      GlobalSettings.kem.print();

      if (!K.debug && !K.guidebug) {
        normalExecution(KAST, lang, rp, cmd_options, context);
      } else {
        if (K.do_search) {
          Error.report(
              "Cannot specify --search with --debug. In order to search\nnside the debugger, use the step-all command.");
        }
        if (K.guidebug) guiDebugExecution(KAST, lang, null, context);
        else debugExecution(KAST, lang, null, context);
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Exemple #4
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);
  }