Ejemplo n.º 1
0
  // compilationUnit parses MicroScala programs.
  public CompEnvironment CompilationUnit() throws java.io.IOException {
    CompEnvironment env;
    VariableEnvironment varEnv = null;
    String componentId;
    // object id { {Def} MainDef	}
    // object
    if (token.symbol() != TokenClass.OBJECT) {
      ErrorMessage.print("object expected");
    }
    getToken();

    // id
    if (token.symbol() != TokenClass.ID) {
      ErrorMessage.print("id expected");
    }
    componentId = token.lexeme();
    env = new CompEnvironment(componentId);
    varEnv = new VariableEnvironment(componentId);
    getToken();

    // {
    if (token.symbol() != TokenClass.LEFTBRACE) {
      ErrorMessage.print("Left Brace expected");
    }
    getToken();

    // one or more defs
    while (token.symbol() == TokenClass.DEF || token.symbol() == TokenClass.VAR) {
      if (token.symbol() == TokenClass.DEF) {
        Def(env);
      } else {
        VarDef(varEnv);
      }
    }

    // }
    if (token.symbol() != TokenClass.RIGHTBRACE) {
      ErrorMessage.print("Right BRACE Expected");
    }
    getToken();

    if (token.symbol() != TokenClass.EOF) {
      ErrorMessage.print("EOF expected");
    }
    varEnv.print();
    return env;
  }
Ejemplo n.º 2
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;
  }
Ejemplo n.º 3
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();
  }