PikaScript
Protected Types | List of all members
Pika::Script< Config >::Frame Class Reference

The execution context and interpreter for PikaScript. More...

#include <PikaScript.h>

Inheritance diagram for Pika::Script< Config >::Frame:
Inheritance graph
[legend]
Collaboration diagram for Pika::Script< Config >::Frame:
Collaboration graph
[legend]

Protected Types

typedef std::pair< bool, ValueXValue
 The XValue differentiates lvalues and rvalues and is used internally in the interpreter. More...
 
typedef std::pair< bool, ValueXValue
 The XValue differentiates lvalues and rvalues and is used internally in the interpreter. More...
 

Construction.

 Frame (Variables &vars, Root &root, Frame *previous)
 Constructs the Frame and associates it with the variable space vars. More...
 
 Frame (Variables &vars, Root &root, Frame *previous)
 Constructs the Frame and associates it with the variable space vars. More...
 

Properties.

VariablesgetVariables () const throw ()
 Returns a reference to the Variable instance associated with this Frame. Simple as that.
 
RootgetRoot () const throw ()
 Returns a reference to the "root frame" for this Frame. (No brainer.)
 
FramegetPrevious () const throw ()
 Returns a reference to the previous frame (i.e. the frame of the caller of this frame). Must not be called on the root frame (will assert).
 
VariablesgetVariables () const throw ()
 Returns a reference to the Variable instance associated with this Frame. Simple as that.
 
RootgetRoot () const throw ()
 Returns a reference to the "root frame" for this Frame. (No brainer.)
 
FramegetPrevious () const throw ()
 Returns a reference to the previous frame (i.e. the frame of the caller of this frame). Must not be called on the root frame (will assert).
 

Getting, setting and referencing variables.

Value get (const String &identifier, bool fallback=false) const
 Gets a variable value. More...
 
Value getOptional (const String &identifier, const Value &defaultValue=Value()) const
 Tries to get the variable value as with get() (but never "falls back"). More...
 
const Valueset (const String &identifier, const Value &v)
 Sets a variable value. More...
 
Value reference (const String &identifier) const
 Creates a reference to the variable identified by identifier by prefixing it with a "frame label". More...
 
std::pair< Frame *, StringresolveFrame (const String &identifier) const
 Resolves the frame for identifier and returns it together with identifier stripped of any prefixed "frame identifier". More...
 
Value get (const String &identifier, bool fallback=false) const
 Gets a variable value. More...
 
Value getOptional (const String &identifier, const Value &defaultValue=Value()) const
 Tries to get the variable value as with get() (but never "falls back"). More...
 
const Valueset (const String &identifier, const Value &v)
 Sets a variable value. More...
 
Value reference (const String &identifier) const
 Creates a reference to the variable identified by identifier by prefixing it with a "frame label". More...
 
std::pair< Frame *, StringresolveFrame (const String &identifier) const
 Resolves the frame for identifier and returns it together with identifier stripped of any prefixed "frame identifier". More...
 

Calling functions and evaluating source code.

Value call (const String &callee, const Value &body, long argc, const Value *argv=0)
 Calls a Pika function (by setting up a new "sub-frame" and executing the function body). More...
 
Value execute (const String &body)
 A low-level function that executes body directly on the Frame instance. More...
 
Value evaluate (const String source)
 Evaluates the PikaScript expression in source directly on this Frame. More...
 
StringIt parse (const StringIt &begin, const StringIt &end, bool literal)
 Parses a PikaScript expression or literal (without evaluating it) and returns an iterator pointing at the end of the expression.
 
Value call (const String &callee, const Value &body, long argc, const Value *argv=0)
 Calls a Pika function (by setting up a new "sub-frame" and executing the function body). More...
 
Value execute (const String &body)
 A low-level function that executes body directly on the Frame instance. More...
 
Value evaluate (const String source)
 Evaluates the PikaScript expression in source directly on this Frame. More...
 
StringIt parse (const StringIt &begin, const StringIt &end, bool literal)
 Parses a PikaScript expression or literal (without evaluating it) and returns an iterator pointing at the end of the expression.
 

Registering native functions (or objects).

void registerNative (const String &identifier, Native *native)
 Registers the native function (or object) native with identifier in the appropriate variable space (determined by any "frame identifier" present in identifier). More...
 
template<class A0 , class R >
void registerNative (const String &i, R(*f)(A0))
 Helper template for easily registering a unary C++ function. More...
 
template<class A0 , class A1 , class R >
void registerNative (const String &i, R(*f)(A0, A1))
 Helper template for easily registering a binary C++ function. More...
 
template<class C , class A0 , class R >
void registerNative (const String &i, C *o, R(C::*m)(A0))
 Helper template for easily registering a unary C++ member function in the C++ object pointed to by o. More...
 
void unregisterNative (const String &identifier)
 Helper function for unregistering a native function / object. More...
 
void registerNative (const String &identifier, Native *native)
 Registers the native function (or object) native with identifier in the appropriate variable space (determined by any "frame identifier" present in identifier). More...
 
template<class A0 , class R >
void registerNative (const String &i, R(*f)(A0))
 Helper template for easily registering a unary C++ function. More...
 
template<class A0 , class A1 , class R >
void registerNative (const String &i, R(*f)(A0, A1))
 Helper template for easily registering a binary C++ function. More...
 
template<class C , class A0 , class R >
void registerNative (const String &i, C *o, R(C::*m)(A0))
 Helper template for easily registering a unary C++ member function in the C++ object pointed to by o. More...
 
void unregisterNative (const String &identifier)
 Helper function for unregistering a native function / object. More...
 

Destruction.

virtual ~Frame ()
 The default destructor does nothing, but it is always good practice to have a virtual destructor.
 
virtual ~Frame ()
 The default destructor does nothing, but it is always good practice to have a virtual destructor.
 

Detailed Description

template<class Config>
class Pika::Script< Config >::Frame

The execution context and interpreter for PikaScript.

This is where the magic happens. A Frame represents an execution context for a PikaScript function and it contains the source code interpreter. Normally you do not create instances of Frame yourself. They are created on stack whenever a function call is made. Notice that this implementation of PikaScript does not run in a virtual machine, instead it is interpreted directly and it shares calling stack etc with your C++ application.

Definition at line 311 of file PikaScript.h.

Member Typedef Documentation

template<class Config >
typedef std::pair<bool, Value> Pika::Script< Config >::Frame::XValue
protected

The XValue differentiates lvalues and rvalues and is used internally in the interpreter.

first = lvalue or not, second = symbol (for lvalue) or actual value (for rvalue).

Definition at line 356 of file PikaScript.h.

template<class Config >
typedef std::pair<bool, Value> Pika::Script< Config >::Frame::XValue
protected

The XValue differentiates lvalues and rvalues and is used internally in the interpreter.

first = lvalue or not, second = symbol (for lvalue) or actual value (for rvalue).

Definition at line 356 of file PikaScript.h.

Constructor & Destructor Documentation

template<class Config >
TMPL Pika::Script< Config >::Frame::Frame ( Variables vars,
Root root,
Frame previous 
)

Constructs the Frame and associates it with the variable space vars.

All frames on the calling stack have direct access to the "root frame" which is designated by root (will be = *this for the Root). previous should point to the caller Frame (or 0 for the Root). The "frame label" of a root frame is always :: . Root::generateLabel() is called to create unique labels for other frames.

Definition at line 294 of file PikaScriptImpl.h.

template<class Config >
Pika::Script< Config >::Frame::Frame ( Variables vars,
Root root,
Frame previous 
)

Constructs the Frame and associates it with the variable space vars.

All frames on the calling stack have direct access to the "root frame" which is designated by root (will be = *this for the Root). previous should point to the caller Frame (or 0 for the Root). The "frame label" of a root frame is always :: . Root::generateLabel() is called to create unique labels for other frames.

Member Function Documentation

template<class Config >
Value Pika::Script< Config >::Frame::call ( const String callee,
const Value body,
long  argc,
const Value argv = 0 
)

Calls a Pika function (by setting up a new "sub-frame" and executing the function body).

You may pass Value() (the void value) for callee or body. If only callee is specified, it will be used to retrieve the function body (through get()). If only body is specified, the called function will not have a $callee variable ($callee is used for debugging and object-oriented solutions). If both are present, the $callee variable will be set to callee, and body will be executed. argc is the number of arguments and if this is not zero, the argv parameter should point to an array of arguments (of at least argc elements in size). The return value is that of the PikaScript function.

template<class Config >
Value Pika::Script< Config >::Frame::call ( const String callee,
const Value body,
long  argc,
const Value argv = 0 
)

Calls a Pika function (by setting up a new "sub-frame" and executing the function body).

You may pass Value() (the void value) for callee or body. If only callee is specified, it will be used to retrieve the function body (through get()). If only body is specified, the called function will not have a $callee variable ($callee is used for debugging and object-oriented solutions). If both are present, the $callee variable will be set to callee, and body will be executed. argc is the number of arguments and if this is not zero, the argv parameter should point to an array of arguments (of at least argc elements in size). The return value is that of the PikaScript function.

template<class Config >
Value Pika::Script< Config >::Frame::evaluate ( const String  source)

Evaluates the PikaScript expression in source directly on this Frame.

This differs from execute() in that source is not expected to be in the format of a "function body" of an "ordinary", "lambda" or "native" function. (Notice that we are not passing a reference to the source string here so that we are safe in case the PikaScript code manipulates the very string it is running on.) The return value is that of the evaluated expression.

template<class Config >
Value Pika::Script< Config >::Frame::evaluate ( const String  source)

Evaluates the PikaScript expression in source directly on this Frame.

This differs from execute() in that source is not expected to be in the format of a "function body" of an "ordinary", "lambda" or "native" function. (Notice that we are not passing a reference to the source string here so that we are safe in case the PikaScript code manipulates the very string it is running on.) The return value is that of the evaluated expression.

template<class Config >
Value Pika::Script< Config >::Frame::execute ( const String body)

A low-level function that executes body directly on the Frame instance.

This means that unlike call(), you need to setup a "sub-frame" yourself, populate it with argument variables and then use this function. The return value is that of the PikaScript function.

template<class Config >
Value Pika::Script< Config >::Frame::execute ( const String body)

A low-level function that executes body directly on the Frame instance.

This means that unlike call(), you need to setup a "sub-frame" yourself, populate it with argument variables and then use this function. The return value is that of the PikaScript function.

template<class Config >
Value Pika::Script< Config >::Frame::get ( const String identifier,
bool  fallback = false 
) const

Gets a variable value.

If identifier is prefixed with a "frame identifier" (e.g. a "frame label" or ^), it will be "resolved" and used for retrieving the variable. Otherwise the variable space associated with this Frame instance will be checked and if the variable cannot be found and fallback is true, the global variable space will also be checked. If the variable cannot be found in any of the checked locations, an exception will be thrown.

template<class Config >
Value Pika::Script< Config >::Frame::get ( const String identifier,
bool  fallback = false 
) const

Gets a variable value.

If identifier is prefixed with a "frame identifier" (e.g. a "frame label" or ^), it will be "resolved" and used for retrieving the variable. Otherwise the variable space associated with this Frame instance will be checked and if the variable cannot be found and fallback is true, the global variable space will also be checked. If the variable cannot be found in any of the checked locations, an exception will be thrown.

template<class Config >
Value Pika::Script< Config >::Frame::getOptional ( const String identifier,
const Value defaultValue = Value() 
) const

Tries to get the variable value as with get() (but never "falls back").

If the variable cannot be found, defaultValue will be returned instead.

template<class Config >
Value Pika::Script< Config >::Frame::getOptional ( const String identifier,
const Value defaultValue = Value() 
) const

Tries to get the variable value as with get() (but never "falls back").

If the variable cannot be found, defaultValue will be returned instead.

template<class Config >
Value Pika::Script< Config >::Frame::reference ( const String identifier) const

Creates a reference to the variable identified by identifier by prefixing it with a "frame label".

If the identifier is already prefixed with a "frame identifier" (such as ^) it will be resolved to determine the frame. Otherwise, the label of this Frame instance is used.

template<class Config >
Value Pika::Script< Config >::Frame::reference ( const String identifier) const

Creates a reference to the variable identified by identifier by prefixing it with a "frame label".

If the identifier is already prefixed with a "frame identifier" (such as ^) it will be resolved to determine the frame. Otherwise, the label of this Frame instance is used.

template<class Config >
void Pika::Script< Config >::Frame::registerNative ( const String identifier,
Native native 
)

Registers the native function (or object) native with identifier in the appropriate variable space (determined by any "frame identifier" present in identifier).

Once registered, the native is considered "owned" by the variable space. In other words, all registered natives will be deleted by the Variables destructor. Also, if you register a new native on an already used identifier, the old native for that identifier will be deleted automatically. Besides assigning the native with Variables::assignNative() this method also sets the variable identifier to <identifier> (unless native is a null-pointer).

template<class Config >
TMPL void Pika::Script< Config >::Frame::registerNative ( const String identifier,
Native native 
)

Registers the native function (or object) native with identifier in the appropriate variable space (determined by any "frame identifier" present in identifier).

Once registered, the native is considered "owned" by the variable space. In other words, all registered natives will be deleted by the Variables destructor. Also, if you register a new native on an already used identifier, the old native for that identifier will be deleted automatically. Besides assigning the native with Variables::assignNative() this method also sets the variable identifier to <identifier> (unless native is a null-pointer).

Definition at line 775 of file PikaScriptImpl.h.

template<class Config >
template<class A0 , class R >
void Pika::Script< Config >::Frame::registerNative ( const String i,
R(*)(A0)  f 
)
inline

Helper template for easily registering a unary C++ function.

The C++ function should take a single argument of either Frame& or any of the native types that are convertible from Script::Value. It should return a value of any type that is convertible to Script::Value or void.

Definition at line 341 of file PikaScript.h.

template<class Config >
template<class A0 , class R >
void Pika::Script< Config >::Frame::registerNative ( const String i,
R(*)(A0)  f 
)
inline

Helper template for easily registering a unary C++ function.

The C++ function should take a single argument of either Frame& or any of the native types that are convertible from Script::Value. It should return a value of any type that is convertible to Script::Value or void.

Definition at line 341 of file PikaScript.h.

template<class Config >
template<class A0 , class A1 , class R >
void Pika::Script< Config >::Frame::registerNative ( const String i,
R(*)(A0, A1)  f 
)
inline

Helper template for easily registering a binary C++ function.

The C++ function should take two arguments of any of the native types that are convertible from Script::Value. It should return a value of any type that is convertible to Script::Value or void.

Definition at line 344 of file PikaScript.h.

template<class Config >
template<class A0 , class A1 , class R >
void Pika::Script< Config >::Frame::registerNative ( const String i,
R(*)(A0, A1)  f 
)
inline

Helper template for easily registering a binary C++ function.

The C++ function should take two arguments of any of the native types that are convertible from Script::Value. It should return a value of any type that is convertible to Script::Value or void.

Definition at line 344 of file PikaScript.h.

template<class Config >
template<class C , class A0 , class R >
void Pika::Script< Config >::Frame::registerNative ( const String i,
C *  o,
R(C::*)(A0)  m 
)
inline

Helper template for easily registering a unary C++ member function in the C++ object pointed to by o.

The C++ member function should take a single argument of either Frame& or any of the native types that are convertible from Script::Value. It should return a value of any type that is convertible to Script::Value or void. Normally, this registration technique is used for bridging Pika function calls to methods of a C++ object which is guaranteed to live as long as the target Frame.

Definition at line 347 of file PikaScript.h.

template<class Config >
template<class C , class A0 , class R >
void Pika::Script< Config >::Frame::registerNative ( const String i,
C *  o,
R(C::*)(A0)  m 
)
inline

Helper template for easily registering a unary C++ member function in the C++ object pointed to by o.

The C++ member function should take a single argument of either Frame& or any of the native types that are convertible from Script::Value. It should return a value of any type that is convertible to Script::Value or void. Normally, this registration technique is used for bridging Pika function calls to methods of a C++ object which is guaranteed to live as long as the target Frame.

Definition at line 347 of file PikaScript.h.

template<class Config >
std::pair< Frame*, String > Pika::Script< Config >::Frame::resolveFrame ( const String identifier) const

Resolves the frame for identifier and returns it together with identifier stripped of any prefixed "frame identifier".

The rules are as follows: 1) If the identifier has a leading ::, the "root frame" is returned. 2) If the identifier begins with an existing frame label, this frame is used for resolving the rest of the identifier. (If a frame label cannot be found an exception is thrown.) 3) For each leading '^' the previous frame is used for resolving the rest of the identifier. 4) Finally, if the identifier does not begin with the '$' character, the "closure" of the current frame is returned, otherwise the current frame is returned.

template<class Config >
TMPL std::pair< T_TYPE(Frame *), T_TYPE(String) > Pika::Script< Config >::Frame::resolveFrame ( const String identifier) const

Resolves the frame for identifier and returns it together with identifier stripped of any prefixed "frame identifier".

The rules are as follows: 1) If the identifier has a leading ::, the "root frame" is returned. 2) If the identifier begins with an existing frame label, this frame is used for resolving the rest of the identifier. (If a frame label cannot be found an exception is thrown.) 3) For each leading '^' the previous frame is used for resolving the rest of the identifier. 4) Finally, if the identifier does not begin with the '$' character, the "closure" of the current frame is returned, otherwise the current frame is returned.

Definition at line 325 of file PikaScriptImpl.h.

template<class Config >
const Value& Pika::Script< Config >::Frame::set ( const String identifier,
const Value v 
)

Sets a variable value.

Just as with get(), identifier may be prefixed with a "frame identifier" to address a different Frame.

template<class Config >
const Value& Pika::Script< Config >::Frame::set ( const String identifier,
const Value v 
)

Sets a variable value.

Just as with get(), identifier may be prefixed with a "frame identifier" to address a different Frame.

template<class Config >
void Pika::Script< Config >::Frame::unregisterNative ( const String identifier)
inline

Helper function for unregistering a native function / object.

Unregistering a native is the same as registering a null-pointer to the identifier. Any PikaScript variable referring to <identifier> will still do so (including the one created automatically by registerNative()). However, performing a function call on such a variable will generate a run-time exception.

Definition at line 350 of file PikaScript.h.

template<class Config >
void Pika::Script< Config >::Frame::unregisterNative ( const String identifier)
inline

Helper function for unregistering a native function / object.

Unregistering a native is the same as registering a null-pointer to the identifier. Any PikaScript variable referring to <identifier> will still do so (including the one created automatically by registerNative()). However, performing a function call on such a variable will generate a run-time exception.

Definition at line 350 of file PikaScript.h.


The documentation for this class was generated from the following files: