Esempio n. 1
0
  @Override
  public String convertNameReference(final ASTQualifiedName astQualifiedName) {
    checkArgument(
        astQualifiedName.getEnclosingScope().isPresent(),
        "No scope is assigned. Please, build the symbol " + "table before calling this function.");
    final String name = Names.getQualifiedName(astQualifiedName.getParts());
    final Scope scope = astQualifiedName.getEnclosingScope().get();
    if ("E".equals(name)) {
      return "numerics::e";
    } else {
      final Optional<NESTMLVariableSymbol> variableSymbol =
          scope.resolve(name, NESTMLVariableSymbol.KIND);

      checkState(variableSymbol.isPresent(), "Cannot resolve the variable: " + name);

      if (variableSymbol.get().getBlockType().equals(NESTMLVariableSymbol.BlockType.LOCAL)) {
        return name;
      } else {
        if (variableSymbol.get().getArraySizeParameter().isPresent()) {
          return "get_" + name + "()[i]";
        } else {
          return "get_" + name + "()";
        }
      }
    }
  }
Esempio n. 2
0
  @Override
  public void check(final ASTDeclaration astDeclaration) {
    checkArgument(
        astDeclaration.getEnclosingScope().isPresent(),
        "Declaration hast no scope. Run symboltable creator.");
    final Scope scope = astDeclaration.getEnclosingScope().get();

    for (String var : astDeclaration.getVars()) {
      // tries to resolve the variable name as type. if it is possible, then the variable name
      // clashes with type name is reported as an error
      final Optional<NESTMLTypeSymbol> res = scope.resolve(var, NESTMLTypeSymbol.KIND);

      // could resolve type as variable, report an error
      if (res.isPresent()) {
        Log.error(
            ERROR_CODE + ":" + String.format(ERROR_MSG_FORMAT, var),
            astDeclaration.get_SourcePositionEnd());
      }
    }
  }
Esempio n. 3
0
  public static Optional<NESTMLMethodSymbol> resolveMethod(
      final Scope scope, final String methodName, final List<String> parameters) {
    // it is OK. The cast is secured through the symboltable infrastructure
    @SuppressWarnings("unchecked")
    final Optional<NESTMLMethodSymbol> standAloneFunction =
        (Optional<NESTMLMethodSymbol>)
            scope.resolve(new NESTMLMethodSignaturePredicate(methodName, parameters));

    final String calleeVariableNameCandidate = Names.getQualifier(methodName);
    final String simpleMethodName = Names.getSimpleName(methodName);

    if (!calleeVariableNameCandidate.isEmpty()) {
      final Optional<NESTMLVariableSymbol> calleeVariableSymbol =
          scope.resolve(calleeVariableNameCandidate, NESTMLVariableSymbol.KIND);
      if (calleeVariableSymbol.isPresent()) {

        final Optional<NESTMLMethodSymbol> builtInMethod =
            calleeVariableSymbol
                .get()
                .getType()
                .getBuiltInMethod(simpleMethodName); // TODO parameters are not considered!
        if (standAloneFunction.isPresent() && builtInMethod.isPresent()) {
          final String errorDescription =
              "Unambiguous function exception. Function '"
                  + simpleMethodName
                  + "'. Can be resolved as a standalone function and as a method of: '"
                  + calleeVariableSymbol
                  + "' variable.";

          throw new RuntimeException(errorDescription);
        }

        if (builtInMethod.isPresent()) {
          return builtInMethod;
        }
      }
    }

    return standAloneFunction;
  }
  protected void createSymbolTableFromAST(
      ASTHost ast,
      String modelName,
      MutableScope enclosingScope,
      ResolverConfiguration resolverConfiguration) {
    final CompositeSymbolTableCreator symbolTableCreator =
        getModelingLanguage()
            .getSymbolTableCreator(resolverConfiguration, enclosingScope)
            .orElse(null);

    if (symbolTableCreator != null) {
      Log.debug(
          "Start creation of symbol table for model \"" + modelName + "\".",
          CompositeModelLoader.class.getSimpleName());
      final Scope scope = symbolTableCreator.createFromAST(ast);

      if (!(scope instanceof ArtifactScope)) {
        Log.warn(
            "Top scope of model "
                + modelName
                + " is expected to be an artifact scope, but"
                + " is scope \""
                + scope.getName()
                + "\"");
      }

      Log.debug(
          "Created symbol table for model \"" + modelName + "\".",
          CompositeModelLoader.class.getSimpleName());
    } else {
      Log.warn(
          "No symbol created, because '"
              + getModelingLanguage().getName()
              + "' does not define a symbol table creator.");
    }
  }
Esempio n. 5
0
  public void check(final ASTUSE_Stmt use) {
    checkArgument(
        use.getEnclosingScope().isPresent(),
        "No scope was assigned. Please, run symboltable creator.");
    final String typeName = Names.getQualifiedName(use.getName().getParts());

    final Scope scope = use.getEnclosingScope().get();

    final Optional<NESTMLTypeSymbol> predefinedType =
        scope.resolve(typeName, NESTMLTypeSymbol.KIND);

    if (predefinedType.isPresent()) {
      final String msg =
          "Only components can be used by neurons/components and not "
              + typeName
              + " of the type: "
              + predefinedType.get().getType()
              + " .";
      Log.error(ERROR_CODE + ":" + msg, use.get_SourcePositionStart());
    }

    final Optional<NESTMLNeuronSymbol> neuronType =
        scope.resolve(typeName, NESTMLNeuronSymbol.KIND);

    if (neuronType.isPresent()
        && !neuronType.get().getType().equals(NESTMLNeuronSymbol.Type.COMPONENT)) {
      final String msg =
          "Only components can be used by components and not "
              + typeName
              + " that is a neuron, not a "
              + "component";

      Log.error(ERROR_CODE + ":" + msg, use.get_SourcePositionStart());
    }
    // Undefined type of the name
  }
Esempio n. 6
0
 private NESTMLVariableSymbol resolveVariable(final String variableName, final Scope scope) {
   final Optional<NESTMLVariableSymbol> variableSymbol =
       scope.resolve(variableName, NESTMLVariableSymbol.KIND);
   checkState(variableSymbol.isPresent(), "Cannot resolve the variable: " + variableName);
   return variableSymbol.get();
 }