PikaScript
|
The execution context and interpreter for PikaScript. More...
#include <PikaScript.h>
Protected Types | |
typedef std::pair< bool, Value > | XValue |
The XValue differentiates lvalues and rvalues and is used internally in the interpreter. More... | |
typedef std::pair< bool, Value > | XValue |
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. | |
Variables & | getVariables () const throw () |
Returns a reference to the Variable instance associated with this Frame. Simple as that. | |
Root & | getRoot () const throw () |
Returns a reference to the "root frame" for this Frame. (No brainer.) | |
Frame & | getPrevious () 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). | |
Variables & | getVariables () const throw () |
Returns a reference to the Variable instance associated with this Frame. Simple as that. | |
Root & | getRoot () const throw () |
Returns a reference to the "root frame" for this Frame. (No brainer.) | |
Frame & | getPrevious () 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 Value & | set (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 *, String > | resolveFrame (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 Value & | set (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 *, String > | resolveFrame (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. | |
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.
|
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.
|
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.
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.
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.
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.
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.
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.
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.
Value Pika::Script< Config >::Frame::execute | ( | const String & | body | ) |
Value Pika::Script< Config >::Frame::execute | ( | const String & | body | ) |
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.
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.
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.
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.
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.
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.
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).
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
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.
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.
const Value& Pika::Script< Config >::Frame::set | ( | const String & | identifier, |
const Value & | v | ||
) |
const Value& Pika::Script< Config >::Frame::set | ( | const String & | identifier, |
const Value & | v | ||
) |
|
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.
|
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.