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
56d9c12a
Commit
56d9c12a
authored
Jun 06, 2014
by
Joost Rijneveld
Browse files
Added file IO (full unicode support), renumbered traps
parent
87b50325
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/nl/uu/cs/ssm/Instruction.java
View file @
56d9c12a
...
...
@@ -161,12 +161,16 @@ public class Instruction
/**
* Traps
*/
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
;
protected
final
static
int
TR_IN_CHAR_ARRAY
=
4
;
protected
final
static
int
TR_PR_INT
=
00
;
protected
final
static
int
TR_PR_CHAR
=
01
;
protected
final
static
int
TR_IN_INT
=
10
;
protected
final
static
int
TR_IN_CHAR
=
11
;
protected
final
static
int
TR_IN_CHAR_ARRAY
=
12
;
protected
final
static
int
TR_FILE_OPEN_READ
=
20
;
protected
final
static
int
TR_FILE_OPEN_WRITE
=
21
;
protected
final
static
int
TR_FILE_READ
=
22
;
protected
final
static
int
TR_FILE_WRITE
=
23
;
protected
final
static
int
TR_FILE_CLOSE
=
24
;
/**
* Metas
*/
...
...
src/nl/uu/cs/ssm/Machine.java
View file @
56d9c12a
...
...
@@ -9,6 +9,7 @@
package
nl.uu.cs.ssm
;
import
java.awt.Color
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
public
class
Machine
...
...
@@ -503,6 +504,80 @@ public class Machine
push
(
i
);
}
break
;
case
Instruction
.
TR_FILE_OPEN_READ
:
case
Instruction
.
TR_FILE_OPEN_WRITE
:
StringBuilder
filename
=
new
StringBuilder
();
int
n
=
pop
();
while
(
n
!=
0
)
{
filename
.
append
((
char
)
n
);
n
=
pop
();
}
String
fname
=
filename
.
reverse
().
toString
();
try
{
boolean
readOnly
=
state
.
inlineOpnds
[
0
]
==
Instruction
.
TR_FILE_OPEN_READ
;
push
(
state
.
openFile
(
fname
,
readOnly
));
}
catch
(
IOException
e
)
{
messenger
.
println
(
"Error: file "
+
fname
+
" not found"
);
}
break
;
case
Instruction
.
TR_FILE_READ
:
try
{
push
(
state
.
readFromFile
(
pop
()));
}
catch
(
IOException
e
)
{
messenger
.
println
(
"Error: cannot read from file."
);
}
catch
(
IndexOutOfBoundsException
e
)
{
messenger
.
println
(
"Error: invalid file pointer."
);
}
catch
(
NullPointerException
e
)
{
messenger
.
println
(
"Error: invalid file pointer."
);
}
break
;
case
Instruction
.
TR_FILE_WRITE
:
try
{
push
(
state
.
writeToFile
(
pop
(),
pop
()));
}
catch
(
IOException
e
)
{
messenger
.
println
(
"Error: cannot write to file."
);
}
catch
(
IndexOutOfBoundsException
e
)
{
messenger
.
println
(
"Error: invalid file pointer."
);
}
catch
(
NullPointerException
e
)
{
messenger
.
println
(
"Error: invalid file pointer."
);
}
break
;
case
Instruction
.
TR_FILE_CLOSE
:
try
{
state
.
closeFile
(
pop
());
}
catch
(
IOException
e
)
{
messenger
.
println
(
"Error: cannot close file."
);
}
catch
(
IndexOutOfBoundsException
e
)
{
messenger
.
println
(
"Error: invalid file pointer."
);
}
catch
(
NullPointerException
e
)
{
messenger
.
println
(
"Error: invalid file pointer."
);
}
break
;
default
:
break
;
}
break
;
...
...
src/nl/uu/cs/ssm/MachineState.java
View file @
56d9c12a
...
...
@@ -8,6 +8,11 @@
package
nl.uu.cs.ssm
;
import
java.io.Closeable
;
import
java.io.FileReader
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.Enumeration
;
public
class
MachineState
extends
Model
...
...
@@ -25,6 +30,7 @@ public class MachineState extends Model
protected
Memory
memory
;
protected
Registers
registers
;
protected
MemoryUser
memoryUser
;
protected
ArrayList
<
Closeable
>
filePtrs
;
public
boolean
isHalted
;
...
...
@@ -34,6 +40,7 @@ public class MachineState extends Model
registers
=
new
Registers
(
memory
,
m
)
;
stackGrowthDir
=
1
;
this
.
startAddressOfHeap
=
startAddressOfHeap
;
filePtrs
=
new
ArrayList
<
Closeable
>();
reset
()
;
}
...
...
@@ -66,11 +73,26 @@ public class MachineState extends Model
public
void
resetToInitialState
()
{
registers
.
setPC
(
0
)
;
stackBottom
=
memory
.
getUsedForCode
()
+
16
;
stackBottom
=
memory
.
getUsedForCode
()
+
16
;
registers
.
setSP
(
stackBottom
-
stackGrowthDir
)
;
registers
.
setMP
(
registers
.
getSP
()
)
;
registers
.
setHP
(
startAddressOfHeap
);
isHalted
=
false
;
try
{
for
(
Closeable
f
:
filePtrs
)
{
if
(
f
!=
null
)
{
f
.
close
();
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
filePtrs
=
new
ArrayList
<
Closeable
>();
}
public
int
dir
(
int
v
)
...
...
@@ -162,6 +184,65 @@ public class MachineState extends Model
{
return
"state code="
+
code
+
" instr-pc="
+
instrPC
+
" n-inl="
+
nInlineOpnds
;
}
/**
* Opens a file
* @param fname Name of the file
* @param readOnly True if the file should be open for reading, false otherwise
* @return The 'file pointer' of the file
* @throws IOException
*/
public
int
openFile
(
String
fname
,
boolean
readOnly
)
throws
IOException
{
if
(
readOnly
)
{
filePtrs
.
add
(
new
FileReader
(
fname
));
}
else
{
filePtrs
.
add
(
new
FileWriter
(
fname
));
}
return
filePtrs
.
size
()
-
1
;
}
/**
* Reads a unicode character from the file indicated by index
* @param index The 'file pointer' of the file
* @return The unicode codepoint of the read character
* @throws IOException
*/
public
int
readFromFile
(
int
index
)
throws
IOException
{
FileReader
file
=
(
FileReader
)
filePtrs
.
get
(
index
);
int
i16
=
file
.
read
();
// UTF-16 as int
char
c16
=
(
char
)
i16
;
// UTF-16
if
(
Character
.
isHighSurrogate
(
c16
))
{
int
low_i16
=
file
.
read
();
// low surrogate UTF-16 as int
char
low_c16
=
(
char
)
low_i16
;
return
Character
.
toCodePoint
(
c16
,
low_c16
);
}
return
i16
;
}
/**
* Writes a unicode character to the file indicated by the index
* @param n The unicode codepoint of the character
* @param index The 'file pointer' of the file
* @return index The 'file pointer' of the file
* @throws IOException
*/
public
int
writeToFile
(
int
n
,
int
index
)
throws
IOException
{
FileWriter
file
=
(
FileWriter
)
filePtrs
.
get
(
index
);
file
.
write
(
Utils
.
codePointToString
(
n
));
return
index
;
}
/**
* Closes a file
* @param index The 'file pointer' of the file
* @throws IOException
*/
public
void
closeFile
(
int
index
)
throws
IOException
{
filePtrs
.
get
(
index
).
close
();
// cannot remove from filePtrs, as that messes up index
filePtrs
.
set
(
index
,
null
);
}
}
\ No newline at end of file
Write
Preview
Supports
Markdown
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