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

📄 check.java

📁 脚本开发工具和源代码,用于J2me中RPG游戏的脚本开发和执行,相当好用的一套工具,并含有脚本开发工具的源代码和一个现成的游戏脚本
💻 JAVA
字号:
package scriptedit;

import java.util.*;
import scriptedit.data.*;
/**
 * <p>Title: Check</p>
 * <p>Description: semantic check</p>
 * <p>Copyright: CoCoMo Copyright (c) 2006</p>
 * <p>Company: 9you</p>
 * @author 郭昉
 * @version 1.1
 */

public class Check {
  public Check() {
  }

  public static boolean check() throws Exception {
    if (rule_1() && rule_2() && rule_3() && rule_4() && rule_5() && rule_6()) {
      return true;
    }
    return false;
  }

  //常量名不能与其他常量,变量重复,函数重名,不能为关键字
  public static boolean rule_1() throws Exception {
    Constant con1, con2;
    Vector vector = DataStore.getConList();
    int size = vector.size();
    //与其他常量
    for (int i = 0; i < size - 1; i++) {
      con1 = (Constant) vector.elementAt(i);
      for (int j = i + 1; j < size; j++) {
        con2 = (Constant) vector.elementAt(j);
        if (con1.getName().equals(con2.getName())) {
          throw new Exception("The Constant " + con1.getName() + " Can't Be Same As Others!");
        }
      }
    }

    //与变量及函数,关键字重名
    for(int i = 0; i < size; i++)
    {
      con1 = (Constant) vector.elementAt(i);
      Vector varVector = DataStore.getVarList();
      int size2 = varVector.size();
      for(int j = 0; j < size2; j++)
      {
        Variable var = (Variable) varVector.elementAt(j);
        if(con1.getName().equals(var.getName())) {
          throw new Exception("The Constant " + con1.getName() + " Can't Be Same As Variable!");
        }
      }

      Vector funVector = DataStore.getFunList();
      size2 = funVector.size();
      for(int j = 0; j < size2; j++)
      {
        Function fun = (Function) funVector.elementAt(j);
        if(con1.getName().equals(fun.getName())) {
          throw new Exception("The Constant " + con1.getName() + " Can't Be Same As Function!");
        }
      }

      if(Function.isKeyWord(con1.getName())) {
        throw new Exception("The Constant " + con1.getName() + " Can't Be Same As Key Word!");
      }
    }
    return true;
  }

  //变量不能与其他变量重名, 函数重名,不能为关键字
  public static boolean rule_2() throws Exception {
    Variable var1, var2;
    Vector vector = DataStore.getVarList();
    int size = vector.size();
    //与其他变量
    for (int i = 0; i < size - 1; i++) {
      var1 = (Variable) vector.elementAt(i);
      for (int j = i + 1; j < size; j++) {
        var2 = (Variable) vector.elementAt(j);
        if (var1.getName().equals(var2.getName())) {
          throw new Exception("The Variable " + var1.getName() + " Can't Be Same As Others!");
        }
      }

    }

    //与常量及函数,关键字
    for(int i = 0; i < size; i++)
    {
      var1 = (Variable) vector.elementAt(i);
      Vector funVector = DataStore.getFunList();
      int size2 = funVector.size();
      for(int j = 0; j < size2; j++)
      {
        Function fun = (Function) funVector.elementAt(j);
        if(var1.getName().equals(fun.getName())) {
          throw new Exception("The Variable " + var1.getName() + " Can't Be Same As Function!");
        }
      }

      if(Function.isKeyWord(var1.getName())) {
        throw new Exception("The Variable " + var1.getName() + " Can't Be Same As Key Word!");
      }
    }
    return true;
  }

  //函数不能与其他函数重名,不能为关键字
  public static boolean rule_3() throws Exception {
    Function fun1, fun2;
    Vector vector = DataStore.getFunList();
    int size = vector.size();
    for (int i = 0; i < size - 1; i++) {
      fun1 = (Function) vector.elementAt(i);

      if(Function.isKeyWord(fun1.getName())) {
        throw new Exception("The Function " + fun1.getName() + " Can't Be Same As Key Word!");
      }

      for (int j = i + 1; j < size; j++) {
        fun2 = (Function) vector.elementAt(j);
        if (fun1.getName().equals(fun2.getName())) {
          throw new Exception("The Function " + fun1.getName() + " Can't Be Same As Others!");
        }
      }
    }
    return true;
  }

  //脚本不能重名
  public static boolean rule_4() throws Exception {
    Script scr1, scr2;
    Vector vector = DataStore.getScrList();
    int size = vector.size();
    for (int i = 0; i < size - 1; i++) {
      scr1 = (Script) vector.elementAt(i);
      for (int j = i + 1; j < size; j++) {
        scr2 = (Script) vector.elementAt(j);
        if (scr1.getName().equals(scr2.getName())) {
          throw new Exception("The Script " + scr1.getName() + " Can't Be Same As Others!");
        }
      }
    }
    return true;
  }

  //函数号不能重复
  public static boolean rule_5() throws Exception {
    Function fun1, fun2;
    Vector vector = DataStore.getFunList();
    int size = vector.size();
    for (int i = 0; i < size - 1; i++) {
      fun1 = (Function) vector.elementAt(i);
      for (int j = i + 1; j < size; j++) {
        fun2 = (Function) vector.elementAt(j);
        if (fun1.getID().equals(fun2.getID())) {
          throw new Exception("The Function No. " + fun1.getID() + " Can't Be Same As Others!");
        }
      }
    }
    return true;
  }

  //指令检查
  public static boolean rule_6() throws Exception {
    Script script;
    Vector scrVector = DataStore.getScrList();
    int scrSize = scrVector.size();
    for (int i = 0; i < scrSize; i++) {
      script = (Script) scrVector.elementAt(i);
      Vector insVector = (Vector) script.getInsList();
      int insSize = insVector.size();
      Instruction preIns = null; //保留前一指令
      for (int m = 0; m < insSize; m++) {
        Instruction ins = (Instruction) insVector.elementAt(m);
        String name = ins.getName();
        if (name.equals(Instruction.PUSH) ||
            name.equals(Instruction.CALP)) {
          Teminal t1 = ins.getNum1();
          if(t1 == null) {
            throw new Exception("The Instruction " + Instruction.PUSH + " Syntax Error!");
          }
          else if(!checkTeminal(t1)) {
            if(t1.getType() == Teminal.IDENTIFER) {
              throw new Exception("The Parameter " + t1.getValue() + " At Line:" + t1.getLine() + " Haven't Been Declared!");
            }
            else if(t1.getType() == Teminal.SCRIPT) {
              throw new Exception("The Script " + t1.getValue() + " At Line:" + t1.getLine() + " Haven't Been Declared!");
            }
          }
          Teminal t2 = ins.getNum2();
          if(t2 == null) {
            //do nothing
          }
          else if(!checkTeminal(t2)) {
            if(t2.getType() == Teminal.IDENTIFER) {
              throw new Exception("The Parameter " + t2.getValue() + " At Line:" + t2.getLine() + " Haven't Been Declared!");
            }
            else if(t2.getType() == Teminal.SCRIPT) {
              throw new Exception("The Script " + t2.getValue() + " At Line:" + t2.getLine() + " Haven't Been Declared!");
            }
          }
        }
        else if (name.equals(Instruction.PCALL)) { //PCALL指令检查
          Teminal t = ins.getNum1();
          if(!(t != null && checkTeminal(t, preIns))) {
            if(t.getValue().equals(Function.KEYWORD_CALL) ||
               t.getValue().equals(Function.KEYWORD_LOADSCRIPT) ||
               t.getValue().equals(Function.KEYWORD_UNLOADSCRIPT)) {
              if(!(preIns != null && preIns.getName().equals(Instruction.PUSH))) {
                System.out.println("The Function " + t.getValue() + " At Line:" + t.getLine() + " Must Have one Parameter!" );
              }
              else {
                Teminal t1 = preIns.getNum1();
                if(t1.getType() != Teminal.SCRIPT) {
                  throw new Exception("The Fuction " + t.getValue() + " At Line:" + t.getLine() + "'s Parameter Type Must Be SCRIPT Struct!");
                }
              }
            }
            else {
              throw new Exception("The Function " + t.getValue() + " At Line:" + t.getLine() +  " Haven't Been Declared!");
            }
          }
        }
        preIns = ins;
      }
    }
    return true;
  }

  //终结符如果是标识符类型,标识符应该被定义
  //终结符如果是脚本类型,脚本应该被定义
  public static boolean checkTeminal(Teminal t) {
    if(t.getType() == Teminal.IDENTIFER) {
      //常量查找
      Vector vector = DataStore.getConList();
      int size = vector.size();
      for(int i = 0; i < size; i++)
      {
        Constant con = (Constant) vector.elementAt(i);
        if(t.getValue().equals(con.getName())) {
          return true;
        }
      }

      //变量查找
      vector = DataStore.getVarList();
      size = vector.size();
      for(int i = 0; i < size; i++)
      {
        Variable var = (Variable) vector.elementAt(i);
        if(t.getValue().equals(var.getName())) {
          return true;
        }
      }
      return false;
    }
    else if(t.getType() == Teminal.SCRIPT) {
      //脚本查找
      Vector vector = DataStore.getScrList();
      int size = vector.size();
      for(int i = 0; i < size; i++)
      {
        Script scr = (Script) vector.elementAt(i);
        if(t.getValue().equals(scr.getName())) {
          return true;
        }
      }
      return false;
    }
    return true;
  }

  //pcall检查:
  //调用函数必须被定义
  //如果是call, LoadScript, UnLoadScrit函数
  //前一个指令参数必须为脚本类型
  public static boolean checkTeminal(Teminal t, Instruction preIns) {
    //保留指令
    if(t.getValue().equals(Function.KEYWORD_CALL) ||
       t.getValue().equals(Function.KEYWORD_LOADSCRIPT) ||
       t.getValue().equals(Function.KEYWORD_UNLOADSCRIPT)) {
      if(preIns != null && preIns.getName().equals(Instruction.PUSH)) {
        Teminal t1 = preIns.getNum1();
        if(t1.getType() == Teminal.SCRIPT) {
          return true;
        }
      }
      return false;
    }
    else {
      //函数查找
      Vector vector = DataStore.getFunList();
      int size = vector.size();
      for(int i = 0; i < size; i++)
      {
        Function fun = (Function) vector.elementAt(i);
        if(t.getValue().equals(fun.getName())) {
          return true;
        }
      }
      return false;
    }
  }
}

⌨️ 快捷键说明

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