📄 localfile.java
字号:
package scriptedit;import java.util.*;import scriptedit.data.*;import java.io.*;/** * <p>Title: Local File</p> * <p>Description: 2006年4月6日</p> * <p>Copyright: CoCoMo Copyright (c) 2006</p> * <p>Company: 9you</p> * @author 郭昉 * @version 1.0 */public class LocalFile { public static Vector temList = new Vector(); public static int[] temCount; //终结符数量 public static final String HEAD_BLOCK = "CoCoMo"; public LocalFile() { } public static void writeData() { try { } catch(Exception e) { e.printStackTrace(); } } public static void writeData(String file) throws IOException{ DataOutputStream out = new DataOutputStream(new FileOutputStream(file, false)); // Head ------------------------------------------------------------------------ out.writeUTF(HEAD_BLOCK); // Variable -------------------------------------------------------------------- Vector vector = DataStore.getVarList(); int size = vector.size(); out.writeShort(size); for (int i = 0; i < size; i++) { Variable var = (Variable) vector.elementAt(i); int value = Integer.parseInt(var.getValue()); out.writeShort(value); } // Script Count------------------------------------------------------------------- vector = DataStore.getScrList(); size = vector.size(); out.writeShort(size); // Script Data -------------------------------------------------------------------- ByteArrayOutputStream[] baosData = new ByteArrayOutputStream[size]; ByteArrayOutputStream[] baosIns = new ByteArrayOutputStream[size]; DataOutputStream[] dosData = new DataOutputStream[size]; //数据缓冲 DataOutputStream[] dosIns = new DataOutputStream[size]; //指令缓冲 temCount = new int[size]; for(int i = 0; i < size; i++) { baosData[i] = new ByteArrayOutputStream(); baosIns[i] = new ByteArrayOutputStream(); dosData[i] = new DataOutputStream(baosData[i]); dosIns[i] = new DataOutputStream(baosIns[i]); temList.removeAllElements(); Script script = (Script) vector.elementAt(i); Vector insVector = script.getInsList(); int size2 = insVector.size(); dosIns[i].writeShort(size2); for(int j = 0; j < size2; j++) { Instruction ins = (Instruction) insVector.elementAt(j); int id = ins.getID(); dosIns[i].writeByte(id); // Parameter------------------------------------------------------------------- if(id == Instruction.CALU_ID) { //三参数指令 Teminal t = ins.getNum1(); int index = searchTeminal(t); dosIns[i].writeShort(index); t = ins.getOp(); index = searchTeminal(t); dosIns[i].writeShort(index); t = ins.getNum2(); index = searchTeminal(t); dosIns[i].writeShort(index); } else if(id == Instruction.PCALL_ID) { //双参数指令 Teminal t = ins.getNum1(); int index = searchTeminal(t); dosIns[i].writeShort(index); t = ins.getNum2(); index = searchTeminal(t); dosIns[i].writeShort(index); } else if(id == Instruction.POPU_ID || id == Instruction.PUSH_ID || id == Instruction.JUMP_ID || id == Instruction.IF_ID) { //单参数指令 Teminal t = ins.getNum1(); int index = searchTeminal(t); dosIns[i].writeShort(index); } else { //无参数指令 break continue return stop } } //写出Teminal temCount[i] = temList.size(); for(int j = 0; j < temCount[i]; j++) { Teminal t = (Teminal) temList.elementAt(j); writeTeminal(dosData[i], t); } } // Offset------------------------------------------------------------------- vector = DataStore.getScrList(); size = vector.size(); int len = (size - 1) * 2; out.writeShort(len); for (int i = 1; i < size; i++) { byte[] data = baosData[i - 1].toByteArray(); byte[] ins = baosIns[i - 1].toByteArray(); len += data.length + ins.length - 2 + 2; //-2为offset长度 +2为终结符个数 out.writeShort(len); } for (int i = 0; i < size; i++) { byte[] data = baosData[i].toByteArray(); byte[] ins = baosIns[i].toByteArray(); out.writeShort(temCount[i]); out.write(data); out.write(ins); } out.flush(); out.close(); out = null; } private static void writeTeminal(DataOutputStream out, Teminal t) throws IOException { String strValue; Vector vector; int size, type; type = t.getType(); out.writeByte(type); switch (type) { case Teminal.IDENTIFER: //常量替换 变量地址写入 函数号写入 //常量查找 vector = DataStore.getConList(); size = vector.size(); for(int i = 0; i < size; i++) { Constant con = (Constant) vector.elementAt(i); if(t.getValue().equals(con.getName())) { out.writeShort(Integer.parseInt(con.getValue())); return; } } //变量查找 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())) { out.writeShort(i); return; } } //函数查找 vector = DataStore.getFunList(); size = vector.size(); for(int i = 0; i < size; i++) { Function fun = (Function) vector.elementAt(i); if(t.getValue().equals(fun.getName())) { out.writeShort(Integer.parseInt(fun.getID())); return; } } //脚本查找 vector = DataStore.getScrList(); size = vector.size(); for(int i = 0; i < size; i++) { Script scr = (Script) vector.elementAt(i); if(t.getValue().equals(scr.getName())) { out.writeShort(i); return; } } //关键字查找 if(Function.isKeyWord(t.getValue())) { out.writeShort(Function.getKeyWordID(t.getValue())); return; } throw new IOException("writeTeminal() -> Cann't Match IDENTIFER " + t.getValue()); case Teminal.INTEGER: //Integer out.writeShort(Integer.parseInt(t.getValue())); break; case Teminal.STRING: //String strValue = t.getValue(); out.writeShort(strValue.length()); for (int i = 0; i < strValue.length(); i++) { out.writeChar(strValue.charAt(i)); } break; case Teminal.BOOLEAN: if(t.getValue().equals(Teminal.TRUE)) { out.writeByte(1); } else if(t.getValue().equals(Teminal.FALSE)) { out.writeByte(0); } break; case Teminal.OSYMBOL: //操作符 case Teminal.RSYMBOL: //关系符 out.writeByte(Integer.parseInt(t.getValue())); break; case Teminal.SCRIPT: //写入脚本号 vector = DataStore.getScrList(); size = vector.size(); for (int i = 0; i < size; i++) { Script script = (Script) vector.elementAt(i); if (t.getValue().equals(script.getName())) { out.writeShort(i); return; } } break; } } //t1为被检查对象t private static int searchTeminal(Teminal t) { int size = temList.size(); for(int i = 0; i < size; i++) { Teminal tem = (Teminal) temList.elementAt(i); if(t.getType() == tem.getType()) { if(t.getValue().equals(tem.getValue())) { return i; } } } temList.add(t); return size; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -