2 * Acess2 - SpiderScript
8 #include <spiderscript.h>
12 extern tAST_Script *Parse_Buffer(tSpiderVariant *Variant, char *Buffer);
13 extern tSpiderFunction *gpExports_First;
14 extern tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name);
15 extern void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value);
19 * \brief Library Entry Point
27 * \brief Parse a script
29 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
36 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;
81 int bFound = 0; // Used to keep nesting levels down
82 tSpiderValue *ret = ERRPTR;
85 if( Function[0] == '.' ) {
86 trueName = (char*)&Function[1];
88 else if( !Script->CurNamespace ) {
89 trueName = (char*)Function;
92 int len = strlen(Script->CurNamespace) + 1 + strlen(Function);
93 trueName = malloc( len + 1 );
94 strcpy(trueName, Script->CurNamespace);
95 strcat(trueName, ".");
96 strcat(trueName, Function);
99 // First: Find the function in the script
101 tAST_Function *fcn = Script->Script->Functions;
102 for( ; fcn; fcn = fcn->Next ) {
103 if( strcmp(fcn->Name, trueName) == 0 )
116 for( arg = fcn->Arguments; arg; arg = arg->NextSibling, i++ )
119 Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name);
120 if( i >= NArguments ) break; // TODO: Return gracefully
121 Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]);
124 ret = AST_ExecuteNode(&bs, fcn->Code);
125 Object_Dereference(ret);
131 // Didn't find it in script?
134 tSpiderFunction *fcn;
135 // Second: Search the variant's exports
136 for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
138 if( strcmp( fcn->Name, trueName ) == 0 )
143 // TODO: Type Checking
144 ret = fcn->Handler( Script, NArguments, Arguments );
149 // Not in variant exports? Search the language internal ones
152 tSpiderFunction *fcn;
153 // Third: Search language exports
154 for( fcn = gpExports_First; fcn; fcn = fcn->Next )
156 if( strcmp( fcn->Name, trueName ) == 0 )
161 ret = fcn->Handler( Script, NArguments, Arguments );
169 fprintf(stderr, "Undefined reference to '%s'\n", trueName);
172 if( trueName != Function && trueName != &Function[1] )
180 * \brief Free a script
182 void SpiderScript_Free(tSpiderScript *Script)
184 tAST_Function *fcn = Script->Script->Functions;
185 tAST_Function *nextFcn;
186 tAST_Node *var, *nextVar;
190 AST_FreeNode( fcn->Code );
192 var = fcn->Arguments;
195 nextVar = var->NextSibling;
205 // TODO: Pass this off to AST for a proper cleanup
206 free(Script->Script);