pnuts.lang
Class Runtime

java.lang.Object
  |
  +--pnuts.lang.Runtime
All Implemented Interfaces:
PnutsParserTreeConstants
Direct Known Subclasses:
Compiler, DynamicRuntime, Function, JavaAdapter, PnutsInterpreter, TerminalDebugger, VisualDebugger

public class Runtime
extends java.lang.Object
implements PnutsParserTreeConstants

This class provides runtime supports for Pnuts compiler/interpreter. Most of the methods are protected static, so that only subclasses can access them.


Fields inherited from interface pnuts.lang.PnutsParserTreeConstants
JJTADDNODE, JJTANDNODE, JJTAPPLICATIONNODE, JJTARRAYTYPE, JJTASSIGNMENT, JJTASSIGNMENTAA, JJTASSIGNMENTDA, JJTASSIGNMENTEA, JJTASSIGNMENTLA, JJTASSIGNMENTMA, JJTASSIGNMENTOA, JJTASSIGNMENTPA, JJTASSIGNMENTRA, JJTASSIGNMENTRAA, JJTASSIGNMENTSA, JJTASSIGNMENTTA, JJTBLOCK, JJTBREAK, JJTCASTEXPRESSION, JJTCATCHNODE, JJTCHARACTERNODE, JJTCLASS, JJTCONTINUE, JJTDIVIDENODE, JJTELSEIFNODE, JJTELSENODE, JJTEQUALNODE, JJTEXPRESSIONLIST, JJTFALSENODE, JJTFLOATINGNODE, JJTFOREACHSTATEMENT, JJTFORINIT, JJTFORSTATEMENT, JJTFORUPDATE, JJTFUNCTIONSTATEMENT, JJTGENODE, JJTGLOBAL, JJTGTNODE, JJTIDNODE, JJTIFSTATEMENT, JJTINDEXNODE, JJTINSTANCEOFEXPRESSION, JJTINTEGERNODE, JJTLENODE, JJTLISTELEMENTS, JJTLOCAL, JJTLOGANDNODE, JJTLOGEXORNODE, JJTLOGNOTNODE, JJTLOGORNODE, JJTLTNODE, JJTMEMBERNODE, JJTMETHODNODE, JJTMODNODE, JJTMULTNODE, JJTNEGATIVENODE, jjtNodeName, JJTNOTEQNODE, JJTNOTNODE, JJTNULLNODE, JJTORNODE, JJTPACKAGE, JJTPARAM, JJTPARAMLIST, JJTPOSTDECRNODE, JJTPOSTINCRNODE, JJTPREDECRNODE, JJTPREINCRNODE, JJTRANGENODE, JJTRETURN, JJTSHIFTARITHMETICNODE, JJTSHIFTLEFTNODE, JJTSHIFTRIGHTNODE, JJTSTART, JJTSTARTSET, JJTSTATICMEMBERNODE, JJTSTATICMETHODNODE, JJTSTRINGNODE, JJTSUBTRACTNODE, JJTSWITCHBLOCK, JJTSWITCHLABEL, JJTSWITCHSTATEMENT, JJTTRUENODE, JJTVOID, JJTWHILESTATEMENT
 
Constructor Summary
protected Runtime()
           
 
Method Summary
static java.lang.Class arrayType(java.lang.Class c, int dim)
          Creates an array type
protected static java.lang.Object assignRange(java.lang.Object target, java.lang.Number idx1, java.lang.Number idx2, java.lang.Object expr)
          This method is called by the syntax "id[from..to] = sth"
protected static java.lang.Object binary(java.lang.Object n1, java.lang.Object n2, int operator, Context context)
          Binary operation
protected static java.lang.Object call(Context context, java.lang.Object funcOrClass, java.lang.Object[] args, java.lang.Class[] casts)
          This method is called by the syntax "funcOrClass(args...)"
static java.lang.Object callConstructor(java.lang.Class c, java.lang.Object[] args)
          Call a constructor
static java.lang.Object callConstructor(java.lang.Class c, java.lang.Object[] args, java.lang.Class[] types)
          Call a constructor
static java.lang.Object callConstructor(Context context, java.lang.Class c, java.lang.Object[] args, java.lang.Class[] types)
          Call a constructor
protected static java.lang.Object callFunction(Context context, PnutsFunction func, java.lang.Object[] args)
          Call a function
static java.lang.Object callMethod(java.lang.Class c, java.lang.String name, java.lang.Object[] args, java.lang.Class[] types, java.lang.Object target)
          Call a method
static java.lang.Object callMethod(java.lang.Class c, java.lang.String name, java.lang.Object[] args, java.lang.Object target)
          Call a method
static java.lang.Object callMethod(Context context, java.lang.Class c, java.lang.String name, java.lang.Object[] args, java.lang.Class[] types, java.lang.Object target)
          Call a method
static java.lang.Object cast(Context context, java.lang.Class type, java.lang.Object object, boolean flag)
          This method is called by the syntax "(Class)object"
protected static void catchException(java.lang.Class type, PnutsFunction func, Context context)
          This method is called when catch() function is called in a package(non-local) scope
protected static void checkException(Context context, java.lang.Throwable throwable)
           
protected static void checkException(Context context, java.lang.Throwable throwable, java.util.Hashtable tab)
          Check if any exception handler is registered to the specified exception.
protected static int compareTo(java.lang.Object n1, java.lang.Object n2, Context context)
          Comparison operation
protected static void escape(java.lang.Object v)
           
protected  java.lang.Object exec(Context context)
          This method is overrided by classes generated by the compiler.
 java.lang.Object execute(Context context)
          Executes a compiled script.
static java.net.URL fileToURL(java.io.File file)
           
static java.lang.String format(java.lang.Object object, int maxArrayLength)
          Get the String representation of the specified object.
protected static int getBeginLine(Context context)
           
protected static java.lang.Class getBottomType(java.lang.Class clazz)
          Get true component type from an array type.
static Runtime getCompiledScript(java.lang.String name, Context context)
          This method is called by Pnuts.load() when the property "pnuts.compiled.script.prefix" is defined, to load pre-compiled scripts.
protected static int getEndLine(Context context)
           
static java.lang.Object getField(Context context, java.lang.Object target, java.lang.String name)
          Get the value of a instance field.
protected static Function getFunction(Context context)
           
protected static Function getFunction(PnutsFunction pf, int nargs)
           
protected static java.util.Enumeration getFunctions(PnutsFunction pf)
           
protected static java.lang.Object getIndex(java.lang.Object target, int index, Context context)
          This method is called by the syntax "target[index]"
protected static Runtime getRuntime(Context context)
           
protected static java.lang.Object getScriptSource(Context context)
           
static java.net.URL getScriptURL(java.lang.String name, Context context)
           
static java.lang.Object getStaticField(Context context, java.lang.Class clazz, java.lang.String name)
          Get the value of a static field.
static boolean isArray(java.lang.Object obj)
          Check if the parameter is an array
protected static void jump(java.lang.Object v)
           
protected static int matchType(java.lang.Class type, java.lang.Object obj)
           
protected static boolean namespaceRefreshed(Context context)
           
protected static java.lang.Object newInstance(Context context, java.lang.Class clazz, java.lang.Object[] args, java.lang.Class[] casts)
          This method is called by the syntax "clazz(args...)"
static java.lang.Character parseChar(java.lang.String str)
          Parse a character literal.
static java.lang.Object[] parseFloat(java.lang.String str)
          Parse a floating point number.
static java.lang.Object[] parseInt(java.lang.String str)
          Parse an integer.
static java.lang.String parseString(java.lang.String str)
          Parse a string literal.
static java.lang.Object primitive(Context context, java.lang.Class primitiveType, java.lang.Object param, boolean flag)
          This method is called by the syntax "primitiveType(object)" and "(primitiveType)object"
static void printError(java.lang.Throwable t, Context context)
           
static void putField(Context context, java.lang.Object target, java.lang.String name, java.lang.Object expr)
          Assign an object to a instance field.
static void putStaticField(Context context, java.lang.Class clazz, java.lang.String name, java.lang.Object expr)
          Assign an object to a static field.
static java.lang.Object quantity(java.lang.Number number, java.lang.String numberString, java.lang.String unit, Context context)
          Creates an object from a number literal and a unit symbol
protected static java.lang.Object range(java.lang.Object target, java.lang.Number idx1, java.lang.Number idx2, Context context)
          Range expression 'target[idx1..idx2]'
static void readSymbolTable(pnuts.lang.SymbolTable table, java.io.ObjectInputStream s)
          Read a hashtable from objectInputStream.
protected static void setLine(Context context, int line)
           
protected static void setLine(Context context, int beginLine, int endLine)
          Set line number information for error reporting
protected static void setPackage(Package pkg, Context context)
           
protected static java.lang.Object transform(java.lang.Class baseType, java.lang.Object array)
          Transform an array to a different array type.
protected static java.lang.Object unary(java.lang.Object n, int operator, Context context)
          Unary operation
static int writeSymbolTable(pnuts.lang.SymbolTable tab, pnuts.lang.SymbolTable notToSerialize, java.io.ObjectOutputStream s)
          Write a hashtable to the objectOutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Runtime

protected Runtime()
Method Detail

execute

public java.lang.Object execute(Context context)
Executes a compiled script. Exceptions are checked if an exception handler is registered with the catch() function. Output stream of the specified context is flushed after the script is executed or an exception is thrown.
Parameters:
context - the context in which this object is executed
Returns:
the result of the execution

exec

protected java.lang.Object exec(Context context)
This method is overrided by classes generated by the compiler.
Parameters:
context - the context in which this object is executed
Returns:
the result of the execution

callMethod

public static java.lang.Object callMethod(Context context,
                                          java.lang.Class c,
                                          java.lang.String name,
                                          java.lang.Object[] args,
                                          java.lang.Class[] types,
                                          java.lang.Object target)
Call a method
Parameters:
context - the context in which the method is called
c - the class of method
name - the method name
args - the paramters
types - the types of the paramters
target - the target object
Returns:
the return value of the call

callMethod

public static java.lang.Object callMethod(java.lang.Class c,
                                          java.lang.String name,
                                          java.lang.Object[] args,
                                          java.lang.Object target)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.IllegalArgumentException,
                                          java.lang.reflect.InvocationTargetException
Call a method
Parameters:
c - the class of method
name - the method name
args - the paramters
target - the target object
Returns:
the return value

callMethod

public static java.lang.Object callMethod(java.lang.Class c,
                                          java.lang.String name,
                                          java.lang.Object[] args,
                                          java.lang.Class[] types,
                                          java.lang.Object target)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.IllegalArgumentException,
                                          java.lang.reflect.InvocationTargetException
Call a method
Parameters:
c - the class of method
name - the method name
args - the paramters
types - the types of the formal arguments
target - the target object
Returns:
the return value

callConstructor

public static java.lang.Object callConstructor(Context context,
                                               java.lang.Class c,
                                               java.lang.Object[] args,
                                               java.lang.Class[] types)
Call a constructor
Parameters:
context - the context in which the constructor is called
c - the class of method
args - the paramters
types - the types of the formal arguments
Returns:
the created instance

callConstructor

public static java.lang.Object callConstructor(java.lang.Class c,
                                               java.lang.Object[] args)
                                        throws java.lang.IllegalAccessException,
                                               java.lang.IllegalArgumentException,
                                               java.lang.reflect.InvocationTargetException,
                                               java.lang.InstantiationException
Call a constructor
Parameters:
c - the class of constructor
args - the paramters
Returns:
the created instance

callConstructor

public static java.lang.Object callConstructor(java.lang.Class c,
                                               java.lang.Object[] args,
                                               java.lang.Class[] types)
                                        throws java.lang.IllegalAccessException,
                                               java.lang.IllegalArgumentException,
                                               java.lang.reflect.InvocationTargetException,
                                               java.lang.InstantiationException
Call a constructor
Parameters:
c - the class of constructor
args - the paramters
types - the types of the formal arguments
Returns:
the created instance

putStaticField

public static void putStaticField(Context context,
                                  java.lang.Class clazz,
                                  java.lang.String name,
                                  java.lang.Object expr)
Assign an object to a static field.
Parameters:
context - the context in which the field is accessed
clazz - the class in which the static field is defined
name - the name of the static field
expr - the value to be assigned

getStaticField

public static java.lang.Object getStaticField(Context context,
                                              java.lang.Class clazz,
                                              java.lang.String name)
Get the value of a static field.
Parameters:
context - the context in which the field is accessed
clazz - the class in which the static field is defined
name - the name of the static field
Returns:
the value

putField

public static void putField(Context context,
                            java.lang.Object target,
                            java.lang.String name,
                            java.lang.Object expr)
Assign an object to a instance field.
Parameters:
context - the context in which the field is accessed
target - the target object of the field
name - the name of the field
expr - the value to be assigned

getField

public static java.lang.Object getField(Context context,
                                        java.lang.Object target,
                                        java.lang.String name)
Get the value of a instance field.
Parameters:
context - the context in which the field is accessed
target - the target object of the field
name - the name of the field
Returns:
the value

getBottomType

protected static java.lang.Class getBottomType(java.lang.Class clazz)
Get true component type from an array type. e.g. int[][] ==> int, String[] ==> String
Parameters:
clazz - An array type to be examined
Returns:
The component type of the array type.

arrayType

public static java.lang.Class arrayType(java.lang.Class c,
                                        int dim)
Creates an array type
Parameters:
c - the component type
dim - the number of dimensions

transform

protected static java.lang.Object transform(java.lang.Class baseType,
                                            java.lang.Object array)
Transform an array to a different array type.
Parameters:
baseType - The component type of which the array to create
array - The source array of the transformation
Returns:
An array of the clazz type that has same values as obj

writeSymbolTable

public static int writeSymbolTable(pnuts.lang.SymbolTable tab,
                                   pnuts.lang.SymbolTable notToSerialize,
                                   java.io.ObjectOutputStream s)
                            throws java.io.IOException
Write a hashtable to the objectOutputStream. If a value of the hashtable is a Class object, it is encapsulated to avoid NotSerializableException. But if a value contains a Class object in its structure e.g. "new Class[]{ Object.class }", NotSerializableException is thrown, even though the Class object is Serializable. If notToSerialize is not null, falied keys are accumulated in notToSerialize. This is a workaround for bug:4075221.

readSymbolTable

public static void readSymbolTable(pnuts.lang.SymbolTable table,
                                   java.io.ObjectInputStream s)
                            throws java.io.IOException,
                                   java.lang.ClassNotFoundException
Read a hashtable from objectInputStream. If Class objects are encapsulated, it retrieves the Class objects from them.

matchType

protected static int matchType(java.lang.Class type,
                               java.lang.Object obj)

parseInt

public static java.lang.Object[] parseInt(java.lang.String str)
Parse an integer.
Returns:
an array [Number number, int offset_of_unit_symbol]

parseFloat

public static java.lang.Object[] parseFloat(java.lang.String str)
Parse a floating point number.
Returns:
an array [Number number, int offset_of_unit_symbol]

parseString

public static java.lang.String parseString(java.lang.String str)
Parse a string literal.
Returns:
the value

parseChar

public static java.lang.Character parseChar(java.lang.String str)
Parse a character literal.
Returns:
the value

quantity

public static java.lang.Object quantity(java.lang.Number number,
                                        java.lang.String numberString,
                                        java.lang.String unit,
                                        Context context)
Creates an object from a number literal and a unit symbol
Parameters:
number - a number object
numberString - a symbol of the number literal
unit - a unit symbol
context - a context in which the quantity is created

primitive

public static java.lang.Object primitive(Context context,
                                         java.lang.Class primitiveType,
                                         java.lang.Object param,
                                         boolean flag)
This method is called by the syntax "primitiveType(object)" and "(primitiveType)object"
Parameters:
context - the context
primitiveType - a primitive type
param - the parameter
flag - string<->number conversion

cast

public static java.lang.Object cast(Context context,
                                    java.lang.Class type,
                                    java.lang.Object object,
                                    boolean flag)
This method is called by the syntax "(Class)object"
Parameters:
context - the context
type - the type
flag - object_array<->primitive_array conversion

isArray

public static final boolean isArray(java.lang.Object obj)
Check if the parameter is an array
Parameters:
obj - the object to be checked
Returns:
true if the obj is an array, false otherwise.

range

protected static java.lang.Object range(java.lang.Object target,
                                        java.lang.Number idx1,
                                        java.lang.Number idx2,
                                        Context context)
Range expression 'target[idx1..idx2]'

assignRange

protected static java.lang.Object assignRange(java.lang.Object target,
                                              java.lang.Number idx1,
                                              java.lang.Number idx2,
                                              java.lang.Object expr)
This method is called by the syntax "id[from..to] = sth"

checkException

protected static void checkException(Context context,
                                     java.lang.Throwable throwable)

checkException

protected static void checkException(Context context,
                                     java.lang.Throwable throwable,
                                     java.util.Hashtable tab)
Check if any exception handler is registered to the specified exception. If any an exception handler is executed. If not, the exception is thrown.
Parameters:
context - the Context in which the exception is checked
throwable - the exception
tab - the exception handler table

catchException

protected static void catchException(java.lang.Class type,
                                     PnutsFunction func,
                                     Context context)
This method is called when catch() function is called in a package(non-local) scope
Parameters:
type - the exception type of which an exception handler is registered
func - the function to be registered as an exception handler
context - the context in which the exception handler is registered

getIndex

protected static java.lang.Object getIndex(java.lang.Object target,
                                           int index,
                                           Context context)
This method is called by the syntax "target[index]"

newInstance

protected static java.lang.Object newInstance(Context context,
                                              java.lang.Class clazz,
                                              java.lang.Object[] args,
                                              java.lang.Class[] casts)
This method is called by the syntax "clazz(args...)"

callFunction

protected static final java.lang.Object callFunction(Context context,
                                                     PnutsFunction func,
                                                     java.lang.Object[] args)
Call a function
Parameters:
context - the context in which the function is called
func - the function to be called
args - the arguments

call

protected static final java.lang.Object call(Context context,
                                             java.lang.Object funcOrClass,
                                             java.lang.Object[] args,
                                             java.lang.Class[] casts)
This method is called by the syntax "funcOrClass(args...)"

jump

protected static void jump(java.lang.Object v)

escape

protected static void escape(java.lang.Object v)

setLine

protected static void setLine(Context context,
                              int beginLine,
                              int endLine)
Set line number information for error reporting

setLine

protected static void setLine(Context context,
                              int line)

getBeginLine

protected static int getBeginLine(Context context)

getEndLine

protected static int getEndLine(Context context)

getFunction

protected static Function getFunction(PnutsFunction pf,
                                      int nargs)

getFunctions

protected static java.util.Enumeration getFunctions(PnutsFunction pf)

getRuntime

protected static Runtime getRuntime(Context context)

getScriptSource

protected static java.lang.Object getScriptSource(Context context)

getFunction

protected static Function getFunction(Context context)

namespaceRefreshed

protected static boolean namespaceRefreshed(Context context)

setPackage

protected static void setPackage(Package pkg,
                                 Context context)

format

public static java.lang.String format(java.lang.Object object,
                                      int maxArrayLength)
Get the String representation of the specified object.
Parameters:
object - the target object.
maxArrayLength - When the target object is an array and maxArrayLength is greater than zero, only the first maxArrayLength elements are printed and the rest of the elements are omitted as "...".

binary

protected static java.lang.Object binary(java.lang.Object n1,
                                         java.lang.Object n2,
                                         int operator,
                                         Context context)
Binary operation
Parameters:
n1 - the first argument
n2 - the second argument
operator - an integer which is corresponds to the operator
context - the context in which the operation is taken place
Returns:
the result of the operation

compareTo

protected static int compareTo(java.lang.Object n1,
                               java.lang.Object n2,
                               Context context)
Comparison operation
Parameters:
n1 - the first argument
n2 - the second argument
context - the context in which the operation is taken place
Returns:
the result of the operation; one of:
  • Arithmetic.EQUAL
  • Arithmetic.NOT_EQUAL
  • Arithmetic.LEFT_IS_BIGGER
  • Arithmetic.RIGHT_IS_BIGGER

unary

protected static java.lang.Object unary(java.lang.Object n,
                                        int operator,
                                        Context context)
Unary operation
Parameters:
n - the argument
operator - an integer which corresponds to the operator
context - the context in which the operation is taken place
Returns:
the result of the operation

getScriptURL

public static java.net.URL getScriptURL(java.lang.String name,
                                        Context context)

getCompiledScript

public static Runtime getCompiledScript(java.lang.String name,
                                        Context context)
This method is called by Pnuts.load() when the property "pnuts.compiled.script.prefix" is defined, to load pre-compiled scripts.
Parameters:
name - the script name
context - the context in which the class is loaded.
Returns:
pnuts.lang.Runtime object if the class is found, otherwise null.

fileToURL

public static java.net.URL fileToURL(java.io.File file)
                              throws java.net.MalformedURLException

printError

public static void printError(java.lang.Throwable t,
                              Context context)