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

📄 javasourcegenerator.java

📁 java实现的一个pascal编译器
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
      out.println("    }");
      
      if (node.getExceptionBlock() != null) {
        node.getExceptionBlock().apply(this);
      }
  }

  /**
   * {raise} raise identifier |
   */
  public void outARaiseStatement(ARaiseStatement node) {
    Variable var;
    String key = node.getIdentifier().getText().toUpperCase();
    var = (Variable) global_table.get(key);
    out.println("    throw new " + var.getImage() + "();");
  }

  /**
   * {wait} wait on identifier |
   */
  public void outAWaitStatement(AWaitStatement node) {
    String key = node.getIdentifier().getText().toUpperCase(),
           image;
    if (previous_entity == MONITOR) {
      image = ((Variable) monitor_table.get(key)).getImage();
    }
    else {
      image = ((Variable) global_table.get(key)).getImage();
    }
    out.println("    " + image + ".Wait();");
  }

  /**
   * {signal} signal on identifier |
   */
  public void outASignalStatement(ASignalStatement node) {
    String key = node.getIdentifier().getText().toUpperCase(),
           image;
 
    if (previous_entity == MONITOR) {
       if (monitor_table.containsKey(key)) {
         image = ((Variable) monitor_table.get(key)).getImage();
       }
       else {
         image = ((Variable) global_table.get(key)).getImage();
       }
    }
    else {
      image = ((Variable) global_table.get(key)).getImage();
    }
    out.println("    " + image + ".Signal();");
  }

  /**
   * {sleep} sleep for time_value  |
   */
  public void outASleepStatement(ASleepStatement node) {
      out.println("    try {");
      out.println("      java.lang.Thread.sleep(" + value + ");");
      out.println("    }");
      out.println("    catch(InterruptedException e) {");
      out.println("      System.out.println(e);");
      out.println("     }");
  }


  /**
   * exception_handler =
   * when identifier do statement semicolon ;
   */
  public void caseAExceptionHandler(AExceptionHandler node) {
    String exception_image,
           key = node.getIdentifier().getText().toUpperCase();
    exception_image = ((Variable) global_table.get(key)).getImage();
    out.println("    catch (" + exception_image + " exc"+ ") {");
    if (node.getStatement() != null) {
      node.getStatement().apply(this);
    }
    out.println("    }");
  }

  /**
   * default_handler =
   * {empty} statement_sequence end |
   * {present} else statement_sequence end ;
   */
  public void inAPresentDefaultHandler(APresentDefaultHandler node) {
    out.println("    catch (java.lang.Exception exc) {");
  }

  public void outAPresentDefaultHandler(APresentDefaultHandler node) {
    out.println("    }");
  }

  public void inAEmptyDefaultHandler(AEmptyDefaultHandler node) {
    out.println("    finally {");
  }

  public void outAEmptyDefaultHandler(AEmptyDefaultHandler node) {
    out.println("    }");
  }

  /**
   * writeln_stmt =
   * {simple} writeln |
   */
  public void outASimpleWritelnStmt(ASimpleWritelnStmt node) {
    out.println("    uk.edu.sbu.seeie.jpascal.lang.System.out.writeln();");
  }

  /**
   * {arguments} writeln l_paren expression_list r_paren ;
   */
  public void inAArgumentsWritelnStmt(AArgumentsWritelnStmt node) {
    out.print("    uk.edu.sbu.seeie.jpascal.lang.System.out.writeln(");
    write_stmt = true;
  }

  public void outAArgumentsWritelnStmt(AArgumentsWritelnStmt node) {
    out.println(");");
    write_stmt = false;
  }

  /**
   * write_stmt =
   *  {simple} write |
   */
  public void outASimpleWriteStmt(ASimpleWriteStmt node) {
      out.println("    uk.edu.sbu.seeie.jpascal.lang.System.out.write();");
  }

  /**
   * {arguments} write l_paren expression_list r_paren ;
   */
  public void inAArgumentsWriteStmt(AArgumentsWriteStmt node) {
    out.print("    uk.edu.sbu.seeie.jpascal.lang.System.out.write(");
    write_stmt = true;
  }

  public void outAArgumentsWriteStmt(AArgumentsWriteStmt node) {
    out.println(");");
    write_stmt = false;
  }

  /**
   * readln_stmt =
   * {simple} readln |
   */
  public void outASimpleReadlnStmt(ASimpleReadlnStmt node) {
     System.out.println("    uk.edu.sbu.seeie.jpascal.lang.System.in.readln();");
  }

  /**
   * clears the idlist
   */
  public void inAArgumentsReadlnStmt(AArgumentsReadlnStmt node) {
    if (!idlist.isEmpty()) {
      idlist.removeAllElements();
    }
  }

  /**
   * {arguments} readln l_paren identifier_list r_paren ;
   */
  public void outAArgumentsReadlnStmt(AArgumentsReadlnStmt node) {
      String id_name = "", key;
      String _type = "";
     
      for (Enumeration e = idlist.elements(); e.hasMoreElements();) {
        key = ((String) e.nextElement()).toUpperCase();
        if (program) {
          id_name = ((Variable) global_table.get(key)).getImage();
          _type = ((Variable) global_table.get(key)).getType();
        }
        else if (monitor) {
          if (monitor_table.containsKey(key)) {
            id_name = ((Variable) monitor_table.get(key)).getImage();
            _type = ((Variable) monitor_table.get(key)).getType();
          }
          else {
            id_name = ((Variable) monitor_table.get(key)).getImage();
            _type = ((Variable) monitor_table.get(key)).getType();
          }
        }
        else {
          if (local_table.containsKey(key)) {
            id_name = ((Variable) local_table.get(key)).getImage();
            _type = ((Variable) local_table.get(key)).getType();
          }
          else {
            id_name = ((Variable) global_table.get(key)).getImage();
            _type = ((Variable) global_table.get(key)).getType();
          }
        }
        out.print("    " + id_name + " = " + "uk.edu.sbu.seeie.jpascal.lang.System.in.read");
        if (_type.equals("int")) {
          out.println("Int();");
        }
        else if (_type.equals("double")) {
          out.println("Double();");  
        }
        else if (_type.equals("char")) {
          out.println("Char();");
        }
        else if (_type.equals("java.lang.String")) {
          out.println("String();");
        }
      }
    }

 /**
  *  procedure_call =
  *  {identifier} identifier |
  */
 public void outAIdentifierProcedureCall(AIdentifierProcedureCall node) {
   String key = node.getIdentifier().getText().toUpperCase(),
	 id_name = "";
   Object entity;
   if (program) {
     entity = global_table.get(key);
     if (entity instanceof Procedure) {
       id_name = ((Procedure) entity).getImage();
     }
     else {
       id_name = ((Function) entity).getImage();
     }
     out.print("    ");
   }
   else if (monitor) {
     if (monitor_table.containsKey(key)) {
       entity = monitor_table.get(key);
       if (entity instanceof Procedure) {
         id_name = ((Procedure) entity).getImage();
       }
       else {
         id_name = ((Function) entity).getImage();
       }
     }
     else {
       entity = global_table.get(key);
       if (entity instanceof Procedure) {
         id_name = ((Procedure) entity).getImage();
       }
       else {
         id_name = ((Function) entity).getImage();
       }
     }
   }
   else {
      if (monitor_call) {
        entity = monitor_procs.get(key);
        id_name = ((Procedure) entity).getImage();
      }
      else {
        entity = global_table.get(key);
        if (entity instanceof Procedure) {
           id_name = ((Procedure) entity).getImage();
        }
        else {
          id_name = ((Function) entity).getImage();
        }
      }
   }
   out.println(id_name + "();");
 }

  /**
   *   {arguments} identifier l_paren expression_list r_paren ;
   */
  public void inAArgumentsProcedureCall(AArgumentsProcedureCall node) {
    String key = node.getIdentifier().getText().toUpperCase(),
           id_name = "";
    Object entity = null;

    if (program) {
      entity = global_table.get(key);
      if (entity instanceof Procedure) {
        id_name = ((Procedure) entity).getImage();
      }
      else {
        id_name = ((Function) entity).getImage();
      }
    }
    else if (monitor) {
      if (monitor_table.containsKey(key)) {
        id_name = ((Procedure) monitor_table.get(key)).getImage();
      }
      else {
        entity = global_table.get(key);
        if (entity instanceof Procedure) {
          id_name = ((Procedure) entity).getImage();
        }
        else {
          id_name = ((Function) entity).getImage();
        }
      }
    }
    else {
      if (monitor_call) {
        id_name = ((Procedure) monitor_procs.get(key)).getImage();
      }
      else {
        entity = global_table.get(key);
        if (entity instanceof Procedure) {
          id_name = ((Procedure) entity).getImage();
        }
        else {
          id_name = ((Function) entity).getImage();
        }
      }
    }
    out.print("    " + id_name + "(");
  }

   public void outAArgumentsProcedureCall(AArgumentsProcedureCall node) {
     out.println(");");
   }

  /**
   * channel_variable =
   *  identifier ;
   */
   public void outAChannelVariable(AChannelVariable node) {
      String key = node.getIdentifier().getText().toUpperCase();
      String id_name = ((Variable) global_table.get(key)).getImage();
      if (send_stmt) {
        out.print("    " + id_name);
      }
      else {
        out.print(" " + id_name);
      }
   }

  public void inASendStatement(ASendStatement node) {
    send_stmt = true;
  }

  /**
   * statement =
   *  {send} send message_value to channel_variable |
   */
  public void outASendStatement(ASendStatement node) {
    out.println(".send(" + const_value + ");");
    send_stmt = false;
  }
 
  /**
   * generates code for the yield operation
   */
  public void outAYieldStatement(AYieldStatement node) {
    out.println("    java.lang.Thread.yield();");
  }

  /**
   * {receive} receive identifier from channel_variable |
   */
  public void inAReceiveStatement(AReceiveStatement node) {
    String key = node.getIdentifier().getText().toUpperCase(),
           id_dest;
    
    if (local_table.containsKey(key)) {
      id_dest = ((Variable) local_table.get(key)).getImage();
    }
    else {
      id_dest = ((Variable) global_table.get(key)).getImage();
    }
    out.print("    " + id_dest + " = ");
  }

  public void outAReceiveStatement(AReceiveStatement node) {
    out.println(".receive();");
  }

  /**
   * {monitor_call} identifier dot procedure_call;
   */
  public void inAMonitorCallStatement(AMonitorCallStatement node) {
    String key = node.getIdentifier().getText().toUpperCase(),
           image = ((Monitor) global_table.get(key)).getImage();
    out.print("      " + image + ".");
    monitor_call = true;    
  }

  /**
   * sets monitor call to false
   */
  public void outAMonitorCallStatement(AMonitorCallStatement node) {
    monitor_call = false;
  }

  /**
   * value =
   *  {identifier} identifier |
   */
  public void outAIdentifierValue(AIdentifierValue node) {
      String key = node.getIdentifier().getText().toUpperCase();

      if (local_table.containsKey(key)) {
        value = ((Variable) local_table.get(key)).getImage();
      }
      else { 
        value = ((Variable) global_table.get(key)).getImage();
      }
      
  }

  /**
   * {integer} integer_literal ;
   */
  public void outAIntegerValue(AIntegerValue node) {
    value = node.getIntegerLiteral().getText();
    
  }


  // expressions

  /**
   * expression_list =
   *  {single} expression |
   *  {sequence} expression_list comma expression ;
   */
  public void caseASequenceExpressionList(ASequenceExpressionList node) {
      if (node.getExpressionList() != null) {
        node.getExpressionList().apply(this);
      }
      if (write_stmt) {
        out.print(" + ");
      }
      else {
        out.print(", ");
      }
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
  }

  /**
   * expression =
   *  {signedterm} sign term |
   */
  public void inASignedTermExpression(ASignedTermExpression node) {
    if (node.getSign() instanceof APlusSign) {
      out.print(" + ");
    }
    else {
      out.print(" - ");
    }
  }

  // binary operators ( arithmetic and boolean )
  /**
   *  {plus} expression plus term |
   */
  public void caseAPlusExpression(APlusExpression node) {
    
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" + ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * {minus} expression minus term |
   */
  public void caseAMinusExpression(AMinusExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" - ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * {or} expression or term |
   */
  public void caseAOrExpression(AOrExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" && ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   *  {xor} expression xor term |
   */
  public void caseAXorExpression(AXorExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" ^ ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * {equal} expression equal term |
   */
  public void caseAEqualExpression(AEqualExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" == ");
      if (node.getTerm() != null) {

⌨️ 快捷键说明

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