示例#1
0
  @Override
  public void compile(CodeFragment result, ASTNode node, CompilerEngine engine)
      throws CompilerException {
    System.out.println("called, father is " + node.getParent());
    try {
      LetRuleNode letrule = (LetRuleNode) node;
      Map<String, ASTNode> letmap = letrule.getVariableMap();

      result.appendLine("//start of let\n");
      result.appendLine("localStack.pushLayer();\n");
      for (Entry<String, ASTNode> entry : letmap.entrySet()) {
        CodeFragment val = engine.compile(entry.getValue(), CodeType.R);
        result.appendFragment(val);
        result.appendLine("localStack.put(\"" + entry.getKey() + "\", evalStack.pop());\n");
      }

      result.appendFragment(engine.compile(letrule.getInRule(), CodeType.U));
      result.appendLine("localStack.popLayer();\n");
      result.appendLine("//end of let\n");
    } catch (Exception e) {
      throw new CompilerException(e);
    }
  }
  @Override
  public void compile(CodeFragment result, ASTNode node, CompilerEngine engine)
      throws CompilerException {
    // set comprehension was changed in a newer update of CoreASM.
    // there are no longer two different set comprehension node types

    // evaluates a set comprehension of the form
    // {id | id in value with guard}
    // {id is exp | id1 in value1, ... idn in value n with guard}
    // where the guard is optional.
    // in the first case, the exp is simply id

    SetCompNode cnode = (SetCompNode) node;

    // guard might be non existent, so initialize it
    CodeFragment guard = null;
    // optimization: the true guard is always true anyway, so if it is existent, leave it out
    if (!(cnode.getGuard() instanceof TrueGuardNode))
      guard = engine.compile(cnode.getGuard(), CodeType.R);

    List<String> constrnames = new ArrayList<String>();

    result.appendLine(
        "@decl(java.util.List<@[email protected]>,list)=new java.util.ArrayList<@[email protected]>();\n");
    try {
      // evaluate all constrainer domains and collect the list of variable names
      int counter = 0;
      for (Entry<String, ASTNode> e : cnode.getVarBindings().entrySet()) {
        constrnames.add(e.getKey());
        result.appendFragment(engine.compile(e.getValue(), CodeType.R));
        result.appendLine(
            "@decl(java.util.List<@[email protected]>,domain"
                + counter
                + ")=new java.util.ArrayList<@[email protected]>(((@[email protected])evalStack.pop()).enumerate());\n");
        counter++;
      }
      // iterate

      // open for loops
      for (int i = 0; i < constrnames.size(); i++) {
        String var = "@domain" + i + "@";
        String cvar = "@c" + i + "@";
        result.appendLine(
            "for(@decl(int,c" + i + ")=0; " + cvar + " < " + var + ".size(); " + cvar + "++){\n");
      }

      result.appendLine("localStack.pushLayer();\n");

      for (int i = 0; i < constrnames.size(); i++) {
        result.appendLine(
            "localStack.put(\""
                + constrnames.get(i)
                + "\", @domain"
                + i
                + "@.get(@c"
                + i
                + "@));\n");
      }

      if (guard == null) {
        result.appendFragment(engine.compile(cnode.getSetFunction(), CodeType.R));
        result.appendLine("@[email protected]((@[email protected])evalStack.pop());\n");
      } else {
        result.appendFragment(guard);
        result.appendLine("if(evalStack.pop().equals(@[email protected])){\n");
        result.appendFragment(engine.compile(cnode.getSetFunction(), CodeType.R));
        result.appendLine("@[email protected]((@[email protected])evalStack.pop());\n");
        result.appendLine("}\n");
      }

      result.appendLine("localStack.popLayer();\n");

      // close for loops
      for (int i = 0; i < constrnames.size(); i++) {
        result.appendLine("}\n");
      }
      String setelement =
          engine.getPath().getEntryName(LibraryEntryType.STATIC, "SetElement", "SetPlugin");
      result.appendLine("evalStack.push(new " + setelement + "(@list@));\n");
    } catch (EngineException exc) {
      throw new CompilerException(exc);
    }
  }