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

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