SpiderScript! (with a sample script)
[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 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, tSpiderObject *Value);
17
18 // === CODE ===
19 /**
20  * \brief Library Entry Point
21  */
22 int SoMain()
23 {
24         return 0;
25 }
26
27 /**
28  * \brief Parse a script
29  */
30 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
31 {
32         char    *data;
33          int    fLen;
34         FILE    *fp;
35         tSpiderScript   *ret;
36         
37         fp = fopen(Filename, "r");
38         if( !fp ) {
39                 return NULL;
40         }
41         
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 tSpiderObject *SpiderScript_ExecuteMethod(tSpiderScript *Script,
78         const char *Function, int NArguments, tSpiderObject **Arguments)
79 {
80         char    *trueName = NULL;
81          int    i;
82          int    bFound = 0;     // Used to keep nesting levels down
83         tSpiderObject   *ret = ERRPTR;
84         
85         // Handle namespaces
86         if( Function[0] == '.' ) {
87                 trueName = (char*)&Function[1];
88         }
89         else if( !Script->CurNamespace ) {
90                 trueName = (char*)Function;
91         }
92         else {
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);
98         }
99         
100         // First: Find the function in the script
101         {
102                 tAST_Function   *fcn = Script->Script->Functions;
103                 for( ; fcn; fcn = fcn->Next ) {
104                         if( strcmp(fcn->Name, trueName) == 0 )
105                                 break;
106                 }
107                 // Execute!
108                 if(fcn) {
109                         tAST_BlockState bs;
110                         bs.FirstVar = NULL;     //< TODO: Parameters
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                         bFound = 1;
126                 }
127         }
128                 
129         // Didn't find it in script?
130         if(!bFound)
131         {       
132                 // Second: Search the variant's exports
133                 for( i = 0; i < Script->Variant->NFunctions; i ++ )
134                 {
135                         if( strcmp( Script->Variant->Functions[i].Name, trueName) == 0 )
136                                 break;
137                 }
138                 // Execute!
139                 if(i < Script->Variant->NFunctions) {
140                         ret = Script->Variant->Functions[i].Handler( Script, NArguments, Arguments );
141                         bFound = 1;
142                 }
143         }
144         
145         // Not in variant exports? Search the language internal ones
146         if(!bFound)
147         {
148                 for( i = 0; i < giSpiderScript_NumExports; i ++ )
149                 {
150                         if( strcmp( gaSpiderScript_Exports[i].Name, trueName ) == 0 )
151                                 break;
152                 }
153                 // Execute!
154                 if(i < giSpiderScript_NumExports) {
155                         ret = gaSpiderScript_Exports[i].Handler( Script, NArguments, Arguments );
156                         bFound = 1;
157                 }
158         }
159         
160         // Not found?
161         if(!bFound)
162         {
163                 fprintf(stderr, "Undefined reference to '%s'\n", trueName);
164         }
165         
166         if( trueName != Function && trueName != &Function[1] )
167                 free(trueName);
168         
169         return ret;
170         
171 }
172
173 /**
174  * \brief Free a script
175  */
176 void SpiderScript_Free(tSpiderScript *Script)
177 {
178         tAST_Function   *fcn = Script->Script->Functions;
179         tAST_Function   *nextFcn;
180         tAST_Node       *var, *nextVar;
181         
182         // Free functions
183         while(fcn) {
184                 AST_FreeNode( fcn->Code );
185                 
186                 var = fcn->Arguments;
187                 while(var)
188                 {
189                         nextVar = var->NextSibling;
190                         AST_FreeNode( var );
191                         var = nextVar;
192                 }
193                 
194                 nextFcn = fcn->Next;
195                 free( fcn );
196                 fcn = nextFcn;
197         }
198         
199         // TODO: Pass this off to AST for a proper cleanup
200         free(Script->Script);
201         
202         free(Script);
203 }

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