fi.hu.cs.titokone
Class Compiler

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

public class Compiler
extends java.lang.Object

This class knows everything about the relation between symbolic code and binary code. It can transform a full source to binary or one symbolic command to binary or vice versa. Empty out all compiler commands and empty lines at the start of round 2.


Field Summary
private  java.util.Vector code
          This array contains the code.
private  MemoryLine[] codeMemoryLines
          This field holds the Memoryline objects for the code.
private  int commandLineCount
          This field counts the number of actual command lines found during the first round.
private  CompileDebugger compileDebugger
          This field contains the CompileDebugger instance to inform of any compilation happenings.
private  boolean compileFinished
          This value tells if all the lines are processed twice and getApplication can be run.
private  java.lang.String[] data
          This array contains the data.
private  MemoryLine[] dataMemoryLines
          This field holds the Memoryline objects for the data area.
private  java.lang.String defStdin
          This field holds the value of Stdin if it was set with DEF command.
private  java.lang.String defStdout
          This field holds the value of Stdout if it was set with DEF command.
private  int EMPTY
           
private  boolean firstRound
          This field keeps track of whether we are in the first round of compilation or the second.
private  java.util.HashMap invalidLabels
          This field holds the invalid values to be introduced (i.e.
private  int MAXINT
          Maximum value of the EQU and DC.
private  int MININT
          Minimum value of the EQU and DC.
private  int nextLine
          This field tells the next line to be checked.
private  int NOTVALID
           
private  java.lang.String[] source
          This field contains the source code as a String array.
private  SymbolicInterpreter symbolicInterpreter
          This field contains the SymbolicInterpreter instance to use as part of the compilation.
private  java.util.HashMap symbols
          This field holds the declared variables, labels and other symbols.
private  java.util.Vector symbolTable
          This field acts as a symboltable, it is a String array vector where 1:st position holds the name of the symbol and the second either it's value (label, equ) or the command (ds 10, dc 10)
private  java.lang.String VALIDLABELCHARS
          This field holds all the valid symbols on a label.
 
Constructor Summary
Compiler()
          This constructor sets up the class.
 
Method Summary
 void compile(java.lang.String source)
          This function initializes transforms a symbolic source code into an application class.
 CompileInfo compileLine()
          This function goes through one line of the code.
private  CompileInfo firstRoundProcess(java.lang.String line)
          This function gathers new symbol information from the given line and checks its syntax.
 Application getApplication()
          This method returns the readily-compiled application if the compilation is complete, or null otherwise.
 MemoryLine getSymbolicAndBinary(int binary)
          This function transforms a binary command number to a MemoryLine containing both the binary and the symbolic command corresponding to it.
 MemoryLine getSymbolicAndBinary(MemoryLine binaryOnly)
          This function transforms a MemoryLine containing only the binary command to a MemoryLine containing both the binary and the symbolic command corresponding to it.
private  CompileInfo initializeSecondRound()
          This method initializes the code and data area arrays for the second round processing according to the dataAreaSize and commandLineCount variables.
 java.lang.String[] parseCompilerCommandLine(java.lang.String line)
          This function tries to find a compiler command from the line.
 java.lang.String[] parseLine(java.lang.String symbolicOpcode)
          This method parses a String and tries to find a label, opCode and all the other parts of a Command line.
private  CompileInfo secondRoundProcess(java.lang.String line)
          This function transforms any commands to binary and stores both forms in the code array, or sets any initial data values in the data array.
private  boolean validLabelName(java.lang.String labelName)
          This method tests whether a label name contains at least one non-number and consists of 0-9, A-Ö and _.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

source

private java.lang.String[] source
This field contains the source code as a String array.


symbols

private java.util.HashMap symbols
This field holds the declared variables, labels and other symbols. It acts as a pointer to the symbolTable Vector where the actual data is stored.


invalidLabels

private java.util.HashMap invalidLabels
This field holds the invalid values to be introduced (i.e. already used labels can't be re-used.


nextLine

private int nextLine
This field tells the next line to be checked.


VALIDLABELCHARS

private final java.lang.String VALIDLABELCHARS
This field holds all the valid symbols on a label.

See Also:
Constant Field Values

NOTVALID

private final int NOTVALID
See Also:
Constant Field Values

EMPTY

private final int EMPTY
See Also:
Constant Field Values

MAXINT

private final int MAXINT
Maximum value of the EQU and DC.

See Also:
Constant Field Values

MININT

private final int MININT
Minimum value of the EQU and DC.

See Also:
Constant Field Values

defStdin

private java.lang.String defStdin
This field holds the value of Stdin if it was set with DEF command.


defStdout

private java.lang.String defStdout
This field holds the value of Stdout if it was set with DEF command.


firstRound

private boolean firstRound
This field keeps track of whether we are in the first round of compilation or the second. It is set by compile() and updated by compileLine().


commandLineCount

private int commandLineCount
This field counts the number of actual command lines found during the first round.


code

private java.util.Vector code
This array contains the code. During the first round this field holds the clean version of the code (stripped of compiler commands like def, ds, dc etc.)


symbolTable

private java.util.Vector symbolTable
This field acts as a symboltable, it is a String array vector where 1:st position holds the name of the symbol and the second either it's value (label, equ) or the command (ds 10, dc 10)


data

private java.lang.String[] data
This array contains the data.


codeMemoryLines

private MemoryLine[] codeMemoryLines
This field holds the Memoryline objects for the code. These are passed to the Application constructor at the end of the compile process, and are gathered during the second round from first round commands in code-array


dataMemoryLines

private MemoryLine[] dataMemoryLines
This field holds the Memoryline objects for the data area. Much like the code part, this is gathered during the second round and passed to the Application when getApplication() method is called.


compileFinished

private boolean compileFinished
This value tells if all the lines are processed twice and getApplication can be run.


compileDebugger

private CompileDebugger compileDebugger
This field contains the CompileDebugger instance to inform of any compilation happenings.


symbolicInterpreter

private SymbolicInterpreter symbolicInterpreter
This field contains the SymbolicInterpreter instance to use as part of the compilation.

Constructor Detail

Compiler

public Compiler()
This constructor sets up the class. It also initializes an instance of CompileDebugger.

Method Detail

compile

public void compile(java.lang.String source)
This function initializes transforms a symbolic source code into an application class. After this, call compileLine() to actually compile the application one line at a time, and finally getApplication() to get the finished application.

Parameters:
source - The symbolic source code to be compiled.

compileLine

public CompileInfo compileLine()
                        throws TTK91CompileException
This function goes through one line of the code. On the first round, it gathers the symbols and their definitions to a symbol table and conducts syntax-checking, on the second round it transforms each command to its binary format. For the transformations, the CompileConstants class is used. It calls the private methods firstRoundProcess() and secondRoundProcess() to do the actual work, if there is any to do. The transfer from first round of compilation to the second is done automatically; during it, initializeSecondRound() is called.

Returns:
A CompileInfo debug information object, describing what happened during the compilation of this line and whether this is the first or second round of compilation or null if there are no more lines left to process.
Throws:
TTK91CompileException - If a) there is a syntax error during the first round of checking (error code 101) or b) a symbol is still undefined after the first round of compilation is finished.

getApplication

public Application getApplication()
                           throws java.lang.IllegalStateException
This method returns the readily-compiled application if the compilation is complete, or null otherwise.

Throws:
java.lang.IllegalStateException

getSymbolicAndBinary

public MemoryLine getSymbolicAndBinary(int binary)
This function transforms a binary command number to a MemoryLine containing both the binary and the symbolic command corresponding to it.

Parameters:
binary - The command to be translated as binary.
Returns:
A MemoryLine instance containing both the information about the symbolic command and the binary version of it.

getSymbolicAndBinary

public MemoryLine getSymbolicAndBinary(MemoryLine binaryOnly)
This function transforms a MemoryLine containing only the binary command to a MemoryLine containing both the binary and the symbolic command corresponding to it.

Parameters:
binaryOnly - A MemoryLine containing the binary only of the command to be translated as binary. If the MemoryLine contains both, the pre-set symbolic value is ignored.
Returns:
A MemoryLine instance containing both the information about the symbolic command and the binary version of it.

firstRoundProcess

private CompileInfo firstRoundProcess(java.lang.String line)
                               throws TTK91CompileException
This function gathers new symbol information from the given line and checks its syntax. If a data reservation is detected, the dataAreaSize is incremented accordingly. If the line contains an actual command, commandLineCount is incremented.

Parameters:
line - The line of code to process.
Returns:
A CompileInfo object describing what was done, or null if the first round has been completed. This will be the sign for the compiler to prepare for the second round and start it.
Throws:
TTK91CompileException

initializeSecondRound

private CompileInfo initializeSecondRound()
This method initializes the code and data area arrays for the second round processing according to the dataAreaSize and commandLineCount variables. It also resets the StringTokenizer by calling tokenize(). Before entering the second round we must clear all the empty lines like compiler-codes (pseudo-codes) and lines with nothing but comments.


secondRoundProcess

private CompileInfo secondRoundProcess(java.lang.String line)
                                throws TTK91CompileException
This function transforms any commands to binary and stores both forms in the code array, or sets any initial data values in the data array. It uses CompileConstants as its aid.

Parameters:
line - The line of code to process.
Returns:
A CompileInfo object describing what was done, or null if the second round and thus the compilation has been completed.
Throws:
TTK91CompileException

parseLine

public java.lang.String[] parseLine(java.lang.String symbolicOpcode)
                             throws TTK91CompileException
This method parses a String and tries to find a label, opCode and all the other parts of a Command line.

Parameters:
symbolicOpcode - Symbolic form of an operation code.
Throws:
TTK91CompileException

parseCompilerCommandLine

public java.lang.String[] parseCompilerCommandLine(java.lang.String line)
                                            throws TTK91CompileException
This function tries to find a compiler command from the line. Works much like parseLine but this time valid opcodes are DEF, EQU, DC and DS.

Parameters:
line - String representing one line from source code.
Returns:
String array with label in position 0, command in the position 1 and position 2 contains the parameter.
Throws:
TTK91CompileException

validLabelName

private boolean validLabelName(java.lang.String labelName)
This method tests whether a label name contains at least one non-number and consists of 0-9, A-Ö and _. It does not check whether the label is in use already or if it is a reserved word.

Parameters:
labelName - The label name to test.
Returns:
True if the label consists of valid characters, false otherwise.