|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.scheinerman.phoenix.interpreter.Interpreter
public class Interpreter
Interprets a block of code and allocates variables and functions. This class provides basic parsing of Phoenix programs. This class is meant to be subclassed in order to provide more specific parsing capability.
Field Summary | |
---|---|
protected boolean |
_break
Indicates whether or not a break was encountered in this scope. |
protected boolean |
_continue
Indicates whether or not a continue was encountered in this scope. |
protected boolean |
_return
Indicates whether or not a return was encountered in this scope. |
protected int |
breakValue
Indicates the value after a break statement that was called within this scope. |
protected java.lang.String |
code
The raw code that is to be interpreted in this section. |
protected java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> |
fat
The Function Allocation Table (FAT) stores functions and their values for all scopes of the interpretation process. |
protected java.lang.String |
file
The path of the file that is being interpreted. |
private java.util.regex.Pattern |
identPattern
Sets up a compiled pattern which is used to test the validity of function and variable identifiers. |
java.util.HashSet<java.lang.String> |
keywordList
This set contains all the Phoenix keywords for cross referencing when testing whether a identifier for a function or a variable is valid. |
protected int |
line
The current relative line that is being interpreted. |
protected int |
line_diff
The amount of lines of code that were encountered before this interpretation. |
protected boolean |
printValues
Indicates whether or not answer values should be printed to the screen. |
protected Variable |
retType
A simple variable whose type indicates what the return type of this interpreter is. |
protected boolean |
retTypeSet
Indicates whether or not the return type of this interpreter has been set. |
protected Variable |
retValue
The value returned after a return statement. |
protected java.lang.String[] |
run_code
The code to be interpreted broken into an array of strings by new line characters. |
protected boolean |
stop
Indicates whether or not the interpretation should stop prematurely. |
private boolean |
topLevel
Indicates whether or not this is a top level interpreter. |
protected java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> |
vat
The Variable Allocation Table (VAT) stores variables and their values for all scopes of the interpretation process. |
Constructor Summary | |
---|---|
Interpreter()
Instantiates a new Interpreter with no given code to interpret. |
|
Interpreter(boolean topLevel,
java.lang.String file,
java.lang.String code,
int line,
java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> vat,
java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> fat)
Instantiates a new Interpreter that starts at some point in a file. |
|
Interpreter(boolean topLevel,
java.lang.String file,
java.lang.String code,
java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> vat,
java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> fat)
Instantiates a new Interpreter that starts at the beginning of a file. |
Method Summary | |
---|---|
private void |
_break(java.lang.String code)
Deals with what should happen if a break is encountered. |
private int |
_continue(int line)
Deals with what should happen if a continue is encountered. |
private int |
_do(java.lang.String code,
int line)
Deals with what should happen if a do block is encountered. |
private int |
_for(java.lang.String code,
int line)
Deals with what should happen if a for block is encountered. |
private int |
_if(java.lang.String code,
int line)
Deals with what should happen if a if/else statement is encountered. |
private int |
_return(java.lang.String code,
int line)
Deals with what should happen if a return statement is encountered. |
private int |
_switch(java.lang.String code,
int line)
Deals with what should happen if a switch block is encountered. |
private void |
_throw(java.lang.String code)
|
private int |
_try(int line)
|
private int |
_while(java.lang.String trimmed,
int line)
Deals with what should happen if a while block is encountered. |
protected void |
dealWithBreak(int value)
Deals with a break statement with a given integer value. |
protected int |
dealWithContinue(int line)
Deals with a continue statement on a given line of code. |
protected void |
dealWithError(PhoenixRuntimeException e)
|
protected void |
dealWithReturn(Variable passed)
Deals with a return statement with a given variable that has been passed through the
return statement. |
boolean |
delete(java.lang.String identifier)
Deletes any instance of the use of the identifier from the VAT or FAT. |
protected void |
doRun()
Does simple interpretation of the given code for this Interpreter class. |
java.lang.String |
getBlock(int start)
Returns a block of code with greater indentation than the current block starting at the given line. |
Function |
getFunction(java.lang.String identifier)
Returns the function from the FAT with the given identifier. |
java.lang.Object[][] |
getFunctionArguments(java.lang.String line)
|
Function |
getFunctionFromDefinition(java.lang.String line,
java.lang.String code,
boolean nat,
boolean setupDocs)
|
Variable |
getReturnValue()
Gets the variable returned by a return statement in this interpretation. |
Variable |
getVariable(java.lang.String identifier)
Returns the variable with the given identifier. |
static int |
getWhitespace(java.lang.String line)
Returns the amount of whitespace prior to the code in a line of code. |
private void |
initKeywordList()
Sets up the list of keywords in the language. |
protected void |
initNewTables()
Creates a new VAT and a new FAT and adds them to the front of the overall VAT queue and FAT queue. |
boolean |
isValidIdentifier(java.lang.String identifier)
Determines whether or not a given identifier is a valid variable or function identifier in the current scope. |
static Variable |
makeDefaultVariable(java.lang.String type)
Creates an empty variable (using that variable's default constructor) given the identifier of the variable. |
int |
makeFunction(java.lang.String line,
java.lang.String code,
java.lang.String prefix)
Creates a Function based on the given line of code and adds it to the appropriate place
in the FAT. |
void |
makeVariable(java.lang.String code)
Creates a variable and adds it to the appropriate VAT based on a line of code. |
Variable |
makeVariable(java.lang.String type,
Variable value)
Given a variable type and a value, creates a Variable object with that value. |
Variable |
parsePhrase(java.lang.String phrase)
Returns the variable equivalent of a line of code. |
void |
putFunction(Function f,
java.util.HashMap<java.lang.String,Function> hm)
Adds a function to the FAT if it has a valid identifier. |
static java.lang.String |
removeComments(java.lang.String line)
Returns a version of the string with the comments stripped from it. |
static void |
removeWhitespace(java.lang.String code)
Strips a string of code of any extraneous whitespace outside of quotes within that string. |
protected boolean |
retValueMatches()
Tests whether or not the value returned by a return has the same type as the
predetermined return value type for a function. |
void |
run()
Begins the interpretation process. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected java.lang.String file
private boolean topLevel
protected boolean retTypeSet
Function
class.
protected Variable retType
Function
class to determine whether the returned variable is of the correct
type. If this is an instance of NumberVariable
the return type is num
.
If it is an instance of StringVariable
the return type is str
. If this is
null
, then the return type is void
.
protected Variable retValue
retType
. This is used in the Function
class.
protected boolean printValues
protected int breakValue
break
statement that was called within this scope.
protected boolean _break
break
was encountered in this scope.
protected boolean _continue
continue
was encountered in this scope.
protected boolean _return
return
was encountered in this scope.
protected boolean stop
protected int line
protected int line_diff
protected java.lang.String code
protected java.lang.String[] run_code
protected java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> vat
Variable
class equivalent. The last element of the list
is the global scope, the second to the last is the top-level local scope. The front element of the
list is the the most local scope.
protected java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> fat
Function
class equivalent. The last element of the list
is the global scope, the second to the last is the top-level local scope. The front element of the
list is the the most local scope.
public java.util.HashSet<java.lang.String> keywordList
initKeywordList()
method in the constructors of this class.
private java.util.regex.Pattern identPattern
Constructor Detail |
---|
public Interpreter()
Interpreter
with no given code to interpret. The interpretation
cannot be started using run()
.
public Interpreter(boolean topLevel, java.lang.String file, java.lang.String code, java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> vat, java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> fat)
topLevel
- - This is true
if this interpreter is on the top level scope of the
file, false
otherwise.file
- - The relative path of the file of input. Used for error message outputs.code
- - The code contained in the file. If this is a top-level interpretation, this should
be the entire contents of the file. Otherwise, it should be the section of the file meant to be
interpreted.vat
- - The Variable Allocation Table (VAT) for this interpretation. If this is a top-level
interpretation, this should only contain a table for global variables.fat
- - The Function Allocation Table (FAT) for this interpretation. If this is a top-level
interpretation, this should only contain a table for global functions.public Interpreter(boolean topLevel, java.lang.String file, java.lang.String code, int line, java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> vat, java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> fat)
topLevel
- - This is true
if this interpreter is on the top level scope of the
file, false
otherwise.file
- - The relative path of the file of input. Used for error message outputs.code
- - The code contained in the file. If this is a top-level interpretation, this should
be the entire contents of the file. Otherwise, it should be the section of the file meant to be
interpreted.line
- - The starting line of this interpretation.vat
- - The Variable Allocation Table (VAT) for this interpretation. If this is a top-level
interpretation, this should only contain a table for global variables.fat
- - The Function Allocation Table (FAT) for this interpretation. If this is a top-level
interpretation, this should only contain a table for global functions.Method Detail |
---|
private final void initKeywordList()
protected final void initNewTables()
public void run()
doRun()
method and then
removes the front entries of the VAT and FAT queues. If the interpreter class is to be subclassed,
this method should be overridden to provide surrounding functionality to the doRun()
method.
protected void doRun()
Interpreter
class. For most
subclasses,this method should not be overridden. However, there are some instances where it might
prove useful to override this method (for example, the SwitchInterpreter
class overrides
this method).
SwitchInterpreter}
protected void dealWithError(PhoenixRuntimeException e)
protected boolean retValueMatches()
return
has the same type as the
predetermined return value type for a function. This method is only used by Interpreter
s
that are within the scope of a function.
true
if the types match, false
otherwise.public final void putFunction(Function f, java.util.HashMap<java.lang.String,Function> hm)
f
- - The function to add to the table.hm
- - The table the function will be added to.
IllegalIdentifierException
- if the identifier is invalid, as determined by the
isValidIdentifier()
method.public static final java.lang.String removeComments(java.lang.String line)
public final Variable parsePhrase(java.lang.String phrase)
Parser
class.
phrase
- - a phrase of code
private void _break(java.lang.String code)
break
is encountered.
code
- - The code of the break
statement.private int _continue(int line)
continue
is encountered.
line
- - The number of the line on which the continue
is encountered.
continue
statement.private int _do(java.lang.String code, int line)
do
block is encountered.
code
- - The code of the line containing the do
statement.line
- - The number of the line on which the do
statement is encountered.
do
block.
SyntaxException
- if there is no colon at the end of the statement.private int _for(java.lang.String code, int line)
for
block is encountered.
code
- - The code of the line containing the for
statement.line
- - The number of the line on which the for
statement is encountered.
for
block.
SyntaxException
- if there is no colon at the end of the statement.private int _if(java.lang.String code, int line)
if/else
statement is encountered.
code
- - The code of the line containing the if/else
statement.line
- - The number of the line on which the if/ese
statement is encountered.
if/else
block.
SyntaxException
- if there is no colon at the end of the statement.private int _return(java.lang.String code, int line)
return
statement is encountered.
code
- - The code of the line containing the return
statement.line
- - The number of the line on which the return
statement is encountered.
return
statement.private int _switch(java.lang.String code, int line)
switch
block is encountered.
code
- - The code of the line containing the switch
statement.line
- - The number of the line on which the switch
statement is encountered.
switch
block.
SyntaxException
- if there is no colon at the end of the statement.private void _throw(java.lang.String code)
private int _try(int line)
private int _while(java.lang.String trimmed, int line)
while
block is encountered.
code
- - The code of the line containing the while
statement.line
- - The number of the line on which the while
statement is encountered.
while
block.
SyntaxException
- if there is no colon at the end of the statement.protected void dealWithBreak(int value)
break
statement with a given integer value. This method is meant to be
overridden in subclasses if subclasses deal with breaks differently. For example, this class just
passes the break up to the Interpreter
of greater scope. However, subclasses of
LoopInterpreter
subtract one from the break value and pass it up if it is greater than zero.
value
- - The number of breaks that should be executed.
SyntaxException
- if this is a top level interpretation or the value is less than or equal
to zero.protected int dealWithContinue(int line)
continue
statement on a given line of code. This method is meant to be
overridden in subclasses where continues make a difference. For example, LoopInterpreter
classes override this method to perform their specific continue action.
line
- - The number of the line on which the continue is encountered.
SyntaxException
- if this is a top level interpretation.protected void dealWithReturn(Variable passed)
return
statement with a given variable that has been passed through the
return statement. If no variable was passed, the parameter should just be left as null
.
passed
- - The variable passed via the return statement.
SyntaxException
- - if this is a top level interpretation.public final java.lang.String getBlock(int start)
start
- - The number of the line to start on.
public final int makeFunction(java.lang.String line, java.lang.String code, java.lang.String prefix)
Function
based on the given line of code and adds it to the appropriate place
in the FAT.
line
- - The line of code to create the function from.code
- - The code that the function will run.prefix
- - Any prefix to the functions name that might be appended. This is used for imported
functions.public final Function getFunctionFromDefinition(java.lang.String line, java.lang.String code, boolean nat, boolean setupDocs)
public final void makeVariable(java.lang.String code)
code
- - A line of code.public final Variable makeVariable(java.lang.String type, Variable value)
Variable
object with that value.
type
- - The type of value. This must be either num
or str
.value
- - The value to assign to that variable type.
public static final Variable makeDefaultVariable(java.lang.String type)
null
.
name
- - The variable identifier
null
if "void."public final boolean isValidIdentifier(java.lang.String identifier)
identifier
- - A variable or function identifier.
true
if it is valid, false
otherwise.public final Function getFunction(java.lang.String identifier)
null
value will be returned.
identifier
- - The identifier of the desired function.
null
if no such identifier is taken for a function.public final Variable getVariable(java.lang.String identifier)
null
value is returned.
identifier
- - The identifier of the desired variable.
null
if no such identifier is taken for a variable.public final boolean delete(java.lang.String identifier)
identifier
- - The identifier of a variable or function to be deleted.
true
if the identifier is in use and has been deleted, false
otherwise.public final java.lang.Object[][] getFunctionArguments(java.lang.String line)
public static final int getWhitespace(java.lang.String line)
line
- - A line of code.
public static final void removeWhitespace(java.lang.String code)
code
- - The code to be stripped of whitespace.public final Variable getReturnValue()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |