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; }
private static String convertAssignment( final S_Assignment stm, String keyword, final ISyntacticElement elt, final Expression expr, final Set<Diagnostic> errors) { IExpressionDescription value = convExpr(stm.getValue(), errors); if (keyword.endsWith("<-") || keyword.equals(SET)) { // Translation of "container[index] <- value" to // "put item: value in: container at: index" // 20/1/14: Translation of container[index] +<- value" to // "add item: value in: container at: index" if (expr instanceof Access && expr.getOp().equals("[")) { String kw = keyword.equals("+<-") ? ADD : PUT; String to = keyword.equals("+<-") ? TO : IN; elt.setKeyword(kw); addFacet(elt, ITEM, value, errors); addFacet(elt, to, convExpr(expr.getLeft(), errors), errors); List<Expression> args = EGaml.getExprsOf(((Access) expr).getArgs()); if (args.size() == 0) { // Add facet all: true when no index is provided addFacet(elt, ALL, ConstantExpressionDescription.create(true), errors); } else { if (args.size() == 1) { // Integer index addFacet(elt, AT, convExpr(args.get(0), errors), errors); } else { // Point index IExpressionDescription p = new OperatorExpressionDescription( POINT, convExpr(args.get(0), errors), convExpr(args.get(1), errors)); addFacet(elt, AT, p, errors); } } keyword = kw; } else { // Translation of "var <- value" to "set var value: value" elt.setKeyword(SET); addFacet(elt, VALUE, value, errors); keyword = SET; } } else if (keyword.startsWith("<<") || keyword.equals("<+")) { // Translation of "container <+ item" or "container << item" to "add item: item to: container" // 08/01/14: Addition of the "<<+" (add all) elt.setKeyword(ADD); addFacet(elt, TO, convExpr(expr, errors), errors); addFacet(elt, ITEM, value, errors); if (keyword.equals("<<+")) { addFacet(elt, ALL, ConstantExpressionDescription.create(true), errors); } keyword = ADD; } else if (keyword.startsWith(">>") || keyword.equals(">-")) { // Translation of "container >> item" or "container >- item" to // "remove item: item from: container" // 08/01/14: Addition of the ">>-" keyword (remove all) elt.setKeyword(REMOVE); // 20/01/14: Addition of the access [] to remove from the index if (expr instanceof Access && expr.getOp().equals("[") && EGaml.getExprsOf(((Access) expr).getArgs()).size() == 0) { addFacet(elt, FROM, convExpr(expr.getLeft(), errors), errors); addFacet(elt, INDEX, value, errors); } else { addFacet(elt, FROM, convExpr(expr, errors), errors); addFacet(elt, ITEM, value, errors); } if (keyword.equals(">>-")) { addFacet(elt, ALL, ConstantExpressionDescription.create(true), errors); } keyword = REMOVE; } else if (keyword.equals(EQUATION_OP)) { // conversion of left member (either a var or a function) IExpressionDescription left = null; if (expr instanceof VariableRef) { left = new OperatorExpressionDescription(ZERO, convExpr(expr, errors)); } else { left = convExpr(expr, errors); } addFacet(elt, EQUATION_LEFT, left, errors); // Translation of right member addFacet(elt, EQUATION_RIGHT, value, errors); } return keyword; }