SpiderScript - Fixes, SpiderWeb's print_test.sw works
[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         // - Save AST to a file
77         {
78                 char    cacheFilename[strlen(Filename)+6+1];
79                 strcpy(cacheFilename, Filename);
80                 strcat(cacheFilename, ".ast");
81         
82                 SpiderScript_SaveAST(ret, cacheFilename);       
83         }
84         // - Save Bytecode too
85         {
86                 char    cacheFilename[strlen(Filename)+6+1];
87                 strcpy(cacheFilename, Filename);
88                 strcat(cacheFilename, ".bc");
89         
90                 SpiderScript_SaveBytecode(ret, cacheFilename);  
91         }
92         
93         return ret;
94 }
95
96 int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename)
97 {
98         size_t  size;
99         FILE    *fp;
100         void    *data;
101         printf("Total Size: ");
102         fflush(stdout);
103         size = AST_WriteScript(NULL, Script);
104         printf("0x%x bytes\n", (unsigned)size);
105         
106         fp = fopen(Filename, "wb");
107         if(!fp) return 1;
108
109         data = malloc(size);
110         if(!data) {
111                 fclose(fp);
112                 return -1;
113         }
114         
115         size = AST_WriteScript(data, Script);
116         fwrite(data, size, 1, fp);
117         free(data);
118
119         fclose(fp);
120         return 0;
121 }
122
123 /**
124  * \brief Free a script
125  */
126 void SpiderScript_Free(tSpiderScript *Script)
127 {
128         tScript_Function        *fcn = Script->Functions;
129         tScript_Function        *nextFcn;
130         
131         // Free functions
132         while(fcn)
133         {
134                 if(fcn->ASTFcn) AST_FreeNode( fcn->ASTFcn );
135                 if(fcn->BCFcn)  Bytecode_DeleteFunction( fcn->BCFcn );
136
137                 nextFcn = fcn->Next;
138                 free( fcn );
139                 fcn = nextFcn;
140         }
141         
142         free(Script);
143 }

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