Class sfi

java.lang.Object
  extended bysfi
Direct Known Subclasses:
interpreter

public class sfi
extends java.lang.Object

Small Forth Interpreter
Description:
The Simple Forth Interpreter (SFI in short) is a Java™ implementation of a little subset of the ANS Forth language. The goal of SFI is to develop a small interpreter to be embedded in any application and provide scripting functionality with low resources.

$Id: sfi.java,v 1.25 2004/02/03 22:18:04 jjm Exp $

Since:
JDK 1.4.2

Constructor Summary
sfi()
          Creates a new instance of the interpreter.
 
Method Summary
 void addSystemWord(java.lang.String id, sfiInterface sfii)
          Adds a system word.
 int col()
          Column number consultor.
 boolean defineConstant(java.lang.String id, int value)
          Define a constant.
 void defineVariable(java.lang.String id, int value)
          Defines a variable.
 void deleteString(int address)
          Deletes a memory stored string.
 void error(java.lang.String message)
          Default error method.
 void execute(java.lang.String program)
          Execute a program.
 boolean execWord(java.lang.String id)
          Executes an user defined word.
 int getConstant(java.lang.String id)
          Retrieves the value of a constant.
 java.lang.String getString(int address)
          Retrieves a memory stored string.
 int getVariableValue(java.lang.String id)
          Retrieves the value stored in a variable.
 int ip()
          Instruction pointer consultor.
 boolean isDefinedConstant(java.lang.String id)
          Tell if a given constant is defined.
 boolean isDefinedVariable(java.lang.String id)
          Tell if a given variable is defined.
 boolean isDefinedWord(java.lang.String id)
          Tell if a given word is defined.
 boolean isStackEmpty()
          Checks if the stack is empty.
 int line()
          Line number consultor.
 void output(java.lang.String o)
          Default output method.
 void stackClear()
          Clears the stack.
 int stackPop()
          Retrieves a value from the stack.
 void stackPush(int value)
          Puts a value in the stack.
 int storeString(java.lang.String value)
          Stores a string in memory like C".
 void storeVariableValue(java.lang.String id, int value)
          Stores a value in a variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

sfi

public sfi()
Creates a new instance of the interpreter.

It defines the TRUE and FALSE constants.

Method Detail

isDefinedWord

public boolean isDefinedWord(java.lang.String id)
Tell if a given word is defined. This method works only for user defined WORDS, so system words will return 'not found'.

Parameters:
id - String that identifies the WORD.
Returns:
true if the word is defined.

execWord

public boolean execWord(java.lang.String id)
Executes an user defined word. The word must exist or an error will be triggered.

Parameters:
id - String that identifies the user defined WORD.
Returns:
false on error.

defineConstant

public boolean defineConstant(java.lang.String id,
                              int value)
Define a constant.

Parameters:
id - Identifier for the constant. Should be uppercase.
value - Value for the constant.
Returns:
false if the constant is already defined.

isDefinedConstant

public boolean isDefinedConstant(java.lang.String id)
Tell if a given constant is defined. System constants and user defined constants share de same space.

Parameters:
id - The constant identifier.
Returns:
true if the constant is defined.

getConstant

public int getConstant(java.lang.String id)
Retrieves the value of a constant. If the return value is 0 the only way to know if the constant is defined and that's its value is by calling isDefinedConstant.

Parameters:
id - The constant identifier.
Returns:
The value of the constant. Returns 0 if the constant is undefined.

storeString

public int storeString(java.lang.String value)
Stores a string in memory like C". A program is needed, so execute must be called before this method is used. An error message is triggered for debug purposes.

Parameters:
value - The string to store.
Returns:
The address of the string.

getString

public java.lang.String getString(int address)
Retrieves a memory stored string.

Parameters:
address - Address of the string.
Returns:
The string. Returns null if there's no string associated to the address.

deleteString

public void deleteString(int address)
Deletes a memory stored string.

Parameters:
address - Address of the string.

isDefinedVariable

public boolean isDefinedVariable(java.lang.String id)
Tell if a given variable is defined.

Parameters:
id - The variable identifier.
Returns:
true if the variable is defined.

defineVariable

public void defineVariable(java.lang.String id,
                           int value)
Defines a variable.

Parameters:
id - The variable identifier.
value - Initial value.

storeVariableValue

public void storeVariableValue(java.lang.String id,
                               int value)
Stores a value in a variable.

Parameters:
id - The variable identifier.
value - The value to store.

getVariableValue

public int getVariableValue(java.lang.String id)
Retrieves the value stored in a variable.

Parameters:
id - The variable identifier.
Returns:
The stored value.

stackPush

public void stackPush(int value)
Puts a value in the stack.

Parameters:
value - Value to push in the stack.

stackPop

public int stackPop()
Retrieves a value from the stack.

Returns:
The value. If the stack is empty, returns 0 and an error message is triggered for debug purposes.

isStackEmpty

public boolean isStackEmpty()
Checks if the stack is empty.

Returns:
true if there aren't items in the program stack.

stackClear

public void stackClear()
Clears the stack.


addSystemWord

public void addSystemWord(java.lang.String id,
                          sfiInterface sfii)
Adds a system word.

Parameters:
id - Word identifier.
sfii - Interface to the execWord method.

execute

public void execute(java.lang.String program)
Execute a program.

Parameters:
program - The program String.

line

public int line()
Line number consultor.

Returns:
Current line number.

col

public int col()
Column number consultor.

Returns:
Current column number.

ip

public int ip()
Instruction pointer consultor.

Returns:
Current instruction pointer.

error

public void error(java.lang.String message)
Default error method. System.err used by default.

Parameters:
message - Error message.

output

public void output(java.lang.String o)
Default output method. System.out used by default.

Parameters:
o - Output.