62 #define PIKA_SCRIPT_VERSION L"0.942"
65 #define PIKA_SCRIPT_VERSION "0.942"
68 typedef unsigned char uchar;
69 typedef unsigned int uint;
70 typedef unsigned long ulong;
83 template<
class S> std::string
toStdString(
const S& s);
84 template<
class S> ulong
hexToLong(
typename S::const_iterator& p,
const typename S::const_iterator& e);
85 template<
class S>
long stringToLong(
typename S::const_iterator& p,
const typename S::const_iterator& e);
86 template<
class S,
typename T> S
intToString(T i,
int radix = 10,
int minLength = 1);
87 template<
class S>
double stringToDouble(
typename S::const_iterator& p,
const typename S::const_iterator& e);
90 template<
class S> S
unescape(
typename S::const_iterator& p,
const typename S::const_iterator& e);
91 template<
class S> S
escape(
const S& s);
102 template<
class C,
class A0,
class R>
class bound_mem_fun_t :
public std::unary_function<A0, R> {
104 public: R operator()(A0 a)
const {
return (o->*m)(a); }
105 protected: R (C::*m)(A0);
127 template<
class T>
class Dumb { };
135 template<
class S>
class Exception :
public std::exception {
178 public:
typedef typename S::value_type
Char;
189 public:
STLValue(
bool b) : S(b ? S(STR(
"true")) : S(STR(
"false"))) { }
190 public:
template<
class T>
STLValue(
const T& s) : S(s) { }
194 public:
operator bool()
const;
195 public:
operator long()
const;
196 public:
operator double()
const;
197 public:
operator float()
const {
return float(
double(*
this)); }
198 public:
operator ulong()
const {
return ulong(
long(*
this)); }
199 public:
operator int()
const {
return int(
long(*
this)); }
200 public:
operator uint()
const {
return uint(
int(*
this)); }
214 public:
bool isVoid()
const {
return S::empty(); }
268 typedef typename Config::Value
Value;
270 typedef typename String::value_type
Char;
293 public:
typedef std::vector< std::pair<String, Value> > VarList;
296 public:
virtual bool erase(
const String& symbol) = 0;
325 public:
Value get(
const String& identifier,
bool fallback =
false)
const;
356 protected:
typedef std::pair<bool, Value>
XValue;
358 protected:
Value rvalue(
const XValue& v,
bool fallback =
true);
369 protected:
template<
class E,
class I,
class S>
bool lgtOp(
StringIt& p,
const StringIt& e,
XValue& v,
bool dry
370 ,
Precedence thres,
const E& excl,
const I& incl, S shift);
378 protected:
Root& root;
379 protected:
Frame*
const previous;
380 protected:
Frame* closure;
381 protected:
const String* source;
382 protected:
const String label;
423 public:
FullRoot(
bool includeIONatives =
true) :
Root(*(typename Config::Globals*)(this)) {
436 public:
typedef std::map<const String, Value> VariableMap;
437 public:
typedef std::map<const String, Native* > NativeMap;
438 public:
virtual bool lookup(
const String& symbol,
Value& result);
439 public:
virtual bool assign(
const String& symbol,
const Value& value) { vars[symbol] = value;
return true; }
440 public:
virtual bool erase(
const String& symbol) {
return (vars.
erase(symbol) > 0); }
441 public:
virtual void list(
const String& key,
typename Variables::VarList& list);
442 public:
virtual Native* lookupNative(
const String& name);
443 public:
virtual bool assignNative(
const String& name,
Native* native);
445 public: VariableMap vars;
446 public: NativeMap natives;
461 public:
virtual ~
Native();
484 template<
class F,
class A0 =
typename F::argument_type,
class R =
typename F::result_type>
class UnaryFunctor
486 public:
UnaryFunctor(
const F& functor) : func(functor) { }
487 protected:
template<
class T>
const Value arg(
Frame& f,
const T&) {
return f.
get(STR(
"$0")); }
499 template<
class F,
class A0 =
typename F::first_argument_type,
class A1 =
typename F::second_argument_type
502 protected:
template<
class T>
Value call(
const Value& a0,
const Value& a1,
const T&) {
return func(a0, a1); }
535 static String character(
double d);
536 static bool deleter(
const Frame& frame);
538 static bool exists(
const Frame& frame);
540 static void foreach(
Frame& frame);
543 static ulong length(
const String& s);
546 static ulong mismatch(
const String& a,
const String& b);
547 static uint ordinal(
const String& s);
548 static ulong parse(
Frame& frame);
550 static void print(
const String& s);
552 static double random(
double m);
554 static void save(
const String& file,
const String& chars);
555 static int system(
const String& command);
558 static void thrower(
const String& s);
560 static void trace(
const Frame& frame);
x=y x*=y x/=y x\=y x%=y x+=y x-=y x<<=y x>>=y x#=y x&=y x^=y x|=y
static void addLibraryNatives(Frame &frame, bool includeIO=true)
Registers the standard library native functions to frame. If includeIO is false, 'load', 'save', 'input', 'print' and 'system' will not be registered. Please, refer to the PikaScript standard library reference guide for more info on individual native functions.
static Value getMethod(Frame &frame)
Returns only the "method" value as descripted in getThisAndMethod().
Value call(const Value &a0, const Value &a1, const T &)
Call the functor with the arguments a0 and a1.
Value call(A &a, const Dumb< void > &)
Overloaded to return the void value for functors that returns the void type.
Char * autoLabelStart
The first character of the last generated frame label (begins at autoLabel + 30 and slowly moves back...
Value call(A &a, const Dumb< T > &)
Call the functor with the argument a.
Frame & arg(Frame &f, const Dumb< Frame & > &)
Overloaded arg() that returns the actual frame instead of getting the argument value if the functor a...
String::value_type Char
The character type for all strings (defined by the string class). E.g. char.
virtual bool lookup(const String &symbol, Value &result)=0
Lookup symbol.
The PikaScript exception class.
Value getOptional(const String &identifier, const Value &defaultValue=Value()) const
Tries to get the variable value as with get() (but never "falls back").
virtual ~Variables()
Destructor.
Exception(const S &error)
Simply constructs the exception with error string error.
bool operator>=(const STLValue &r) const
Greater than or equal to comparison operator.
std::pair< bool, Value > XValue
The XValue differentiates lvalues and rvalues and is used internally in the interpreter.
S unescape(typename S::const_iterator &p, const typename S::const_iterator &e)
Converts a string that is either enclosed in single (' ') or double (" ") quotes. ...
String::const_iterator StringIt
The const_iterator of the string is used so frequently it deserves its own typedef.
FullRoot(bool includeIONatives=true)
We use this dummy class to specialize member functions for arbitrary types (including void...
STLValue(const T &s)
Pass other types of construction onwards to the super-class S.
Value get(const String &identifier, bool fallback=false) const
Gets a variable value.
bool operator<(const STLValue &r) const
Less than comparison operator.
Variables & getVariables() const
Returns a reference to the Variable instance associated with this Frame. Simple as that...
String generateLabel()
Each "sub-frame" requires a unique "frame label".
We provide two Native template classes for bridging PikaScript calls to C++ "functors".
S String
The class to use for all strings (i.e. the super-class).
bool operator>(const STLValue &r) const
Greater than comparison operator.
Value execute(const String &body)
A low-level function that executes body directly on the Frame instance.
StdConfig is a configuration class for Script that uses the reference implementations of STLValue and...
Config::Value Value
The class used for all values and variables (defined by the configuration meta-class). E.g. STLValue.
std::pair< Frame *, String > resolveFrame(const String &identifier) const
Resolves the frame for identifier and returns it together with identifier stripped of any prefixed "f...
virtual const char * what() const
Returns the error as a null-terminated char string.
bool isVoid() const
Returns true if the value represents the empty string.
const Frame & arg(Frame &f, const Dumb< const Frame & > &)
Overloaded arg() that returns the actual frame instead of getting the argument value if the functor a...
STLValue(ulong i)
Constructs a value representing the ulong integer l.
virtual bool assignNative(const String &identifier, Native *native)=0
Assign the native function (or object) native to identifier, replacing any already existing definitio...
The Root is the first Frame you instantiate.
Value call(const Value &a0, const Value &a1, const Dumb< void > &)
Overloaded to return the void value for functors that returns the void type.
S escape(const S &s)
Depending on the contents of the source string s it is encoded either in single (' ') or double (" ")...
void unregisterNative(const String &identifier)
Helper function for unregistering a native function / object.
void registerNative(const String &i, R(*f)(A0))
Helper template for easily registering a unary C++ function.
bool operator<=(const STLValue &r) const
Less than or equal to comparison operator.
Script< StdConfig > StdScript
This typedef exist for your convenience.
Precedence
Precedence levels are used both internally for the parser and externally for the tracing mechanism...
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...
virtual S getError() const
Return the error string for this exception.
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...
virtual Value pikaCall(Frame &)
Process the PikaScript call.
Root & getRoot() const
Returns a reference to the "root frame" for this Frame. (No brainer.)
STLValue(uint i)
Constructs a value representing the unsigned integer i.
Value reference(const String &identifier) const
Creates a reference to the variable identified by identifier by prefixing it with a "frame label"...
used only for tracing with tick()
S intToString(T i, int radix=10, int minLength=1)
Converts the integer i to a string with a radix and minimum length of your choice.
void registerNative(const String &identifier, Native *native)
Registers the native function (or object) native with identifier in the appropriate variable space (d...
See UnaryFunctor for documentation.
STLValue(float f)
Constructs a value representing the single precision floating point f.
double stringToDouble(typename S::const_iterator &p, const typename S::const_iterator &e)
Converts a string in scientific e notation (e.g. -12.34e-3) to a double floating point value...
STLValue()
The default constructor initializes the value to the empty string (or "void").
The execution context and interpreter for PikaScript.
ulong hexToLong(typename S::const_iterator &p, const typename S::const_iterator &e)
Converts a string in hexadecimal form to an ulong integer.
static BinaryFunctor< F > * newBinaryFunctor(const F &f)
Helper function to create a BinaryFunctor class with correct template parameters. ...
Value::String String
The class used for strings (defined by the string class). E.g. std::string.
STLValue(long i)
Constructs a value representing the signed long integer l.
Value tracerFunction
Pika-script tracer function (used by the default trace() implementation).
const STLValue operator[](const STLValue &i) const
The subscript operator returns the concatenation of the value with the dot (.) separator (if necessar...
virtual bool assign(const String &symbol, const Value &value)=0
Assign value to symbol and return true if the assignment succeeded.
static Value getThis(Frame &frame)
Returns only the "this" value as descripted in getThisAndMethod().
String::size_type SizeType
The length type for all strings (defined by the string class). E.g. size_t.
void registerNative(const String &i, R(*f)(A0, A1))
Helper template for easily registering a binary C++ function.
Script is a meta-class that groups all the core classes of the PikaScript interpreter together (excep...
STLValue is the reference implementation of a PikaScript variable.
static std::pair< Value, String > getThisAndMethod(Frame &frame)
getThisAndMethod splits the $callee variable of frame into object ("this") and method.
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)...
Value evaluate(const String source)
Evaluates the PikaScript expression in source directly on this Frame.
Char autoLabel[32]
The last generated frame label (padded with leading ':').
bool operator==(const STLValue &r) const
Equality operator.
bound_mem_fun_t< C, A0, R > bound_mem_fun(R(C::*m)(A0), C *o)
bound_mem_fun creates a member functor bound to a specific C++ object through a pointer.
virtual Native * lookupNative(const String &identifier)=0
Lookup the native function (or object) with identifier.
S::value_type Char
The character type for all strings (e.g. char or wchar_t).
used only for tracing with tick()
Exception< String > Xception
The exception type.
STLValue(bool b)
Constructs a value representing the boolean b.
const Value & set(const String &identifier, const Value &v)
Sets a variable value.
static UnaryFunctor< F > * newUnaryFunctor(const F &f)
Helper function to create a UnaryFunctor class with correct template parameters.
bool isInsideTracer
Set to prevent recursive calling of tracer (used by the default trace() implementation).
STLVariables is the reference implementation of a variable space.
std::string converted
Since what() is defined to return a pointer only, we need storage for the converted string within thi...
used only for tracing with tick()
virtual void list(const String &key, VarList &list)=0
Iterate all symbols that begins with key and push back names and values to list.
virtual ~Frame()
The default destructor does nothing, but it is always good practice to have a virtual destructor...
Native is the base class for the native functions and objects that can be accessed from PikaScript...
long stringToLong(typename S::const_iterator &p, const typename S::const_iterator &e)
Converts a string in decimal form to a signed long integer.
virtual void trace(Frame &frame, const String &source, SizeType offset, bool lvalue, const Value &value, Precedence level, bool exit)
Overload this member function if you want to customize the tracing mechanism in PikaScript.
bool doTrace(Precedence level) const
virtual void setTracer(Precedence traceLevel, const Value &tracerFunction)
Called by the standard library function "trace" to assign a PikaScript tracer function and a trace le...
Frame & getPrevious() const
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).
S doubleToString(double d, int precision=14)
Converts the double d to a string (in scientific e notation, e.g. -12.34e-3).
static Value elevate(Frame &frame)
Used to "aggregate" different method calls into a single function call.
Frame(Variables &vars, Root &root, Frame *previous)
Constructs the Frame and associates it with the variable space vars.
used only for tracing with tick()
Precedence traceLevel
Calls to trace() will only happen when the "precedence level" is less or equal to this...
virtual bool erase(const String &symbol)=0
Erase symbol. Return true if the symbol existed and was successfully erased.
bool operator!=(const STLValue &r) const
Non-equality operator.
FullRoot inherits from both Root and Config::Globals (which should be a descendant to Variable)...
Variables is an abstract base class which implements the interface to the variable space that a Frame...
const Value arg(Frame &f, const T &)
Get the first argument from the frame f.
STLValue(int i)
Constructs a value representing the signed integer i.
std::string toStdString(const S &s)
Converts the string s to a standard C++ string.
STLValue(double d)
Constructs a value representing the double precision floating point d.
bound_mem_fun_t is a member functor bound to a specific C++ object through a pointer.