Ejemplo n.º 1
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;
  }