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) ) {
77 // - Save AST to a file
79 char cacheFilename[strlen(Filename)+6+1];
80 strcpy(cacheFilename, Filename);
81 strcat(cacheFilename, ".ast");
83 SpiderScript_SaveAST(ret, cacheFilename);
86 // - Save Bytecode too
88 char cacheFilename[strlen(Filename)+6+1];
89 strcpy(cacheFilename, Filename);
90 strcat(cacheFilename, ".bc");
92 SpiderScript_SaveBytecode(ret, cacheFilename);
98 int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename)
103 printf("Total Size: ");
105 size = AST_WriteScript(NULL, Script);
106 printf("0x%x bytes\n", (unsigned)size);
108 fp = fopen(Filename, "wb");
117 size = AST_WriteScript(data, Script);
118 fwrite(data, size, 1, fp);
126 * \brief Free a script
128 void SpiderScript_Free(tSpiderScript *Script)
130 tScript_Function *fcn = Script->Functions;
131 tScript_Function *nextFcn;
136 if(fcn->ASTFcn) AST_FreeNode( fcn->ASTFcn );
137 if(fcn->BCFcn) Bytecode_DeleteFunction( fcn->BCFcn );