コード例 #1
0
  /*
   * evaluate the given expression.
   */
  public Object evaluate(Expression e) {
    if (e == null) {
      return null;
    }

    if (debug) {
      System.out.println("Evaluating expression: " + e);
    }

    if (e instanceof Literal) {
      return ((Literal) e).getValue();
    }

    if (e instanceof Identifier) {
      Identifier id = (Identifier) e;
      if (map.containsKey(id.getName())) {
        return map.get(id.getName());
      } else {
        // cache the data values for coherency of the values over
        // the life of this expression executer.
        Monitor m = (Monitor) id.getValue();
        Object v = m.getValue();
        map.put(id.getName(), v);
        return v;
      }
    }

    Expression l = e.getLeft();
    Expression r = e.getRight();

    Operator op = e.getOperator();

    if (op == null) {
      return evaluate(l);
    } else {
      Double lval = new Double(((Number) evaluate(l)).doubleValue());
      Double rval = new Double(((Number) evaluate(r)).doubleValue());
      double result = op.eval(lval.doubleValue(), rval.doubleValue());
      if (debug) {
        System.out.println("Performed Operation: " + lval + op + rval + " = " + result);
      }
      return new Double(result);
    }
  }
コード例 #2
0
 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;
 }