Esempio n. 1
0
  public String VarDef(VariableEnvironment env) throws java.io.IOException {
    String varId;
    Type varType;

    if (token.symbol() != TokenClass.VAR) {
      ErrorMessage.print(
          "Var expected, current token is "
              + token.symbol()
              + " with the lexeme "
              + token.lexeme());
    }
    getToken();

    if (token.symbol() != TokenClass.ID) {
      ErrorMessage.print(
          "ID expected, current token is " + token.symbol() + " with the lexeme " + token.lexeme());
    }
    varId = token.lexeme();
    getToken();

    if (token.symbol() != TokenClass.COLON) {
      ErrorMessage.print(
          ": expected, current token is " + token.symbol() + " with the lexeme " + token.lexeme());
    }
    getToken();

    varType = Type();

    if (token.symbol() != TokenClass.ASSIGN) {
      ErrorMessage.print(
          "= expected, current token is " + token.symbol() + " with the lexeme " + token.lexeme());
    }
    getToken();

    Literal();

    if (token.symbol() != TokenClass.SEMICOLON) {
      ErrorMessage.print(
          "; expected, current token is " + token.symbol() + " with the lexeme " + token.lexeme());
    }
    getToken();
    // TODO this needs to have the value of the var in the null spot, taken from the literal.
    env.update(varId, new ExpressibleValue(varType, null));
    return varId;
  }
Esempio n. 2
0
  // Def
  public void Def(CompEnvironment env) throws java.io.IOException {
    SyntaxTree syntaxTree = null;
    String defID = "";
    Type defType = null;
    DefDenot defDenot;
    ArrayList<String> parameterList = null;
    VariableEnvironment varEnv = null;

    if (token.symbol() != TokenClass.DEF) {
      ErrorMessage.print(
          "Def expected, current token is "
              + token.symbol()
              + " with the lexeme "
              + token.lexeme());
    }
    getToken();
    if (token.symbol() == TokenClass.MAIN) {
      defID = "MAIN";
      varEnv = new VariableEnvironment(defID);
      defType = Type.NULL;
      // this is a maindef
      getToken();
      if (token.symbol() != TokenClass.LEFTPAREN) {
        ErrorMessage.print(
            "( expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();
      // time to get the args
      parameterList = new ArrayList<String>();
      if (token.symbol() != TokenClass.ARGS) {
        ErrorMessage.print(
            "args expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      if (token.symbol() != TokenClass.COLON) {
        ErrorMessage.print(
            ": expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      if (token.symbol() != TokenClass.ARRAY) {
        ErrorMessage.print(
            "Array expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      if (token.symbol() != TokenClass.LEFTBRACKET) {
        ErrorMessage.print(
            "[ expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      if (token.symbol() != TokenClass.STRING) {
        ErrorMessage.print(
            "String expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      if (token.symbol() != TokenClass.RIGHTBRACKET) {
        ErrorMessage.print(
            "] expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      if (token.symbol() != TokenClass.RIGHTPAREN) {
        ErrorMessage.print(
            ") expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      if (token.symbol() != TokenClass.LEFTBRACE) {
        ErrorMessage.print(
            "{ expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();

      while (token.symbol() == TokenClass.VAR) {
        VarDef(varEnv);
      }
      int stmtNum = 0;
      do {
        if (stmtNum == 0) {
          syntaxTree = Statement(varEnv);
          stmtNum++;
        } else {
          syntaxTree = new SyntaxTree(";", syntaxTree, Statement(varEnv));
        }
      } while (token.symbol() == TokenClass.IF
          || token.symbol() == TokenClass.WHILE
          || token.symbol() == TokenClass.ID
          || token.symbol() == TokenClass.PRINTLN
          || token.symbol() == TokenClass.LEFTBRACE);

      if (token.symbol() != TokenClass.RIGHTBRACE) {
        ErrorMessage.print(
            "} expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();
    } else if (token.symbol() == TokenClass.ID) {
      defID = token.lexeme();
      varEnv = new VariableEnvironment(defID);
      // normal def
      getToken();
      if (token.symbol() != TokenClass.LEFTPAREN) {
        ErrorMessage.print(
            "( expected, current token is "
                + token.symbol()
                + " with the lexeme "
                + token.lexeme());
      }
      getToken();
      parameterList = new ArrayList<String>();
      if (token.symbol() == TokenClass.ID) {
        String varID = token.lexeme();
        getToken();

        if (token.symbol() != TokenClass.COLON) {
          ErrorMessage.print(
              ": expected, current token is "
                  + token.symbol()
                  + " with the lexeme "
                  + token.lexeme());
        }
        getToken();

        Type varType = Type();
        varEnv.update(varID, new ExpressibleValue(varType, null));
        while (token.symbol() == TokenClass.COMMA) {
          getToken();
          if (token.symbol() != TokenClass.ID) {
            ErrorMessage.print(
                "ID expected, current token is "
                    + token.symbol()
                    + " with the lexeme "
                    + token.lexeme());
          }
          varID = token.lexeme();
          getToken();
          if (token.symbol() != TokenClass.COLON) {
            ErrorMessage.print(
                ": expected, current token is "
                    + token.symbol()
                    + " with the lexeme "
                    + token.lexeme());
          }
          getToken();
          varType = Type();

          varEnv.update(varID, new ExpressibleValue(varType, null));
        }
        if (token.symbol() != TokenClass.RIGHTPAREN) {
          ErrorMessage.print(
              ") expected, current token is "
                  + token.symbol()
                  + " with the lexeme "
                  + token.lexeme());
        }
        getToken();
        if (token.symbol() != TokenClass.COLON) {
          ErrorMessage.print(
              ": expected, current token is "
                  + token.symbol()
                  + " with the lexeme "
                  + token.lexeme());
        }
        getToken();
        // TODO this is where the return value of the def is set
        defType = Type();
        if (token.symbol() != TokenClass.ASSIGN) {
          ErrorMessage.print(
              "= expected, current token is "
                  + token.symbol()
                  + " with the lexeme "
                  + token.lexeme());
        }
        getToken();
        if (token.symbol() != TokenClass.LEFTBRACE) {
          ErrorMessage.print(
              "{ expected, current token is "
                  + token.symbol()
                  + " with the lexeme "
                  + token.lexeme());
        }
        getToken();
        while (token.symbol() == TokenClass.VAR) {
          VarDef(varEnv);
        }
        int stmtNum = 0;
        while (token.symbol() == TokenClass.IF
            || token.symbol() == TokenClass.WHILE
            || token.symbol() == TokenClass.ID
            || token.symbol() == TokenClass.PRINTLN
            || token.symbol() == TokenClass.LEFTBRACE) {
          if (stmtNum == 0) {
            syntaxTree = Statement(varEnv);
            stmtNum++;
          } else {
            syntaxTree = new SyntaxTree(";", syntaxTree, Statement(varEnv));
            stmtNum++;
          }
        }
        if (token.symbol() != TokenClass.RETURN) {
          ErrorMessage.print(
              "Return expected, current token is "
                  + token.symbol()
                  + " with the lexeme "
                  + token.lexeme());
        }
        getToken();
        // TODO fix this.
        syntaxTree = new SyntaxTree(";", syntaxTree, new SyntaxTree("RETURN", ListExpr()));
        if (token.symbol() != TokenClass.SEMICOLON) {
          ErrorMessage.print("; expected, current token is " + token.lexeme());
        }
        getToken();

        if (token.symbol() != TokenClass.RIGHTBRACE) {
          ErrorMessage.print(
              "} expected, current token is "
                  + token.symbol()
                  + " with the lexeme "
                  + token.lexeme());
        }
        getToken();
      }
    } else {
      varEnv = new VariableEnvironment(defID);
      VarDef(varEnv);
    }

    defDenot = new DefDenot(parameterList, defType, varEnv, syntaxTree);
    env.update(defID, defDenot);
    // syntaxTree.print(defID);
    System.out.println();
    System.out.println();
  }