Usermode/SpiderScript - Added language namespace exports
[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         tSpiderFunction *Methods;       //!< Method Definitions (linked list)
204         
205          int    NAttributes;    //!< Number of attributes
206         
207         //! Attribute definitions
208         struct {
209                 const char      *Name;  //!< Attribute Name
210                  int    bReadOnly;      //!< Allow writes to the attribute?
211         }       AttributeDefs[];
212 };
213
214 /**
215  * \brief Object Instance
216  */
217 struct sSpiderObject
218 {
219         tSpiderObjectDef        *Type;  //!< Object Type
220          int    ReferenceCount; //!< Number of references
221         void    *OpaqueData;    //!< Pointer to the end of the \a Attributes array
222         tSpiderValue    *Attributes[];  //!< Attribute Array
223 };
224
225 /**
226  * \brief Represents a function avaliable to a script
227  */
228 struct sSpiderFunction
229 {
230         /**
231          * \brief Next function in list
232          */
233         struct sSpiderFunction  *Next;
234         
235         /**
236          * \brief Function name
237          */
238         const char      *Name;
239         /**
240          * \brief Function handler
241          */
242         tSpiderValue    *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
243
244         /**
245          * \brief What type is returned
246          */
247          int    ReturnType;     
248
249         /**
250          * \brief Argument types
251          * 
252          * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
253          * If the final entry is zero, the function has a fixed number of
254          * parameters, if the final entry is -1, the function has a variable
255          * number of arguments.
256          */
257          int    ArgTypes[];     // Zero (or -1) terminated array of parameter types
258 };
259
260
261 // === FUNCTIONS ===
262 /**
263  * \brief Parse a file into a script
264  * \param Variant       Variant structure
265  * \param Filename      File to parse
266  * \return Script suitable for execution
267  */
268 extern tSpiderScript    *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
269 /**
270  * \brief Execute a function from a script
271  * \param Script        Script to run
272  * \param Function      Name of function to run ("" for the 'main')
273  * \return Return value
274  */
275 extern tSpiderValue     *SpiderScript_ExecuteFunction(tSpiderScript *Script,
276         const char *Function, const char *DefaultNamespaces[],
277         int NArguments, tSpiderValue **Arguments,
278         void **FunctionIdent
279         );
280 /**
281  * \brief Execute an object method
282  */
283 extern tSpiderValue     *SpiderScript_ExecuteMethod(tSpiderScript *Script,
284         tSpiderObject *Object, const char *MethodName,
285         int NArguments, tSpiderValue **Arguments
286         );
287 /**
288  * \brief Creates an object instance
289  */
290 extern tSpiderValue     *SpiderScript_CreateObject(tSpiderScript *Script,
291         const char *ClassName, const char *DefaultNamespaces[],
292         int NArguments, tSpiderValue **Arguments
293         );
294
295 /**
296  * \brief Convert a script to bytecode and save to a file
297  */
298 extern int      SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile);
299 /**
300  * \brief Save the AST of a script to a file
301  */
302 extern int      SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename);
303
304 /**
305  * \brief Free a script
306  * \param Script        Script structure to free
307  */
308 extern void     SpiderScript_Free(tSpiderScript *Script);
309
310 extern tSpiderObject    *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
311
312 /**
313  * \name tSpiderValue Manipulation functions
314  * \{
315  */
316 extern void     SpiderScript_DereferenceValue(tSpiderValue *Object);
317 extern void     SpiderScript_ReferenceValue(tSpiderValue *Object);
318 extern tSpiderValue     *SpiderScript_CreateInteger(uint64_t Value);
319 extern tSpiderValue     *SpiderScript_CreateReal(double Value);
320 extern tSpiderValue     *SpiderScript_CreateString(int Length, const char *Data);
321 extern tSpiderValue     *SpiderScript_StringConcat(const tSpiderValue *Str1, const tSpiderValue *Str2);
322 extern tSpiderValue     *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
323 extern int      SpiderScript_IsValueTrue(tSpiderValue *Value);
324 extern void     SpiderScript_FreeValue(tSpiderValue *Value);
325 extern char     *SpiderScript_DumpValue(tSpiderValue *Value);
326
327 extern tSpiderValue     *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right);
328 /**
329  * \}
330  */
331
332 #endif

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