3 * by John Hodge (thePowersGang)
6 * - Generate a bytecode file
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);
21 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...);
22 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
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
33 tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
34 tSpiderNamespace *Namespace, const char *Function,
35 int NArguments, tSpiderValue **Arguments)
37 int bFound = 0; // Used to keep nesting levels down
38 tSpiderValue *ret = ERRPTR;
40 // First: Find the function in the script
42 tScript_Function *fcn;
43 for( fcn = Script->Functions; fcn; fcn = fcn->Next )
45 if( strcmp(fcn->Name, Function) == 0 )
52 ret = Bytecode_ExecuteFunction(Script, fcn, NArguments, Arguments);
54 ret = AST_ExecuteFunction(Script, fcn, NArguments, Arguments);
59 // Didn't find it in script?
63 fcn = NULL; // Just to allow the below code to be neat
65 // Second: Scan current namespace
66 if( !fcn && Namespace )
68 for( fcn = Namespace->Functions; fcn; fcn = fcn->Next )
70 if( strcmp( fcn->Name, Function ) == 0 )
75 // Third: Search the variant's global exports
78 for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
80 if( strcmp( fcn->Name, Function ) == 0 )
85 // Fourth: Search language exports
88 for( fcn = gpExports_First; fcn; fcn = fcn->Next )
90 if( strcmp( fcn->Name, Function ) == 0 )
98 // TODO: Type Checking
99 ret = fcn->Handler( Script, NArguments, Arguments );
107 fprintf(stderr, "Undefined reference to function '%s' (ns='%s')\n",
108 Function, Namespace->Name);
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
123 tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
124 tSpiderObject *Object, const char *MethodName,
125 int NArguments, tSpiderValue **Arguments)
127 tSpiderFunction *fcn;
129 tSpiderValue *newargs[NArguments+1];
132 // TODO: Support program defined objects
134 // Search for the function
135 for( fcn = Object->Type->Methods; fcn; fcn = fcn->Next )
137 if( strcmp(fcn->Name, MethodName) == 0 )
143 AST_RuntimeError(NULL, "Class '%s' does not have a method '%s'",
144 Object->Type->Name, MethodName);
148 // Create the "this" argument
149 this.Type = SS_DATATYPE_OBJECT;
150 this.ReferenceCount = 1;
151 this.Object = Object;
153 memcpy(&newargs[1], Arguments, NArguments*sizeof(tSpiderValue*));
155 // Check the type of the arguments
156 for( i = 0; fcn->ArgTypes[i]; i ++ )
158 if( i >= NArguments ) {
159 for( ; fcn->ArgTypes[i]; i ++ ) ;
160 AST_RuntimeError(NULL, "Argument count mismatch (%i passed, %i expected)",
164 if( Arguments[i] && Arguments[i]->Type != fcn->ArgTypes[i] )
166 AST_RuntimeError(NULL, "Argument type mismatch (%i, expected %i)",
167 Arguments[i]->Type, fcn->ArgTypes[i]);
173 return fcn->Handler(Script, NArguments+1, newargs);
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
183 tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
184 tSpiderNamespace *Namespace, const char *ClassName,
185 int NArguments, tSpiderValue **Arguments)
187 int bFound = 0; // Used to keep nesting levels down
188 tSpiderValue *ret = ERRPTR;
189 tSpiderObjectDef *class;
191 // First: Find the function in the script
192 // TODO: Implement script-defined classes
195 tAST_Function *astClass;
196 for( astClass = Script->Script->Classes; astClass; astClass = astClass->Next )
198 if( strcmp(astClass->Name, ClassName) == 0 )
208 // Build a block State
212 bs.BaseNamespace = &Script->Variant->RootNamespace;
213 bs.CurNamespace = NULL;
215 bs.Ident = giNextBlockIdent ++;
217 for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
219 if( i >= NArguments ) break; // TODO: Return gracefully
222 arg->DefVar.DataType, arg->DefVar.Name,
227 ret = AST_ExecuteNode(&bs, astFcn->Code);
230 SpiderScript_DereferenceValue(ret); // Dereference output of last block statement
231 ret = bs.RetVal; // Set to return value of block
237 tAST_Variable *nextVar = bs.FirstVar->Next;
238 Variable_Destroy( bs.FirstVar );
239 bs.FirstVar = nextVar;
245 // Didn't find it in script?
248 class = NULL; // Just to allow the below code to be neat
251 // Namespace = &Script->Variant->RootNamespace;
253 // Second: Scan current namespace
254 if( !class && Namespace )
256 for( class = Namespace->Classes; class; class = class->Next )
258 if( strcmp( class->Name, ClassName ) == 0 )
264 // Third: Search the variant's global exports
267 for( class = Script->Variant->Classes; class; class = fcn->Next )
269 if( strcmp( class->Name, Function ) == 0 )
276 // Fourth: Search language exports
279 for( class = gpExports_First; class; class = fcn->Next )
281 if( strcmp( class->Name, ClassName ) == 0 )
291 // TODO: Type Checking
294 obj = class->Constructor( NArguments, Arguments );
295 if( obj == NULL || obj == ERRPTR )
298 // Creatue return object
299 ret = malloc( sizeof(tSpiderValue) );
300 ret->Type = SS_DATATYPE_OBJECT;
301 ret->ReferenceCount = 1;
310 fprintf(stderr, "Undefined reference to class '%s'\n", ClassName);
317 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...)
322 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
324 fprintf(stderr, "%s: ", Type);
325 va_start(args, Format);
326 vfprintf(stderr, Format, args);
328 fprintf(stderr, "\n");
330 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...)
335 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
337 fprintf(stderr, "error: ");
338 va_start(args, Format);
339 vfprintf(stderr, Format, args);
341 fprintf(stderr, "\n");