f26a40cd100f225e0b9877d2fb5118438053ca28
[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 #include "bytecode_gen.h"
11
12 // === IMPORTS ===
13 extern tAST_Script      *Parse_Buffer(tSpiderVariant *Variant, const char *Buffer, const char *Filename);
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 extern void     Variable_Destroy(tAST_Variable *Variable);
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    cacheFilename[strlen(Filename)+6+1];
33         char    *data;
34          int    fLen;
35         FILE    *fp;
36         tSpiderScript   *ret;
37         
38         strcpy(cacheFilename, Filename);
39         strcat(cacheFilename, ".cache");
40         
41         fp = fopen(Filename, "r");
42         if( !fp ) {
43                 return NULL;
44         }
45         
46         fseek(fp, 0, SEEK_END);
47         fLen = ftell(fp);
48         fseek(fp, 0, SEEK_SET);
49         
50         // Allocate and read data
51         data = malloc(fLen + 1);
52         if(!data)       return NULL;
53         fLen = fread(data, 1, fLen, fp);
54         fclose(fp);
55         if( fLen < 0 ) {
56                 free(data);
57                 return NULL;
58         }
59         data[fLen] = '\0';
60         
61         
62         // Create the script
63         ret = malloc(sizeof(tSpiderScript));
64         ret->Variant = Variant;
65         
66         ret->CurNamespace = NULL;
67         ret->Script = Parse_Buffer(Variant, data, Filename);
68         if( ret->Script == NULL ) {
69                 free(data);
70                 free(ret);
71                 return NULL;
72         }
73         
74         free(data);
75         
76         
77         // HACK!!
78         {
79                 size_t  size;
80                 
81                 printf("Total Size: "); fflush(stdout);
82                 size = AST_WriteScript(NULL, ret->Script);
83                 printf("0x%x bytes\n", (unsigned)size);
84                 
85                 fp = fopen(cacheFilename, "wb");
86                 if(!fp) return ret;
87                 
88                 data = malloc(size);
89                 size = AST_WriteScript(data, ret->Script);
90                 fwrite(data, size, 1, fp);
91                 free(data);
92                 fclose(fp);
93         }
94         
95         return ret;
96 }
97
98 int SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile)
99 {
100         return Bytecode_ConvertScript(Script, DestFile);
101 }
102
103 /**
104  * \brief Free a script
105  */
106 void SpiderScript_Free(tSpiderScript *Script)
107 {
108         tAST_Function   *fcn = Script->Script->Functions;
109         tAST_Function   *nextFcn;
110         tAST_Node       *var, *nextVar;
111         
112         // Free functions
113         while(fcn)
114         {
115                 
116                 AST_FreeNode( fcn->Code );
117                 
118                 var = fcn->Arguments;
119                 while(var)
120                 {
121                         nextVar = var->NextSibling;
122                         AST_FreeNode( var );
123                         var = nextVar;
124                 }
125                 
126                 nextFcn = fcn->Next;
127                 free( fcn );
128                 fcn = nextFcn;
129         }
130         
131         // TODO: Pass this off to AST for a proper cleanup
132         free(Script->Script);
133         
134         free(Script);
135 }

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