pnuts.lang
Class Context

java.lang.Object
  extended by pnuts.lang.Context
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
DebugContext

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

Context represents an internal state of a particular script execution. A Context is created when start executing a script and passed around during the execution. A pnuts.lang.Context object contains the following information.

  1. Current Package (which Pnuts-package being used)
  2. Imported Java-package list
  3. Writer to which print() write data
  4. Writer to which error() write message
  5. ClassLoader
  6. Modules added with use() function.
  7. Units
  8. Environments (accessed by Context.get() and set())
  9. Stack frame (for the pure interpreter)
  10. Encoding
A clone is created when eval(), load(), or loadFile() is called in a script. When a clone is created, (1) and (2) of the clone are reset to the default value.


Field Summary
protected  int beginColumn
           
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.ImportEnv importEnv
           
protected  org.pnuts.util.Cell loadingResource
           
protected  pnuts.lang.ModuleList moduleList
           
protected  pnuts.lang.SymbolTable provideTable
           
protected  java.util.Hashtable unitTable
           
 
Constructor Summary
Context()
          Create a new context
Context(Context context)
          Creates a context from a template
Context(Package pkg)
          Creates a context.
Context(java.util.Properties properties)
           
Context(java.lang.String pkg)
          Creates a context.
 
Method Summary
 void autoload(java.lang.String name, AutoloadHook hook)
          Registers an AutoloadHook for the name in the current package.
 void autoload(java.lang.String name, java.lang.String file)
          Registers an autoload script for the name.
 void clearPackages()
          Unregisteres all use()'d packages
 java.lang.Object clone()
          Make a clone of the context
 java.lang.Object clone(boolean clear_attributes, boolean clear_locals)
          Make a clone of the context
protected  void close(Function func, java.lang.Object[] args)
           
 boolean defined(java.lang.String name)
          Checks if the name is defined in the context.
 java.lang.Object get(java.lang.String symbol)
          Gets an environemnt variable associated with this context.
 java.lang.ClassLoader getClassLoader()
          Gets the current class loader.
 Configuration getConfiguration()
           
 Package getCurrentPackage()
          get the current package
 java.io.PrintWriter getErrorStream()
          Deprecated. replaced by getErrorWriter
 java.io.PrintWriter getErrorWriter()
          Get an PrintWriter to which error() write messages
 Executable getExitHook()
          Gets the hook to be executed at the end of a script
 java.lang.Object getId(java.lang.String interned)
          Gets the value of a symbol.
 Implementation getImplementation()
          Gets the Implementation object associated with this context
 java.lang.String getName()
          Gets the name of the context
 java.io.OutputStream getOutputStream()
          Get the standard output stream of the context, to which write() writes data.
 PnutsImpl getPnutsImpl()
          Deprecated. replaced by getImplementation()
 java.lang.String getScriptEncoding()
          Gets the current script encoding
protected  java.lang.Object getScriptSource()
          Get the source of the script.
 java.io.PrintWriter getTerminalStream()
          Deprecated. replaced by getTerminalWriter(Writer)
 java.io.PrintWriter getTerminalWriter()
          get terminal-output-stream of the context
protected  java.lang.Object getValue(java.lang.String symbol)
           
 java.io.PrintWriter getWriter()
          Get the standard writer of the context, to which print()/println() write messages.
 boolean isVerbose()
          Check the current verbose mode
 java.util.Enumeration keys()
          Returns an enumeration of the keys in the environment of this context.
protected  void loadModule(java.lang.String name, Package pkg)
          Loads a module is it has not been loaded yet.
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(Function f, java.lang.Object[] args)
           
 void registerQuantityFactory(java.lang.String unit, QuantityFactory fac)
          Defines a unit.
 java.lang.Class resolveClass(java.lang.String symbol)
           
 java.lang.Object resolveSymbol(java.lang.String interned)
          Resolves the value of a symbol in the following order: (1) current package (2) builtin functions, primitive types, pnuts_version (3) module exports (4) imported classes (5) parent packages
 void set(java.lang.String symbol, java.lang.Object value)
          Defines an environemnt variable associated with this context To access those environment variables, Context.get(String) should be called.
 void setClassLoader(java.lang.ClassLoader loader)
          Changes the current class loader for this context.
 void setConfiguration(Configuration config)
          Changes the configuration for this context.
 void setCurrentPackage(Package pkg)
          set the current package
 void setErrorStream(java.lang.Object errorStream)
          Deprecated. replaced by setErrorWriter(Writer)
 void setErrorStream(java.lang.Object errorStream, boolean autoFlush)
          Deprecated. replaced by setErrorWriter(Writer, boolean)
 void setErrorWriter(java.io.Writer w)
           
 void setErrorWriter(java.io.Writer w, boolean autoFlush)
           
 void setExitHook(Executable hook)
          Sets a hook to be executed at the end of a script.
 void setImplementation(Implementation impl)
          Changes the Implementation object associated with this context
 void setName(java.lang.String name)
          Sets the name of the context
 void setOutputStream(java.lang.Object outputStream)
          Deprecated. replaced by setTerminalWriter(Writer)
 void setOutputStream(java.lang.Object out, boolean autoFlush)
          Deprecated. replaced by setTerminalWriter(Writer, boolean)
 void setOutputStream(java.io.OutputStream out)
          Set the specified OutputStream as the standard output stream of the context, to which write() writes data.
 void setPnutsImpl(PnutsImpl impl)
          Deprecated. replaced by setImplementation()
 void setScriptEncoding(java.lang.String encoding)
          Changes the script encoding for the context
 void setTerminalStream(java.lang.Object stream)
          Deprecated. replaced by setTerminalWriter(Writer)
 void setTerminalStream(java.lang.Object str, boolean autoFlush)
          Deprecated. replaced by setTerminalWriter(Writer, boolean)
 void setTerminalWriter(java.io.Writer w)
          Set the terminal writer of the context
 void setTerminalWriter(java.io.Writer w, boolean autoFlush)
          Set the terminal writer of the context
protected  void setValue(java.lang.String symbol, java.lang.Object obj)
           
 void setVerbose(boolean b)
          Sets the verbose mode
 void setWriter(java.io.Writer out)
          Set the specified Writer as the standard writer of the context.
 void setWriter(java.io.Writer out, boolean autoFlush)
          Set the specified Writer as the standard writer of the context.
 boolean unusePackage(Package pkg)
           
protected  void updateColumn(int column)
           
protected  void updateLine(int line)
          Both AST interpreter and compiler call this method when line number changes.
protected  void updateLine(SimpleNode node, int beginLine, int beginColumn)
          AST interpreter calls this method when line number changes, giving AST nodes and line information Not that compiler does not call this method.
 java.lang.String[] usedPackages()
          Returns the list of use()'d packages
 boolean usePackage(Package pkg, boolean checkException)
           
 boolean usePackage(java.lang.String name)
          Add a package to the use()'d package list.
 boolean usePackage(java.lang.String name, boolean checkException)
          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 org.pnuts.util.Cell loadingResource

beginLine

protected int beginLine

endLine

protected int endLine

beginColumn

protected int beginColumn

importEnv

protected pnuts.lang.ImportEnv importEnv

moduleList

protected pnuts.lang.ModuleList moduleList

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. If null, the global package is used.

Context

public Context(Context context)
Creates a context from a template

Parameters:
context - The template
Since:
1.0beta9

Context

public Context(java.util.Properties properties)
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_attributes,
                              boolean clear_locals)
Make a clone of the context

Parameters:
clear_attributes - If true, import() state and current package are reset to the default values.
clear_locals - If true, local stack is reset.

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)
Deprecated. replaced by setImplementation()

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()
Deprecated. replaced by getImplementation()

Gets the PnutsImpl object associated with this context


setImplementation

public void setImplementation(Implementation impl)
Changes the Implementation object associated with this context

Parameters:
impl - The Implementation 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.

getImplementation

public Implementation getImplementation()
Gets the Implementation object associated with this context


get

public java.lang.Object get(java.lang.String symbol)
Gets an environemnt variable associated with this context.

Parameters:
symbol - the name of the variable, which must be intern'ed.
Returns:
the value of the variable

set

public void set(java.lang.String symbol,
                java.lang.Object value)
Defines an environemnt variable associated with this context To access those environment variables, Context.get(String) should be called. Note that those variables can not be accessed just by specifying their names in Pnuts interpreter. Since the environment varariables are bound to the executing context, they are accessible from various modules that the script uses. Therefore, the name of environment variables should have prefixes so that name conflict is unlikely to occur. The name that starts with "pnuts." is reserved.

Parameters:
symbol - the name of the variable, which must be intern'ed.
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)
Deprecated. replaced by setTerminalWriter(Writer, boolean)

set output stream of the context


setOutputStream

public void setOutputStream(java.lang.Object outputStream)
Deprecated. replaced by setTerminalWriter(Writer)

set output stream of the context


setOutputStream

public void setOutputStream(java.io.OutputStream out)
Set the specified OutputStream as the standard output stream of the context, to which write() writes data. A PrintWriter is created from the specified OutputStream, which is returned by getWriter(). If null is specified, both getOutputStream() and getWriter() return null.

Parameters:
out - the OutputStream

setWriter

public void setWriter(java.io.Writer out)
Set the specified Writer as the standard writer of the context. PrintWriter is created from the specified Writer if the Writer is not an instance of PrintWriter. If this method has been called, getOutputStream() returns null. If null is specifed to this method, both getWriter() and getOutputStream() return null.

Parameters:
out - the Writer

setWriter

public void setWriter(java.io.Writer out,
                      boolean autoFlush)
Set the specified Writer as the standard writer of the context. PrintWriter is created from the specified Writer if the Writer is not an instance of PrintWriter. If this method has been called, getOutputStream() returns null. If null is specifed to this method, both getWriter() and getOutputStream() return null.

Parameters:
out - the Writer
autoFlush - A boolean; if true, the PrintWriter.println() methods will flush the output buffer

getOutputStream

public java.io.OutputStream getOutputStream()
Get the standard output stream of the context, to which write() writes data. This method returns the OutputStream previously set by setOutputStream(). If setWriter() has been called, getOutputStream() returns null.

Returns:
the standard output stream of the context

getWriter

public java.io.PrintWriter getWriter()
Get the standard writer of the context, to which print()/println() write messages.

Returns:
the standard writer of the context

setTerminalStream

public void setTerminalStream(java.lang.Object str,
                              boolean autoFlush)
Deprecated. replaced by setTerminalWriter(Writer, boolean)

Set the terminal stream of the context, in which the prompt is shown.


setTerminalStream

public void setTerminalStream(java.lang.Object stream)
Deprecated. replaced by setTerminalWriter(Writer)

Set the terminal stream of the context


setTerminalWriter

public void setTerminalWriter(java.io.Writer w)
Set the terminal writer of the context

Parameters:
w - the Writer

setTerminalWriter

public void setTerminalWriter(java.io.Writer w,
                              boolean autoFlush)
Set the terminal writer of the context

Parameters:
w - the Writer

getTerminalStream

public java.io.PrintWriter getTerminalStream()
Deprecated. replaced by getTerminalWriter(Writer)

get terminal-output-stream of the context


getTerminalWriter

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


setErrorStream

public void setErrorStream(java.lang.Object errorStream,
                           boolean autoFlush)
Deprecated. replaced by setErrorWriter(Writer, boolean)

Set an OutputStream or a Writer to which error() write messages If errorStream is null, exception is thrown out of eval loop.


setErrorStream

public void setErrorStream(java.lang.Object errorStream)
Deprecated. replaced by setErrorWriter(Writer)

Set ar PrintWriter to which error() write messages


setErrorWriter

public void setErrorWriter(java.io.Writer w,
                           boolean autoFlush)

setErrorWriter

public void setErrorWriter(java.io.Writer w)

getErrorStream

public java.io.PrintWriter getErrorStream()
Deprecated. replaced by getErrorWriter

Get an OutputStream or a Writer to which error() write messages


getErrorWriter

public java.io.PrintWriter getErrorWriter()
Get an PrintWriter to which error() write messages


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)
Changes the current class loader for this context. The initial value is set to Thread.currentThread().getContextClassLoader() when the instance is created.

Parameters:
loader - the class loader

getClassLoader

public java.lang.ClassLoader getClassLoader()
Gets the current class loader. The initial value is set to Thread.currentThread().getContextClassLoader() when the instance is created.

Returns:
the class loader

setConfiguration

public void setConfiguration(Configuration config)
Changes the configuration for this context.

Parameters:
config - the configuration

getConfiguration

public Configuration getConfiguration()

setExitHook

public void setExitHook(Executable hook)
Sets a hook to be executed at the end of a script. The default value is null.

Parameters:
hook - the hook

getExitHook

public Executable getExitHook()
Gets the hook to be executed at the end of a script


unusePackage

public boolean unusePackage(Package pkg)

usePackage

public boolean usePackage(Package pkg,
                          boolean checkException)

usePackage

public boolean usePackage(java.lang.String name)
Add a package to the use()'d package list.

Parameters:
name - the package name
Returns:
true if successfully use()'d.

usePackage

public boolean usePackage(java.lang.String name,
                          boolean checkException)
Add a package to the use()'d package list.

Parameters:
name - the package name
checkException - if false exceptions are ignored
Returns:
true if successfully use()'d.

loadModule

protected void loadModule(java.lang.String name,
                          Package pkg)
                   throws java.io.IOException
Loads a module is it has not been loaded yet. The initialization script is: 1) Replace :: and . with / then append "/init", e.g. pnuts.lib => pnuts/lib/init 2) The 1st line in META-INF/pnuts/module/

Parameters:
name - the name of the module
pkg - the associated package (name space)
Throws:
java.io.FileNotFoundException - thrown when the initialization script is not found.
java.io.IOException

clearPackages

public void clearPackages()
Unregisteres all use()'d packages


usedPackages

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


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,
                          int beginLine,
                          int beginColumn)
AST interpreter calls this method when line number changes, giving AST nodes and line information Not that compiler does not call this method.

Parameters:
node - the current AST node
beginLine - the line number at which the current expression starts.
beginColumn - the column number at which theh current expression ends.

updateLine

protected void updateLine(int line)
Both AST interpreter and compiler call this method when line number changes. Subclasses may override this method to interact with running script. For example, a subclass may redefine this method so that it can stop the execution if Thread.interrupt() has been called.

Parameters:
line - the line number

updateColumn

protected void updateColumn(int column)

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
Throws:
PnutsException - if the specified symbol is not defined

resolveSymbol

public java.lang.Object resolveSymbol(java.lang.String interned)
Resolves the value of a symbol in the following order: (1) current package (2) builtin functions, primitive types, pnuts_version (3) module exports (4) imported classes (5) parent packages

Parameters:
interned - a symbol (interned string)
Returns:
the value of the symbol, or null if it is not defined.

resolveClass

public java.lang.Class resolveClass(java.lang.String symbol)

autoload

public void autoload(java.lang.String name,
                     java.lang.String file)
Registers an autoload script for the name. If name is not defined when accessed, the registerred file is loaded.

Parameters:
name - variable name
file - the file

autoload

public void autoload(java.lang.String name,
                     AutoloadHook hook)
Registers an AutoloadHook for the name in the current package.

Parameters:
name - variable name
hook - the AutoloadHook

defined

public boolean defined(java.lang.String name)
Checks if the name is defined in the context.


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(Function f,
                    java.lang.Object[] args)

close

protected void close(Function func,
                     java.lang.Object[] args)

getValue

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

setValue

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

setVerbose

public void setVerbose(boolean b)
Sets the verbose mode


isVerbose

public boolean isVerbose()
Check the current verbose mode

Returns:
the current verbose mode

setScriptEncoding

public void setScriptEncoding(java.lang.String encoding)
Changes the script encoding for the context

Parameters:
encoding - the encoding

getScriptEncoding

public java.lang.String getScriptEncoding()
Gets the current script encoding

Returns:
the current script encoding