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

📄 typechecker.java

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

  /**
   * 
  /** 
   * if the identifier is not a variable or constant and if
   * it is not of type integer, an error is reported.
   */
  public void outAIdentifierValue(AIdentifierValue node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();
    Object entity = null;

    if (program) {
      entity = global_table.get(key);    
    }
    else if (process) {
      if (local_table.containsKey(key)) {
        entity = local_table.get(key);
      }
      else {
        entity = global_table.get(key);
      }
    }
    if (entity instanceof Constant) {
      if (((Constant) entity).getType() != INTEGER) {
        err.report(47, ident.getLine(), ident.getPos());
      }
    }
    else if (entity instanceof Variable) {
      if (((Variable) entity).getType() != INTEGER) {
        err.report(47, ident.getLine(), ident.getPos());
      }
    }       
    else if (entity instanceof Function) {
      if (((Function) entity).getType() != INTEGER) {
        err.report(47, ident.getLine(), ident.getPos());
      }
    }
    else {
      err.report(48, ident.getLine(), ident.getPos());
    }
  }

  /**
   * variable =
   *  {identifier} identifier |
   *  checks if the identifier is a variable or function
   * and returns an error if its not.
   */
  public void outAIdentifierVariable(AIdentifierVariable node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();
    Object entity = null;

    if (program) {
      entity = global_table.get(key);
      if (entity instanceof Variable) {
        type = ((Variable) entity).getType();
      }
      else if (entity instanceof Constant){
        err.report(27, ident.getLine(), ident.getPos());
      }
      else {
        err.report(26, ident.getLine(), ident.getPos());
      }
    }
    else if (monitor) {
      if (monitor_table.containsKey(key)) {
        entity = monitor_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
          err.report(27, ident.getLine(), ident.getPos());
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
      else {
        entity = global_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
         err.report(27, ident.getLine(), ident.getPos());
        }
        else { 
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
    }
    else if (function) {
      if (local_table.containsKey(key)) {
        entity = local_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
          err.report(27, ident.getLine(), ident.getPos());
        }
        else {
          err.report(42, ident.getLine(), ident.getPos());
        }
      }
      else if (key.equalsIgnoreCase(current_function)) {
         type = ((Function) global_table.get(key)).getType(); 
      }
      else {
        err.report(26, ident.getLine(), ident.getPos());
      }
    }
    else if (process) {
      if (local_table.containsKey(key)) {
        entity = local_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
          err.report(27, ident.getLine(), ident.getPos());
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
      else {
        entity = global_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
          err.report(27, ident.getLine(), ident.getPos());
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
    }
    else {
      if (local_table.containsKey(key)) {
        entity = local_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
          err.report(27, ident.getLine(), ident.getPos());
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
      else if (previous_entity == MONITOR) {
        entity = monitor_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
          err.report(27, ident.getLine(), ident.getPos());
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }    
      else {
        entity = global_table.get(key);
        if (entity instanceof Variable) {
          type = ((Variable) entity).getType();
        }
        else if (entity instanceof Constant){
          err.report(27, ident.getLine(), ident.getPos());
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
    }
  }

  /**
   *  {array} identifier l_bracket expression r_bracket
   * @see TypeChecker.outAIdentifierVariable
   */
  public void outAArrayVariable(AArrayVariable node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();
    Object entity;

    if (program) {
      entity = global_table.get(key); 
      if (entity instanceof Variable) {
        if (((Variable) entity).getType() instanceof ARRAY) {
          type = ((ARRAY) ((Variable) entity).getType()).getType();
        }
        else {
          err.report(12, ident.getLine(), ident.getPos());
        }
      }
      else {
        err.report(26, ident.getLine(), ident.getPos());
      }
    }
    else if (monitor) {
      if (monitor_table.containsKey(key)) {
        entity = monitor_table.get(key);
        if (entity instanceof Variable) {
          if (((Variable) entity).getType() instanceof ARRAY) {
            type = ((ARRAY) ((Variable) entity).getType()).getType();
          }
          else {
            err.report(12, ident.getLine(), ident.getPos());
          }
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
      else {
        entity = global_table.get(key);
        if (entity instanceof Variable) {
          if (((Variable) entity).getType() instanceof ARRAY) {
            type = ((ARRAY) ((Variable) entity).getType()).getType();
          }
          else {
            err.report(12, ident.getLine(), ident.getPos());
          }
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
    }
    else if (function) {
      if (local_table.containsKey(key)) {
        entity = local_table.get(key);
        if (entity instanceof Variable) {
          if (((Variable) entity).getType() instanceof ARRAY) {
            type = ((ARRAY) ((Variable) entity).getType()).getType();
          }
          else {
            err.report(12, ident.getLine(), ident.getPos());
          }
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
      else {
        entity = global_table.get(key);
        if (entity instanceof Variable) {
          if (((Variable) entity).getType() instanceof ARRAY) {
            type = ((ARRAY) ((Variable) entity).getType()).getType(); 
          }
          else {
            err.report(12, ident.getLine(), ident.getPos());
          }
        }
        else if (entity instanceof Function) {
          type = ((Function) entity).getType();
        }
        else {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
    }
    else {
      if (local_table.containsKey(key)) {
        entity = local_table.get(key);
        if (entity instanceof Variable) {
          if (((Variable) entity).getType() instanceof ARRAY) {
            type = ((ARRAY) ((Variable) entity).getType()).getType();
          }
          else {
            err.report(12, ident.getLine(), ident.getPos());
          }
        }
        else {
          err.report(12, ident.getLine(), ident.getPos());
        }
      }
      else if (previous_entity == MONITOR) {
         if(local_table.containsKey(key)) {
           entity = local_table.get(key);
           if (entity instanceof Variable) {
             if (((Variable) entity).getType() instanceof ARRAY) {
               type = ((ARRAY) ((Variable) entity).getType()).getType();
             }
             else {
               err.report(12, ident.getLine(), ident.getPos());
             }
           }
           else {
             err.report(12, ident.getLine(), ident.getPos());
           }
         }
         else if (monitor_table.containsKey(key)) {
           entity = monitor_table.get(key);
           if (entity instanceof Variable) {
             if (((Variable) entity).getType() instanceof ARRAY) {
               type = ((ARRAY) ((Variable) entity).getType()).getType();
             }
             else {
               err.report(12, ident.getLine(), ident.getPos());
             }
           }
           else {
             err.report(12, ident.getLine(), ident.getPos());
           }
         }
         else {
           entity = global_table.get(key);
           if (entity instanceof Variable) {
             if (((Variable) entity).getType() instanceof ARRAY) {
               type = ((ARRAY) ((Variable) entity).getType()).getType();
             }
             else {
               err.report(12, ident.getLine(), ident.getPos());
             }
           }
           else {
             err.report(12, ident.getLine(), ident.getPos()); 
           }
         }
       }
       else {
         if (local_table.containsKey(key)) {
           entity = local_table.get(key);
           if (entity instanceof Variable) {
             if (((Variable) entity).getType() instanceof ARRAY) {
               type = ((ARRAY) ((Variable) entity).getType()).getType();
             }
             else {
               err.report(12, ident.getLine(), ident.getPos());
             }
           }
           else {
             err.report(12, ident.getLine(), ident.getPos());
           }
         }
         else {
           entity = global_table.get(key);
           if (entity instanceof Variable) {
             if (((Variable) entity).getType() instanceof ARRAY) {
               type = ((ARRAY) ((Variable) entity).getType()).getType();
             }
             else {
               err.report(12, ident.getLine(), ident.getPos());
             }
           }
           else {
             err.report(12, ident.getLine(), ident.getPos());
           }
         }
       }
    }
  }
  
  /**
   * expression_list =
   *  {single} expression |
   *  puts the type of the expression into the table;
   */   
  public void outASingleExpressionList(ASingleExpressionList node) {
    exp_list.addElement(transExpression(node.getExpression()));
  }

  /**
   * expression_list =
   *   {sequence} expression_list comma expression |
   * @see TypeChecker.outASingleExpressionList
   */
  public void outASequenceExpressionList(ASequenceExpressionList node) {
    exp_list.addElement(transExpression(node.getExpression()));
  }
  
  /**
   * factor =
   *   {identifier} identifier |
   * @see TypeChecker.outAIdentifierVariable
   */
  public void outAIdentifierFactor(AIdentifierFactor node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (program) {
      if (!(global_table.get(key) instanceof Variable) &&
          !(global_table.get(key) instanceof Function) &&
          !(global_table.get(key) instanceof Constant)){
        err.report(26, ident.getLine(), ident.getPos());
      }
      else if (global_table.get(key) instanceof Function) {
        if (((Function) global_table.get(key)).raisesException() && !try_statement) {
          err.report(37, ident.getLine(), ident.getPos());
        }
      }
    }
    else if (monitor) {
      if (monitor_table.containsKey(key)) {
        if (!(monitor_table.get(key) instanceof Variable) &&
            !(monitor_table.get(key) instanceof Constant)) {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
      else {
        if (!(global_table.get(key) instanceof Variable) &&
            !(global_table.get(key) instanceof Function) &&
            !(global_table.get(key) instanceof Constant)) {
          err.report(26, ident.getLine(), ident.getPos());
        }
        else if (global_table.get(key) instanceof Function) {
          if (((Function) global_table.get(key)).raisesException() && !try_statement) {
            err.report(37, ident.getLine(), ident.getPos());
          }
        }
      }
    }
    else if (process) {
      if (local_table.containsKey(key)) {
        if (!(local_table.get(key) instanceof Variable) &&
            !(local_table.get(key) instanceof Constant)) {
          err.report(26, ident.getLine(), ident.getPos());
        }
      }
      else {
        if (!(global_table.get(key) instanceof Variable) &&
            !(global_table.get(k

⌨️ 快捷键说明

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