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
43 tScript_Function *fcn;
44 for( fcn = Script->Functions; fcn; fcn = fcn->Next )
46 if( strcmp(fcn->Name, Function) == 0 )
53 ret = Bytecode_ExecuteFunction(Script, fcn, NArguments, Arguments);
55 ret = AST_ExecuteFunction(Script, fcn, NArguments, Arguments);
61 // Didn't find it in script?
65 fcn = NULL; // Just to allow the below code to be neat
67 // Second: Scan current namespace
68 if( !fcn && Namespace )
70 for( fcn = Namespace->Functions; fcn; fcn = fcn->Next )
72 if( strcmp( fcn->Name, Function ) == 0 )
77 // Third: Search the variant's global exports
80 for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
82 if( strcmp( fcn->Name, Function ) == 0 )
87 // Fourth: Search language exports
90 for( fcn = gpExports_First; fcn; fcn = fcn->Next )
92 if( strcmp( fcn->Name, Function ) == 0 )
100 // TODO: Type Checking
101 ret = fcn->Handler( Script, NArguments, Arguments );
109 fprintf(stderr, "Undefined reference to function '%s' (ns='%s')\n",
110 Function, Namespace->Name);
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
125 tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
126 tSpiderObject *Object, const char *MethodName,
127 int NArguments, tSpiderValue **Arguments)
129 tSpiderFunction *fcn;
131 tSpiderValue *newargs[NArguments+1];
134 // TODO: Support program defined objects
136 // Search for the function
137 for( fcn = Object->Type->Methods; fcn; fcn = fcn->Next )
139 if( strcmp(fcn->Name, MethodName) == 0 )
145 AST_RuntimeError(NULL, "Class '%s' does not have a method '%s'",
146 Object->Type->Name, MethodName);
150 // Create the "this" argument
151 this.Type = SS_DATATYPE_OBJECT;
152 this.ReferenceCount = 1;
153 this.Object = Object;
155 memcpy(&newargs[1], Arguments, NArguments*sizeof(tSpiderValue*));
157 // Check the type of the arguments
158 for( i = 0; fcn->ArgTypes[i]; i ++ )
160 if( i >= NArguments ) {
161 for( ; fcn->ArgTypes[i]; i ++ ) ;
162 AST_RuntimeError(NULL, "Argument count mismatch (%i passed, %i expected)",
166 if( Arguments[i] && Arguments[i]->Type != fcn->ArgTypes[i] )
168 AST_RuntimeError(NULL, "Argument type mismatch (%i, expected %i)",
169 Arguments[i]->Type, fcn->ArgTypes[i]);
175 return fcn->Handler(Script, NArguments+1, newargs);
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
185 tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
186 tSpiderNamespace *Namespace, const char *ClassName,
187 int NArguments, tSpiderValue **Arguments)
189 int bFound = 0; // Used to keep nesting levels down
190 tSpiderValue *ret = ERRPTR;
191 tSpiderObjectDef *class;
193 // First: Find the function in the script
194 // TODO: Implement script-defined classes
197 tAST_Function *astClass;
198 for( astClass = Script->Script->Classes; astClass; astClass = astClass->Next )
200 if( strcmp(astClass->Name, ClassName) == 0 )
210 // Build a block State
214 bs.BaseNamespace = &Script->Variant->RootNamespace;
215 bs.CurNamespace = NULL;
217 bs.Ident = giNextBlockIdent ++;
219 for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
221 if( i >= NArguments ) break; // TODO: Return gracefully
224 arg->DefVar.DataType, arg->DefVar.Name,
229 ret = AST_ExecuteNode(&bs, astFcn->Code);
232 SpiderScript_DereferenceValue(ret); // Dereference output of last block statement
233 ret = bs.RetVal; // Set to return value of block
239 tAST_Variable *nextVar = bs.FirstVar->Next;
240 Variable_Destroy( bs.FirstVar );
241 bs.FirstVar = nextVar;
247 // Didn't find it in script?
250 class = NULL; // Just to allow the below code to be neat
253 // Namespace = &Script->Variant->RootNamespace;
255 // Second: Scan current namespace
256 if( !class && Namespace )
258 for( class = Namespace->Classes; class; class = class->Next )
260 if( strcmp( class->Name, ClassName ) == 0 )
266 // Third: Search the variant's global exports
269 for( class = Script->Variant->Classes; class; class = fcn->Next )
271 if( strcmp( class->Name, Function ) == 0 )
278 // Fourth: Search language exports
281 for( class = gpExports_First; class; class = fcn->Next )
283 if( strcmp( class->Name, ClassName ) == 0 )
293 // TODO: Type Checking
296 obj = class->Constructor( NArguments, Arguments );
297 if( obj == NULL || obj == ERRPTR )
300 // Creatue return object
301 ret = malloc( sizeof(tSpiderValue) );
302 ret->Type = SS_DATATYPE_OBJECT;
303 ret->ReferenceCount = 1;
312 fprintf(stderr, "Undefined reference to class '%s'\n", ClassName);
319 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...)
324 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
326 fprintf(stderr, "%s: ", Type);
327 va_start(args, Format);
328 vfprintf(stderr, Format, args);
330 fprintf(stderr, "\n");
332 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...)
337 fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
339 fprintf(stderr, "error: ");
340 va_start(args, Format);
341 vfprintf(stderr, Format, args);
343 fprintf(stderr, "\n");