SpiderScript - Speedup fixes to bytecode
[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 "common.h"
10 #include "ast.h"
11 #include "bytecode_gen.h"
12
13 // === IMPORTS ===
14 extern  int     Parse_Buffer(tSpiderScript *Script, const char *Buffer, const char *Filename);
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, tSpiderValue *Value);
17 extern void     Variable_Destroy(tAST_Variable *Variable);
18
19 // === CODE ===
20 /**
21  * \brief Library Entry Point
22  */
23 int SoMain()
24 {
25         return 0;
26 }
27
28 /**
29  * \brief Parse a script
30  */
31 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
32 {
33         char    *data;
34          int    fLen;
35         FILE    *fp;
36         tSpiderScript   *ret;
37         
38         fp = fopen(Filename, "r");
39         if( !fp ) {
40                 return NULL;
41         }
42         
43         fseek(fp, 0, SEEK_END);
44         fLen = ftell(fp);
45         fseek(fp, 0, SEEK_SET);
46         
47         // Allocate and read data
48         data = malloc(fLen + 1);
49         if(!data)       return NULL;
50         fLen = fread(data, 1, fLen, fp);
51         fclose(fp);
52         if( fLen < 0 ) {
53                 free(data);
54                 return NULL;
55         }
56         data[fLen] = '\0';
57         
58         
59         // Create the script
60         ret = malloc(sizeof(tSpiderScript));
61         ret->Variant = Variant;
62         ret->Functions = NULL;
63         ret->LastFunction = NULL;
64         
65         ret->CurNamespace = NULL;
66         if( Parse_Buffer(ret, data, Filename) ) {
67                 free(data);
68                 free(ret);
69                 return NULL;
70         }
71         
72         free(data);
73         
74         
75         // HACK!!
76         #if 1
77         // - Save AST to a file
78         {
79                 char    cacheFilename[strlen(Filename)+6+1];
80                 strcpy(cacheFilename, Filename);
81                 strcat(cacheFilename, ".ast");
82         
83                 SpiderScript_SaveAST(ret, cacheFilename);       
84         }
85         #endif
86         // - Save Bytecode too
87         {
88                 char    cacheFilename[strlen(Filename)+6+1];
89                 strcpy(cacheFilename, Filename);
90                 strcat(cacheFilename, ".bc");
91         
92                 SpiderScript_SaveBytecode(ret, cacheFilename);  
93         }
94         
95         return ret;
96 }
97
98 int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename)
99 {
100         size_t  size;
101         FILE    *fp;
102         void    *data;
103         printf("Total Size: ");
104         fflush(stdout);
105         size = AST_WriteScript(NULL, Script);
106         printf("0x%x bytes\n", (unsigned)size);
107         
108         fp = fopen(Filename, "wb");
109         if(!fp) return 1;
110
111         data = malloc(size);
112         if(!data) {
113                 fclose(fp);
114                 return -1;
115         }
116         
117         size = AST_WriteScript(data, Script);
118         fwrite(data, size, 1, fp);
119         free(data);
120
121         fclose(fp);
122         return 0;
123 }
124
125 /**
126  * \brief Free a script
127  */
128 void SpiderScript_Free(tSpiderScript *Script)
129 {
130         tScript_Function        *fcn = Script->Functions;
131         tScript_Function        *nextFcn;
132         
133         // Free functions
134         while(fcn)
135         {
136                 if(fcn->ASTFcn) AST_FreeNode( fcn->ASTFcn );
137                 if(fcn->BCFcn)  Bytecode_DeleteFunction( fcn->BCFcn );
138
139                 nextFcn = fcn->Next;
140                 free( fcn );
141                 fcn = nextFcn;
142         }
143         
144         free(Script);
145 }

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