2 * Acess2 - SpiderScript
8 #include <spiderscript.h>
11 #include "bytecode_gen.h"
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);
21 * \brief Library Entry Point
29 * \brief Parse a script
31 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
38 fp = fopen(Filename, "r");
43 fseek(fp, 0, SEEK_END);
45 fseek(fp, 0, SEEK_SET);
47 // Allocate and read data
48 data = malloc(fLen + 1);
49 if(!data) return NULL;
50 fLen = fread(data, 1, fLen, fp);
60 ret = malloc(sizeof(tSpiderScript));
61 ret->Variant = Variant;
62 ret->Functions = NULL;
63 ret->LastFunction = NULL;
65 ret->CurNamespace = NULL;
66 if( Parse_Buffer(ret, data, Filename) ) {
76 // - Save AST to a file
78 char cacheFilename[strlen(Filename)+6+1];
79 strcpy(cacheFilename, Filename);
80 strcat(cacheFilename, ".ast");
82 SpiderScript_SaveAST(ret, cacheFilename);
84 // - Save Bytecode too
86 char cacheFilename[strlen(Filename)+6+1];
87 strcpy(cacheFilename, Filename);
88 strcat(cacheFilename, ".bc");
90 SpiderScript_SaveBytecode(ret, cacheFilename);
96 int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename)
101 printf("Total Size: ");
103 size = AST_WriteScript(NULL, Script);
104 printf("0x%x bytes\n", (unsigned)size);
106 fp = fopen(Filename, "wb");
115 size = AST_WriteScript(data, Script);
116 fwrite(data, size, 1, fp);
124 * \brief Free a script
126 void SpiderScript_Free(tSpiderScript *Script)
128 tScript_Function *fcn = Script->Functions;
129 tScript_Function *nextFcn;
134 if(fcn->ASTFcn) AST_FreeNode( fcn->ASTFcn );
135 if(fcn->BCFcn) Bytecode_DeleteFunction( fcn->BCFcn );