SpiderScript - Implementing objects and classes, fixing bugs
[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         // Create the script
46         ret = malloc(sizeof(tSpiderScript));
47         ret->Variant = Variant;
48         
49         fseek(fp, 0, SEEK_END);
50         fLen = ftell(fp);
51         fseek(fp, 0, SEEK_SET);
52         
53         // Allocate and read data
54         data = malloc(fLen + 1);
55         if(!data)       return NULL;
56         fread(data, fLen, 1, fp);
57         data[fLen] = '\0';
58         
59         fclose(fp);
60         
61         ret->CurNamespace = NULL;
62         ret->Script = Parse_Buffer(Variant, data);
63         if( ret->Script == NULL ) {
64                 free(data);
65                 free(ret);
66                 return NULL;
67         }
68         
69         free(data);
70         
71         
72         // HACK!!
73         {
74                 size_t  size;
75                 
76                 printf("Total Size: "); fflush(stdout);
77                 size = AST_WriteScript(NULL, ret->Script);
78                 printf("0x%x bytes\n", (unsigned)size);
79                 
80                 fp = fopen(cacheFilename, "wb");
81                 if(!fp) return ret;
82                 
83                 data = malloc(size);
84                 size = AST_WriteScript(data, ret->Script);
85                 fwrite(data, size, 1, fp);
86                 free(data);
87                 fclose(fp);
88         }
89         
90         return ret;
91 }
92
93 /**
94  * \brief Free a script
95  */
96 void SpiderScript_Free(tSpiderScript *Script)
97 {
98         tAST_Function   *fcn = Script->Script->Functions;
99         tAST_Function   *nextFcn;
100         tAST_Node       *var, *nextVar;
101         
102         // Free functions
103         while(fcn)
104         {
105                 
106                 AST_FreeNode( fcn->Code );
107                 
108                 var = fcn->Arguments;
109                 while(var)
110                 {
111                         nextVar = var->NextSibling;
112                         AST_FreeNode( var );
113                         var = nextVar;
114                 }
115                 
116                 nextFcn = fcn->Next;
117                 free( fcn );
118                 fcn = nextFcn;
119         }
120         
121         // TODO: Pass this off to AST for a proper cleanup
122         free(Script->Script);
123         
124         free(Script);
125 }

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