SpiderScript - Bugfixes and speed improvements
[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 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name);
14 extern void     Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value);
15 extern void     Variable_Destroy(tAST_Variable *Variable);
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    cacheFilename[strlen(Filename)+6+1];
32         char    *data;
33          int    fLen;
34         FILE    *fp;
35         tSpiderScript   *ret;
36         
37         strcpy(cacheFilename, Filename);
38         strcat(cacheFilename, ".cache");
39         
40         fp = fopen(Filename, "r");
41         if( !fp ) {
42                 return NULL;
43         }
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         fLen = fread(data, 1, fLen, fp);
53         if( fLen < 0 ) {
54                 free(data);
55                 return NULL;
56         }
57         data[fLen] = '\0';
58         
59         fclose(fp);
60         
61         // Create the script
62         ret = malloc(sizeof(tSpiderScript));
63         ret->Variant = Variant;
64         
65         ret->CurNamespace = NULL;
66         ret->Script = Parse_Buffer(Variant, data);
67         if( ret->Script == NULL ) {
68                 free(data);
69                 free(ret);
70                 return NULL;
71         }
72         
73         free(data);
74         
75         
76         // HACK!!
77         {
78                 size_t  size;
79                 
80                 printf("Total Size: "); fflush(stdout);
81                 size = AST_WriteScript(NULL, ret->Script);
82                 printf("0x%x bytes\n", (unsigned)size);
83                 
84                 fp = fopen(cacheFilename, "wb");
85                 if(!fp) return ret;
86                 
87                 data = malloc(size);
88                 size = AST_WriteScript(data, ret->Script);
89                 fwrite(data, size, 1, fp);
90                 free(data);
91                 fclose(fp);
92         }
93         
94         return ret;
95 }
96
97 /**
98  * \brief Free a script
99  */
100 void SpiderScript_Free(tSpiderScript *Script)
101 {
102         tAST_Function   *fcn = Script->Script->Functions;
103         tAST_Function   *nextFcn;
104         tAST_Node       *var, *nextVar;
105         
106         // Free functions
107         while(fcn)
108         {
109                 
110                 AST_FreeNode( fcn->Code );
111                 
112                 var = fcn->Arguments;
113                 while(var)
114                 {
115                         nextVar = var->NextSibling;
116                         AST_FreeNode( var );
117                         var = nextVar;
118                 }
119                 
120                 nextFcn = fcn->Next;
121                 free( fcn );
122                 fcn = nextFcn;
123         }
124         
125         // TODO: Pass this off to AST for a proper cleanup
126         free(Script->Script);
127         
128         free(Script);
129 }

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