Usermode/libc - Look ma! setjmp
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / main.c
index bfeaf50..bd29280 100644 (file)
@@ -6,10 +6,12 @@
 #include <stdio.h>
 #include <string.h>
 #include <spiderscript.h>
+#include "common.h"
 #include "ast.h"
+#include "bytecode_gen.h"
 
 // === IMPORTS ===
-extern tAST_Script     *Parse_Buffer(tSpiderVariant *Variant, const char *Buffer, const char *Filename);
+extern  int    Parse_Buffer(tSpiderScript *Script, const char *Buffer, const char *Filename);
 extern tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name);
 extern void    Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value);
 extern void    Variable_Destroy(tAST_Variable *Variable);
@@ -28,15 +30,11 @@ int SoMain()
  */
 tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename)
 {
-       char    cacheFilename[strlen(Filename)+6+1];
        char    *data;
         int    fLen;
        FILE    *fp;
        tSpiderScript   *ret;
        
-       strcpy(cacheFilename, Filename);
-       strcat(cacheFilename, ".cache");
-       
        fp = fopen(Filename, "r");
        if( !fp ) {
                return NULL;
@@ -61,10 +59,11 @@ tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filen
        // Create the script
        ret = malloc(sizeof(tSpiderScript));
        ret->Variant = Variant;
+       ret->Functions = NULL;
+       ret->LastFunction = NULL;
        
        ret->CurNamespace = NULL;
-       ret->Script = Parse_Buffer(Variant, data, Filename);
-       if( ret->Script == NULL ) {
+       if( Parse_Buffer(ret, data, Filename) ) {
                free(data);
                free(ret);
                return NULL;
@@ -74,56 +73,73 @@ tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filen
        
        
        // HACK!!
+       #if 1
+       // - Save AST to a file
        {
-               size_t  size;
-               
-               printf("Total Size: "); fflush(stdout);
-               size = AST_WriteScript(NULL, ret->Script);
-               printf("0x%x bytes\n", (unsigned)size);
-               
-               fp = fopen(cacheFilename, "wb");
-               if(!fp) return ret;
-               
-               data = malloc(size);
-               size = AST_WriteScript(data, ret->Script);
-               fwrite(data, size, 1, fp);
-               free(data);
-               fclose(fp);
+               char    cacheFilename[strlen(Filename)+6+1];
+               strcpy(cacheFilename, Filename);
+               strcat(cacheFilename, ".ast");
+       
+               SpiderScript_SaveAST(ret, cacheFilename);       
+       }
+       #endif
+       // - Save Bytecode too
+       {
+               char    cacheFilename[strlen(Filename)+6+1];
+               strcpy(cacheFilename, Filename);
+               strcat(cacheFilename, ".bc");
+       
+               SpiderScript_SaveBytecode(ret, cacheFilename);  
        }
        
        return ret;
 }
 
+int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename)
+{
+       size_t  size;
+       FILE    *fp;
+       void    *data;
+       printf("Total Size: ");
+       fflush(stdout);
+       size = AST_WriteScript(NULL, Script);
+       printf("0x%x bytes\n", (unsigned)size);
+       
+       fp = fopen(Filename, "wb");
+       if(!fp) return 1;
+
+       data = malloc(size);
+       if(!data) {
+               fclose(fp);
+               return -1;
+       }
+       
+       size = AST_WriteScript(data, Script);
+       fwrite(data, size, 1, fp);
+       free(data);
+
+       fclose(fp);
+       return 0;
+}
+
 /**
  * \brief Free a script
  */
 void SpiderScript_Free(tSpiderScript *Script)
 {
-       tAST_Function   *fcn = Script->Script->Functions;
-       tAST_Function   *nextFcn;
-       tAST_Node       *var, *nextVar;
+       tScript_Function        *fcn = Script->Functions;
+       tScript_Function        *nextFcn;
        
        // Free functions
        while(fcn)
        {
-               
-               AST_FreeNode( fcn->Code );
-               
-               var = fcn->Arguments;
-               while(var)
-               {
-                       nextVar = var->NextSibling;
-                       AST_FreeNode( var );
-                       var = nextVar;
-               }
-               
+               if(fcn->ASTFcn) AST_FreeNode( fcn->ASTFcn );
+               if(fcn->BCFcn)  Bytecode_DeleteFunction( fcn->BCFcn );
+
                nextFcn = fcn->Next;
                free( fcn );
                fcn = nextFcn;
        }
        
-       // TODO: Pass this off to AST for a proper cleanup
-       free(Script->Script);
-       
        free(Script);
 }

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