2 * Acess2 - SpiderScript
8 #include <spiderscript.h>
12 extern tAST_Script *Parse_Buffer(tSpiderVariant *Variant, char *Buffer);
13 extern const int giSpiderScript_NumExports;
14 extern tSpiderFunction gaSpiderScript_Exports[];
15 extern tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name);
16 extern void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value);
20 * \brief Library Entry Point
28 * \brief Parse a script
30 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
37 fp = fopen(Filename, "r");
42 ret = malloc(sizeof(tSpiderScript));
43 ret->Variant = Variant;
45 fseek(fp, 0, SEEK_END);
47 fseek(fp, 0, SEEK_SET);
49 // Allocate and read data
50 data = malloc(fLen + 1);
51 if(!data) return NULL;
52 fread(data, fLen, 1, fp);
57 ret->CurNamespace = NULL;
58 ret->Script = Parse_Buffer(Variant, data);
59 if( ret->Script == NULL ) {
71 * \brief Execute a script function
72 * \param Script Script context to execute in
73 * \param Function Function name to execute
74 * \param NArguments Number of arguments to pass
75 * \param Arguments Arguments passed
77 tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
78 const char *Function, int NArguments, tSpiderValue **Arguments)
80 char *trueName = NULL;
82 int bFound = 0; // Used to keep nesting levels down
83 tSpiderValue *ret = ERRPTR;
86 if( Function[0] == '.' ) {
87 trueName = (char*)&Function[1];
89 else if( !Script->CurNamespace ) {
90 trueName = (char*)Function;
93 int len = strlen(Script->CurNamespace) + 1 + strlen(Function);
94 trueName = malloc( len + 1 );
95 strcpy(trueName, Script->CurNamespace);
96 strcat(trueName, ".");
97 strcat(trueName, Function);
100 // First: Find the function in the script
102 tAST_Function *fcn = Script->Script->Functions;
103 for( ; fcn; fcn = fcn->Next ) {
104 if( strcmp(fcn->Name, trueName) == 0 )
117 for( arg = fcn->Arguments; arg; arg = arg->NextSibling, i++ )
120 Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name);
121 if( i >= NArguments ) break; // TODO: Return gracefully
122 Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]);
125 ret = AST_ExecuteNode(&bs, fcn->Code);
126 //Object_Dereference(ret);
132 // Didn't find it in script?
135 // Second: Search the variant's exports
136 for( i = 0; i < Script->Variant->NFunctions; i ++ )
138 if( strcmp( Script->Variant->Functions[i].Name, trueName) == 0 )
142 if(i < Script->Variant->NFunctions) {
143 ret = Script->Variant->Functions[i].Handler( Script, NArguments, Arguments );
148 // Not in variant exports? Search the language internal ones
151 for( i = 0; i < giSpiderScript_NumExports; i ++ )
153 if( strcmp( gaSpiderScript_Exports[i].Name, trueName ) == 0 )
157 if(i < giSpiderScript_NumExports) {
158 ret = gaSpiderScript_Exports[i].Handler( Script, NArguments, Arguments );
166 fprintf(stderr, "Undefined reference to '%s'\n", trueName);
169 if( trueName != Function && trueName != &Function[1] )
177 * \brief Free a script
179 void SpiderScript_Free(tSpiderScript *Script)
181 tAST_Function *fcn = Script->Script->Functions;
182 tAST_Function *nextFcn;
183 tAST_Node *var, *nextVar;
187 AST_FreeNode( fcn->Code );
189 var = fcn->Arguments;
192 nextVar = var->NextSibling;
202 // TODO: Pass this off to AST for a proper cleanup
203 free(Script->Script);