Commit 56d9c12a authored by Joost Rijneveld's avatar Joost Rijneveld

Added file IO (full unicode support), renumbered traps

parent 87b50325
...@@ -161,12 +161,16 @@ public class Instruction ...@@ -161,12 +161,16 @@ public class Instruction
/** /**
* Traps * Traps
*/ */
protected final static int TR_PR_INT = 0 ; protected final static int TR_PR_INT = 00;
protected final static int TR_PR_CHAR = 1 ; protected final static int TR_PR_CHAR = 01;
protected final static int TR_IN_INT = 2 ; protected final static int TR_IN_INT = 10;
protected final static int TR_IN_CHAR = 3 ; protected final static int TR_IN_CHAR = 11;
protected final static int TR_IN_CHAR_ARRAY = 4; 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 * Metas
*/ */
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
package nl.uu.cs.ssm ; package nl.uu.cs.ssm ;
import java.awt.Color; import java.awt.Color;
import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
public class Machine public class Machine
...@@ -503,6 +504,80 @@ public class Machine ...@@ -503,6 +504,80 @@ public class Machine
push(i); push(i);
} }
break; 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 ; default : break ;
} }
break ; break ;
......
...@@ -8,6 +8,11 @@ ...@@ -8,6 +8,11 @@
package nl.uu.cs.ssm ; 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; import java.util.Enumeration;
public class MachineState extends Model public class MachineState extends Model
...@@ -25,6 +30,7 @@ public class MachineState extends Model ...@@ -25,6 +30,7 @@ public class MachineState extends Model
protected Memory memory ; protected Memory memory ;
protected Registers registers ; protected Registers registers ;
protected MemoryUser memoryUser ; protected MemoryUser memoryUser ;
protected ArrayList<Closeable> filePtrs;
public boolean isHalted ; public boolean isHalted ;
...@@ -34,6 +40,7 @@ public class MachineState extends Model ...@@ -34,6 +40,7 @@ public class MachineState extends Model
registers = new Registers( memory, m ) ; registers = new Registers( memory, m ) ;
stackGrowthDir = 1 ; stackGrowthDir = 1 ;
this.startAddressOfHeap = startAddressOfHeap; this.startAddressOfHeap = startAddressOfHeap;
filePtrs = new ArrayList<Closeable>();
reset() ; reset() ;
} }
...@@ -71,6 +78,21 @@ public class MachineState extends Model ...@@ -71,6 +78,21 @@ public class MachineState extends Model
registers.setMP( registers.getSP() ) ; registers.setMP( registers.getSP() ) ;
registers.setHP(startAddressOfHeap); registers.setHP(startAddressOfHeap);
isHalted = false ; 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 ) public int dir( int v )
...@@ -163,5 +185,64 @@ public class MachineState extends Model ...@@ -163,5 +185,64 @@ public class MachineState extends Model
return "state code=" + code + " instr-pc=" + instrPC + " n-inl=" + nInlineOpnds ; 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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment