Esempio n. 1
0
  // Parse a method declaration.
  static String getMethod(SamTokenizer f) throws TokenizerException {
    try {
      // The return type for all methods must be int.
      myCheck(f, "int");

      // methodName stores the name of the method.
      String methodName = f.getWord();
      if (symMap.containsKey(methodName))
        throw new TokenizerException("Error: Duplicate declaration of method " + methodName);
      String asmCode = methodName;

      // The name of the method <-> index of the symbol table of the method in the symTables vector
      symMap.put(methodName, symTables.size());
      symTables.addElement(new LinkedHashMap<String, Integer>());

      // Since there cannot be another method declaration inside of a, the symbol table we are
      // dealing with is
      // always the last element of the symTables vector.
      symTables.lastElement().put(methodName, 0);

      asmCode += ":\n";
      myCheck(f, '(');
      if (!f.check(')')) {
        // No SaM code generated by getFormals
        getFormals(f);
        myCheck(f, ')');
      }
      actMap.put(methodName, params.size());
      for (int i = 1; i <= params.size(); ++i) {
        // Add all formals to the symbol tabel
        symTables.lastElement().put(params.elementAt(params.size() - i), -i);
      }
      asmCode += getBody(f);
      // Clear the params vector and set varCounter back to 2.
      params.clear();
      varCounter = 2;
      return asmCode;
    } catch (Exception e) {
      System.out.println(e.getMessage());
      throw new TokenizerException("Error: Invalid method declaration.");
    }
  }