SpiderScript - Implementing object element/attribute support
[tpg/acess2.git] / Usermode / include / spiderscript.h
1 /*
2  * 
3  */
4 #ifndef _SPIDERSCRIPT_H_
5 #define _SPIDERSCRIPT_H_
6
7 #include <stdint.h>
8
9 #define ERRPTR  ((void*)((intptr_t)0-1))
10
11 /**
12  * \brief Opaque script handle
13  */
14 typedef struct sSpiderScript    tSpiderScript;
15
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;
22
23
24 /**
25  * \brief SpiderScript Variable Datatypes
26  * \todo Expand the descriptions
27  */
28 enum eSpiderScript_DataTypes
29 {
30         /**
31          * \brief Undefined data
32          * \note Default type of an undefined dynamic variable
33          */
34         SS_DATATYPE_UNDEF,
35         /**
36          * \brief Dynamically typed variable
37          * \note Used to dentote a non-fixed type for function parameters
38          */
39         SS_DATATYPE_DYNAMIC,
40         /**
41          * \brief Opaque Data Pointer
42          * 
43          * Opaque data types are used for resource handles or for system buffers.
44          */
45         SS_DATATYPE_OPAQUE,
46         /**
47          * \brief Object reference
48          * 
49          * A reference to a SpiderScript class instance. Can be accessed
50          * using the -> operator.
51          */
52         SS_DATATYPE_OBJECT,
53         /**
54          * \brief Array data type (invalid when using static typing)
55          */
56         SS_DATATYPE_ARRAY,
57         /**
58          * \brief Integer datatype
59          * 
60          * 64-bit integer
61          */
62         SS_DATATYPE_INTEGER,
63         SS_DATATYPE_REAL,       //!< Real Number (double)
64         SS_DATATYPE_STRING,     //!< String
65         NUM_SS_DATATYPES
66 };
67
68 #define SS_MAKEARRAY(_type)     ((_type) + 0x10000)
69 #define SS_DOWNARRAY(_type)     ((_type) - 0x10000)
70 #define SS_GETARRAYDEPTH(_type) ((_type) >> 16)
71
72 enum eSpiderValueOps
73 {
74         SS_VALUEOP_NOP,
75
76         SS_VALUEOP_ADD,
77         SS_VALUEOP_SUBTRACT,
78         SS_VALUEOP_NEGATE,
79         SS_VALUEOP_MULIPLY,
80         SS_VALUEOP_DIVIDE,
81         SS_VALUEOP_MODULO,
82
83         SS_VALUEOP_BITNOT,
84         SS_VALUEOP_BITAND,
85         SS_VALUEOP_BITOR,
86         SS_VALUEOP_BITXOR,
87
88         SS_VALUEOP_SHIFTLEFT,
89         SS_VALUEOP_SHIFTRIGHT,
90         SS_VALUEOP_ROTATELEFT
91 };
92
93 /**
94  * \brief Namespace definition
95  */
96 struct sSpiderNamespace
97 {
98         tSpiderNamespace        *Next;
99         
100         tSpiderNamespace        *FirstChild;
101         
102         tSpiderFunction *Functions;
103         
104         tSpiderObjectDef        *Classes;
105         
106          int    NConstants;     //!< Number of constants
107         tSpiderValue    *Constants;     //!< Number of constants
108         
109         const char      Name[];
110 };
111
112 /**
113  * \brief Variant of SpiderScript
114  */
115 struct sSpiderVariant
116 {
117         const char      *Name;  // Just for debug
118         
119          int    bDyamicTyped;   //!< Use dynamic typing
120          int    bImplicitCasts; //!< Allow implicit casts (casts to lefthand side)
121         
122         tSpiderFunction *Functions;     //!< Functions (Linked List)
123         
124          int    NConstants;     //!< Number of constants
125         tSpiderValue    *Constants;     //!< Number of constants
126         
127         tSpiderNamespace        RootNamespace;
128 };
129
130 /**
131  * \brief SpiderScript data object
132  */
133 struct sSpiderValue
134 {
135         enum eSpiderScript_DataTypes    Type;   //!< Variable type
136          int    ReferenceCount; //!< Reference count
137         
138         union {
139                 int64_t Integer;        //!< Integer data
140                 double  Real;   //!< Real Number data
141                 /**
142                  * \brief String data
143                  */
144                 struct {
145                          int    Length; //!< Length
146                         char    Data[]; //!< Actual string (\a Length bytes)
147                 }       String;
148                 /**
149                  * \brief Variable data
150                  */
151                 struct {
152                          int    Length; //!< Length of the array
153                         tSpiderValue    *Items[];       //!< Array elements (\a Length long)
154                 }       Array;
155                 
156                 /**
157                  * \brief Opaque data
158                  */
159                 struct {
160                         void    *Data;  //!< Data (can be anywhere)
161                         void    (*Destroy)(void *Data); //!< Called on GC
162                 }       Opaque;
163                 
164                 /**
165                  * \brief Object Instance
166                  */
167                 tSpiderObject   *Object;
168         };
169 };
170
171 /**
172  * \brief Object Definition
173  * 
174  * Internal representation of an arbitary object.
175  */
176 struct sSpiderObjectDef
177 {
178         /**
179          */
180         struct sSpiderObjectDef *Next;  //!< Internal linked list
181         /**
182          * \brief Object type name
183          */
184         const char * const      Name;
185         /**
186          * \brief Construct an instance of the object
187          * \param NArgs Number of arguments
188          * \param Args  Argument array
189          * \return Pointer to an object instance (which must be fully valid)
190          * \retval NULL Invalid parameter (usually, actually just a NULL value)
191          * \retval ERRPTR       Invalid parameter count
192          */
193         tSpiderObject   *(*Constructor)(int NArgs, tSpiderValue **Args);
194         
195         /**
196          * \brief Clean up and destroy the object
197          * \param This  Object instace
198          * \note The object pointer (\a This) should be invalidated and freed
199          *       by this function.
200          */
201         void    (*Destructor)(tSpiderObject *This);
202
203
204         /**
205          * \brief Get/Set an attribute's value
206          */
207         tSpiderValue    *(*GetSetAttribute)(tSpiderObject *This, int AttibuteID, tSpiderValue *NewValue);
208         
209         /**
210          * \brief Method Definitions (linked list)
211          */
212         tSpiderFunction *Methods;
213         
214         /**
215          * \brief Number of attributes
216          */
217          int    NAttributes;
218         
219         //! Attribute definitions
220         struct {
221                 const char      *Name;  //!< Attribute Name
222                  int    Type;   //!< Datatype
223                 char    bReadOnly;      //!< Allow writes to the attribute?
224                 char    bMethod;        //!< IO Goes via GetSetAttribute function
225         }       AttributeDefs[];
226 };
227
228 /**
229  * \brief Object Instance
230  */
231 struct sSpiderObject
232 {
233         tSpiderObjectDef        *Type;  //!< Object Type
234          int    ReferenceCount; //!< Number of references
235         void    *OpaqueData;    //!< Pointer to the end of the \a Attributes array
236         tSpiderValue    *Attributes[];  //!< Attribute Array
237 };
238
239 /**
240  * \brief Represents a function avaliable to a script
241  */
242 struct sSpiderFunction
243 {
244         /**
245          * \brief Next function in list
246          */
247         struct sSpiderFunction  *Next;
248         
249         /**
250          * \brief Function name
251          */
252         const char      *Name;
253         /**
254          * \brief Function handler
255          */
256         tSpiderValue    *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
257
258         /**
259          * \brief What type is returned
260          */
261          int    ReturnType;     
262
263         /**
264          * \brief Argument types
265          * 
266          * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
267          * If the final entry is zero, the function has a fixed number of
268          * parameters, if the final entry is -1, the function has a variable
269          * number of arguments.
270          */
271          int    ArgTypes[];     // Zero (or -1) terminated array of parameter types
272 };
273
274
275 // === FUNCTIONS ===
276 /**
277  * \brief Parse a file into a script
278  * \param Variant       Variant structure
279  * \param Filename      File to parse
280  * \return Script suitable for execution
281  */
282 extern tSpiderScript    *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
283 /**
284  * \brief Execute a function from a script
285  * \param Script        Script to run
286  * \param Function      Name of function to run ("" for the 'main')
287  * \return Return value
288  */
289 extern tSpiderValue     *SpiderScript_ExecuteFunction(tSpiderScript *Script,
290         const char *Function, const char *DefaultNamespaces[],
291         int NArguments, tSpiderValue **Arguments,
292         void **FunctionIdent
293         );
294 /**
295  * \brief Execute an object method
296  */
297 extern tSpiderValue     *SpiderScript_ExecuteMethod(tSpiderScript *Script,
298         tSpiderObject *Object, const char *MethodName,
299         int NArguments, tSpiderValue **Arguments
300         );
301 /**
302  * \brief Creates an object instance
303  */
304 extern tSpiderValue     *SpiderScript_CreateObject(tSpiderScript *Script,
305         const char *ClassName, const char *DefaultNamespaces[],
306         int NArguments, tSpiderValue **Arguments
307         );
308
309 /**
310  * \brief Convert a script to bytecode and save to a file
311  */
312 extern int      SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile);
313 /**
314  * \brief Save the AST of a script to a file
315  */
316 extern int      SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename);
317
318 /**
319  * \brief Free a script
320  * \param Script        Script structure to free
321  */
322 extern void     SpiderScript_Free(tSpiderScript *Script);
323
324 extern tSpiderObject    *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
325
326 /**
327  * \name tSpiderValue Manipulation functions
328  * \{
329  */
330 extern void     SpiderScript_DereferenceValue(tSpiderValue *Object);
331 extern void     SpiderScript_ReferenceValue(tSpiderValue *Object);
332 extern tSpiderValue     *SpiderScript_CreateInteger(uint64_t Value);
333 extern tSpiderValue     *SpiderScript_CreateReal(double Value);
334 extern tSpiderValue     *SpiderScript_CreateString(int Length, const char *Data);
335 extern tSpiderValue     *SpiderScript_CreateArray(int InnerType, int ItemCount);
336 extern tSpiderValue     *SpiderScript_StringConcat(const tSpiderValue *Str1, const tSpiderValue *Str2);
337 extern tSpiderValue     *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
338 extern int      SpiderScript_IsValueTrue(tSpiderValue *Value);
339 extern void     SpiderScript_FreeValue(tSpiderValue *Value);
340 extern char     *SpiderScript_DumpValue(tSpiderValue *Value);
341
342 extern tSpiderValue     *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right);
343 /**
344  * \}
345  */
346
347 #endif

UCC git Repository :: git.ucc.asn.au