Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
118 lines (87 loc) · 3.95 KB
/
Copy pathMain.java
File metadata and controls
118 lines (87 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class Main extends JFrame {
private JTextArea zoneCode;
private JTextArea zoneResultat;
private JButton boutonCompiler;
private JButton boutonEffacer;
public Main() {
// Configuration de la fenêtre
setTitle("Mini-Compilateur Python (Thème: FOR)");
setSize(900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Centrer
// 1. Panneau Haut : Titre
JLabel titre = new JLabel("Éditeur de Code Python (Compilateur)", SwingConstants.CENTER);
titre.setFont(new Font("Arial", Font.BOLD, 18));
titre.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
// 2. Panneau Central : Les deux zones de texte
zoneCode = new JTextArea();
zoneCode.setFont(new Font("Monospaced", Font.PLAIN, 14));
// Code par défaut
zoneCode.setText("x = 10\nfor i in range(x):\n print i\n Saidani Rabah\n");
zoneResultat = new JTextArea();
zoneResultat.setFont(new Font("Monospaced", Font.PLAIN, 14));
zoneResultat.setEditable(false); // On ne peut pas écrire dans le résultat
zoneResultat.setBackground(new Color(30, 30, 30)); // Fond sombre
zoneResultat.setForeground(Color.GREEN); // Texte vert style hacker
JScrollPane scrollCode = new JScrollPane(zoneCode);
scrollCode.setBorder(BorderFactory.createTitledBorder("Code Source"));
JScrollPane scrollResultat = new JScrollPane(zoneResultat);
scrollResultat.setBorder(BorderFactory.createTitledBorder("Console de Compilation"));
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollCode, scrollResultat);
splitPane.setDividerLocation(350);
JPanel panelBoutons = new JPanel();
boutonCompiler = new JButton("COMPILER");
boutonCompiler.setFont(new Font("Arial", Font.BOLD, 14));
boutonCompiler.setBackground(new Color(0, 120, 215));
boutonCompiler.setForeground(Color.WHITE);
boutonEffacer = new JButton("Effacer Console");
panelBoutons.add(boutonCompiler);
panelBoutons.add(boutonEffacer);
boutonCompiler.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lancerCompilation();
}
});
boutonEffacer.addActionListener(e -> zoneResultat.setText(""));
add(titre, BorderLayout.NORTH);
add(splitPane, BorderLayout.CENTER);
add(panelBoutons, BorderLayout.SOUTH);
}
private void lancerCompilation() {
zoneResultat.setText("");
zoneResultat.append(">>> Démarrage de l'analyse...\n\n");
String code = zoneCode.getText();
if (code.trim().isEmpty()) {
zoneResultat.append("Le code est vide !\n");
return;
}
try {
// 1. Lexer
AnalyseurLexical lexer = new AnalyseurLexical(code);
List<AnalyseurLexical.Token> tokens = lexer.tokeniser();
zoneResultat.append("--- TOKENS ---\n");
for (AnalyseurLexical.Token t : tokens) {
zoneResultat.append(t.toString() + "\n");
}
zoneResultat.append("\n");
// 2. Parser
AnalyseurSyntaxique parser = new AnalyseurSyntaxique(tokens, zoneResultat);
parser.Z();
} catch (Exception ex) {
zoneResultat.append("\n ERREUR CRITIQUE : " + ex.getMessage());
}
}
public static void main(String[] args) {
// Lancer l'interface graphique
SwingUtilities.invokeLater(() -> {
Main frame = new Main();
frame.setVisible(true);
});
}
}