public class Context
extends java.lang.Object
implements java.lang.Cloneable
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public static final java.io.PrintWriter defaultOutputStream
public static final java.io.PrintWriter defaultTerminalStream
public static final java.io.PrintWriter defaultErrorStream
protected int depth
protected org.pnuts.util.Cell loadingResource
protected int beginLine
protected int endLine
protected int beginColumn
protected pnuts.lang.ImportEnv importEnv
protected pnuts.lang.ModuleList moduleList
protected pnuts.lang.SymbolTable provideTable
protected java.util.Hashtable unitTable
protected pnuts.lang.SymbolTable environment
public Context()
public Context(java.lang.String pkg)
pkg
- the name of the package.public Context(Package pkg)
pkg
- the initial package of the context. If null, the global
package is used.public Context(Context context)
context
- The templatepublic Context(java.util.Properties properties)
public java.lang.Object clone()
clone
in class java.lang.Object
public java.lang.Object clone(boolean clear_attributes, boolean clear_locals)
clear_attributes
- If true, import() state and current package are reset to the
default values.clear_locals
- If true, local stack is reset.public void setName(java.lang.String name)
name
- The name of the context.public java.lang.String getName()
public void setPnutsImpl(PnutsImpl impl)
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.public PnutsImpl getPnutsImpl()
public void setImplementation(Implementation impl)
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.public Implementation getImplementation()
public java.lang.Object get(java.lang.String symbol)
symbol
- the name of the variable, which must be intern'ed.public void set(java.lang.String symbol, java.lang.Object value)
symbol
- the name of the variable, which must be intern'ed.value
- the value of the variablepublic java.util.Enumeration keys()
public void setOutputStream(java.lang.Object out, boolean autoFlush)
public void setOutputStream(java.lang.Object outputStream)
public void setOutputStream(java.io.OutputStream out)
out
- the OutputStreampublic void setWriter(java.io.Writer out)
out
- the Writerpublic void setWriter(java.io.Writer out, boolean autoFlush)
out
- the WriterautoFlush
- A boolean; if true, the PrintWriter.println() methods will
flush the output bufferpublic java.io.OutputStream getOutputStream()
public java.io.PrintWriter getWriter()
public void setTerminalStream(java.lang.Object str, boolean autoFlush)
public void setTerminalStream(java.lang.Object stream)
public void setTerminalWriter(java.io.Writer w)
w
- the Writerpublic void setTerminalWriter(java.io.Writer w, boolean autoFlush)
w
- the Writerpublic java.io.PrintWriter getTerminalStream()
public java.io.PrintWriter getTerminalWriter()
public void setErrorStream(java.lang.Object errorStream, boolean autoFlush)
public void setErrorStream(java.lang.Object errorStream)
public void setErrorWriter(java.io.Writer w, boolean autoFlush)
public void setErrorWriter(java.io.Writer w)
public java.io.PrintWriter getErrorStream()
public java.io.PrintWriter getErrorWriter()
public Package getCurrentPackage()
public void setCurrentPackage(Package pkg)
public void setClassLoader(java.lang.ClassLoader loader)
loader
- the class loaderpublic java.lang.ClassLoader getClassLoader()
public void setConfiguration(Configuration config)
config
- the configurationpublic Configuration getConfiguration()
public void setExitHook(Executable hook)
hook
- the hookpublic Executable getExitHook()
public boolean unusePackage(Package pkg)
public boolean usePackage(Package pkg, boolean checkException)
public boolean usePackage(java.lang.String name)
name
- the package namepublic boolean usePackage(java.lang.String name, boolean checkException)
name
- the package namecheckException
- if false exceptions are ignoredprotected void loadModule(java.lang.String name, Package pkg) throws java.io.IOException
name
- the name of the modulepkg
- the associated package (name space)java.io.FileNotFoundException
- thrown when the initialization script is not found.java.io.IOException
public void clearPackages()
public java.lang.String[] usedPackages()
protected java.lang.Object getScriptSource()
java.net.URL object, when the script is not precompiled pnuts.lang.Runtime object, when the script is precompiled
protected void updateLine(SimpleNode node, int beginLine, int beginColumn)
node
- the current AST nodebeginLine
- the line number at which the current expression starts.beginColumn
- the column number at which theh current expression ends.protected void updateLine(int line)
line
- the line numberprotected void updateColumn(int column)
protected void onExit(java.lang.Object arg)
protected void onError(java.lang.Throwable t)
public java.lang.Object getId(java.lang.String interned)
interned
- a symbol (interned string)PnutsException
- if the specified symbol is not definedpublic java.lang.Object resolveSymbol(java.lang.String interned)
interned
- a symbol (interned string)public java.lang.Class resolveClass(java.lang.String symbol)
public void autoload(java.lang.String name, java.lang.String file)
name
- variable namefile
- the filepublic void autoload(java.lang.String name, AutoloadHook hook)
name
- variable namehook
- the AutoloadHookpublic boolean defined(java.lang.String name)
public void registerQuantityFactory(java.lang.String unit, QuantityFactory fac)
unit
- The unit symbolfac
- A QuantityFactory object which defines what kind of object is
created when a decimal number with this unit symbol is
evaluated.protected void open(Function f, java.lang.Object[] args)
protected void close(Function func, java.lang.Object[] args)
protected java.lang.Object getValue(java.lang.String symbol)
protected void setValue(java.lang.String symbol, java.lang.Object obj)
public void setVerbose(boolean b)
public boolean isVerbose()
public void setScriptEncoding(java.lang.String encoding)
encoding
- the encodingpublic java.lang.String getScriptEncoding()