SpiderScript - Implementing objects and classes, fixing bugs
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / main.c
index ce39c08..20554f5 100644 (file)
@@ -10,6 +10,9 @@
 
 // === IMPORTS ===
 extern tAST_Script     *Parse_Buffer(tSpiderVariant *Variant, char *Buffer);
+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);
 
 // === CODE ===
 /**
@@ -25,16 +28,21 @@ 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;
        }
        
+       // Create the script
        ret = malloc(sizeof(tSpiderScript));
        ret->Variant = Variant;
        
@@ -42,36 +50,44 @@ tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filen
        fLen = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        
-       data = malloc(fLen);
+       // Allocate and read data
+       data = malloc(fLen + 1);
        if(!data)       return NULL;
        fread(data, fLen, 1, fp);
+       data[fLen] = '\0';
        
        fclose(fp);
        
+       ret->CurNamespace = NULL;
        ret->Script = Parse_Buffer(Variant, data);
+       if( ret->Script == NULL ) {
+               free(data);
+               free(ret);
+               return NULL;
+       }
        
        free(data);
        
-       return ret;
-}
-
-/**
- * \brief Execute a script function
- * \todo Arguments?
- */
-tSpiderVariable *SpiderScript_ExecuteMethod(tSpiderScript *Script, const char *Function)
-{
-       tAST_Function   *fcn = Script->Script->Functions;
        
-       // Find the function
-       for( ; fcn; fcn = fcn->Next ) {
-               if( strcmp(fcn->Name, Function) == 0 )
-                       break;
+       // HACK!!
+       {
+               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);
        }
-       if(!fcn)        return NULL;
        
-       // Execute!
-       return AST_ExecuteNode(Script, fcn->Code);
+       return ret;
 }
 
 /**
@@ -84,7 +100,9 @@ void SpiderScript_Free(tSpiderScript *Script)
        tAST_Node       *var, *nextVar;
        
        // Free functions
-       while(fcn) {
+       while(fcn)
+       {
+               
                AST_FreeNode( fcn->Code );
                
                var = fcn->Arguments;
@@ -99,4 +117,9 @@ void SpiderScript_Free(tSpiderScript *Script)
                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