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

📄 execution.java

📁 JAVA的CMM编译器(词法+语法+语义+输出结果)
💻 JAVA
字号:

/*
 * Execution.java
 *
 * Created on 2007年12月28日, 下午2:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cmm;
import java.util.Stack;
import java.util.LinkedList;
//import java.awt.*;
//import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author zxy
 */
public class Execution extends JFrame{
    //运行栈
    private Stack running;
    //运行栈中变量的类型
    private Stack type;
    //名字表
    private LinkedList name_table;
    //目标代码
    private LinkedList aim_code;
    //分析程序的实例
    private Analyse analyse;
    //目标代码的编号
    private int index;
    private Cmm cmm;
    
    /** Creates a new instance of Execution */
    public Execution() {
    }
    /* 创建一个Execution的实例*/
    public Execution(Analyse a, Cmm c) {
        this();
        running = new Stack();
        type = new Stack();
        analyse = a;
        aim_code = analyse.getCode();
        name_table = analyse.getTable();
        index = 0;
        cmm = c;
        cmm.output2.setText("");
    }  
    
    /*解释目标代码*/
   public void Run() {
       do {
           //读取一条目标代码
           Instruction instruction = (Instruction)aim_code.get(index);
           //处理目标代码lit
           if (instruction.getIns() == InsType.lit){
               if (instruction.getFlag()) {
                   running.push(instruction.getA_r());
                   type.push("real");
               }else {
                       running.push(instruction.getA_i());
                       type.push("int");
               }
               index++;
           }else //处理目标代码lod
           if (instruction.getIns() == InsType.lod) {
               int address = instruction.getA_i();
               Variable v = (Variable)name_table.get(address);
               //处理数组
               if (v.getIsArray()) {
                   index ++;
                   instruction = (Instruction)aim_code.get(index);
                   if (instruction.getIns() == InsType.lit) {
                       address = address + instruction.getA_i();
                   }
                   if (instruction.getIns() == InsType.lod) {
                       v = (Variable)name_table.get(instruction.getA_i());
                       address = address + v.getValue();
                   }
               }
               v = (Variable)name_table.get(address);
               if (v.getFlag()) {
                   running.push(v.getValue_r());
                   type.push("real");
               }else {
                   running.push(v.getValue());
                   type.push("int");
               }
               index ++;
           }else  //处理目标代码sto
           if (instruction.getIns() == InsType.sto) {
               
               int address = instruction.getA_i();
               Variable v = (Variable)name_table.get(address);
               //处理数组
               if (v.getIsArray()) {
                   index ++;
                   instruction = (Instruction)aim_code.get(index);

                           
        if (instruction.getIns() == InsType.lit) {
                       address = address + instruction.getA_i();
                   }
                   if (instruction.getIns() == InsType.lod) {
                       v = (Variable)name_table.get(instruction.getA_i());
                       address = address + v.getValue();
                   }
               }
               v = (Variable)name_table.get(address);
               int value1 = 0;
               double value = 0;
               if (v.getFlag()) {
                   value = Double.parseDouble(String.valueOf(running.peek()));
               } else {
                  
                   value = Double.parseDouble(String.valueOf(running.peek()));
                   value1 = (int)value;
               }
               running.pop();
               type.pop();
               if (v.getFlag()) {
                   v.setValue(value);
                  
               }else {
                   v.setValue(value1);
                   
               }
               index ++;
           }else  //处理目标代码jmp
           if (instruction.getIns() == InsType.jmp) {
               index = instruction.getA_i();
           }else  //处理目标代码jpc
           if (instruction.getIns() == InsType.jpc) {
               int flag = (Integer)running.peek();
               running.pop();
               type.pop();
               if (flag == 1) {
                   index ++;
               }else {
                   index = instruction.getA_i(); 
               }
           }else  //处理目标代码opr
           if (instruction.getIns() == InsType.opr) {
               int a = instruction.getA_i();
               double i1 = 0,i2 = 0;
               if (a >= 1 && a <= 8) {
               i1 = Double.parseDouble(String.valueOf(running.peek()));
               running.pop();
               i2 = Double.parseDouble(String.valueOf(running.peek()));
               running.pop();
               }
               if (a == 1) {
                   running.push(i2 + i1);
                   if (String.valueOf(type.peek()).equals("int")) {
                       type.pop();type.pop();
                       type.push("int");
                   }else {
                       type.pop();type.pop();
                       type.push("real");
                   }
               }
               if (a == 2) {
                   running.push(i2 - i1);
                   if (String.valueOf(type.peek()).equals("int")) {
                       type.pop();type.pop();
                       type.push("int");
                   }else {
                       type.pop();type.pop();
                       type.push("real");
                   }
               }
               if (a == 3) {
                   running.push(i2 * i1);
                   if (String.valueOf(type.peek()).equals("int")) {
                       type.pop();type.pop();
                       type.push("int");
                   }else {
                       type.pop();type.pop();
                       type.push("real");
                   }
               }
               if (a == 4) {
                   running.push(i2 / i1);
                   if (String.valueOf(type.peek()).equals("int")) {
                       type.pop();type.pop();
                       type.push("int");
                   }else {
                       type.pop();type.pop();
                       type.push("real");
                   }
               }
               if (a == 6) {
                   if (i2 == i1) {
                       running.push(1);
                   }else {
                       running.push(0);
                   }
                   type.pop();type.pop();
                   type.push("int");
               }
               if (a == 7) {
                   if (i2 < i1) {
                       running.push(1);
                   }else {
                       running.push(0);
                   }
                   type.pop();type.pop();
                   type.push("int");
               }
               if (a == 8) {
                   if (i2 != i1) {
                       running.push(1);
                   }else {
                       running.push(0);
                   }
                   type.pop();type.pop();
                   type.push("int");        
               }
               if (a == 10) {
                   double result = Double.parseDouble(String.valueOf(running.pop()));
                   int result1 = (int)result;
                   if (String.valueOf(type.pop()).equals("int")) {
                           cmm.output2.append(String.valueOf(result1));
                   }else {
                       cmm.output2.append(String.valueOf(result));
                   }
               }
               if (a == 11) {
                   cmm.output2.append("\n");
               }
               if (a == 12) {
                       try {
                
                         String inputValue = JOptionPane.showInputDialog("请输入一个数字");
                         Double value = Double.parseDouble(inputValue);
                       
                             running.push(value);
                             type.push("real");
             
                             cmm.output2.append("您的输入为 = " + inputValue +"\n");
                         
                       }catch (java.lang.NumberFormatException e) {
                           JOptionPane.showMessageDialog(null,"输入错误,请重新运行!");
                           
                       }catch (NullPointerException ne) {
                           JOptionPane.showMessageDialog(null,"输入错误,请重新运行!");
                       }          
               }
               index ++;
           }
       }while(index != aim_code.size());
    }
}

⌨️ 快捷键说明

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