Ejemplo n.º 1
0
 private static void assignDependencies(
     final Statement stm,
     final String keyword,
     final ISyntacticElement elt,
     final Set<Diagnostic> errors) {
   // COMPATIBILITY with the definition of environment
   // if ( !SymbolProto.nonTypeStatements.contains(keyword) ) {
   if (!DescriptionFactory.isStatementProto(keyword)) {
     Set<String> s = varDependenciesOf(stm);
     if (s != null && !s.isEmpty()) {
       elt.setFacet(DEPENDS_ON, new StringListExpressionDescription(s));
     }
     // 25/01/14: this test is cancelled for the moment, as the facet type is now defined earlier
     // when dealing
     // with type var_name;
     // if ( !(stm instanceof S_Var) ) {
     // IExpressionDescription type = elt.getExpressionAt(TYPE);
     // if ( type != null ) {
     // if ( type.toString().equals(keyword) ) {
     // addWarning("Duplicate declaration of type", stm, errors);
     // } else {
     // addWarning("Conflicting declaration of type (" + type + " and " + keyword +
     // "), only the last one will be considered", stm, errors);
     // }
     // }
     // }
   }
 }
Ejemplo n.º 2
0
 private static void addFacet(
     final ISyntacticElement e,
     final String key,
     final IExpressionDescription expr,
     final Set<Diagnostic> errors) {
   if (e.hasFacet(key)) {
     // if ( key.equals(TYPE) ) {
     // GuiUtils.debug("GamlCompatibilityConverter.addFacet:");
     // }
     addWarning(
         "Double definition of facet " + key + ". Only the last one will be considered",
         e.getElement(),
         errors);
   }
   e.setFacet(key, expr);
 }
Ejemplo n.º 3
0
  private static void convertFacets(
      final Statement stm,
      final String keyword,
      final ISyntacticElement elt,
      final Set<Diagnostic> errors) {
    SymbolProto p = DescriptionFactory.getProto(keyword, null);
    for (Facet f : EGaml.getFacetsOf(stm)) {
      String fname = EGaml.getKey.caseFacet(f);

      // We change the "<-" and "->" symbols into full names
      if (fname.equals("<-")) {
        fname = keyword.equals(LET) || keyword.equals(SET) ? VALUE : INIT;
      } else if (fname.equals("->")) {
        fname = FUNCTION;
      }

      // We compute (and convert) the expression attached to the facet
      boolean label = p == null ? false : p.isLabel(fname);
      IExpressionDescription fexpr = convExpr(f, label, errors);
      addFacet(elt, fname, fexpr, errors);
    }

    // We add the "default" (or omissible) facet to the syntactic element
    String def = stm.getFirstFacet();
    if (def != null) {
      if (def.endsWith(":")) {
        def = def.substring(0, def.length() - 1);
      }
    } else {
      def = DescriptionFactory.getOmissibleFacetForSymbol(keyword);
    }
    if (def != null && !def.isEmpty() && !elt.hasFacet(def)) {
      IExpressionDescription ed = findExpr(stm, errors);
      if (ed != null) {
        elt.setFacet(def, ed);
      }
    }
  }
Ejemplo n.º 4
0
  private static final ISyntacticElement convStatement(
      final ISyntacticElement upper, final Statement stm, final Set<Diagnostic> errors) {
    // We catch its keyword
    String keyword = EGaml.getKey.caseStatement(stm);
    if (keyword == null) {
      throw new NullPointerException(
          "Trying to convert a statement with a null keyword. Please debug to understand the cause.");
    } else if (keyword.equals(ENTITIES)) {
      convertBlock(stm, upper, errors);
      return null;
    } else {
      keyword = convertKeyword(keyword, upper.getKeyword());
    }
    final ISyntacticElement elt = SyntacticFactory.create(keyword, stm, EGaml.hasChildren(stm));

    if (keyword.equals(ENVIRONMENT)) {
      convertBlock(stm, upper, errors);
    } else if (stm instanceof S_Assignment) {
      keyword = convertAssignment((S_Assignment) stm, keyword, elt, stm.getExpr(), errors);
      // } else if ( stm instanceof S_Definition && !SymbolProto.nonTypeStatements.contains(keyword)
      // ) {
    } else if (stm instanceof S_Definition && !DescriptionFactory.isStatementProto(keyword)) {
      S_Definition def = (S_Definition) stm;
      // If we define a variable with this statement
      TypeRef t = (TypeRef) def.getTkey();

      // 20/01/14: The type is now passed plainly
      if (t != null) {
        addFacet(elt, TYPE, convExpr(t, errors), errors);
      }
      // convertType(elt, t, errors);
      if (t != null && doesNotDefineAttributes(upper.getKeyword())) {
        // Translation of "type var ..." to "let var type: type ..." if we are not in a
        // top-level statement (i.e. not in the declaration of a species or an experiment)
        elt.setKeyword(LET);
        // addFacet(elt, TYPE, convertToConstantString(null, keyword), errors);
        keyword = LET;
      } else {
        // Translation of "type1 ID1 (type2 ID2, type3 ID3) {...}" to
        // "action ID1 type: type1 { arg ID2 type: type2; arg ID3 type: type3; ...}"
        Block b = def.getBlock();
        if (b != null && b.getFunction() == null) {
          elt.setKeyword(ACTION);
          // addFacet(elt, TYPE, convertToConstantString(null, keyword), errors);
          keyword = ACTION;
        }
        convertArgs(def.getArgs(), elt, errors);
      }
    } else if (stm instanceof S_Do) {
      // Translation of "stm ID (ID1: V1, ID2:V2)" to "stm ID with:(ID1: V1, ID2:V2)"
      Expression e = stm.getExpr();
      addFacet(elt, ACTION, convertToLabel(e, EGaml.getKeyOf(e)), errors);
      if (e instanceof Function) {
        addFacet(elt, INTERNAL_FUNCTION, convExpr(e, errors), errors);
        Function f = (Function) e;
        Parameters p = f.getParameters();
        if (p != null) {
          addFacet(elt, WITH, convExpr(p, errors), errors);
        } else {
          ExpressionList list = f.getArgs();
          if (list != null) {
            addFacet(elt, WITH, convExpr(list, errors), errors);
          }
        }
      }
    } else if (stm instanceof S_If) {
      // If the statement is "if", we convert its potential "else" part and put it as a child
      // of the syntactic element (as GAML expects it)
      convElse((S_If) stm, elt, errors);
    } else if (stm instanceof S_Action) {
      // Conversion of "action ID (type1 ID1 <- V1, type2 ID2)" to
      // "action ID {arg ID1 type: type1 default: V1; arg ID2 type: type2}"
      convertArgs(((S_Action) stm).getArgs(), elt, errors);
    } else if (stm instanceof S_Reflex) {
      // We add the "when" facet to reflexes and inits if necessary
      S_Reflex ref = (S_Reflex) stm;
      if (ref.getExpr() != null) {
        addFacet(elt, WHEN, convExpr(ref.getExpr(), errors), errors);
      }
    } else if (stm instanceof S_Solve) {
      Expression e = stm.getExpr();
      addFacet(elt, EQUATION, convertToLabel(e, EGaml.getKeyOf(e)), errors);
    }

    // We apply some conversions to the facets expressed in the statement
    convertFacets(stm, keyword, elt, errors);

    if (stm instanceof S_Var && (keyword.equals(CONST) || keyword.equals(VAR))) {
      // We modify the "var", "const" declarations in order to replace the
      // keyword by the type
      IExpressionDescription type = elt.getExpressionAt(TYPE);
      if (type == null) {
        addWarning("Facet 'type' is missing, set by default to 'unknown'", stm, errors);
        elt.setKeyword(UNKNOWN);
      } else {

        // WARNING FALSE (type is now more TypeRef)
        elt.setKeyword(type.toString());
      }
      if (keyword.equals(CONST)) {
        IExpressionDescription constant = elt.getExpressionAt(CONST);
        if (constant != null && constant.toString().equals(FALSE)) {
          addWarning("Is this variable constant or not ?", stm, errors);
        }
        elt.setFacet(CONST, ConstantExpressionDescription.create(true));
      }
    } else if (stm instanceof S_Experiment) {
      // We do it also for experiments, and change their name
      IExpressionDescription type = elt.getExpressionAt(TYPE);
      if (type == null) {
        addInfo("Facet 'type' is missing, set by default to 'gui'", stm, errors);
        elt.setFacet(TYPE, ConstantExpressionDescription.create(GUI_));
      }
      // if ( type == null ) {
      // addWarning("Facet 'type' is missing, set by default to 'gui'", stm, errors);
      // elt.setFacet(TYPE, ConstantExpressionDescription.create(GUI_));
      // elt.setKeyword(GUI_);
      // } else {
      // elt.setKeyword(type);
      // }
      // We modify the names of experiments so as not to confuse them with species
      String name = elt.getName();
      elt.setFacet(TITLE, convertToLabel(null, "Experiment " + name));
      elt.setFacet(NAME, convertToLabel(null, name));
    } else // TODO Change this by implementing only one class of methods (that delegates to
    // others)
    if (keyword.equals(METHOD)) {
      // We apply some conversion for methods (to get the name instead of the "method"
      // keyword)
      String type = elt.getName();
      if (type != null) {
        elt.setKeyword(type);
      }
    } else if (stm instanceof S_Equations) {
      convStatements(elt, EGaml.getEquationsOf((S_Equations) stm), errors);
    }
    // We add the dependencies (only for variable declarations)
    assignDependencies(stm, keyword, elt, errors);
    // We convert the block of statements (if any)
    convertBlock(stm, elt, errors);

    return elt;
  }