📄 newjframe.java
字号:
import java.io.File;/* * NewJFrame.java * * Created on 2009年3月6日, 下午8:21 */import javax.swing.JFileChooser;/** * * @author lilac */public class NewJFrame extends javax.swing.JFrame { private String sourcefilepath; private String outputfilepath; /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { textField1 = new java.awt.TextField(); button1 = new java.awt.Button(); button3 = new java.awt.Button(); textArea1 = new java.awt.TextArea(); textArea2 = new java.awt.TextArea(); textArea3 = new java.awt.TextArea(); label1 = new java.awt.Label(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); textField1.setText("textField1"); button1.setLabel("open"); button1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { button1MouseClicked(evt); } }); button3.setLabel("compile"); button3.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { button3MouseClicked(evt); } }); textArea1.setName("fouroutput"); // NOI18N textArea2.setName("assembleroutput"); // NOI18N label1.setText("C-Compiler"); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(textArea1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 356, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(10, 10, 10) .add(textArea2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 271, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(layout.createSequentialGroup() .addContainerGap() .add(textArea3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 637, Short.MAX_VALUE))) .addContainerGap()) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 208, Short.MAX_VALUE) .add(textField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(button3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(27, 27, 27)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(textField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(button1, 0, 0, Short.MAX_VALUE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)) .add(layout.createSequentialGroup() .add(button3, 0, 0, Short.MAX_VALUE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))))) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(org.jdesktop.layout.GroupLayout.LEADING, textArea2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.LEADING, textArea1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)) .add(9, 9, 9) .add(textArea3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void button1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button1MouseClicked // TODO add your handling code here: JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(false); if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { sourcefilepath = chooser.getSelectedFile().getAbsolutePath(); outputfilepath = sourcefilepath + ".asm"; textField1.setText(sourcefilepath); } }//GEN-LAST:event_button1MouseClicked private void button3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button3MouseClicked // TODO add your handling code here: File file = new File(sourcefilepath); Lex lx = new Lex(file); lx.lex_analy(); if(lx.erroroutput!=null){ textArea3.append("词法分析错误:"); textArea3.append(lx.erroroutput); return; } Compile cc = new Compile(lx.list); cc.Prog(); //打印 if (cc.error == 0) { //四元式输出 textArea1.append("topconst:\n"); for (Constpair pair : cc.topconst) { textArea1.append("const " + pair.toString() + "\n"); } textArea1.append("topvar:\n"); for (String ss : cc.topvar) { textArea1.append("int " + ss + "\n"); } textArea1.append("===============================================\n"); for (Function func : cc.funclist) { textArea1.append("FUNCTION:\n"); textArea1.append("===============================================\n"); textArea1.append(func.retype + " " + func.name + "\n"); textArea1.append("PARA:\n"); for (String para : func.para) { textArea1.append("int " + para + "\t" + func.pmap.get(para) + "\n"); } textArea1.append("CONST:\n"); for (Constpair pair : func.con) { textArea1.append("const " + pair.toString() + "\n"); } textArea1.append("VAR:\n"); for (String var : func.var) { textArea1.append("int " + var + "\t" + func.vmap.get(var) + "\n"); } for (Four f : func.fourlist) { if (f.result != null && f.result.startsWith("@")) { textArea1.append("int " + f.result + "\t" + func.vmap.get(f.result) + "\n"); } } textArea1.append("FOUR:\n"); textArea1.append("===============================================\n"); for (Four four : func.fourlist) { textArea1.append(four.toString() + "\n"); } } //汇编输出 Generate gen = new Generate(cc.topconst, cc.topvar, cc.funclist, outputfilepath); gen.Generate(); textArea2.append(".386\n"); textArea2.append(".MODEL FLAT\n"); textArea2.append("includelib msvcrt.lib\n"); textArea2.append("puts proto C\n"); textArea2.append("printf proto C\n"); textArea2.append("scanf proto C\n"); textArea2.append(".STACK 4096\n"); textArea2.append(".DATA\n"); for(String s : gen.topvar){ textArea2.append("$"+s + " DWORD 0\n"); } for (String s : gen.st) { textArea2.append(s + '\n'); } textArea2.append(".CODE\n"); for (String s : gen.assembler) { textArea2.append(s + '\n'); } textArea2.append("_main:\n"); textArea2.append("jmp near ptr @fmain\n"); textArea2.append("end _main\n"); //结果输出 if(gen.warn!=null){ textArea3.append(gen.warn); } textArea3.append("成功生成汇编!"); } else { textArea3.append("ERROR " + new Integer(cc.error).toString() + "\n"); textArea3.append(cc.err_report(cc.error)); } }//GEN-LAST:event_button3MouseClicked /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private java.awt.Button button1; private java.awt.Button button3; private java.awt.Label label1; private java.awt.TextArea textArea1; private java.awt.TextArea textArea2; private java.awt.TextArea textArea3; private java.awt.TextField textField1; // End of variables declaration//GEN-END:variables }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -