Está en la página 1de 7

package compilador;

import
import
import
import
import
import

javax.swing.JFrame;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.io.FileNotFoundException;
java.io.BufferedReader;
java.io.FileReader;

import javax.swing.*;
import sintactico.*;
import maquinaVirtual.*;
import tablaSimbolos.*;
public class Compilador extends JFrame{
private static final long serialVersionUID = 1L;
//Fichero que contiene el cdigo fuente
public FileReader ficheroFuente;
private JFileChooser selFichero;
private ASintactico sintactico;
private MaquinaVirtual maquinaV;
private CodigoObjeto codObj;
/*Fichero que contiene los errores que se hayan producido al
*analizar el programa y el cdigo generado por la mquina virtual*/
//public static PrintWriter ficheroObjeto;
//public static PrintWriter ficheroErrores;
private boolean seguir = true;
private boolean error = false;
private JDesktopPane jDesktopPane1;
private JMenuItem jMenuItemFuente;
private JMenuItem jMenuItemAyuda;
private JMenuItem jMenuItemInfo;
private JLabel jLabelFuente;
private JLabel jLabelObjeto;
private JLabel jLabelMemoria;
private JButton jButtonCompilar;
private JButton jButtonEjecutar;
private JTextArea jTextAreaFuente;
private JScrollPane jScrollPaneFuente;
private JTextArea jTextAreaObjeto;
private JScrollPane jScrollPaneObjeto;
private JTextArea jTextAreaMemoria;
private JScrollPane jScrollPaneMemoria;
public Compilador(){
setTitle("Compilador Primera Entrega Grupo 06");
setLocation(100,100);
initComponents();
}
private void initComponents() {
//Titulo de la aplicacin
setTitle("Compilador Primera Entrega Grupo 06");
//Etiquetas
jLabelFuente = new JLabel();
jLabelFuente.setText("Cdigo Fuente");
jLabelFuente.setBounds(100, 30, 80, 20);
jLabelObjeto = new JLabel();

jLabelObjeto.setText("Cdigo Objeto");
jLabelObjeto.setBounds(620,30,80,20);
jLabelMemoria = new JLabel();
jLabelMemoria.setText("Memoria");
jLabelMemoria.setBounds(375,370,100,20);
//Botones
jButtonCompilar = new JButton();
jButtonCompilar.setText("Compilar");
jButtonCompilar.setBounds(350, 150, 100, 20);
jButtonCompilar.setEnabled(false);
jButtonCompilar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButtonCompilarActionPerformed(evt);
}
});
jButtonEjecutar = new JButton();
jButtonEjecutar.setText("Ejecutar");
jButtonEjecutar.setBounds(350, 225, 100, 20);
jButtonEjecutar.setEnabled(false);
jButtonEjecutar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButtonEjecutarActionPerformed(evt);
}
});
//Paneles y reas de texto
jTextAreaFuente = new JTextArea();
jScrollPaneFuente = new JScrollPane();
jTextAreaFuente.setColumns(20);
jTextAreaFuente.setRows(5);
jTextAreaFuente.setEditable(false);
jScrollPaneFuente.setViewportView(jTextAreaFuente);
jScrollPaneFuente.setBounds(20, 60, 246, 273);
jTextAreaObjeto = new JTextArea();
jScrollPaneObjeto = new JScrollPane();
jTextAreaObjeto.setColumns(20);
jTextAreaObjeto.setRows(5);
jTextAreaObjeto.setEditable(false);
jScrollPaneObjeto.setViewportView(jTextAreaObjeto);
jScrollPaneObjeto.setBounds(525, 60, 246, 273);
jTextAreaMemoria = new JTextArea();
jScrollPaneMemoria = new JScrollPane();
jTextAreaMemoria.setColumns(10);
jTextAreaMemoria.setRows(5);
jTextAreaMemoria.setEditable(false);
jScrollPaneMemoria.setViewportView(jTextAreaMemoria);
jScrollPaneMemoria.setBounds(100, 400, 600, 100);
//Mens de la aplicacin
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenu jMenu2 = new JMenu();
JMenu jMenu3 = new JMenu();
jMenuItemFuente = new JMenuItem();
jMenuItemAyuda = new JMenuItem();
jMenuItemInfo = new JMenuItem();
//Men Fichero Fuente
jMenu1.setText("Fichero Fuente");

jMenuItemFuente.setText("Buscar");
jMenuItemFuente.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jMenuItemFuenteActionPerformed(evt);
}
});
jMenu1.add(jMenuItemFuente);
//Men Ayuda
jMenu2.setText("Ayuda");
jMenuItemAyuda.setText("Mostrar Ayuda");
jMenuItemAyuda.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jMenuItemAyudaActionPerformed(evt);
}
});
jMenu2.add(jMenuItemAyuda);
//Men Componentes del Grupo
jMenu3.setText("Componentes del Grupo");
jMenuItemInfo.setText("Mostrar Componentes");
jMenuItemInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jMenuItemInfoActionPerformed(evt);
}
});
jMenu3.add(jMenuItemInfo);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
jDesktopPane1 = new javax.swing.JDesktopPane();
jDesktopPane1.setBackground(new java.awt.Color(255, 255, 102
));
jDesktopPane1.add(jLabelFuente);
jDesktopPane1.add(jLabelObjeto);
jDesktopPane1.add(jLabelMemoria);
jDesktopPane1.add(jButtonCompilar);
jDesktopPane1.add(jButtonEjecutar);
jDesktopPane1.add(jScrollPaneFuente);
jDesktopPane1.add(jScrollPaneObjeto);
jDesktopPane1.add(jScrollPaneMemoria);
setBackground(new java.awt.Color(255, 255, 102));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.la
yout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.L
EADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jDesktopPane1
, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.L
EADING)
.add(jDesktopPane1, org.jdesktop.layout.GroupLayout.DEFAULT_
SIZE, 577, Short.MAX_VALUE)

);
setSize(800,600);
setResizable(false);
}
private void jButtonCompilarActionPerformed(ActionEvent evt){
if (ficheroFuente != null){
try{
FileReader fuente = new FileReader(selFichero.ge
tSelectedFile());
sintactico= ASintacticoImp.getInstance();
((ASintacticoImp)sintactico).analizar(fuente);
fuente.close();
error = ((ASintacticoImp)sintactico).getErrores(
);
if (error!= true){
codObj= ((ASintacticoImp)sintactico).get
CodigoObjeto();
jTextAreaObjeto.setText(codObj.toString(
));
jButtonCompilar.setEnabled(false);
jButtonEjecutar.setEnabled(true);
}
else{
jButtonCompilar.setEnabled(false);
String mensaje = new String();
mensaje = "ERROR : "+"\n";
mensaje = ((ASintacticoImp)sintactico).g
etMensajeError();
jTextAreaMemoria.setText(mensaje);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error con el
fichero");
}
}
}
private void jButtonEjecutarActionPerformed(ActionEvent evt) {
if(!error){
maquinaV = new MaquinaVirtual(codObj);
maquinaV.ejecutar();
TablaSimbolos ts;
ts = ((ASintacticoImp)sintactico).getTablaSimbolos();
jTextAreaMemoria.setText(ts.toString(maquinaV.getMemoria
Datos()));
jButtonEjecutar.setEnabled(false);
}
else{
String mensaje = new String();
mensaje = "ERROR : "+"\n";
mensaje = ((ASintacticoImp)sintactico).getMensajeError()
;
jTextAreaMemoria.setText(mensaje);
jButtonEjecutar.setEnabled(false);
}
try{
ficheroFuente.close();
}

catch(Exception e){
JOptionPane.showMessageDialog(null,"El fichero ya estaba
cerrado");
}
}
private void jMenuItemAyudaActionPerformed(ActionEvent evt) {
//Muestra el fichero de ayuda por pantalla
String mensaje="";
try{
BufferedReader flujo = new BufferedReader(new FileReader("AyudaPLG06
.txt"));
String frase = flujo.readLine();
while(frase != null){
mensaje = mensaje + frase + "\n";
frase = flujo.readLine();
}
flujo.close();
Ventana ventana = new Ventana(this,mensaje);
ventana.setSize(502, 350);
ventana.reSizeBoton(210, 275);
ventana.reSizeScrollPane(2, 2, 490, 250);
ventana.setLocation(250,200);
ventana.setTitle("Manual de ayuda para usar el compilador Grupo
06");
ventana.setVisible(true);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Coloque el fichero A
yudaPLG06.txt en el directorio del proyecto");
}
}
private void jMenuItemFuenteActionPerformed(java.awt.event.ActionEvent e
vt) {
ficheroFuente = buscarFicheroFuente();
if(seguir == true){
if(ficheroFuente == null){
JOptionPane.showMessageDialog(null,"El fichero n
o existe o no es vlido");
}
else{
try{
BufferedReader flujo = new BufferedReade
r(ficheroFuente);
String frase = flujo.readLine();
String mensaje = "";
while(frase != null){
mensaje = mensaje + frase + "\n";
frase = flujo.readLine();
}
flujo.close();
jTextAreaFuente.setText(mensaje);
jButtonCompilar.setEnabled(true);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Se h
a producido un error en la lectura del fichero");
jButtonCompilar.setEnabled(false);
jButtonEjecutar.setEnabled(false);

}
}
}
else{
seguir = true;
}
}
private void jMenuItemInfoActionPerformed(java.awt.event.ActionEvent evt
) {
//Muestra en una ventana nueva los miembros del grupo
String mensaje = new String();
mensaje = " MIGUEL ANGEL CASADO HERNANDEZ\n"+
" JOSE ALBERTO CONTRERAS FERNANDEZ\n" +
" JAVIER DORIA DULANTO\n"+
" LUIS GONZALEZ DE PAULA\n"+
" ALBERTO MAQUEDA VIAS\n"+
" ANTONIO MURILLO MELERO\n"+
" JAVIER MURILLO YAGUE\n"+
" IRENE PEREZ MEDINA\n"+
" ALMUDENA RUIZ INIESTA." ;
Ventana ventana = new Ventana(this,mensaje);
ventana.setSize(308, 325);
ventana.setLocation(300,200);
ventana.setTitle("Componentes del Grupo 06");
ventana.setVisible(true);
}
public FileReader buscarFicheroFuente(){
//Mtodo que sirve para elegir el fichero fuente
FileReader fuente;
try{
selFichero = new JFileChooser();
selFichero.setApproveButtonText("Cargar");
selFichero.setDialogTitle("Seleccione el fichero a compi
lar");
FiltroPes filtro = new FiltroPes();
selFichero.setFileFilter(filtro);
selFichero.setFileSelectionMode(JFileChooser.FILES_ONLY)
;
int opcion = selFichero.showOpenDialog(null);
if (opcion == JFileChooser.APPROVE_OPTION){
fuente = new FileReader(selFichero.getSelectedFi
le());
}
else{
fuente = null;
seguir = false;
}
}
catch(FileNotFoundException e){
fuente = null;
}
return fuente;
}
public static void main(String[] args){
Compilador compilador = new Compilador();
compilador.setVisible(true);
}

También podría gustarte