Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
compilerconstruction
ssm
Commits
e2fecdf9
Commit
e2fecdf9
authored
Jun 04, 2014
by
Joost Rijneveld
Browse files
Added 'trap 2' and 'trap 3' for int/char prompts
parent
574228ee
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/nl/uu/cs/ssm/Instruction.java
View file @
e2fecdf9
...
...
@@ -163,6 +163,8 @@ public class Instruction
*/
protected
final
static
int
TR_PR_INT
=
0
;
protected
final
static
int
TR_PR_CHAR
=
1
;
protected
final
static
int
TR_IN_INT
=
2
;
protected
final
static
int
TR_IN_CHAR
=
3
;
/**
* Metas
...
...
src/nl/uu/cs/ssm/Machine.java
View file @
e2fecdf9
...
...
@@ -490,6 +490,12 @@ public class Machine
messenger
.
println
(
"Error: UTF-32 encoding missing."
);
}
break
;
case
Instruction
.
TR_IN_INT
:
push
(
messenger
.
promptInt
());
break
;
case
Instruction
.
TR_IN_CHAR
:
push
(
messenger
.
promptChar
());
break
;
default
:
break
;
}
break
;
...
...
src/nl/uu/cs/ssm/Messenger.java
View file @
e2fecdf9
...
...
@@ -12,4 +12,16 @@ public interface Messenger
{
public
void
println
(
String
s
)
;
/**
* Asks the user for an integer value (i.e. via a dialog)
* @return Returns the integer value as provided by the user
*/
public
int
promptInt
();
/**
* Asks the user for a character (i.e. via a dialog)
* @return Returns the unicode integer code point of the provided character
*/
public
int
promptChar
();
}
\ No newline at end of file
src/nl/uu/cs/ssmui/SSMRunner.java
View file @
e2fecdf9
...
...
@@ -12,6 +12,8 @@ import java.awt.event.MouseAdapter;
import
java.awt.event.MouseEvent
;
import
java.io.File
;
import
java.io.FileReader
;
import
java.io.UnsupportedEncodingException
;
import
java.nio.ByteBuffer
;
import
java.util.Arrays
;
import
java.util.Enumeration
;
import
java.util.Vector
;
...
...
@@ -22,6 +24,7 @@ import javax.swing.JFileChooser;
import
javax.swing.JFrame
;
import
javax.swing.JMenu
;
import
javax.swing.JMenuItem
;
import
javax.swing.JOptionPane
;
import
javax.swing.JScrollBar
;
import
javax.swing.JTable
;
import
javax.swing.KeyStroke
;
...
...
@@ -653,6 +656,42 @@ public class SSMRunner extends JFrame
outputTextArea
.
append
(
s
)
;
outputTextArea
.
append
(
"\n"
)
;
}
public
int
promptInt
()
{
while
(
true
)
{
try
{
String
s
=
JOptionPane
.
showInputDialog
(
this
,
"Please enter an integer."
,
"Integer requested"
,
JOptionPane
.
QUESTION_MESSAGE
);
if
(
s
==
null
)
{
return
0
;
}
return
Integer
.
parseInt
(
s
);
}
catch
(
NumberFormatException
e
)
{
continue
;
// Ask again
}
}
}
public
int
promptChar
()
{
while
(
true
)
{
String
s
=
JOptionPane
.
showInputDialog
(
this
,
"Please enter a character."
,
"Character requested"
,
JOptionPane
.
QUESTION_MESSAGE
);
if
(
s
==
null
)
{
return
0
;
}
if
(
s
.
length
()
==
0
)
{
continue
;
}
byte
[]
b
=
{};
try
{
b
=
s
.
getBytes
(
"UTF-32"
);
}
catch
(
UnsupportedEncodingException
e
)
{
println
(
"Error: UTF-32 encoding missing."
);
}
return
ByteBuffer
.
wrap
(
b
).
getInt
();
}
}
// Close the window when the close box is clicked
void
thisWindowClosing
(
java
.
awt
.
event
.
WindowEvent
e
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment