ce39c0809d06aa1c20dc418493532a31059e1977
[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
14 // === CODE ===
15 /**
16  * \brief Library Entry Point
17  */
18 int SoMain()
19 {
20         return 0;
21 }
22
23 /**
24  * \brief Parse a script
25  */
26 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
27 {
28         char    *data;
29          int    fLen;
30         FILE    *fp;
31         tSpiderScript   *ret;
32         
33         fp = fopen(Filename, "r");
34         if( !fp ) {
35                 return NULL;
36         }
37         
38         ret = malloc(sizeof(tSpiderScript));
39         ret->Variant = Variant;
40         
41         fseek(fp, 0, SEEK_END);
42         fLen = ftell(fp);
43         fseek(fp, 0, SEEK_SET);
44         
45         data = malloc(fLen);
46         if(!data)       return NULL;
47         fread(data, fLen, 1, fp);
48         
49         fclose(fp);
50         
51         ret->Script = Parse_Buffer(Variant, data);
52         
53         free(data);
54         
55         return ret;
56 }
57
58 /**
59  * \brief Execute a script function
60  * \todo Arguments?
61  */
62 tSpiderVariable *SpiderScript_ExecuteMethod(tSpiderScript *Script, const char *Function)
63 {
64         tAST_Function   *fcn = Script->Script->Functions;
65         
66         // Find the function
67         for( ; fcn; fcn = fcn->Next ) {
68                 if( strcmp(fcn->Name, Function) == 0 )
69                         break;
70         }
71         if(!fcn)        return NULL;
72         
73         // Execute!
74         return AST_ExecuteNode(Script, fcn->Code);
75 }
76
77 /**
78  * \brief Free a script
79  */
80 void SpiderScript_Free(tSpiderScript *Script)
81 {
82         tAST_Function   *fcn = Script->Script->Functions;
83         tAST_Function   *nextFcn;
84         tAST_Node       *var, *nextVar;
85         
86         // Free functions
87         while(fcn) {
88                 AST_FreeNode( fcn->Code );
89                 
90                 var = fcn->Arguments;
91                 while(var)
92                 {
93                         nextVar = var->NextSibling;
94                         AST_FreeNode( var );
95                         var = nextVar;
96                 }
97                 
98                 nextFcn = fcn->Next;
99                 free( fcn );
100                 fcn = nextFcn;
101         }
102 }

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