pnuts.lang
Class Context

java.lang.Object
  |
  +--pnuts.lang.Context
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
CompileContext, DebugContext

public class Context
extends java.lang.Object
implements java.lang.Cloneable

Context represents an internal state in Pnuts runtime environment. A Context object contains the following information.


Field Summary
protected  pnuts.lang.SymbolTable autoloadTable
           
protected  int beginLine
           
static java.io.PrintWriter defaultErrorStream
           
static java.io.PrintWriter defaultOutputStream
           
static java.io.PrintWriter defaultTerminalStream
           
protected  int depth
           
protected  int endLine
           
protected  pnuts.lang.SymbolTable environment
           
protected  pnuts.lang.SymbolTable importedFunctions
           
protected  pnuts.lang.ImportEnv importEnv
           
protected  pnuts.lang.Stack loadingResource
           
protected  pnuts.lang.SymbolTable provideTable
           
protected  java.util.Hashtable unitTable
           
protected  java.util.Hashtable usedPackage
           
 
Constructor Summary
Context()
          Create a new context
Context(Context context)
          Creates a context from a template
Context(Package pkg)
          Creates a context.
Context(java.lang.String pkg)
          Creates a context.
 
Method Summary
 void autoload(java.lang.String name, AutoloadHook hook)
           
 void autoload(java.lang.String name, java.lang.String file)
           
 java.lang.Object clone()
          Make a clone of the context
 java.lang.Object clone(boolean clear)
          Make a clone of the context
protected  void close()
           
 boolean defined(java.lang.String name)
           
 java.lang.Object get(java.lang.String key)
          Get an environemnt variable associated with this context
 java.lang.ClassLoader getClassLoader()
           
 Package getCurrentPackage()
          get the current package
 java.io.PrintWriter getErrorStream()
          Get an OutputStream or a Writer to which error() write message
 java.lang.Object getId(java.lang.String interned)
          Gets the value of a symbol
 java.lang.String getName()
          Gets the name of the context
 java.io.PrintWriter getOutputStream()
          get output stream of the context
 PnutsImpl getPnutsImpl()
          Gets the PnutsImpl object associated with this context
protected  java.lang.Object getScriptSource()
          Get the source of the script.
 java.io.PrintWriter getTerminalStream()
          get terminal-output-stream of the context
 java.util.Enumeration keys()
          Returns an enumeration of the keys in the environment of this context.
protected  void onError(java.lang.Throwable t)
          This method is called when an exception is thrown.
protected  void onExit(java.lang.Object arg)
          This method is called when the excecution is terminated normally.
protected  void open(java.lang.String[] locals)
           
 void registerQuantityFactory(java.lang.String unit, QuantityFactory fac)
          Defines a unit.
 void set(java.lang.String key, java.lang.Object value)
          Define an environemnt variable associated with this context
 void setClassLoader(java.lang.ClassLoader loader)
           
 void setCurrentPackage(Package pkg)
          set the current package
 void setErrorStream(java.lang.Object errorStream)
          set an OutputStream or a Writer to which error() write message If errorStream is null, exception is thrown out of eval loop.
 void setErrorStream(java.lang.Object errorStream, boolean autoFlush)
          Set an OutputStream or a Writer to which error() write message If errorStream is null, exception is thrown out of eval loop.
 void setName(java.lang.String name)
          Sets the name of the context
 void setOutputStream(java.lang.Object outputStream)
           
 void setOutputStream(java.lang.Object out, boolean autoFlush)
          set output stream of the context
 void setPnutsImpl(PnutsImpl impl)
          Changes the PnutsImpl object associated with this context
 void setTerminalStream(java.lang.Object stream)
           
 void setTerminalStream(java.lang.Object str, boolean autoFlush)
          set terminal-output-stream of the context
protected  void setValue(java.lang.String symbol, java.lang.Object obj)
           
protected  java.lang.Object undefinedHook(java.lang.String symbol)
           
protected  void updateLine(SimpleNode node)
           
protected  void updateLine(SimpleNode node, int beginLine, int endLine)
           
 java.lang.String[] usedPackages()
          Returns the list of use()'d packages
 void usePackage(java.lang.String name)
          Add a package to the use()'d package list.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultOutputStream

public static final java.io.PrintWriter defaultOutputStream

defaultTerminalStream

public static final java.io.PrintWriter defaultTerminalStream

defaultErrorStream

public static final java.io.PrintWriter defaultErrorStream

depth

protected int depth

loadingResource

protected pnuts.lang.Stack loadingResource

beginLine

protected int beginLine

endLine

protected int endLine

importEnv

protected pnuts.lang.ImportEnv importEnv

usedPackage

protected java.util.Hashtable usedPackage

autoloadTable

protected pnuts.lang.SymbolTable autoloadTable

importedFunctions

protected pnuts.lang.SymbolTable importedFunctions

provideTable

protected pnuts.lang.SymbolTable provideTable

unitTable

protected java.util.Hashtable unitTable

environment

protected pnuts.lang.SymbolTable environment
Constructor Detail

Context

public Context()
Create a new context

Context

public Context(java.lang.String pkg)
Creates a context.
Parameters:
pkg - the name of the package.

Context

public Context(Package pkg)
Creates a context.
Parameters:
pkg - the initial package of the context.

Context

public Context(Context context)
Creates a context from a template
Parameters:
context - The template
Since:
1.0beta9
Method Detail

clone

public java.lang.Object clone()
Make a clone of the context
Overrides:
clone in class java.lang.Object

clone

public java.lang.Object clone(boolean clear)
Make a clone of the context
Parameters:
clear - If true, import() state and current package are reset to the default values.

setName

public void setName(java.lang.String name)
Sets the name of the context
Parameters:
name - The name of the context.

getName

public java.lang.String getName()
Gets the name of the context
Returns:
The name of the context.

setPnutsImpl

public void setPnutsImpl(PnutsImpl impl)
Changes the PnutsImpl object associated with this context
Parameters:
impl - The PnutsImpl object, which defines the implementation of the interpreter. eval(), load(), and loadFile() of pnuts.lang.Pnuts select an implementation (pure interpreter, on-the-fly compiler, etc.), according to the context passed to the methods.

getPnutsImpl

public PnutsImpl getPnutsImpl()
Gets the PnutsImpl object associated with this context

get

public java.lang.Object get(java.lang.String key)
Get an environemnt variable associated with this context
Parameters:
key - the name of the variable
Returns:
the value of the variable
Since:
1.0beta8

set

public void set(java.lang.String key,
                java.lang.Object value)
Define an environemnt variable associated with this context

The key name which starts with "pnuts." is reserved.

Parameters:
key - the name of the variable
value - the value of the variable
Since:
1.0beta8

keys

public java.util.Enumeration keys()
Returns an enumeration of the keys in the environment of this context.

setOutputStream

public void setOutputStream(java.lang.Object out,
                            boolean autoFlush)
set output stream of the context

setOutputStream

public void setOutputStream(java.lang.Object outputStream)

getOutputStream

public java.io.PrintWriter getOutputStream()
get output stream of the context

setTerminalStream

public void setTerminalStream(java.lang.Object str,
                              boolean autoFlush)
set terminal-output-stream of the context

setTerminalStream

public void setTerminalStream(java.lang.Object stream)

getTerminalStream

public java.io.PrintWriter getTerminalStream()
get terminal-output-stream of the context

setErrorStream

public void setErrorStream(java.lang.Object errorStream,
                           boolean autoFlush)
Set an OutputStream or a Writer to which error() write message If errorStream is null, exception is thrown out of eval loop.

setErrorStream

public void setErrorStream(java.lang.Object errorStream)
set an OutputStream or a Writer to which error() write message If errorStream is null, exception is thrown out of eval loop.

getErrorStream

public java.io.PrintWriter getErrorStream()
Get an OutputStream or a Writer to which error() write message

getCurrentPackage

public Package getCurrentPackage()
get the current package

setCurrentPackage

public void setCurrentPackage(Package pkg)
set the current package

setClassLoader

public void setClassLoader(java.lang.ClassLoader loader)

getClassLoader

public java.lang.ClassLoader getClassLoader()

usedPackages

public java.lang.String[] usedPackages()
Returns the list of use()'d packages

usePackage

public void usePackage(java.lang.String name)
Add a package to the use()'d package list.
Parameters:
name - the package name

getScriptSource

protected java.lang.Object getScriptSource()
Get the source of the script.
Returns:
 java.net.URL object, when the script is not precompiled
 pnuts.lang.Runtime object, when the script is precompiled
 

updateLine

protected void updateLine(SimpleNode node)

updateLine

protected void updateLine(SimpleNode node,
                          int beginLine,
                          int endLine)

onExit

protected void onExit(java.lang.Object arg)
This method is called when the excecution is terminated normally.

onError

protected void onError(java.lang.Throwable t)
This method is called when an exception is thrown.

getId

public java.lang.Object getId(java.lang.String interned)
Gets the value of a symbol
Parameters:
interned - a symbol (interned string)
Returns:
the value of the symbol

autoload

public void autoload(java.lang.String name,
                     java.lang.String file)

autoload

public void autoload(java.lang.String name,
                     AutoloadHook hook)

undefinedHook

protected java.lang.Object undefinedHook(java.lang.String symbol)

defined

public boolean defined(java.lang.String name)

registerQuantityFactory

public void registerQuantityFactory(java.lang.String unit,
                                    QuantityFactory fac)
Defines a unit.
Parameters:
unit - The unit symbol
fac - A QuantityFactory object which defines what kind of object is created when a decimal number with this unit symbol is evaluated.

open

protected void open(java.lang.String[] locals)

close

protected void close()

setValue

protected void setValue(java.lang.String symbol,
                        java.lang.Object obj)