SpiderScript - Implementing objects and classes, fixing bugs
[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 /**
69  * \brief Namespace definition
70  */
71 struct sSpiderNamespace
72 {
73         tSpiderNamespace        *Next;
74         
75         tSpiderNamespace        *FirstChild;
76         
77         tSpiderFunction *Functions;
78         
79         tSpiderObjectDef        *Classes;
80         
81          int    NConstants;     //!< Number of constants
82         tSpiderValue    *Constants;     //!< Number of constants
83         
84         const char      Name[];
85 };
86
87 /**
88  * \brief Variant of SpiderScript
89  */
90 struct sSpiderVariant
91 {
92         const char      *Name;  // Just for debug
93         
94          int    bDyamicTyped;   //!< Use dynamic typing
95          int    bImplicitCasts; //!< Allow implicit casts (casts to lefthand side)
96         
97         tSpiderFunction *Functions;     //!< Functions (Linked List)
98         
99          int    NConstants;     //!< Number of constants
100         tSpiderValue    *Constants;     //!< Number of constants
101         
102         tSpiderNamespace        RootNamespace;
103 };
104
105 /**
106  * \brief SpiderScript data object
107  */
108 struct sSpiderValue
109 {
110         enum eSpiderScript_DataTypes    Type;   //!< Variable type
111          int    ReferenceCount; //!< Reference count
112         
113         union {
114                 uint64_t        Integer;        //!< Integer data
115                 double  Real;   //!< Real Number data
116                 /**
117                  * \brief String data
118                  */
119                 struct {
120                          int    Length; //!< Length
121                         char    Data[]; //!< Actual string (\a Length bytes)
122                 }       String;
123                 /**
124                  * \brief Variable data
125                  */
126                 struct {
127                          int    Length; //!< Length of the array
128                         tSpiderValue    *Items[];       //!< Array elements (\a Length long)
129                 }       Array;
130                 
131                 /**
132                  * \brief Opaque data
133                  */
134                 struct {
135                         void    *Data;  //!< Data (can be anywhere)
136                         void    (*Destroy)(void *Data); //!< Called on GC
137                 }       Opaque;
138                 
139                 /**
140                  * \brief Object Instance
141                  */
142                 tSpiderObject   *Object;
143         };
144 };
145
146 /**
147  * \brief Object Definition
148  * 
149  * Internal representation of an arbitary object.
150  */
151 struct sSpiderObjectDef
152 {
153         /**
154          */
155         struct sSpiderObjectDef *Next;  //!< Internal linked list
156         /**
157          * \brief Object type name
158          */
159         const char * const      Name;
160         /**
161          * \brief Construct an instance of the object
162          * \param NArgs Number of arguments
163          * \param Args  Argument array
164          * \return Pointer to an object instance (which must be fully valid)
165          * \retval NULL Invalid parameter (usually, actually just a NULL value)
166          * \retval ERRPTR       Invalid parameter count
167          */
168         tSpiderObject   *(*Constructor)(int NArgs, tSpiderValue **Args);
169         
170         /**
171          * \brief Clean up and destroy the object
172          * \param This  Object instace
173          * \note The object pointer (\a This) should be invalidated and freed
174          *       by this function.
175          */
176         void    (*Destructor)(tSpiderObject *This);
177         
178         tSpiderFunction *Methods;       //!< Method Definitions (linked list)
179         
180          int    NAttributes;    //!< Number of attributes
181         
182         //! Attribute definitions
183         struct {
184                 const char      *Name;  //!< Attribute Name
185                  int    bReadOnly;      //!< Allow writes to the attribute?
186         }       AttributeDefs[];
187 };
188
189 /**
190  * \brief Object Instance
191  */
192 struct sSpiderObject
193 {
194         tSpiderObjectDef        *Type;  //!< Object Type
195          int    ReferenceCount; //!< Number of references
196         void    *OpaqueData;    //!< Pointer to the end of the \a Attributes array
197         tSpiderValue    *Attributes[];  //!< Attribute Array
198 };
199
200 /**
201  * \brief Represents a function avaliable to a script
202  */
203 struct sSpiderFunction
204 {
205         /**
206          * \brief Next function in list
207          */
208         struct sSpiderFunction  *Next;
209         
210         /**
211          * \brief Function name
212          */
213         const char      *Name;
214         /**
215          * \brief Function handler
216          */
217         tSpiderValue    *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
218         /**
219          * \brief Argument types
220          * 
221          * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
222          * If the final entry is zero, the function has a fixed number of
223          * parameters, if the final entry is -1, the function has a variable
224          * number of arguments.
225          */
226          int    ArgTypes[];     // Zero (or -1) terminated array of parameter types
227 };
228
229
230 // === FUNCTIONS ===
231 /**
232  * \brief Parse a file into a script
233  * \param Variant       Variant structure
234  * \param Filename      File to parse
235  * \return Script suitable for execution
236  */
237 extern tSpiderScript    *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
238 /**
239  * \brief Execute a function from a script
240  * \param Script        Script to run
241  * \param Function      Name of function to run ("" for the 'main')
242  * \return Return value
243  */
244 extern tSpiderValue     *SpiderScript_ExecuteFunction(tSpiderScript *Script,
245         tSpiderNamespace *Namespace, const char *Function,
246         int NArguments, tSpiderValue **Arguments
247         );
248
249 /**
250  * \brief Free a script
251  * \param Script        Script structure to free
252  */
253 extern void     SpiderScript_Free(tSpiderScript *Script);
254
255 extern tSpiderObject    *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
256
257 /**
258  * \name tSpiderValue Manipulation functions
259  * \{
260  */
261 extern tSpiderValue     *SpiderScript_CreateInteger(uint64_t Value);
262 extern tSpiderValue     *SpiderScript_CreateReal(double Value);
263 extern tSpiderValue     *SpiderScript_CreateString(int Length, const char *Data);
264 extern tSpiderValue     *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
265 extern int      SpiderScript_IsValueTrue(tSpiderValue *Value);
266 extern void     SpiderScript_FreeValue(tSpiderValue *Value);
267 extern char     *SpiderScript_DumpValue(tSpiderValue *Value);
268 /**
269  * \}
270  */
271
272 #endif

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