SpiderScript - Implementing objects and classes, fixing bugs
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / main.c
index a6b1bde..20554f5 100644 (file)
 
 // === IMPORTS ===
 extern tAST_Script     *Parse_Buffer(tSpiderVariant *Variant, char *Buffer);
-extern const int       giSpiderScript_NumExports;
-extern tSpiderFunction gaSpiderScript_Exports[];
 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 ===
 /**
@@ -29,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;
        
@@ -64,110 +68,26 @@ tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filen
        
        free(data);
        
-       return ret;
-}
-
-/**
- * \brief Execute a script function
- * \param Script       Script context to execute in
- * \param Function     Function name to execute
- * \param NArguments   Number of arguments to pass
- * \param Arguments    Arguments passed
- */
-tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
-       const char *Function, int NArguments, tSpiderValue **Arguments)
-{
-       char    *trueName = NULL;
-        int    i;
-        int    bFound = 0;     // Used to keep nesting levels down
-       tSpiderValue    *ret = ERRPTR;
        
-       // Handle namespaces
-       if( Function[0] == '.' ) {
-               trueName = (char*)&Function[1];
-       }
-       else if( !Script->CurNamespace ) {
-               trueName = (char*)Function;
-       }
-       else {
-                int    len = strlen(Script->CurNamespace) + 1 + strlen(Function);
-               trueName = malloc( len + 1 );
-               strcpy(trueName, Script->CurNamespace);
-               strcat(trueName, ".");
-               strcat(trueName, Function);
-       }
-       
-       // First: Find the function in the script
+       // HACK!!
        {
-               tAST_Function   *fcn = Script->Script->Functions;
-               for( ; fcn; fcn = fcn->Next ) {
-                       if( strcmp(fcn->Name, trueName) == 0 )
-                               break;
-               }
-               // Execute!
-               if(fcn) {
-                       tAST_BlockState bs;
-                       bs.FirstVar = NULL;     //< TODO: Parameters
-                       bs.Parent = NULL;
-                       bs.Script = Script;
-                       {
-                               tAST_Node       *arg;
-                                int    i = 0;
-                               for( arg = fcn->Arguments; arg; arg = arg->NextSibling, i++ )
-                               {
-                                       // TODO: Type checks
-                                       Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name);
-                                       if( i >= NArguments )   break;  // TODO: Return gracefully
-                                       Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]);
-                               }
-                       }
-                       ret = AST_ExecuteNode(&bs, fcn->Code);
-                       bFound = 1;
-               }
-       }
+               size_t  size;
                
-       // Didn't find it in script?
-       if(!bFound)
-       {       
-               // Second: Search the variant's exports
-               for( i = 0; i < Script->Variant->NFunctions; i ++ )
-               {
-                       if( strcmp( Script->Variant->Functions[i].Name, trueName) == 0 )
-                               break;
-               }
-               // Execute!
-               if(i < Script->Variant->NFunctions) {
-                       ret = Script->Variant->Functions[i].Handler( Script, NArguments, Arguments );
-                       bFound = 1;
-               }
-       }
-       
-       // Not in variant exports? Search the language internal ones
-       if(!bFound)
-       {
-               for( i = 0; i < giSpiderScript_NumExports; i ++ )
-               {
-                       if( strcmp( gaSpiderScript_Exports[i].Name, trueName ) == 0 )
-                               break;
-               }
-               // Execute!
-               if(i < giSpiderScript_NumExports) {
-                       ret = gaSpiderScript_Exports[i].Handler( Script, NArguments, Arguments );
-                       bFound = 1;
-               }
-       }
-       
-       // Not found?
-       if(!bFound)
-       {
-               fprintf(stderr, "Undefined reference to '%s'\n", trueName);
+               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( trueName != Function && trueName != &Function[1] )
-               free(trueName);
-       
        return ret;
-       
 }
 
 /**
@@ -180,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;

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