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

📄 code.java

📁 java语言开发的基于tiny语言的编译器
💻 JAVA
字号:

package jeex.tiny;

import java.util.Vector;
import java.util.List;
import java.io.*;
/**
 * Generated code.
 */
class Code {
	// register number
	static int PC =  7; 
	static int AC =  0;
	static int AC1 = 1;
	static int MP =  6;
	static int GP =  5;
	
	// Container of instruction
	List container = new Vector();
	
	int emitLoc = 0; // current emit loc
	int highEmitLoc = 0; 
	int emitIndex = 0;// current emit index in vector
	int highEmitIndex = 0;
	
	Code() {
		super();
	}
	
	void emitComment(String comment) {
		if (emitIndex == highEmitIndex) {
			container.add("\t*" + comment);
		} else {
			container.set(emitIndex , "\t*" + comment);
		}
		emitIndex ++;
		updateHigh();
	}

	void emitRO(String op, int r, int s, int t, String comment) {
		String line = op + " " + r + "," + s + "," + t 
			+ "\t" + ";" +emitLoc + "\t" + comment;	
		emit0(line);
	}
	
	void emitRM(String op, int r, int d, int s, String comment) {
		String line = op + " " + r + "," + d + "(" + s + ")"
			+ "\t" + ";" +emitLoc + "\t" + comment;	
		emit0(line);
	}
	
	void emitRMAbs(String op, int r, int a, String comment){
		String line = op + " " + r + "," + (a - (emitLoc + 1) +","+ PC)
			+ "\t" + ";" +emitLoc + "\t" + comment;	
		emit0(line);
	}
	
	EmitLoc emitSkip(int howMany) {
		int loc = emitLoc;
		int index = emitIndex;
		for(int i = 0;i < howMany; i ++)
			container.add(null);
		emitLoc += howMany;
		emitIndex += howMany;
	   updateHigh();
		return new EmitLoc(loc,index);
	}
	
	void emitRestore() {
		emitLoc = highEmitLoc;
		emitIndex = highEmitIndex;
	}
	
	int currentEmitLoc() {
		return emitLoc;
	}
	
	void emitBackup(EmitLoc el) {
		emitLoc = el.loc;
		emitIndex = el.index;
	}
		
	/**
	 * Writes the instruction set into a text file.
	 */
	void write(String filename) {
		PrintWriter out = null;
		try {
			out = 
				new PrintWriter(
					new BufferedWriter(
						new FileWriter(filename)));
			//>>
			for(int i = 0; i < container.size(); i ++) {
				out.println(container.get(i));
			}
			//<<
			out.flush();
		} catch(IOException e) {
			System.out.println("Write file error:" + e);
			if(out != null) 
				out.close(); 
		}	
	}
	/**
	 * Returns the code container.
	 */
	List getCodes() {
		return container;
	}
	/**
	 * Emit one line code.
	 */
	private void emit0(String line) {
		if (emitIndex == highEmitIndex) {
			container.add(line);
		} else {
			container.set(emitIndex,line);
		}
		emitLoc ++;
		emitIndex ++;
		updateHigh();	
	}
	/**
	 * Updates highEmitLoc and highEmitIndex.
	 */
	private void updateHigh() {
		if (highEmitLoc < emitLoc)  highEmitLoc = emitLoc ;
		if (highEmitIndex < emitIndex) highEmitIndex = emitIndex;		
	}
	
	/**
	 * Map of emitLoc and index of the container.
	 */
	static class EmitLoc {
		int loc;
		int index;
		EmitLoc(int loc,int index) {
			this.loc = loc;
			this.index = index;
		}
	}
}

⌨️ 快捷键说明

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