sleep.runtime
Class SleepUtils

java.lang.Object
  extended bysleep.runtime.SleepUtils

public class SleepUtils
extends Object

This class contains generalized utilities for instantiating/wrapping data into the sleep Scalar type. Included for free are methods for executing sleep blocks as well.

See Also:
Block, SleepClosure, Scalar, ScalarType, ScalarArray, ScalarHash

Field Summary
protected static ScalarType nullScalar
          a shared instance of the dreaded null scalar...
 
Constructor Summary
SleepUtils()
           
 
Method Summary
static void addKeyword(String keyword)
          registers "keyword" as a keyword with the parser.
static Scalar getArrayScalar()
          returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar
static Scalar getArrayScalar(ScalarArray value)
          Creates a scalar with the specified ScalarArray as the value
static Scalar getArrayWrapper(Collection dataStructure)
          An easy way to make your programs data structures available in the script environment.
static Scalar getEmptyScalar()
          returns the null scalar, which will always be equal to 0, "", and null simultaneously.
static SleepClosure getFunctionFromScalar(Scalar value, ScriptInstance script)
          extracts a callable Function from the passed in Scalar.
static Scalar getHashScalar()
          returns an empty hashmap scalar
static Scalar getHashScalar(ScalarHash value)
          Creates a scalar with the specified ScalarHash as the value
static Scalar getHashWrapper(Map dataStructure)
          An easy way to make your programs data structures available in the script environment.
static Scalar getScalar(boolean x)
          if x is true, the value will be 1, if x is false the value will be the empty scalar
static Scalar getScalar(double x)
          returns a double scalar with value x
static Scalar getScalar(int x)
          returns an int scalar with value x
static Scalar getScalar(long x)
          returns a long scalar with value x
static Scalar getScalar(Object x)
          returns an object scalar with value x
static Scalar getScalar(Scalar x)
          Forces a copy of the value of the passed in scalar to be made.
static Scalar getScalar(String x)
          returns a string scalar with value x
static boolean isEmptyScalar(Scalar value)
          returns true if the passed in scalar value is equivalent to the empty scalar or null
static boolean isFunctionScalar(Scalar value)
          determines if the passed in scalar represents a "function" value
static Scalar runCode(Block code, ScriptEnvironment env)
          "safely" run a snippet of code.
static Scalar runCode(ScriptInstance owner, Block code)
          "safely" run a snippet of code.
static Scalar runCode(ScriptInstance owner, Block code, HashMap locals)
          "safely" run a snippet of code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nullScalar

protected static ScalarType nullScalar
a shared instance of the dreaded null scalar...

Constructor Detail

SleepUtils

public SleepUtils()
Method Detail

addKeyword

public static void addKeyword(String keyword)
registers "keyword" as a keyword with the parser. This is a necessity if you have extra non standard bridges in sleep


runCode

public static Scalar runCode(Block code,
                             ScriptEnvironment env)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.

Parameters:
code - the block of code we want to execute
env - the environment to run the code in
Returns:
the scalar returned by the executed code (if their is a return value), null otherwise.

runCode

public static Scalar runCode(ScriptInstance owner,
                             Block code,
                             HashMap locals)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.

Parameters:
owner - the owning script instance of this block of code
code - the block of code we want to execute
locals - a hashmap containing Scalar objects that should be installed into the local scope. The keys should be Strings representing the $names for each of the Scalar variables.
Returns:
the scalar returned by the executed code (if their is a return value), null otherwise.

runCode

public static Scalar runCode(ScriptInstance owner,
                             Block code)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.

Parameters:
owner - the owning script instance of this block of code
code - the block of code we want to execute
Returns:
the scalar returned by the executed code (if their is a return value), null otherwise.

getArrayWrapper

public static Scalar getArrayWrapper(Collection dataStructure)
An easy way to make your programs data structures available in the script environment. Using this wrapper method will make the contents of your Set data structure available in a read only way to scripters using your program. Values your data structure returns are turned into Scalar strings using the toString() method of the object. If you need something more specific than this then take a look at the source code for sleep.runtime.SetWrapper which implements sleep.runtime.ScalarArray.


getHashWrapper

public static Scalar getHashWrapper(Map dataStructure)
An easy way to make your programs data structures available in the script environment. Using this wrapper method will make the contents of your Map'd data structure available in a read only way to scripters using your program. Keys are automatically turned into strings and values your data structure give back are turned into Scalar strings using the toString() method of the object. If you need something more specific than this then take a look at the source code for sleep.runtime.MapWrapper which implements sleep.runtime.ScalarHash.


getHashScalar

public static Scalar getHashScalar(ScalarHash value)
Creates a scalar with the specified ScalarHash as the value


getArrayScalar

public static Scalar getArrayScalar(ScalarArray value)
Creates a scalar with the specified ScalarArray as the value


getArrayScalar

public static Scalar getArrayScalar()
returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar


getEmptyScalar

public static Scalar getEmptyScalar()
returns the null scalar, which will always be equal to 0, "", and null simultaneously. The instance of the null scalar is shared since the null scalar "value" is not modifiable.


isEmptyScalar

public static boolean isEmptyScalar(Scalar value)
returns true if the passed in scalar value is equivalent to the empty scalar or null


isFunctionScalar

public static boolean isFunctionScalar(Scalar value)
determines if the passed in scalar represents a "function" value


getFunctionFromScalar

public static SleepClosure getFunctionFromScalar(Scalar value,
                                                 ScriptInstance script)
extracts a callable Function from the passed in Scalar. The passed in Scalar can be either a SleepClosure scalar or a string scalar specifying a function name


getHashScalar

public static Scalar getHashScalar()
returns an empty hashmap scalar


getScalar

public static Scalar getScalar(int x)
returns an int scalar with value x


getScalar

public static Scalar getScalar(double x)
returns a double scalar with value x


getScalar

public static Scalar getScalar(Scalar x)
Forces a copy of the value of the passed in scalar to be made. Sleep scalars in general are passed by *value*. When passing a scalar, a new scalar should be made with a copy of the old scalars value. Object scalars are passed by reference but this copying mechanism handles that. If you are ever storing scalars in a data structure call this method to get a copy. Otherwise chaos might ensue.


getScalar

public static Scalar getScalar(long x)
returns a long scalar with value x


getScalar

public static Scalar getScalar(String x)
returns a string scalar with value x


getScalar

public static Scalar getScalar(Object x)
returns an object scalar with value x


getScalar

public static Scalar getScalar(boolean x)
if x is true, the value will be 1, if x is false the value will be the empty scalar