409cf493ce617af95f0558c5e1fde27b69443776
[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 sSpiderFunction  tSpiderFunction;
18 typedef struct sSpiderValue     tSpiderValue;
19 typedef struct sSpiderObjectDef tSpiderObjectDef;
20 typedef struct sSpiderObject    tSpiderObject;
21
22
23 /**
24  * \brief SpiderScript Variable Datatypes
25  * \todo Expand the descriptions
26  */
27 enum eSpiderScript_DataTypes
28 {
29         SS_DATATYPE_UNDEF,      //!< Undefined
30         SS_DATATYPE_NULL,       //!< NULL (Probably will never be used)
31         SS_DATATYPE_DYNAMIC,    //!< Dynamically typed variable (will this be used?)
32         SS_DATATYPE_OPAQUE,     //!< Opaque data type
33         SS_DATATYPE_OBJECT,     //!< Object reference
34         SS_DATATYPE_ARRAY,      //!< Array
35         SS_DATATYPE_INTEGER,    //!< Integer (64-bits)
36         SS_DATATYPE_REAL,       //!< Real Number (double)
37         SS_DATATYPE_STRING,     //!< String
38         NUM_SS_DATATYPES
39 };
40
41 /**
42  * \brief Variant of SpiderScript
43  */
44 struct sSpiderVariant
45 {
46         const char      *Name;  // Just for debug
47         
48          int    bDyamicTyped;   //!< Use static typing
49         
50         tSpiderFunction *Functions;     //!< Functions (Linked List)
51         
52          int    NConstants;     //!< Number of constants
53         tSpiderValue    *Constants;     //!< Number of constants
54 };
55
56 /**
57  * \brief SpiderScript data object
58  */
59 struct sSpiderValue
60 {
61         enum eSpiderScript_DataTypes    Type;   //!< Variable type
62          int    ReferenceCount; //!< Reference count
63         
64         union {
65                 uint64_t        Integer;        //!< Integer data
66                 double  Real;   //!< Real Number data
67                 /**
68                  * \brief String data
69                  */
70                 struct {
71                          int    Length; //!< Length
72                         char    Data[]; //!< Actual string (\a Length bytes)
73                 }       String;
74                 /**
75                  * \brief Variable data
76                  */
77                 struct {
78                          int    Length; //!< Length of the array
79                         tSpiderValue    *Items[];       //!< Array elements (\a Length long)
80                 }       Array;
81                 
82                 /**
83                  * \brief Opaque data
84                  */
85                 struct {
86                         void    *Data;  //!< Data (can be anywhere)
87                          int    Size;   //!< Data size (zero means full opaque)
88                         void    (*Destroy)(void *Data); //!< Called on GC
89                 }       Opaque;
90                 
91                 /**
92                  * \brief Object Instance
93                  */
94                 tSpiderObject   *Object;
95         };
96 };
97
98 /**
99  * \brief Object Definition
100  * 
101  * Internal representation of an arbitary object.
102  */
103 struct sSpiderObjectDef
104 {
105         /**
106          */
107         struct sSpiderObjectDef *Next;  //!< Internal linked list
108         /**
109          * \brief Object type name
110          */
111         const char*     const Name;
112         /**
113          * \brief Construct an instance of the object
114          * \param NArgs Number of arguments
115          * \param Args  Argument count
116          * \return Pointer to an object instance (which must be fully valid)
117          * \retval NULL Invalid parameter (usually, actually just a NULL value)
118          * \retval ERRPTR       Invalid parameter count
119          */
120         tSpiderObject   *(*Constructor)(int NArgs, tSpiderValue *Args);
121         
122         /**
123          * \brief Clean up and destroy the object
124          * \param This  Object instace
125          * \note The object pointer (\a This) should be invalidated and freed
126          *       by this function.
127          */
128         void    (*Destructor)(tSpiderObject *This);
129         
130          int    NAttributes;    //!< Number of attributes
131         
132         //! Attribute definitions
133         struct {
134                 const char      *Name;  //!< Attribute Name
135                  int    bReadOnly;      //!< Allow writes to the attribute?
136         }       *AttributeDefs;
137         
138         
139          int    NMethods;       //!< Number of methods
140         tSpiderFunction *Methods;       //!< Method Definitions
141 };
142
143 /**
144  * \brief Object Instance
145  */
146 struct sSpiderObject
147 {
148         tSpiderObjectDef        *Type;  //!< Object Type
149          int    NReferences;    //!< Number of references
150         void    *OpaqueData;    //!< Pointer to the end of the \a Attributes array
151         tSpiderValue    *Attributes[];  //!< Attribute Array
152 };
153
154 /**
155  * \brief Represents a function avaliable to a script
156  */
157 struct sSpiderFunction
158 {
159         /**
160          * \brief Next function in list
161          */
162         struct sSpiderFunction  *Next;
163         
164         /**
165          * \brief Function name
166          */
167         const char      *Name;
168         /**
169          * \brief Function handler
170          */
171         tSpiderValue    *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
172         /**
173          * \brief Argument types
174          * 
175          * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
176          * If the final entry is zero, the function has a fixed number of
177          * parameters, if the final entry is -1, the function has a variable
178          * number of arguments.
179          */
180          int    ArgTypes[];     // Zero (or -1) terminated array of parameter types
181 };
182
183
184 // === FUNCTIONS ===
185 /**
186  * \brief Parse a file into a script
187  * \param Variant       Variant structure
188  * \param Filename      File to parse
189  * \return Script suitable for execution
190  */
191 extern tSpiderScript    *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
192 /**
193  * \brief Execute a method from a script
194  * \param Script        Script to run
195  * \param Function      Name of function to run ("" for the 'main')
196  * \return Return value
197  */
198 extern tSpiderValue     *SpiderScript_ExecuteMethod(tSpiderScript *Script,
199         const char *Function,
200         int NArguments, tSpiderValue **Arguments
201         );
202
203 /**
204  * \brief Free a script
205  * \param Script        Script structure to free
206  */
207 extern void     SpiderScript_Free(tSpiderScript *Script);
208
209 /**
210  * \name tSpiderValue Manipulation functions
211  * \{
212  */
213 extern tSpiderValue     *SpiderScript_CreateInteger(uint64_t Value);
214 extern tSpiderValue     *SpiderScript_CreateReal(double Value);
215 extern tSpiderValue     *SpiderScript_CreateString(int Length, const char *Data);
216 extern tSpiderValue     *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
217 extern int      SpiderScript_IsValueTrue(tSpiderValue *Value);
218 extern char     *SpiderScript_DumpValue(tSpiderValue *Value);
219 /**
220  * \}
221  */
222
223 #endif

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