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

📄 gen.java

📁 用java语言做的L语言编译器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package gen;

import java.io.*;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;

import llanguage.BuildWindow;

import structureGen.GenStruct;

public class Gen {
	
	File desFile=new File("DestinationFile.txt");//目的文件
	
	private ArrayList<GenStruct> genStruct=new ArrayList<GenStruct>();

	private ArrayList<GenStruct> getGenStruct() {
		return genStruct;
	}
	public void startGen(){

		//预定义的四元式
		this.getGenStruct().add(new GenStruct(1,31,"a"," ","c"));//:=
		this.getGenStruct().add(new GenStruct(2,23,"a","b","c"));//+
		this.getGenStruct().add(new GenStruct(3,24,"a","b","c"));//-
		this.getGenStruct().add(new GenStruct(4,25,"a","b","c"));//*
		this.getGenStruct().add(new GenStruct(5,26,"a","b","c"));///
		this.getGenStruct().add(new GenStruct(6,33,"a","b",1));//<=
		this.getGenStruct().add(new GenStruct(7,34,"a","b",3));//<
		this.getGenStruct().add(new GenStruct(8,32,"a","b",5));//=
		this.getGenStruct().add(new GenStruct(9,35,"a","b",5));//<>
		this.getGenStruct().add(new GenStruct(10,36,"a","b",7));//>
		this.getGenStruct().add(new GenStruct(11,37,"a","b",9));//>=
		this.getGenStruct().add(new GenStruct(12,38," "," ",11));//j
		createGen();
	}
	private void createGen() {
		createDestinationFile();
		try {
			RandomAccessFile outDesFile=new RandomAccessFile(this.desFile,"rw");//创建随即读取文件GenStruct
			//以下是代码生成主逻辑
			int lineNum=1;//汇编程序行号
			int[] genNum=new int[genStruct.size()+1];//用于记录第i个四元式在汇编程序中的开始标号
			boolean bx=false;//false为可用
			boolean dx=false;
			outDesFile.writeBytes(".model small\r\n"+
                           ".stack\r\n"+
                           ".data\r\n"+
                           "	v db 0\r\n"+
                           ".code\r\n"+
                           "start: \r\n"+
                           "mov ax,@data\r\n"+
                           "mov ds,ax\r\n\r\n");
			for(Iterator it=genStruct.iterator();it.hasNext();){
				GenStruct gs=(GenStruct) it.next();
				bx=dx=false;
				genNum[gs.getLabel()]=lineNum;
				switch(gs.getCode()){
				case 31://:=
					if (bx==false){
						/*mov bx,a
						mov c,bx*/
						bx=true;
						outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
					}
					else if (dx==false){
						dx=true;
						outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",dx\r\n");
					}
					else {
						/*push
						pop*/
						dx=true;
						outDesFile.writeBytes(lineNum+++" : push bx\r\n");
						outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
						outDesFile.writeBytes(lineNum+++" : pop bx\r\n");
					}
					break;
				case 23://+
					if (bx==false && dx==false){
						bx=true;
						dx=true;
						outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : add bx,dx\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
					}
					else {
						if (bx==true && dx==true){
							outDesFile.writeBytes(lineNum+++" : push bx\r\n");
							outDesFile.writeBytes(lineNum+++" : push dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : add bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop dx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop bx\r\n");						
						}
						else if (bx==true){
							dx=true;
							outDesFile.writeBytes(lineNum+++" : push bx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : add bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop bx\r\n");
						}
						else{
							bx=true;
							outDesFile.writeBytes(lineNum+++" : push dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : add bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop dx\r\n");
						}
					}
					break;
				case 24://-
					if (bx==false && dx==false){
						bx=true;
						dx=true;
						outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : sub bx,dx\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
					}
					else {
						if (bx==true && dx==true){
							outDesFile.writeBytes(lineNum+++" : push bx\r\n");
							outDesFile.writeBytes(lineNum+++" : push dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : sub bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop dx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop bx\r\n");						
						}
						else if (bx==true){
							dx=true;
							outDesFile.writeBytes(lineNum+++" : push bx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : sub bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop bx\r\n");
						}
						else{
							bx=true;
							outDesFile.writeBytes(lineNum+++" : push dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : sub bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",bx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop dx\r\n");
						}
					}
					break;
				case 25://*
					if (bx==false){//8位乘
						bx=true;
						outDesFile.writeBytes(lineNum+++" : mov al,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov bl,"+gs.getVarB()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mul bl\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",ax\r\n");
					}
					else {
						outDesFile.writeBytes(lineNum+++" : push bx\r\n");
						outDesFile.writeBytes(lineNum+++" : mov al,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov bl,"+gs.getVarB()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mul bl\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",ax\r\n");
						outDesFile.writeBytes(lineNum+++" : pop bx\r\n");
					}
					break;
				case 26:///
					if (bx==false){//8位除,与数和商直接存入 result
						bx=true;
						outDesFile.writeBytes(lineNum+++" : mov ax,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarB()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : div bx\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",ax\r\n");
					}
					else {
						outDesFile.writeBytes(lineNum+++" : push bx\r\n");
						outDesFile.writeBytes(lineNum+++" : mov ax,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarB()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : div bx\r\n");
						outDesFile.writeBytes(lineNum+++" : mov "+gs.getResult()+",ax\r\n");
						outDesFile.writeBytes(lineNum+++" : pop bx\r\n");
					}
					break;
				case 33://<=
					if (bx==false && dx==false){
						bx=true;
						dx=true;
						outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
						outDesFile.writeBytes(lineNum+++" : cmp bx,dx\r\n");
						outDesFile.writeBytes(lineNum+++" : jng "+genNum[gs.getAddress()]+"\r\n");
					}
					else {
						if (bx==true && dx==true){
							outDesFile.writeBytes(lineNum+++" : push bx\r\n");
							outDesFile.writeBytes(lineNum+++" : push dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : cmp bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : jng "+genNum[gs.getAddress()]+"\r\n");
							outDesFile.writeBytes(lineNum+++" : pop dx\r\n");
							outDesFile.writeBytes(lineNum+++" : pop bx\r\n");						
						}
						else if (bx==true){
							dx=true;
							outDesFile.writeBytes(lineNum+++" : push bx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : cmp bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : jng "+genNum[gs.getAddress()]+"\r\n");
							outDesFile.writeBytes(lineNum+++" : pop bx\r\n");
						}
						else{
							bx=true;
							outDesFile.writeBytes(lineNum+++" : push dx\r\n");
							outDesFile.writeBytes(lineNum+++" : mov bx,"+gs.getVarA()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : mov dx,"+gs.getVarB()+"\r\n");
							outDesFile.writeBytes(lineNum+++" : cmp bx,dx\r\n");
							outDesFile.writeBytes(lineNum+++" : jng "+genNum[gs.getAddress()]+"\r\n");
							outDesFile.writeBytes(lineNum+++" : pop dx\r\n");
						}
					}
					break;
				case 34://<
					if (bx==false && dx==false){
						bx=true;

⌨️ 快捷键说明

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