Major build system changes
[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
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 enum eSpiderValueOps
69 {
70         SS_VALUEOP_NOP,
71
72         SS_VALUEOP_ADD,
73         SS_VALUEOP_SUBTRACT,
74         SS_VALUEOP_NEGATE,
75         SS_VALUEOP_MULIPLY,
76         SS_VALUEOP_DIVIDE,
77         SS_VALUEOP_MODULO,
78
79         SS_VALUEOP_BITNOT,
80         SS_VALUEOP_BITAND,
81         SS_VALUEOP_BITOR,
82         SS_VALUEOP_BITXOR,
83
84         SS_VALUEOP_SHIFTLEFT,
85         SS_VALUEOP_SHIFTRIGHT,
86         SS_VALUEOP_ROTATELEFT
87 };
88
89 /**
90  * \brief Namespace definition
91  */
92 struct sSpiderNamespace
93 {
94         tSpiderNamespace        *Next;
95         
96         tSpiderNamespace        *FirstChild;
97         
98         tSpiderFunction *Functions;
99         
100         tSpiderObjectDef        *Classes;
101         
102          int    NConstants;     //!< Number of constants
103         tSpiderValue    *Constants;     //!< Number of constants
104         
105         const char      Name[];
106 };
107
108 /**
109  * \brief Variant of SpiderScript
110  */
111 struct sSpiderVariant
112 {
113         const char      *Name;  // Just for debug
114         
115          int    bDyamicTyped;   //!< Use dynamic typing
116          int    bImplicitCasts; //!< Allow implicit casts (casts to lefthand side)
117         
118         tSpiderFunction *Functions;     //!< Functions (Linked List)
119         
120          int    NConstants;     //!< Number of constants
121         tSpiderValue    *Constants;     //!< Number of constants
122         
123         tSpiderNamespace        RootNamespace;
124 };
125
126 /**
127  * \brief SpiderScript data object
128  */
129 struct sSpiderValue
130 {
131         enum eSpiderScript_DataTypes    Type;   //!< Variable type
132          int    ReferenceCount; //!< Reference count
133         
134         union {
135                 int64_t Integer;        //!< Integer data
136                 double  Real;   //!< Real Number data
137                 /**
138                  * \brief String data
139                  */
140                 struct {
141                          int    Length; //!< Length
142                         char    Data[]; //!< Actual string (\a Length bytes)
143                 }       String;
144                 /**
145                  * \brief Variable data
146                  */
147                 struct {
148                          int    Length; //!< Length of the array
149                         tSpiderValue    *Items[];       //!< Array elements (\a Length long)
150                 }       Array;
151                 
152                 /**
153                  * \brief Opaque data
154                  */
155                 struct {
156                         void    *Data;  //!< Data (can be anywhere)
157                         void    (*Destroy)(void *Data); //!< Called on GC
158                 }       Opaque;
159                 
160                 /**
161                  * \brief Object Instance
162                  */
163                 tSpiderObject   *Object;
164         };
165 };
166
167 /**
168  * \brief Object Definition
169  * 
170  * Internal representation of an arbitary object.
171  */
172 struct sSpiderObjectDef
173 {
174         /**
175          */
176         struct sSpiderObjectDef *Next;  //!< Internal linked list
177         /**
178          * \brief Object type name
179          */
180         const char * const      Name;
181         /**
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
188          */
189         tSpiderObject   *(*Constructor)(int NArgs, tSpiderValue **Args);
190         
191         /**
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
195          *       by this function.
196          */
197         void    (*Destructor)(tSpiderObject *This);
198         
199         tSpiderFunction *Methods;       //!< Method Definitions (linked list)
200         
201          int    NAttributes;    //!< Number of attributes
202         
203         //! Attribute definitions
204         struct {
205                 const char      *Name;  //!< Attribute Name
206                  int    bReadOnly;      //!< Allow writes to the attribute?
207         }       AttributeDefs[];
208 };
209
210 /**
211  * \brief Object Instance
212  */
213 struct sSpiderObject
214 {
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
219 };
220
221 /**
222  * \brief Represents a function avaliable to a script
223  */
224 struct sSpiderFunction
225 {
226         /**
227          * \brief Next function in list
228          */
229         struct sSpiderFunction  *Next;
230         
231         /**
232          * \brief Function name
233          */
234         const char      *Name;
235         /**
236          * \brief Function handler
237          */
238         tSpiderValue    *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
239         /**
240          * \brief Argument types
241          * 
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.
246          */
247          int    ArgTypes[];     // Zero (or -1) terminated array of parameter types
248 };
249
250
251 // === FUNCTIONS ===
252 /**
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
257  */
258 extern tSpiderScript    *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
259 /**
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
264  */
265 extern tSpiderValue     *SpiderScript_ExecuteFunction(tSpiderScript *Script,
266         tSpiderNamespace *Namespace, const char *Function,
267         int NArguments, tSpiderValue **Arguments
268         );
269 /**
270  * \brief Execute an object method
271  */
272 extern tSpiderValue     *SpiderScript_ExecuteMethod(tSpiderScript *Script,
273         tSpiderObject *Object, const char *MethodName,
274         int NArguments, tSpiderValue **Arguments
275         );
276 /**
277  * \brief Creates an object instance
278  */
279 extern tSpiderValue     *SpiderScript_CreateObject(tSpiderScript *Script,
280         tSpiderNamespace *Namespace, const char *ClassName,
281         int NArguments, tSpiderValue **Arguments
282         );
283
284 /**
285  * \brief Convert a script to bytecode and save to a file
286  */
287 extern int      SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile);
288 /**
289  * \brief Save the AST of a script to a file
290  */
291 extern int      SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename);
292
293 /**
294  * \brief Free a script
295  * \param Script        Script structure to free
296  */
297 extern void     SpiderScript_Free(tSpiderScript *Script);
298
299 extern tSpiderObject    *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
300
301 /**
302  * \name tSpiderValue Manipulation functions
303  * \{
304  */
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);
315
316 extern tSpiderValue     *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right);
317 /**
318  * \}
319  */
320
321 #endif

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