SpiderScript - Restructured to be able to keep bytecode and AST in memory at one...
[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    cacheFilename[strlen(Filename)+6+1];
34         char    *data;
35          int    fLen;
36         FILE    *fp;
37         tSpiderScript   *ret;
38         
39         strcpy(cacheFilename, Filename);
40         strcat(cacheFilename, ".cache");
41         
42         fp = fopen(Filename, "r");
43         if( !fp ) {
44                 return NULL;
45         }
46         
47         fseek(fp, 0, SEEK_END);
48         fLen = ftell(fp);
49         fseek(fp, 0, SEEK_SET);
50         
51         // Allocate and read data
52         data = malloc(fLen + 1);
53         if(!data)       return NULL;
54         fLen = fread(data, 1, fLen, fp);
55         fclose(fp);
56         if( fLen < 0 ) {
57                 free(data);
58                 return NULL;
59         }
60         data[fLen] = '\0';
61         
62         
63         // Create the script
64         ret = malloc(sizeof(tSpiderScript));
65         ret->Variant = Variant;
66         ret->Functions = NULL;
67         ret->LastFunction = NULL;
68         
69         ret->CurNamespace = NULL;
70         if( Parse_Buffer(ret, data, Filename) ) {
71                 free(data);
72                 free(ret);
73                 return NULL;
74         }
75         
76         free(data);
77         
78         
79         // HACK!!
80         {
81                 size_t  size;
82                 
83                 printf("Total Size: "); fflush(stdout);
84                 size = AST_WriteScript(NULL, ret);
85                 printf("0x%x bytes\n", (unsigned)size);
86                 
87                 fp = fopen(cacheFilename, "wb");
88                 if(!fp) return ret;
89                 
90                 data = malloc(size);
91                 size = AST_WriteScript(data, ret);
92                 fwrite(data, size, 1, fp);
93                 free(data);
94                 fclose(fp);
95         }
96         
97         return ret;
98 }
99
100 int SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile)
101 {
102         return Bytecode_ConvertScript(Script, DestFile);
103 }
104
105 /**
106  * \brief Free a script
107  */
108 void SpiderScript_Free(tSpiderScript *Script)
109 {
110         tScript_Function        *fcn = Script->Functions;
111         tScript_Function        *nextFcn;
112         
113         // Free functions
114         while(fcn)
115         {
116                 if(fcn->ASTFcn) AST_FreeNode( fcn->ASTFcn );
117                 if(fcn->BCFcn)  Bytecode_DeleteFunction( fcn->BCFcn );
118
119                 nextFcn = fcn->Next;
120                 free( fcn );
121                 fcn = nextFcn;
122         }
123         
124         free(Script);
125 }

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