oscript
Class OscriptInterpreter

java.lang.Object
  extended byoscript.OscriptInterpreter
Direct Known Subclasses:
Main

public class OscriptInterpreter
extends java.lang.Object

The toplevel and main interface for the interpreter. There can only be one instance of this object. The scope object can be used to create logically isolated interpreter "instances".

This does need some cleanup, and perhaps should be a front-end for other stuff someone embedding this in an application might want, such as creating a new scope...

Description of properties that are interesting:

property description regular default webstart default
oscript.cache.path where the cache directory is created $CWD/.cache $HOME/.cache
oscript.cwd the current working directory $CWD $HOME/Desktop || $HOME

Author:
Rob Clark (rob@ti.com)

Nested Class Summary
static class OscriptInterpreter.CacheEntry
          An entry in the node-evaluator cache.
 
Field Summary
static java.lang.String CACHE_VERSION
          the CACHE_VERSION should change any time a change is made in the interpreter that could cause incompatibility with previously serialized cache entries.
static Value DEFAULT_ARRAY_SORT_COMPARISION_FXN
           
static NodeEvaluator EMPTY_EXPR_LIST_EVALUATOR
           
static long et
           
 
Constructor Summary
OscriptInterpreter()
           
 
Method Summary
static void __declareInScope(java.lang.String name, Value val, Scope scope)
           
static Value __eval(java.lang.String str)
           
static Value __eval(java.lang.String str, Scope scope)
           
static void addParser(Parser parser)
          Add the parser to list of recognized parsers.
static void addScriptPath(java.lang.String path)
          Add the specified path to the paths that are searched to import a file.
static NodeEvaluator createNodeEvaluator(java.lang.String name, Node node)
          Create a NodeEvaluator to evaluate a node.
static Value eval(AbstractFile file)
          Evaluate from the specified abstract file.
static Value eval(AbstractFile file, Scope scope)
          Evaluate from the specified abstract file.
static Value eval(java.lang.String str)
          Evaluate the specified sting.
static Value eval(java.lang.String str, Scope scope)
          Evaluate the specified sting.
static Scope getGlobalScope()
          Get the global scope object.
static java.util.Iterator getScriptPath()
          Return an iterator of entries in the script-path.
static java.lang.String getVersionString()
          Get a descriptive version string.
static java.lang.Class loadClassFromCache(java.lang.String className)
           
static void mountJarFile(AbstractFile file)
          Mount a .jar file under /jar/file.jar and add to script path
static Node parse(AbstractFile file)
          Parse the input stream to a syntaxtree.
static Node parse(java.lang.String str)
          Parse the string to a syntaxtree.
static void removeParser(Parser parser)
          Remove the parser from list of recognized parsers.
static void removeScriptPath(java.lang.String path)
          Remove the specified path to the paths that are searched to import a file.
static AbstractFile resolve(java.lang.String path, boolean create)
          Try to load the specified file from one of the registered filesystems.
static void setErr(java.io.PrintStream err)
          Set the error stream.
static void setIn(java.io.InputStream in)
          Set the input stream.
static void setOut(java.io.PrintStream out)
          Set the output stream.
static void useCompiler(boolean useCompiler)
          Set whether the compiler should be used or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_EXPR_LIST_EVALUATOR

public static final NodeEvaluator EMPTY_EXPR_LIST_EVALUATOR

CACHE_VERSION

public static final java.lang.String CACHE_VERSION
the CACHE_VERSION should change any time a change is made in the interpreter that could cause incompatibility with previously serialized cache entries. It is made part of the path to serialized cache entry, so entries serialized with different cache versions will not conflict.


DEFAULT_ARRAY_SORT_COMPARISION_FXN

public static Value DEFAULT_ARRAY_SORT_COMPARISION_FXN

et

public static long et
Constructor Detail

OscriptInterpreter

public OscriptInterpreter()
Method Detail

loadClassFromCache

public static java.lang.Class loadClassFromCache(java.lang.String className)
                                          throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

getVersionString

public static java.lang.String getVersionString()
Get a descriptive version string.


useCompiler

public static void useCompiler(boolean useCompiler)
Set whether the compiler should be used or not.

Parameters:
useCompiler - iff true, enabled compiler, otherwise use only the interpreter

setIn

public static void setIn(java.io.InputStream in)
Set the input stream.

Parameters:
in - the stream to use for input

setOut

public static void setOut(java.io.PrintStream out)
Set the output stream.

Parameters:
out - the stream to use for output

setErr

public static void setErr(java.io.PrintStream err)
Set the error stream.

Parameters:
err - the stream to use for error output

getGlobalScope

public static Scope getGlobalScope()
Get the global scope object. The globalScope is static, meaning that all script code in an application shares a single global scope. But, since the interpreter is multi-threaded, you can achieve the same effect of having multiple interpreter instances by creating a new level of scope (ie with globalScope as it's parent, an evaluate within that scope.

Returns:
the globalScope object

eval

public static Value eval(AbstractFile file)
                  throws ParseException,
                         java.io.IOException
Evaluate from the specified abstract file. The stream is evaluated until EOF is hit.

Parameters:
file - the file to evaluate
Returns:
the result of evaluating the input
Throws:
ParseException - if error parsing input
java.io.IOException - if something goes poorly when reading file

eval

public static Value eval(AbstractFile file,
                         Scope scope)
                  throws ParseException,
                         java.io.IOException
Evaluate from the specified abstract file. The stream is evaluated until EOF is hit.

Parameters:
file - the file to evaluate
scope - the scope to evaluate in
Returns:
the result of evaluating the input
Throws:
ParseException - if error parsing input
java.io.IOException - if something goes poorly when reading file

eval

public static Value eval(java.lang.String str)
                  throws ParseException
Evaluate the specified sting.

Parameters:
str - the string to evaluate
Returns:
the result of evaluating the string
Throws:
ParseException - if error parsing string

__eval

public static final Value __eval(java.lang.String str)
                          throws ParseException
Throws:
ParseException

eval

public static Value eval(java.lang.String str,
                         Scope scope)
                  throws ParseException
Evaluate the specified sting.

Parameters:
str - the string to evaluate
scope - the scope to evaluate in
Returns:
the result of evaluating the string
Throws:
ParseException - if error parsing string

__eval

public static final Value __eval(java.lang.String str,
                                 Scope scope)
                          throws ParseException
Throws:
ParseException

__declareInScope

public static final void __declareInScope(java.lang.String name,
                                          Value val,
                                          Scope scope)

addParser

public static void addParser(Parser parser)
Add the parser to list of recognized parsers. The registered parsers will determine what sorts of input the interpreter can handle.

Parameters:
parser - the parser to register
See Also:
removeParser(oscript.Parser)

removeParser

public static void removeParser(Parser parser)
Remove the parser from list of recognized parsers. The registered parsers will determine what sorts of input the interpreter can handle.

Parameters:
parser - the parser to register
See Also:
addParser(oscript.Parser)

parse

public static Node parse(AbstractFile file)
                  throws ParseException,
                         java.io.IOException
Parse the input stream to a syntaxtree.

Parameters:
file - the file to parse
Returns:
the parsed syntaxtree
Throws:
ParseException
java.io.IOException

parse

public static Node parse(java.lang.String str)
                  throws ParseException
Parse the string to a syntaxtree.

Parameters:
str - the string to parse
Returns:
the parsed syntaxtree
Throws:
ParseException

createNodeEvaluator

public static NodeEvaluator createNodeEvaluator(java.lang.String name,
                                                Node node)
Create a NodeEvaluator to evaluate a node. An application embedding the interpreter should use this method to convert the parsed syntax tree to something that can be evaluated within a scope, rather than directly using the visitors. This protects the application against changes to the parsed representation of the program.

Parameters:
node - the node
Returns:
a NodeEvaluator

addScriptPath

public static void addScriptPath(java.lang.String path)
Add the specified path to the paths that are searched to import a file.

Parameters:
path - a path to search for imports

removeScriptPath

public static void removeScriptPath(java.lang.String path)
Remove the specified path to the paths that are searched to import a file.

Parameters:
path - a path to search for imports

getScriptPath

public static java.util.Iterator getScriptPath()
Return an iterator of entries in the script-path. Each entry is a path that is prefixed to a relative path passed to resolve(java.lang.String, boolean) in the process of trying to resolve a file.

Returns:
an iterator of strings

resolve

public static AbstractFile resolve(java.lang.String path,
                                   boolean create)
                            throws java.io.IOException
Try to load the specified file from one of the registered filesystems.

Parameters:
path - the path to the file to resolve
create - create the file if it does not exist
Throws:
java.io.IOException - if something goes wrong when reading file
See Also:
addScriptPath(java.lang.String)

mountJarFile

public static final void mountJarFile(AbstractFile file)
                               throws java.lang.Exception
Mount a .jar file under /jar/file.jar and add to script path

Parameters:
file - the jar file to mount
Throws:
java.lang.Exception