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

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