⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typechecker.java

📁 java实现的一个pascal编译器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    function = false;
  }

  /**
   * monitor_heading =
   *   monitor identifier semicolon ;
   * puts the identifier monitor in the global table
   */
  public void outAMonitorHeading(AMonitorHeading node) {
    String key = node.getIdentifier().getText().toUpperCase();
    program = false;
    monitor = true;
    global_table.put(key, new Monitor());
  }

  /**
   * sets monitor to false and program to true
   */
  public void outAMonitorDeclaration(AMonitorDeclaration node) {
    monitor = false;
    program = true;
  }

  /**
   * sets program to false and process to true
   */
  public void outAProcessHeading(AProcessHeading node) {
    process = true;
    program = false;
  }

  /**
   * sets program to true and process to false.
   */
  public void outAProcessDeclaration(AProcessDeclaration node) {
    program = true;
    process = false;
  }

  /**
   * types : set type to INTEGER type
   */
  public void outAIntegerType(AIntegerType node) { 
    type = INTEGER;
  }

  /**
   * sets type to REAl type
   */
  public void outARealType(ARealType node) {
    type = REAL;
  }

  /**
   * sets type to CHAR type
   */
  public void outACharType(ACharType node) {
    type = CHAR;
  }

  /**
   * sets type to BOOLEAN
   */
  public void outABooleanType(ABooleanType node) {
    type = BOOLEAN;
  }

  /**
   * sets type to array of type
   */
  public void outAArrayType(AArrayType node) {
    if ((type instanceof ARRAY) || (type instanceof CHANNEL) ||
        (type == SEMAPHORE) || (type == EXCEPTION) ||
        (type == CONDITION)) {
      err.report(36, node.getArray().getLine(), node.getArray().getPos());
    }
    type = new ARRAY(type);
  }

  /**
   * sets type to SEMAPHORE
   */
  public void outASemaphoreType(ASemaphoreType node) {
    if (!program) {
      err.report(21, node.getSemaphore().getLine(), node.getSemaphore().getPos());
    }
    type = SEMAPHORE;
  }

  /** 
   * sets type to string.
   */
  public void outAStringType(AStringType node) {
    type = STRING;
  }

  /**
   * sets type to channel of type
   */
  public void outAChannelType(AChannelType node) {
    if (!program) {
      err.report(21, node.getChannel().getLine(), node.getChannel().getLine());
    }
    if ((type instanceof ARRAY) || (type instanceof CHANNEL) ||
        (type == SEMAPHORE) || (type == EXCEPTION) ||
        (type == CONDITION)) {
      err.report(50, node.getChannel().getLine(), node.getChannel().getPos());
    }
    type = new CHANNEL(type);
  }

  /**
   * sets type to CONDITION
   */
  public void outAConditionType(AConditionType node) {
    if (!monitor) {
      err.report(13, node.getCondition().getLine(), node.getCondition().getPos());
    }
    type = CONDITION;
  }

  /**
   * sets type to EXCEPTION
   */
  public void outAExceptionType(AExceptionType node) {
    if (!program) {
      err.report(21, node.getException().getLine(), node.getException().getPos());
    }
    type = EXCEPTION;
  }

  /**
   * reports an error if the index is out of range
   */
  public void outAIntegerIndex(AIntegerIndex node) {
    TIntegerLiteral int_lit = node.getIntegerLiteral();
    String image = int_lit.getText();

    try {
      int value = Integer.parseInt(image);
      
      if (value > Integer.MAX_VALUE) {
        err.report(6, int_lit.getLine(), int_lit.getPos());
      }
    }
    catch (NumberFormatException e) {
      err.report(6, int_lit.getLine(), int_lit.getPos());
    }
  }

  /**
   * reports an error if the identifier is not a constant
   */
  public void outAIdentifierIndex(AIdentifierIndex node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (program) {
      if (!(global_table.get(key) instanceof Constant)) {
        err.report(3, ident.getLine(), ident.getPos());
      }
    }
    else if (monitor) {
      if (monitor_table.containsKey(key)) {
        if (!(monitor_table.get(key) instanceof Constant)) {
          err.report(3, ident.getLine(), ident.getPos());
        }
      }
      else if (!(global_table.get(key) instanceof Constant)) {
        err.report(3, ident.getLine(), ident.getPos());
      }
    }
    else {
      if (local_table.containsKey(key)) {
        if ((local_table.get(key) instanceof Constant)) {
          err.report(3, ident.getLine(), ident.getPos());
        }
      }
      else if (previous_entity == PROGRAM) {
        if (!(global_table.get(key) instanceof Constant)) {
          err.report(3, ident.getLine(), ident.getPos());
        }
      }
      else {
        if (!(monitor_table.get(key) instanceof Constant)) { 
          err.report(3, ident.getLine(), ident.getPos());
        }
      }
    }
  }

  /**
   * {assign} variable assignop expression
   */
  public void outAAssignStatement(AAssignStatement node) {
    Type type2 = transExpression(node.getExpression());

    if ((resultType(type, type2) == TYPE_ERROR) || (type != resultType(type, type2))) {
        err.report(10, node.getAssignop().getLine(), node.getAssignop().getPos());
    }
  }

  /**
   * sets try_statement to true, so that when traversing 
   * the throw statement, it will know that the exception
   * will be caught.
   */
  public void inATryExceptStatement(ATryExceptStatement node) {
    try_statement = true;
  }

  /**
   * sets try_statement to false
   */
  public void outATryExceptStatement(ATryExceptStatement node) {
    try_statement = false;
  }

  /**
   * reports an error if an exception is raised in a body
   * other than procedure or function, and checks
   * if the identifier is an exception.
   */
  public void outARaiseStatement(ARaiseStatement node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (!global_table.containsKey(key) ||
        !(global_table.get(key) instanceof Variable) ||
        ( ( (Variable)global_table.get(key) ).getType() != EXCEPTION)) {
      err.report(8, ident.getLine(), ident.getPos());
    }
    else if (!try_statement) {
      if (program || monitor || process) {
        err.report(28, node.getRaise().getLine(),node.getRaise().getPos());
      } 
      else if (!throws_list.containsKey(key)) {
        err.report(37, ident.getLine(), ident.getPos());
      }
    }
  }

  /**
   * reports an error if the stament is not inside a process or 
   * monitor procedure, and checks if the variable is of type 
   * condition or semaphore.
   */
  public void outAWaitStatement(AWaitStatement node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (process) {
      if (local_table.containsKey(key)) {
        err.report(38, ident.getLine(), ident.getPos());
      }
      else if (!(global_table.get(key) instanceof Variable) ||
               !(((Variable) global_table.get(key)).getType() == SEMAPHORE)) {
        err.report(38, ident.getLine(), ident.getPos());
      }
    }
    else if (previous_entity == MONITOR) {
      if (local_table.containsKey(key)) {
        err.report(39, ident.getLine(), ident.getPos());
      }
      else if (!(monitor_table.containsKey(key)) && 
               (((Variable) monitor_table.get(key)).getType() == CONDITION)) {
        err.report(39, ident.getLine(), ident.getPos());
      }
    }
    else {
      err.report(40, node.getWait().getLine(), node.getWait().getPos());
    }
  }

  /**
   * @see TypeChecker.outAWaitStatement
   */
  public void outASignalStatement(ASignalStatement node) {     
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (process) {
      if (local_table.containsKey(key)) {
        err.report(38, ident.getLine(), ident.getPos());
      }
      else if (!(global_table.get(key) instanceof Variable) ||
               !(((Variable) global_table.get(key)).getType() == SEMAPHORE)) {
        err.report(38, ident.getLine(), ident.getPos());
      }
    }
    else if (previous_entity == MONITOR) {
      if (local_table.containsKey(key)) {
        err.report(39, ident.getLine(), ident.getPos());
      }
      else if (!(monitor_table.containsKey(key)) && 
               (((Variable) monitor_table.get(key)).getType() == CONDITION)) {
        err.report(39, ident.getLine(), ident.getPos());
      }
    }
    else {
      err.report(40, node.getSignal().getLine(), node.getSignal().getPos());
    }
  }

  /**
   * reports an error not used inside a process block.
   */
  public void inASleepStatement(ASleepStatement node) {
    if (!program && !process) {
      err.report(41, node.getSleep().getLine(), node.getSleep().getPos());
    }
  }

  /**
   * reports an error if the statement is used inside a block other than
   * a program or process block.
   */
  public void outAYieldStatement(AYieldStatement node) {
    if (!program && !process) {
      err.report(41, node.getYield().getLine(), node.getYield().getPos());
    }
  }

  /**
   * exception_handler =
   *    when identifier do statement semicolon;
   * if the identifier is not in the global table, then an error is reported.
   * else if the identifier is not of type EXCEPTION it reports an error
   * as well.
   */
  public void inAExceptionHandler(AExceptionHandler node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (!(global_table.get(key) instanceof Variable) ||
        (((Variable) global_table.get(key)).getType() != EXCEPTION)) {
      err.report(8, ident.getLine(), ident.getPos());
    }
  }

  /**
   * clears exp_list
   */
  public void inAIdentifierProcedureCall(AIdentifierProcedureCall node) {
    if (!exp_list.isEmpty()) {
      exp_list.removeAllElements();
    }
  }

  /**
   * procedure_call =
   *   {identifier} identifier |
   *   {arguments} identifier l_paren expression_list r_paren ;
   * if the identifier is not a procedure or function, then 
   * an error is reported. 
   * if the no of arguments being passed to a procedure or function
   * is not equal to the number of arguments required, then an error
   * is reported.
   * if the type of each expression does not match the type of each 
   * argument.
   */
  public void outAIdentifierProcedureCall(AIdentifierProcedureCall node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();
    Object entity;
    if (program) {
       entity = global_table.get(key);
       if (entity instanceof Variable) {
         err.report(7, ident.getLine(), ident.getLine());
       }
       else if (!(entity instanceof Procedure) && !(entity instanceof Function)) {
         err.report(23, ident.getLine(), ident.getPos());
       }
       else if (entity instanceof Procedure) {
         if (((Procedure) entity).raisesException() && !try_statement) {
           err.report(37, ident.getLine(), ident.getPos());
         }
       }
       else if (entity instanceof Function) {
         if (((Function) entity).raisesException() && !try_statement) {
           err.report(37, ident.getLine(), ident.getPos());
         }
       }
    }
    else if (monitor) {
     if (monitor_table.containsKey(key)) {
       entity = monitor_table.get(key);
       if (entity instanceof Variable) {
         err.report(7, ident.getLine(), ident.getPos());
       }
       else if (!(entity instanceof Procedure)) {
         err.report(45, ident.getLine(), ident.getPos());
       }
      else if (entity instanceof Procedure) {
        if (((Procedure) entity).raisesException() && !try_statement) {
          err.report(37, ident.getLine(), ident.getPos());
        }
      }
      else if (entity instanceof Function) {
        if (((Function) entity).raisesException() && !try_statement) {
          err.report(37, ident.getLine(), ident.getPos());
        }
      }
     }
     else {
        entity = global_table.get(key);
        if (entity instanceof Variable) {
          err.report(7, ident.getLine(), ident.getPos());
        }
        else if (!(entity instanceof Procedure) && !(entity instanceof Function)) {
          err.report(23, ident.getLine(), ident.getPos());
        }
        else if (entity instanceof Procedure) {
         if (((Procedure) entity).raisesException() && !try_statement) {
           err.report(37, ident.getLine(), ident.getPos());
         }
       }
       else if (entity instanceof Function) {
         if (((Function) entity).raisesException() && !try_statement) {
           err.report(37, ident.getLine(), ident.getPos());
         }
       }

      }
    }
    else if (process) {
      if (local_table.containsKey(key)) {
        entity = local_table.get(key);
        if (entity instanceof Variable) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -