4 #ifndef _SPIDERSCRIPT_H_
5 #define _SPIDERSCRIPT_H_
9 #define ERRPTR ((void*)((intptr_t)0-1))
12 * \brief Opaque script handle
14 typedef struct sSpiderScript tSpiderScript;
16 typedef struct sSpiderVariant tSpiderVariant;
17 typedef struct sSpiderNamespace tSpiderNamespace;
18 typedef struct sSpiderFunction tSpiderFunction;
19 typedef struct sSpiderValue tSpiderValue;
20 typedef struct sSpiderObjectDef tSpiderObjectDef;
21 typedef struct sSpiderObject tSpiderObject;
25 * \brief SpiderScript Variable Datatypes
26 * \todo Expand the descriptions
28 enum eSpiderScript_DataTypes
31 * \brief Undefined data
32 * \note Default type of an undefined dynamic variable
36 * \brief Dynamically typed variable
37 * \note Used to dentote a non-fixed type for function parameters
41 * \brief Opaque Data Pointer
43 * Opaque data types are used for resource handles or for system buffers.
47 * \brief Object reference
49 * A reference to a SpiderScript class instance. Can be accessed
50 * using the -> operator.
54 * \brief Array data type
58 * \brief Integer datatype
63 SS_DATATYPE_REAL, //!< Real Number (double)
64 SS_DATATYPE_STRING, //!< String
85 SS_VALUEOP_SHIFTRIGHT,
90 * \brief Namespace definition
92 struct sSpiderNamespace
94 tSpiderNamespace *Next;
96 tSpiderNamespace *FirstChild;
98 tSpiderFunction *Functions;
100 tSpiderObjectDef *Classes;
102 int NConstants; //!< Number of constants
103 tSpiderValue *Constants; //!< Number of constants
109 * \brief Variant of SpiderScript
111 struct sSpiderVariant
113 const char *Name; // Just for debug
115 int bDyamicTyped; //!< Use dynamic typing
116 int bImplicitCasts; //!< Allow implicit casts (casts to lefthand side)
118 tSpiderFunction *Functions; //!< Functions (Linked List)
120 int NConstants; //!< Number of constants
121 tSpiderValue *Constants; //!< Number of constants
123 tSpiderNamespace RootNamespace;
127 * \brief SpiderScript data object
131 enum eSpiderScript_DataTypes Type; //!< Variable type
132 int ReferenceCount; //!< Reference count
135 int64_t Integer; //!< Integer data
136 double Real; //!< Real Number data
141 int Length; //!< Length
142 char Data[]; //!< Actual string (\a Length bytes)
145 * \brief Variable data
148 int Length; //!< Length of the array
149 tSpiderValue *Items[]; //!< Array elements (\a Length long)
156 void *Data; //!< Data (can be anywhere)
157 void (*Destroy)(void *Data); //!< Called on GC
161 * \brief Object Instance
163 tSpiderObject *Object;
168 * \brief Object Definition
170 * Internal representation of an arbitary object.
172 struct sSpiderObjectDef
176 struct sSpiderObjectDef *Next; //!< Internal linked list
178 * \brief Object type name
180 const char * const Name;
182 * \brief Construct an instance of the object
183 * \param NArgs Number of arguments
184 * \param Args Argument array
185 * \return Pointer to an object instance (which must be fully valid)
186 * \retval NULL Invalid parameter (usually, actually just a NULL value)
187 * \retval ERRPTR Invalid parameter count
189 tSpiderObject *(*Constructor)(int NArgs, tSpiderValue **Args);
192 * \brief Clean up and destroy the object
193 * \param This Object instace
194 * \note The object pointer (\a This) should be invalidated and freed
197 void (*Destructor)(tSpiderObject *This);
199 tSpiderFunction *Methods; //!< Method Definitions (linked list)
201 int NAttributes; //!< Number of attributes
203 //! Attribute definitions
205 const char *Name; //!< Attribute Name
206 int bReadOnly; //!< Allow writes to the attribute?
211 * \brief Object Instance
215 tSpiderObjectDef *Type; //!< Object Type
216 int ReferenceCount; //!< Number of references
217 void *OpaqueData; //!< Pointer to the end of the \a Attributes array
218 tSpiderValue *Attributes[]; //!< Attribute Array
222 * \brief Represents a function avaliable to a script
224 struct sSpiderFunction
227 * \brief Next function in list
229 struct sSpiderFunction *Next;
232 * \brief Function name
236 * \brief Function handler
238 tSpiderValue *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
240 * \brief Argument types
242 * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
243 * If the final entry is zero, the function has a fixed number of
244 * parameters, if the final entry is -1, the function has a variable
245 * number of arguments.
247 int ArgTypes[]; // Zero (or -1) terminated array of parameter types
253 * \brief Parse a file into a script
254 * \param Variant Variant structure
255 * \param Filename File to parse
256 * \return Script suitable for execution
258 extern tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
260 * \brief Execute a function from a script
261 * \param Script Script to run
262 * \param Function Name of function to run ("" for the 'main')
263 * \return Return value
265 extern tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
266 tSpiderNamespace *Namespace, const char *Function,
267 int NArguments, tSpiderValue **Arguments
270 * \brief Execute an object method
272 extern tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
273 tSpiderObject *Object, const char *MethodName,
274 int NArguments, tSpiderValue **Arguments
277 * \brief Creates an object instance
279 extern tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
280 tSpiderNamespace *Namespace, const char *ClassName,
281 int NArguments, tSpiderValue **Arguments
285 * \brief Convert a script to bytecode and save to a file
287 extern int SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile);
289 * \brief Save the AST of a script to a file
291 extern int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename);
294 * \brief Free a script
295 * \param Script Script structure to free
297 extern void SpiderScript_Free(tSpiderScript *Script);
299 extern tSpiderObject *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
302 * \name tSpiderValue Manipulation functions
305 extern void SpiderScript_DereferenceValue(tSpiderValue *Object);
306 extern void SpiderScript_ReferenceValue(tSpiderValue *Object);
307 extern tSpiderValue *SpiderScript_CreateInteger(uint64_t Value);
308 extern tSpiderValue *SpiderScript_CreateReal(double Value);
309 extern tSpiderValue *SpiderScript_CreateString(int Length, const char *Data);
310 extern tSpiderValue *SpiderScript_StringConcat(const tSpiderValue *Str1, const tSpiderValue *Str2);
311 extern tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
312 extern int SpiderScript_IsValueTrue(tSpiderValue *Value);
313 extern void SpiderScript_FreeValue(tSpiderValue *Value);
314 extern char *SpiderScript_DumpValue(tSpiderValue *Value);
316 extern tSpiderValue *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right);