106ce232039eb55f95cb6df4048ee7192c580d95
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / main.c
1 /*
2  * Acess2 - SpiderScript
3  * Interpreter Library
4  */
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <spiderscript.h>
9 #include "ast.h"
10
11 // === IMPORTS ===
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);
16
17 // === CODE ===
18 /**
19  * \brief Library Entry Point
20  */
21 int SoMain()
22 {
23         return 0;
24 }
25
26 /**
27  * \brief Parse a script
28  */
29 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
30 {
31         char    *data;
32          int    fLen;
33         FILE    *fp;
34         tSpiderScript   *ret;
35         
36         fp = fopen(Filename, "r");
37         if( !fp ) {
38                 return NULL;
39         }
40         
41         // Create the script
42         ret = malloc(sizeof(tSpiderScript));
43         ret->Variant = Variant;
44         
45         fseek(fp, 0, SEEK_END);
46         fLen = ftell(fp);
47         fseek(fp, 0, SEEK_SET);
48         
49         // Allocate and read data
50         data = malloc(fLen + 1);
51         if(!data)       return NULL;
52         fread(data, fLen, 1, fp);
53         data[fLen] = '\0';
54         
55         fclose(fp);
56         
57         ret->CurNamespace = NULL;
58         ret->Script = Parse_Buffer(Variant, data);
59         if( ret->Script == NULL ) {
60                 free(data);
61                 free(ret);
62                 return NULL;
63         }
64         
65         free(data);
66         
67         return ret;
68 }
69
70 /**
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
76  */
77 tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
78         const char *Function, int NArguments, tSpiderValue **Arguments)
79 {
80         char    *trueName = NULL;
81          int    bFound = 0;     // Used to keep nesting levels down
82         tSpiderValue    *ret = ERRPTR;
83         
84         // Handle namespaces
85         if( Function[0] == '.' ) {
86                 trueName = (char*)&Function[1];
87         }
88         else if( !Script->CurNamespace ) {
89                 trueName = (char*)Function;
90         }
91         else {
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);
97         }
98         
99         // First: Find the function in the script
100         {
101                 tAST_Function   *fcn = Script->Script->Functions;
102                 for( ; fcn; fcn = fcn->Next ) {
103                         if( strcmp(fcn->Name, trueName) == 0 )
104                                 break;
105                 }
106                 // Execute!
107                 if(fcn) {
108                         tAST_BlockState bs;
109                         bs.FirstVar = NULL;
110                         bs.RetVal = NULL;
111                         bs.Parent = NULL;
112                         bs.Script = Script;
113                         {
114                                 tAST_Node       *arg;
115                                  int    i = 0;
116                                 for( arg = fcn->Arguments; arg; arg = arg->NextSibling, i++ )
117                                 {
118                                         // TODO: Type checks
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]);
122                                 }
123                         }
124                         ret = AST_ExecuteNode(&bs, fcn->Code);
125                         Object_Dereference(ret);
126                         ret = bs.RetVal;
127                         bFound = 1;
128                 }
129         }
130         
131         // Didn't find it in script?
132         if(!bFound)
133         {
134                 tSpiderFunction *fcn;
135                 // Second: Search the variant's exports
136                 for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
137                 {
138                         if( strcmp( fcn->Name, trueName ) == 0 )
139                                 break;
140                 }
141                 // Execute!
142                 if(fcn) {
143                         // TODO: Type Checking
144                         ret = fcn->Handler( Script, NArguments, Arguments );
145                         bFound = 1;
146                 }
147         }
148         
149         // Not in variant exports? Search the language internal ones
150         if(!bFound)
151         {
152                 tSpiderFunction *fcn;
153                 // Third: Search language exports
154                 for( fcn = gpExports_First; fcn; fcn = fcn->Next )
155                 {
156                         if( strcmp( fcn->Name, trueName ) == 0 )
157                                 break;
158                 }
159                 // Execute!
160                 if(fcn) {
161                         ret = fcn->Handler( Script, NArguments, Arguments );
162                         bFound = 1;
163                 }
164         }
165         
166         // Not found?
167         if(!bFound)
168         {
169                 fprintf(stderr, "Undefined reference to '%s'\n", trueName);
170         }
171         
172         if( trueName != Function && trueName != &Function[1] )
173                 free(trueName);
174         
175         return ret;
176         
177 }
178
179 /**
180  * \brief Free a script
181  */
182 void SpiderScript_Free(tSpiderScript *Script)
183 {
184         tAST_Function   *fcn = Script->Script->Functions;
185         tAST_Function   *nextFcn;
186         tAST_Node       *var, *nextVar;
187         
188         // Free functions
189         while(fcn) {
190                 AST_FreeNode( fcn->Code );
191                 
192                 var = fcn->Arguments;
193                 while(var)
194                 {
195                         nextVar = var->NextSibling;
196                         AST_FreeNode( var );
197                         var = nextVar;
198                 }
199                 
200                 nextFcn = fcn->Next;
201                 free( fcn );
202                 fcn = nextFcn;
203         }
204         
205         // TODO: Pass this off to AST for a proper cleanup
206         free(Script->Script);
207         
208         free(Script);
209 }

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