fi.hu.cs.titokone
Class Binary

java.lang.Object
  extended byfi.hu.cs.titokone.Binary

public class Binary
extends java.lang.Object

This class represents the contents of a binary file. It can interpret an Application instance of itself with the help of the BinaryInterpreter class, as well as transforming an Application into a string in the binary file format.


Field Summary
private  Application application
          This field stores the application this binary represents, or null if it has not yet been resolved.
private  java.lang.String contents
          This field stores the binary contents of the application, or an empty string if the contents have not yet been resolved (ie.
 
Constructor Summary
Binary(Application application)
          This constructor sets up a binary instance which has already been interpreted into an application.
Binary(java.lang.String contents)
          This constructor sets up a binary instance.
 
Method Summary
 Application toApplication()
          This method transforms the binary into an application file.
 java.lang.String toString()
          This method determines (if it is not done already), stores and returns the String representation in .b91 format of this binary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

application

private Application application
This field stores the application this binary represents, or null if it has not yet been resolved.


contents

private java.lang.String contents
This field stores the binary contents of the application, or an empty string if the contents have not yet been resolved (ie. the Application constructor has been used and toString() has not been called).

Constructor Detail

Binary

public Binary(java.lang.String contents)
       throws java.text.ParseException
This constructor sets up a binary instance.

Parameters:
contents - A linebreak-delimited string containing the contents of a binary file.
Throws:
java.text.ParseException - If the string does not represent a syntactically correct binary.

Binary

public Binary(Application application)
This constructor sets up a binary instance which has already been interpreted into an application. It delays interpreting the binary to string form until toString() is called.

Parameters:
application - The application to form a binary of.
Method Detail

toApplication

public Application toApplication()
                          throws java.text.ParseException
This method transforms the binary into an application file. It instantiates a BinaryInterpreter to help with the task and stores the result internally to avoid needing to redo the interpretation. If the class already knows what the application class corresponding to the binary is, it will just return that.

Returns:
An application instance corresponding to the binary.
Throws:
java.text.ParseException - If the binary contents are not syntatically correct.

toString

public java.lang.String toString()
This method determines (if it is not done already), stores and returns the String representation in .b91 format of this binary. The linebreaks used vary, as one of the constructors accepts a premade string; if the Application constructor has been used, the linebreak will be System.getProperty(line.separator, "\n"). An example program in .b91 format might look like this (comments in parentheses are not a part of the file format):
        ___b91___
        ___code___
        0 9          (numbers of the first and the last line of code,
        52428801      the latter also being the initial value of FP)
        18874378
        572522503
        36175883
        287834122
        18874379
        538968064
        36175883
        69206016
        1891631115
        ___data___
        10 11         (numbers the first and the last line of the data 
        0              area, the latter also being the initial value of SP;
        0              then follow the contents of the data area in order)
        ___symboltable___
        luku 10        (only local symbols are included, eg. HALT is 
        summa 11        considered built-in)
        ___end___
        

Returns:
The String representation of this binary.