コード例 #1
0
ファイル: PowVisitor.java プロジェクト: nest/nestml
  public Either<Integer, String> calculateNumericValue(ASTExpr expr) {
    if (expr.isLeftParentheses()) {
      return calculateNumericValue(expr.getExpr().get());
    } else if (expr.getNESTMLNumericLiteral().isPresent()) {
      if (expr.getNESTMLNumericLiteral().get().getNumericLiteral() instanceof ASTIntLiteral) {
        ASTIntLiteral literal =
            (ASTIntLiteral) expr.getNESTMLNumericLiteral().get().getNumericLiteral();
        return Either.value(literal.getValue());
      } else {
        return Either.error(
            ERROR_CODE
                + " "
                + AstUtils.print(expr.get_SourcePositionStart())
                + " : "
                + "No floating point values allowed in the exponent to a UNIT base");
      }
    } else if (expr.isUnaryMinus()) {
      Either<Integer, String> term = calculateNumericValue(expr.getTerm().get());
      if (term.isError()) {
        return term;
      }
      return Either.value(-term.getValue());
    }

    return Either.error(
        ERROR_CODE
            + " "
            + AstUtils.print(expr.get_SourcePositionStart())
            + " : "
            + "Cannot calculate value of exponent. Must be a static value!");
  }
コード例 #2
0
ファイル: NestmlErrorStrings.java プロジェクト: nest/nestml
 static String message(
     final ComponentWithoutOutput coco,
     final String componentName,
     final SourcePosition sourcePosition) {
   final String ERROR_MSG_FORMAT =
       "Problem with the component: "
           + componentName
           + ". Components cannot have any output, since they are not elements of a "
           + "neuronal network, but serve as a part of a neuron declaration.";
   return code(coco) + " " + AstUtils.print(sourcePosition) + ": " + ERROR_MSG_FORMAT;
 }
コード例 #3
0
ファイル: NestmlErrorStrings.java プロジェクト: nest/nestml
 static String message(
     final VectorVariableInNonVectorDeclaration coco,
     final String usedAlias,
     final SourcePosition sourcePosition) {
   final String ERROR_MSG_FORMAT =
       "A vector '"
           + usedAlias
           + "' cannot be used as part of an initial expression of "
           + "non-vector variable declaration.";
   return code(coco) + " " + AstUtils.print(sourcePosition) + ": " + ERROR_MSG_FORMAT;
 }
コード例 #4
0
ファイル: AliasHasNoSetter.java プロジェクト: nest/nestml
  @Override
  public void check(final ASTAliasDecl alias) {
    final ASTDeclaration decl = alias.getDeclaration();
    checkState(decl.getEnclosingScope().isPresent(), "No scope assigned to the node: " + decl);
    final Scope scope = decl.getEnclosingScope().get();

    if (alias.isAlias()) {
      // per default aliases have only a single variable. it is checked by the AliasHasOneVar coco.
      final String aliasVar = decl.getVars().get(0);
      final String varTypeName = AstUtils.computeTypeName(decl.getDatatype());
      if (isSetterPresent(aliasVar, varTypeName, scope)
          || AliasInverter.isRelativeExpression(decl.getExpr().get())) {
        Log.trace(
            "The setter will be generated or used for the alias  at "
                + alias.get_SourcePositionStart().toString(),
            ERROR_CODE);
      }
    }
  }
コード例 #5
0
ファイル: PowVisitor.java プロジェクト: nest/nestml
  @Override
  public void visit(ASTExpr expr) {
    final Either<TypeSymbol, String> baseType = expr.getBase().get().getType();
    final Either<TypeSymbol, String> exponentType = expr.getExponent().get().getType();

    if (baseType.isError()) {
      expr.setType(baseType);
      return;
    }
    if (exponentType.isError()) {
      expr.setType(exponentType);
      return;
    } else if (isNumeric(baseType.getValue()) && isNumeric(exponentType.getValue())) {
      if (isInteger(baseType.getValue()) && isInteger(exponentType.getValue())) {
        expr.setType(Either.value(getIntegerType()));
        return;
      } else if (isUnit(baseType.getValue())) {
        if (!isInteger(exponentType.getValue())) {
          final String errorMsg =
              ERROR_CODE
                  + " "
                  + AstUtils.print(expr.get_SourcePositionStart())
                  + " : "
                  + "With a Unit base, the exponent must be an integer.";
          expr.setType(Either.error(errorMsg));
          error(errorMsg, expr.get_SourcePositionStart());
          return;
        }
        UnitRepresentation baseRep = new UnitRepresentation(baseType.getValue().getName());
        Either<Integer, String> numericValue =
            calculateNumericValue(
                expr.getExponent()
                    .get()); // calculate exponent value if exponent composed of literals
        if (numericValue.isValue()) {
          expr.setType(
              Either.value(
                  getTypeIfExists((baseRep.pow(numericValue.getValue())).serialize()).get()));
          return;
        } else {
          final String errorMsg = numericValue.getError();
          expr.setType(Either.error(errorMsg));
          error(errorMsg, expr.get_SourcePositionStart());
          return;
        }
      } else {
        expr.setType(Either.value(getRealType()));
        return;
      }
    }
    // Catch-all if no case has matched
    final String errorMsg =
        ERROR_CODE
            + " "
            + AstUtils.print(expr.get_SourcePositionStart())
            + " : "
            + "Cannot determine the type of the expression: "
            + baseType.getValue().prettyPrint()
            + "**"
            + exponentType.getValue().prettyPrint();
    expr.setType(Either.error(errorMsg));
    error(errorMsg, expr.get_SourcePositionStart());
  }
コード例 #6
0
ファイル: NestmlErrorStrings.java プロジェクト: nest/nestml
 static String message(
     final ComponentHasNoDynamics coco, final String name, final SourcePosition sourcePosition) {
   final String ERROR_MSG_FORMAT = "Component " + name + " doesn't have dynamics function.";
   return code(coco) + " " + AstUtils.print(sourcePosition) + ": " + ERROR_MSG_FORMAT;
 }
コード例 #7
0
ファイル: NestmlErrorStrings.java プロジェクト: nest/nestml
 static String message(final AliasHasOneVar coco, final SourcePosition sourcePosition) {
   final String ERROR_MESSAGE_FORMAT =
       "'alias' declarations must only declare exactly one variable.";
   return code(coco) + " " + AstUtils.print(sourcePosition) + ": " + ERROR_MESSAGE_FORMAT;
 }