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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org>
This file is part of SlackYouInterpreter.
SlackYouInterpreter is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SlackYouInterpreter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SlackYouInterpreter; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.*;
public class Frame extends JFrame {
public static int valoreChooser;
public static boolean firstSave;
public static String defaultSave;
public static Main main;
public static JTextArea textArea;
public static JButton runButton;
public static JMenuBar bar;
public static JMenu menuFile, menuAiuto;
public static JMenuItem esci, esegui, salva, salvaConNome, apri, info;
public static JFileChooser chooser;
public static FileInputStream in;
public static FileOutputStream out;
public Frame() {
super( "Porky's SlackYouInterpreter - v1.1" );
firstSave = true;
main = new Main();
defaultSave = "";
chooser = new JFileChooser();
textArea = new JTextArea( "Digitare qui il codice SlackYou", 15, 15 );
bar = new JMenuBar();
menuFile = new JMenu( "File" );
menuAiuto = new JMenu( "?" );
esci = new JMenuItem( "Esci" );
esegui = new JMenuItem( "Esegui" );
salva = new JMenuItem( "Salva" );
salvaConNome = new JMenuItem( "Salva con nome" );
apri = new JMenuItem( "Apri" );
info = new JMenuItem( "Aiuto" );
menuFile.setMnemonic( 'F' );
menuAiuto.setMnemonic( '?' );
esci.setMnemonic( 'E' );
esegui.setMnemonic( 's' );
salva.setMnemonic( 'a' );
salvaConNome.setMnemonic( 'n' );
apri.setMnemonic( 'p' );
info.setMnemonic( 'i' );
esci.addActionListener(
new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
System.exit( 0 );
}
}
);
esegui.addActionListener(
new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
main.run( textArea.getText() );
}
}
);
salva.addActionListener(
new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
if ( firstSave ) {
valoreChooser = chooser.showSaveDialog( Frame.this );
switch ( valoreChooser ) {
case JFileChooser.APPROVE_OPTION:
try {
out = new FileOutputStream( chooser.getSelectedFile().getPath() );
defaultSave = chooser.getSelectedFile().getPath();
char caratteri[];
byte bytes[];
caratteri = textArea.getText().toCharArray();
bytes = new byte[caratteri.length];
for ( int i = 0; i < caratteri.length; i++ )
bytes[i] = ( byte ) caratteri[i];
out.write( bytes );
} catch ( Exception ex ) {
JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "SlackYou - Errore", JOptionPane.ERROR_MESSAGE );
}
break;
case JFileChooser.ERROR_OPTION:
JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "SlackYou - Errore", JOptionPane.ERROR_MESSAGE );
break;
//case JFileChooser.CANCEL_OPTION:
}
firstSave = false;
} else {
try {
out = new FileOutputStream( defaultSave );
char caratteri[];
byte bytes[];
caratteri = textArea.getText().toCharArray();
bytes = new byte[caratteri.length];
for ( int i = 0; i < caratteri.length; i++ )
bytes[i] = ( byte ) caratteri[i];
out.write( bytes );
} catch ( Exception ex ) {
JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "SlackYou - Errore", JOptionPane.ERROR_MESSAGE );
}
}
}
}
);
salvaConNome.addActionListener(
new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
valoreChooser = chooser.showSaveDialog( Frame.this );
switch ( valoreChooser ) {
case JFileChooser.APPROVE_OPTION:
try {
out = new FileOutputStream( chooser.getSelectedFile().getPath() );
defaultSave = chooser.getSelectedFile().getPath();
char caratteri[];
byte bytes[];
caratteri = textArea.getText().toCharArray();
bytes = new byte[caratteri.length];
for ( int i = 0; i < caratteri.length; i++ )
bytes[i] = ( byte ) caratteri[i];
out.write( bytes );
} catch ( Exception ex ) {
JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "SlackYou - Errore", JOptionPane.ERROR_MESSAGE );
}
break;
case JFileChooser.ERROR_OPTION:
JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "SlackYou - Errore", JOptionPane.ERROR_MESSAGE );
break;
//case JFileChooser.CANCEL_OPTION:
}
firstSave = false;
}
}
);
apri.addActionListener(
new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
valoreChooser = chooser.showOpenDialog( Frame.this );
switch ( valoreChooser ) {
case JFileChooser.APPROVE_OPTION:
try {
String stringa;
byte bytes[];
char caratteri[];
in = new FileInputStream( chooser.getSelectedFile().getPath() );
bytes = new byte[in.available()];
caratteri = new char[in.available()];
in.read( bytes );
for ( int i = 0; i < bytes.length; i++ )
caratteri[i] = ( char ) bytes[i];
stringa = new String( caratteri );
textArea.setText( stringa );
} catch ( Exception ex ) {
JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "SlackYou - Errore", JOptionPane.ERROR_MESSAGE );
}
break;
case JFileChooser.ERROR_OPTION:
JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "SlackYou - Errore", JOptionPane.ERROR_MESSAGE );
break;
//case JFileChooser.CANCEL_OPTION:
}
}
}
);
info.addActionListener(
new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
JOptionPane.showMessageDialog( Frame.this, "SlackYou è un linguaggio di programmazione esoterico,\n ispirato al famoso BrainFuck, ma con molte funzionalità in più.\nInnanzitutto in SlackYou l'array di byte è bidimensionale.\nIl linguaggio è case-sensitive. Ogni carattere che non è valido\ncome comando può essere usato come commento,\ninclusi i caratteri-comando scritti in maiuscolo.\nTutti gli altri comandi sono spiegati nella lista seguente:\n\n. Incrementa di 1 il byte al puntatore. Corrisponde al + in BF.\n, Decrementa di 1 il byte al puntatore. Corrisponde al - in BF.\n> Incrementa il puntatore orizzontale, come in BF.\n< Decrementa il puntatore orizzontale, come in BF.\nv Incrementa il puntatore verticale.\n^ Decrementa il puntatore verticale.\n+ Somma il byte al puntatore con il byte alla sua destra. Il risultato viene memorizzato nel byte al puntatore.\n- Come sopra, ma sottrae.\n* Come sopra, ma moltiplica.\n/ Come sopra, ma esegue una divisione euclidea (senza virgola e senza resto).\n% Come sopra, ma esegue l'operatore modulo (restituisce il resto della divisione euclidea).\nj Copia il byte al puntatore nel byte a destra e incrementa il puntatore orizzontale.\ng Copia il byte al puntatore nel byte a sinistra e decrementa il puntatore orizzontale.\ny Copia il byte al puntatore nel byte sopra e decrementa il puntatore verticale.\nh Copia il byte al puntatore nel byte sotto e incrementa il puntatore verticale.\n=xy Se x e y sono cifre esadecimali (da '0' a '9' e da 'a' a 'f') il byte al puntatore assume il valore esadecimale xy.\nr Memorizza nel byte al puntatore il codice ASCII del carattere digitato in input.\nw Scrive in output il byte al puntatore tradotto in carattere ASCII.\n? Se il byte al puntatore è 0, salta avanti fino all'istruzione dopo il corrispondente comando ; o :.\n! Se il byte al puntatore non è 0, salta avanti fino all'istruzione dopo il corrispondente comando : o ;.\n; Se il byte al puntatore non è 0, salta indietro fino all'istruzione dopo il corrispondente comando ? o !.\n: Se il byte al puntatore è 0, salta indietro fino all'istruzione dopo il corrispondente comando ! o ?.\n\nNella cartella src trovate un esempio di Hello World in questo linguaggio.\n\nPer maggiori informazioni visitate la pagina:\nhttp://porkynator.altervista.org/SlackYou.html\no contattatemi al mio indirizzo e-mail:\nsebastiano@luganega.org", "SlackYou - informazioni", JOptionPane.INFORMATION_MESSAGE );
}
}
);
menuFile.add( esegui );
menuFile.add( salva );
menuFile.add( salvaConNome );
menuFile.add( apri );
menuFile.add( esci );
menuAiuto.add( info );
bar.add( menuFile );
bar.add( menuAiuto );
runButton = new JButton( "Esegui" );
runButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
main.run( textArea.getText() );
}
}
);
setLayout( new BorderLayout() );
setJMenuBar( bar );
add( new JScrollPane( textArea ), BorderLayout.CENTER );
add( runButton, BorderLayout.SOUTH );
}
}
|