org.extex.ocpware.compiler.parser
Class CompilerState

java.lang.Object
  extended by org.extex.ocpware.compiler.parser.CompilerState

public final class CompilerState
extends java.lang.Object

This class provides a compiler state to translate an otp file into an ocp.

The processing is done in two phases. In the first phase a parse tree is build up. This happens within the method parse(InputStream) or CompilerState(InputStream) In the second phase the parse tree is traversed and code is generated. This happens within the method compile().

Version:
$Revision:6007 $
Author:
Gerd Neugebauer

Constructor Summary
CompilerState()
          Creates a new object.
CompilerState(java.io.InputStream stream)
          Creates a new object.
 
Method Summary
 OcpProgram compile()
          Compile the current compiler state into an ocp program.
 java.util.Map<java.lang.String,Left> getAliases()
          Getter for aliases.
 State getCurrentState()
          Getter for current state.
 java.util.List<Expression> getExpressions()
          Getter for expressions.
 int getIn()
          Getter for in.
 int getOut()
          Getter for out.
 java.util.List<State> getState()
          Getter for state.
 java.util.Map<java.lang.String,java.lang.Integer> getStates()
          Getter for states.
 java.util.Map<java.lang.String,Table> getTables()
          Getter for tables.
 void incrExpr()
          Increment the number of expressions for the current state.
 Left lookupAlias(java.lang.String alias)
          Find a value for an alias name.
 int lookupState(java.lang.String state)
          Find a number representation for a state name.
 int lookupTable(java.lang.String table)
          Get the numeric index of a table from the symbolic name.
 void parse(java.io.InputStream stream)
          Compile an input stream into an oc program.
 int putInstruction(int opCode)
          Put an instruction of one op code and no argument into the store.
 int putInstruction(int opCode, int n)
          Put an instruction of one op code and one argument into the store.
 int putInstruction(int opCode, int n, int a)
          Put an instruction of one op code and three arguments into the store.
 void setAliases(java.util.Map<java.lang.String,Left> aliases)
          Setter for aliases.
 void setCurrentState(int state)
          Setter for current state.
 void setCurrentState(java.lang.String state)
          Setter for the current state.
 void setExpressions(java.util.List<Expression> expressions)
          Setter for expressions.
 void setIn(int in)
          Setter for in.
 void setOut(int out)
          Setter for out.
 void setTables(java.util.Map<java.lang.String,Table> tables)
          Setter for tables.
 void storeState(java.lang.String name)
          Save a new name as name of a state.
 java.lang.String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CompilerState

public CompilerState()
              throws StateDefinedException
Creates a new object.

Throws:
StateDefinedException - in case that the INITIAL state is already known – extremely unlikely

CompilerState

public CompilerState(java.io.InputStream stream)
              throws java.io.IOException,
                     ArgmentTooBigException,
                     StateDefinedException,
                     SyntaxException,
                     TableDefinedException,
                     AliasDefinedException,
                     MissingExpressionsException
Creates a new object.

Parameters:
stream - the input stream
Throws:
java.io.IOException - in case of an I/O error
ArgmentTooBigException - if the argument of the instruction exceeds the 16 bit value
StateDefinedException - in case that a state is about to be defined a second time
TableDefinedException - in case that a table is about to be defined a second time
SyntaxException - in case of a syntax error
AliasDefinedException - in case that an attempt has been encountered to define an alias a second time
MissingExpressionsException - in case of a syntax error
Method Detail

compile

public OcpProgram compile()
                   throws AliasNotDefinedException,
                          ArgmentTooBigException,
                          java.io.IOException,
                          StateNotDefinedException,
                          TableNotDefinedException
Compile the current compiler state into an ocp program.

Returns:
the OCP program
Throws:
AliasNotDefinedException - in case that no matching alias is known for a symbolic table reference
ArgmentTooBigException - in case that an argument is encountered which does not fit into two bytes
java.io.IOException - in case of an I/O error
StateNotDefinedException - in case that no matching state is known for a symbolic table reference
TableNotDefinedException - in case that no matching table is known for a symbolic table reference

getAliases

public java.util.Map<java.lang.String,Left> getAliases()
Getter for aliases.

Returns:
the aliases

getCurrentState

public State getCurrentState()
Getter for current state.

Returns:
the current state

getExpressions

public java.util.List<Expression> getExpressions()
Getter for expressions.

Returns:
the expressions

getIn

public int getIn()
Getter for in. The default is 2.

Returns:
the in

getOut

public int getOut()
Getter for out. The default is 2.

Returns:
the out

getState

public java.util.List<State> getState()
Getter for state.

Returns:
the state

getStates

public java.util.Map<java.lang.String,java.lang.Integer> getStates()
Getter for states.

Returns:
the states

getTables

public java.util.Map<java.lang.String,Table> getTables()
Getter for tables.

Returns:
the tables

incrExpr

public void incrExpr()
Increment the number of expressions for the current state.


lookupAlias

public Left lookupAlias(java.lang.String alias)
                 throws AliasNotDefinedException
Find a value for an alias name.

Parameters:
alias - the string representation of the alias
Returns:
the left item of the alias
Throws:
AliasNotDefinedException - in case that the alias is not defined

lookupState

public int lookupState(java.lang.String state)
                throws StateNotDefinedException
Find a number representation for a state name.

Parameters:
state - the string representation of the state
Returns:
the number of the state
Throws:
StateNotDefinedException - in case that the state is not defined

lookupTable

public int lookupTable(java.lang.String table)
                throws TableNotDefinedException
Get the numeric index of a table from the symbolic name.

Parameters:
table - the name of the table
Returns:
the numeric index of a table
Throws:
TableNotDefinedException - in case that no matching table is known

parse

public void parse(java.io.InputStream stream)
           throws java.io.IOException,
                  ArgmentTooBigException,
                  SyntaxException,
                  StateDefinedException,
                  TableDefinedException,
                  AliasDefinedException,
                  MissingExpressionsException
Compile an input stream into an oc program.

Parameters:
stream - the input stream
Throws:
java.io.IOException - in case of an I/O error
ArgmentTooBigException - if the argument of the instruction exceeds the 16 bit value
SyntaxException - in case of a syntax error
StateDefinedException - in case that a state is about to be defined a second time
TableDefinedException - in case that a table is about to be defined a second time
AliasDefinedException - in case that an attempt has been encountered to define an alias a second time
MissingExpressionsException - in case of a syntax error

putInstruction

public int putInstruction(int opCode)
                   throws java.io.IOException
Put an instruction of one op code and no argument into the store.

Parameters:
opCode - the op code
Returns:
the index of the next free position in the instruction array
Throws:
java.io.IOException - in case of an I/O error
IllegalOpcodeException - in case of an illegal op code

putInstruction

public int putInstruction(int opCode,
                          int n)
                   throws java.io.IOException,
                          ArgmentTooBigException
Put an instruction of one op code and one argument into the store.

Parameters:
opCode - the op code
n - the first argument
Returns:
the index of the next free position in the instruction array
Throws:
java.io.IOException - in case of an I/O error
ArgmentTooBigException - if the argument of the instruction exceeds the 16 bit value
IllegalOpcodeException - in case of an illegal op code

putInstruction

public int putInstruction(int opCode,
                          int n,
                          int a)
                   throws java.io.IOException,
                          ArgmentTooBigException
Put an instruction of one op code and three arguments into the store.

Parameters:
opCode - the op code
n - the first argument
a - the second argument
Returns:
the index of the next free position in the instruction array
Throws:
java.io.IOException - in case of an I/O error
ArgmentTooBigException - if the argument of the instruction exceeds the 16 bit value
IllegalOpcodeException - in case of an illegal op code

setAliases

public void setAliases(java.util.Map<java.lang.String,Left> aliases)
Setter for aliases.

Parameters:
aliases - the aliases to set

setCurrentState

public void setCurrentState(int state)
Setter for current state.

Parameters:
state - the current state index

setCurrentState

public void setCurrentState(java.lang.String state)
                     throws StateNotDefinedException
Setter for the current state.

Parameters:
state - the new state
Throws:
StateNotDefinedException - in case that the state is not defined

setExpressions

public void setExpressions(java.util.List<Expression> expressions)
Setter for expressions.

Parameters:
expressions - the expressions to set

setIn

public void setIn(int in)
Setter for in.

Parameters:
in - the in to set

setOut

public void setOut(int out)
Setter for out.

Parameters:
out - the out to set

setTables

public void setTables(java.util.Map<java.lang.String,Table> tables)
Setter for tables.

Parameters:
tables - the tables to set

storeState

public void storeState(java.lang.String name)
                throws StateDefinedException
Save a new name as name of a state.

Parameters:
name - the name of the state
Throws:
StateDefinedException - in case that the name is already registered as state

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object
See Also:
Object.toString()