Usermode/libspiderscript - Fixing a multitude of bugs
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / exec.c
1 /*
2 * SpiderScript Library
3 * by John Hodge (thePowersGang)
4
5 * bytecode_makefile.c
6 * - Generate a bytecode file
7 */
8 #include <stdlib.h>
9 #include "common.h"
10 #include "ast.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdarg.h>
14
15 // === IMPORTS ===
16 extern tSpiderFunction  *gpExports_First;
17 extern tSpiderValue     *AST_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Arguments);
18 extern tSpiderValue     *Bytecode_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Args);
19
20 // === PROTOTYPES ===
21 void    AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...);
22 void    AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
23
24 // === CODE ===
25 /**
26  * \brief Execute a script function
27  * \param Script        Script context to execute in
28  * \param Namespace     Namespace to search for the function
29  * \param Function      Function name to execute
30  * \param NArguments    Number of arguments to pass
31  * \param Arguments     Arguments passed
32  */
33 tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
34         tSpiderNamespace *Namespace, const char *Function,
35         int NArguments, tSpiderValue **Arguments)
36 {
37          int    bFound = 0;     // Used to keep nesting levels down
38         tSpiderValue    *ret = ERRPTR;
39         
40         // First: Find the function in the script
41         {
42                 tScript_Function        *fcn;
43                 for( fcn = Script->Functions; fcn; fcn = fcn->Next )
44                 {
45                         if( strcmp(fcn->Name, Function) == 0 )
46                                 break;
47                 }
48                 // Execute!
49                 if(fcn)
50                 {
51                         #if 1
52                         if( fcn->BCFcn )
53                                 ret = Bytecode_ExecuteFunction(Script, fcn, NArguments, Arguments);
54                         else
55                         #endif
56                                 ret = AST_ExecuteFunction(Script, fcn, NArguments, Arguments);
57                         bFound = 1;
58                 }
59         }
60         
61         // Didn't find it in script?
62         if(!bFound)
63         {
64                 tSpiderFunction *fcn;
65                 fcn = NULL;     // Just to allow the below code to be neat
66                 
67                 // Second: Scan current namespace
68                 if( !fcn && Namespace )
69                 {
70                         for( fcn = Namespace->Functions; fcn; fcn = fcn->Next )
71                         {
72                                 if( strcmp( fcn->Name, Function ) == 0 )
73                                         break;
74                         }
75                 }
76                 
77                 // Third: Search the variant's global exports
78                 if( !fcn )
79                 {
80                         for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
81                         {
82                                 if( strcmp( fcn->Name, Function ) == 0 )
83                                         break;
84                         }
85                 }
86                 
87                 // Fourth: Search language exports
88                 if( !fcn )
89                 {
90                         for( fcn = gpExports_First; fcn; fcn = fcn->Next )
91                         {
92                                 if( strcmp( fcn->Name, Function ) == 0 )
93                                         break;
94                         }
95                 }
96                 
97                 // Execute!
98                 if(fcn)
99                 {
100                         // TODO: Type Checking
101                         ret = fcn->Handler( Script, NArguments, Arguments );
102                         bFound = 1;
103                 }
104         }
105         
106         // Not found?
107         if(!bFound)
108         {
109                 fprintf(stderr, "Undefined reference to function '%s' (ns='%s')\n",
110                         Function, Namespace->Name);
111                 return ERRPTR;
112         }
113         
114         return ret;
115 }
116
117 /**
118  * \brief Execute an object method function
119  * \param Script        Script context to execute in
120  * \param Object        Object in which to find the method
121  * \param MethodName    Name of method to call
122  * \param NArguments    Number of arguments to pass
123  * \param Arguments     Arguments passed
124  */
125 tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
126         tSpiderObject *Object, const char *MethodName,
127         int NArguments, tSpiderValue **Arguments)
128 {
129         tSpiderFunction *fcn;
130         tSpiderValue    this;
131         tSpiderValue    *newargs[NArguments+1];
132          int    i;
133         
134         // TODO: Support program defined objects
135         
136         // Search for the function
137         for( fcn = Object->Type->Methods; fcn; fcn = fcn->Next )
138         {
139                 if( strcmp(fcn->Name, MethodName) == 0 )
140                         break;
141         }
142         // Error
143         if( !fcn )
144         {
145                 AST_RuntimeError(NULL, "Class '%s' does not have a method '%s'",
146                         Object->Type->Name, MethodName);
147                 return ERRPTR;
148         }
149         
150         // Create the "this" argument
151         this.Type = SS_DATATYPE_OBJECT;
152         this.ReferenceCount = 1;
153         this.Object = Object;
154         newargs[0] = &this;
155         memcpy(&newargs[1], Arguments, NArguments*sizeof(tSpiderValue*));
156         
157         // Check the type of the arguments
158         for( i = 0; fcn->ArgTypes[i]; i ++ )
159         {
160                 if( i >= NArguments ) {
161                         for( ; fcn->ArgTypes[i]; i ++ ) ;
162                         AST_RuntimeError(NULL, "Argument count mismatch (%i passed, %i expected)",
163                                 NArguments, i);
164                         return ERRPTR;
165                 }
166                 if( Arguments[i] && Arguments[i]->Type != fcn->ArgTypes[i] )
167                 {
168                         AST_RuntimeError(NULL, "Argument type mismatch (%i, expected %i)",
169                                 Arguments[i]->Type, fcn->ArgTypes[i]);
170                         return ERRPTR;
171                 }
172         }
173         
174         // Call handler
175         return fcn->Handler(Script, NArguments+1, newargs);
176 }
177
178 /**
179  * \brief Execute a script function
180  * \param Script        Script context to execute in
181  * \param Function      Function name to execute
182  * \param NArguments    Number of arguments to pass
183  * \param Arguments     Arguments passed
184  */
185 tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
186         tSpiderNamespace *Namespace, const char *ClassName,
187         int NArguments, tSpiderValue **Arguments)
188 {
189          int    bFound = 0;     // Used to keep nesting levels down
190         tSpiderValue    *ret = ERRPTR;
191         tSpiderObjectDef        *class;
192         
193         // First: Find the function in the script
194         // TODO: Implement script-defined classes
195         #if 0
196         {
197                 tAST_Function   *astClass;
198                 for( astClass = Script->Script->Classes; astClass; astClass = astClass->Next )
199                 {
200                         if( strcmp(astClass->Name, ClassName) == 0 )
201                                 break;
202                 }
203                 // Execute!
204                 if(astClass)
205                 {
206                         tAST_BlockState bs;
207                         tAST_Node       *arg;
208                          int    i = 0;
209                         
210                         // Build a block State
211                         bs.FirstVar = NULL;
212                         bs.RetVal = NULL;
213                         bs.Parent = NULL;
214                         bs.BaseNamespace = &Script->Variant->RootNamespace;
215                         bs.CurNamespace = NULL;
216                         bs.Script = Script;
217                         bs.Ident = giNextBlockIdent ++;
218                         
219                         for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
220                         {
221                                 if( i >= NArguments )   break;  // TODO: Return gracefully
222                                 // TODO: Type checks
223                                 Variable_Define(&bs,
224                                         arg->DefVar.DataType, arg->DefVar.Name,
225                                         Arguments[i]);
226                         }
227                         
228                         // Execute function
229                         ret = AST_ExecuteNode(&bs, astFcn->Code);
230                         if( ret != ERRPTR )
231                         {
232                                 SpiderScript_DereferenceValue(ret);     // Dereference output of last block statement
233                                 ret = bs.RetVal;        // Set to return value of block
234                         }
235                         bFound = 1;
236                         
237                         while(bs.FirstVar)
238                         {
239                                 tAST_Variable   *nextVar = bs.FirstVar->Next;
240                                 Variable_Destroy( bs.FirstVar );
241                                 bs.FirstVar = nextVar;
242                         }
243                 }
244         }
245         #endif
246         
247         // Didn't find it in script?
248         if(!bFound)
249         {
250                 class = NULL;   // Just to allow the below code to be neat
251                 
252                 //if( !Namespace )
253                 //      Namespace = &Script->Variant->RootNamespace;
254                 
255                 // Second: Scan current namespace
256                 if( !class && Namespace )
257                 {
258                         for( class = Namespace->Classes; class; class = class->Next )
259                         {
260                                 if( strcmp( class->Name, ClassName ) == 0 )
261                                         break;
262                         }
263                 }
264                 
265                 #if 0
266                 // Third: Search the variant's global exports
267                 if( !class )
268                 {
269                         for( class = Script->Variant->Classes; class; class = fcn->Next )
270                         {
271                                 if( strcmp( class->Name, Function ) == 0 )
272                                         break;
273                         }
274                 }
275                 #endif
276                 
277                 #if 0
278                 // Fourth: Search language exports
279                 if( !class )
280                 {
281                         for( class = gpExports_First; class; class = fcn->Next )
282                         {
283                                 if( strcmp( class->Name, ClassName ) == 0 )
284                                         break;
285                         }
286                 }
287                 #endif
288                 
289                 // Execute!
290                 if(class)
291                 {
292                         tSpiderObject   *obj;
293                         // TODO: Type Checking
294                         
295                         // Call constructor
296                         obj = class->Constructor( NArguments, Arguments );
297                         if( obj == NULL || obj == ERRPTR )
298                                 return (void *)obj;
299                         
300                         // Creatue return object
301                         ret = malloc( sizeof(tSpiderValue) );
302                         ret->Type = SS_DATATYPE_OBJECT;
303                         ret->ReferenceCount = 1;
304                         ret->Object = obj;
305                         bFound = 1;
306                 }
307         }
308         
309         // Not found?
310         if(!bFound)
311         {
312                 fprintf(stderr, "Undefined reference to class '%s'\n", ClassName);
313                 return ERRPTR;
314         }
315         
316         return ret;
317 }
318
319 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...)
320 {
321         va_list args;
322         
323         if(Node) {
324                 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
325         }
326         fprintf(stderr, "%s: ", Type);
327         va_start(args, Format);
328         vfprintf(stderr, Format, args);
329         va_end(args);
330         fprintf(stderr, "\n");
331 }
332 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...)
333 {
334         va_list args;
335         
336         if(Node) {
337                 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
338         }
339         fprintf(stderr, "error: ");
340         va_start(args, Format);
341         vfprintf(stderr, Format, args);
342         va_end(args);
343         fprintf(stderr, "\n");
344 }

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